Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add recursive bytes encoding #96

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions kitty/data/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,49 @@ def get(self, key):
return self._sub_reports[key]
return None

def _dict_decode(self, encoding, dic):
res = {}
for k, v in dic:
if isinstance(v, (bytes, bytearray)):
v = StrEncodeEncoder(encoding).encode(v).tobytes().decode()
elif isinstance(v, dict):
v = self._dict_decode(encoding, v)
elif isinstance(v, list):
v = self._list_decode(encoding, v)
elif isinstance(v, tuple):
v = self._tuple_decode(encoding, v)
res[k] = v
return res

def _list_decode(self, encoding, array):
res = []
for v in array:
if isinstance(v, (bytes, bytearray)):
v = StrEncodeEncoder(encoding).encode(v).tobytes().decode()
elif isinstance(v, dict):
v = self._dict_decode(encoding, v)
elif isinstance(v, list):
v = self._list_decode(encoding, v)
elif isinstance(v, tuple):
v = self._tuple_decode(encoding, v)
res.append(v)
return res

def _tuple_decode(self, encoding, tup):
res = []
for i in range(len(tup)):
v = tup[i]
if isinstance(v, (bytes, bytearray)):
v = StrEncodeEncoder(encoding).encode(v).tobytes().decode()
elif isinstance(v, dict):
v = self._dict_decode(encoding, v)
elif isinstance(v, list):
v = self._list_decode(encoding, v)
elif isinstance(v, tuple):
v = self._tuple_decode(encoding, v)
res.append(v)
return res

def to_dict(self, encoding='base64'):
'''
Return a dictionary version of the report
Expand All @@ -174,6 +217,12 @@ def to_dict(self, encoding='base64'):
for k, v in self._data_fields.items():
if isinstance(v, (bytes, bytearray, six.string_types)):
v = StrEncodeEncoder(encoding).encode(v).tobytes().decode()
elif isinstance(v, dict):
v = self._dict_decode(encoding, v)
elif isinstance(v, list):
v = self._list_decode(encoding, v)
elif isinstance(v, tuple):
v = self._tuple_decode(encoding, v)
res[k] = v
for k, v in self._sub_reports.items():
res[k] = v.to_dict(encoding)
Expand Down