Files
basegame-vcko/python3-vckonline/lib/python3.8/site-packages/dict_tools/utils.py
2020-11-03 18:30:14 -08:00

23 lines
755 B
Python

from typing import ByteString, Dict, Iterable, Mapping, Text
def decode_dict(data: Dict[bytes, bytes]) -> Dict[str, str]:
"""
Recursively decode all byte-strings found in a dictionary
"""
ret = {}
for key, value in data.items():
if isinstance(key, ByteString):
key = key.decode()
if isinstance(value, (Mapping, Dict)):
ret[key] = decode_dict(value)
elif isinstance(value, ByteString):
ret[key] = value.decode()
elif isinstance(value, Iterable) and not isinstance(value, Text):
ret[key] = value.__new__(
x.decode() if isinstance(x, ByteString) else x for x in value
)
else:
ret[key] = value
return ret