Skip to content

Commit

Permalink
forgotten commit with more features for encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
kf@hydra authored and klausfmh committed May 4, 2018
1 parent be234e1 commit e0b0621
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 23 deletions.
56 changes: 44 additions & 12 deletions pypeman/helpers/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,60 @@
"""

import json
import logging
import base64

logger = logging.getLogger(__name__)

# attempt to create serializers allowing to encode objects for non
# Python applications.
class JsonableEncoder:
""" Initial simple encoder for objects.
returns either
- the object itself if it can be json encoded
- a slightly transformed object, that can be json encoded (To be implemented)
- a dict containing two fields: type and repr
returns: a dict with type and value
type: 'asis' or 'b64', 'repr'
value:
- the object itself if it can be json encoded (basetype or
struct of base types)
- a b64 encoded string if obj is of type bytes and not ASCII
(- a slightly transformed object, that can be json encoded
(To be implemented))
- a repr string of the object
"""
def encode(self, obj):
logger.debug("try to encode type %s (%r)", type(obj), obj)
typ = None
result = {
'type': 'asis',
'value': obj,
'repr': '',
}
if isinstance(obj, bytes):
try:
result['type'] = "bytes"
result['value'] = obj.decode('ascii') # OR UTF-8?
return result
except UnicodeDecodeError:
pass
try:
result['type'] = "utf8bytes"
# TODO could merge asis and utf8
result['value'] = obj.decode('utf-8')
return result
except UnicodeDecodeError:
result['type'] = "b64"
result['value'] = base64.b64encode(obj).decode()
result['repr'] = obj.decode('utf-8', 'ignore')
return result
try:
rslt = json.dumps(obj)
typ = "json"
return jsonstr
# logger.info("try to dump %s", repr(obj))
json_str = json.dumps(obj) # this line will except if not jsonable
# logger.info("can be dumped as json %s", json_str)
return result
except TypeError:
pass
result['type'] = "repr"
# TODO: might add a base64 pickle for the repr case
result['value'] = None
result['repr'] = repr(obj)
return result

typ = "repr"
rslt = repr(obj)

return rslt

22 changes: 11 additions & 11 deletions pypeman/remoteadmin.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,17 +145,17 @@ async def list_msg(self, channel, start=0, count=10, order_by='timestamp', mk_b6
List first `count` messages from message store of specified channel.
:params channel: The channel name.
:param mk_b64pickle: if True (yield payload / ctx fields as b64
encoded pickles)
if False a dict with a field type and a field repr will be
created.
Perhaps better to have a fmt param, having one of following values
"native": returns native python objects
"b64pickle": all fields b64 encoded pickles of the related
python object
"jsonable": as an object, that can be json encoded if possible
otherwise as a dict containing the type
and a repr of the object.
:param mk_b64pickle: if True (yield payload / ctx fields as b64 encoded pickles)
if False a a dict with a field type, field value and a field repr
will be created.
Field types depend on the channel's jsonable_msg_info_for_admin() method
typical values are:
"asis": value is the object 'asis', as the object could be pickled
"bytes": bytes just as ASCII string
"utf8bytes": bytes as UTF8 string
"b64": base 64 encoded bytes
"repr": a repr() string of the object is returned
"b64pickle": all fields b64 encoded pickles of the related python object
"""
chan = self.get_channel(channel)

Expand Down

0 comments on commit e0b0621

Please sign in to comment.