Skip to content

Commit

Permalink
Convenience functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
jfjlaros committed Dec 2, 2019
1 parent 0b3ec9c commit f5d931c
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 36 deletions.
1 change: 1 addition & 0 deletions simple_rpc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from configparser import ConfigParser

from .simple_rpc import Interface
from .extras import dict_to_object, object_to_dict


config = ConfigParser()
Expand Down
33 changes: 3 additions & 30 deletions simple_rpc/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,10 @@
from time import sleep

from . import doc_split, usage, version
from .extras import json_utf8_decode, json_utf8_encode
from .simple_rpc import Interface


def _json_utf8_encode(obj):
"""Binary encode all strings in an object.
:arg object obj: Object.
:returns object: Object with binary encoded strings.
"""
if isinstance(obj, str):
return obj.encode('utf-8')
if isinstance(obj, list) or isinstance(obj, tuple):
return [_json_utf8_encode(item) for item in obj]
return obj


def _json_utf8_decode(obj):
"""Decode all strings in an object to UTF-8.
:arg object obj: Object.
:returns object: Object with UTF-8 encoded strings.
"""
if isinstance(obj, bytes):
return obj.decode('utf-8')
if isinstance(obj, list) or isinstance(obj, tuple):
return [_json_utf8_decode(item) for item in obj]
return obj


def _describe_method(method):
"""Make a human readable description of a method.
Expand Down Expand Up @@ -98,13 +71,13 @@ def rpc_call(handle, device, baudrate, wait, name, args):
:arg str name: Method name.
:arg list args: Method parameters.
"""
args_ = list(map(lambda x: _json_utf8_encode(_loads(x)), args))
args_ = list(map(lambda x: json_utf8_encode(_loads(x)), args))

with Interface(device, baudrate, wait) as interface:
result = interface.call_method(name, *args_)

if result != None:
handle.write('{}\n'.format(dumps(_json_utf8_decode(result))))
handle.write('{}\n'.format(dumps(json_utf8_decode(result))))


def main():
Expand Down
48 changes: 48 additions & 0 deletions simple_rpc/extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,51 @@ def make_function(method):
context)

return context[method['name']]


def json_utf8_decode(obj):
"""Decode all strings in an object to UTF-8.
:arg object obj: Object.
:returns object: Object with UTF-8 encoded strings.
"""
if isinstance(obj, bytes):
return obj.decode('utf-8')
if isinstance(obj, list) or isinstance(obj, tuple):
return [json_utf8_decode(item) for item in obj]
return obj


def json_utf8_encode(obj):
"""Binary encode all strings in an object.
:arg object obj: Object.
:returns object: Object with binary encoded strings.
"""
if isinstance(obj, str):
return obj.encode('utf-8')
if isinstance(obj, list) or isinstance(obj, tuple):
return [json_utf8_encode(item) for item in obj]
return obj


def dict_to_object(d):
"""Convert a dictionary using UTF-8 to an object using binary strings.
:arg dict d: Dictionary with UTF-8 encoded strings.
:returns object: Object with binary encoded strings.
"""
return json_utf8_encode(list(d.items()))


def object_to_dict(obj):
"""Convert an object using binary strings to a dictionary using UTF-8.
:arg object obj: Object with binary encoded strings.
:returns dict: Dictionary with UTF-8 encoded strings.
"""
return dict(json_utf8_decode(obj))
12 changes: 6 additions & 6 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@

from pytest import mark

from simple_rpc.cli import (
_describe_method, _json_utf8_decode, _json_utf8_encode, rpc_call, rpc_list)
from simple_rpc.cli import _describe_method, rpc_call, rpc_list
from simple_rpc.extras import json_utf8_decode, json_utf8_encode


_device = '/dev/ttyACM0'
_reason = 'device not connected'


def test_json_utf8_encode():
assert _json_utf8_encode(['a', ['b', 10]]) == [b'a', [b'b', 10]]
assert _json_utf8_encode(('a', ('b', 10))) == [b'a', [b'b', 10]]
assert json_utf8_encode(['a', ['b', 10]]) == [b'a', [b'b', 10]]
assert json_utf8_encode(('a', ('b', 10))) == [b'a', [b'b', 10]]


def test_json_utf8_decode():
assert _json_utf8_decode([b'a', [b'b', 10]]) == ['a', ['b', 10]]
assert _json_utf8_decode((b'a', (b'b', 10))) == ['a', ['b', 10]]
assert json_utf8_decode([b'a', [b'b', 10]]) == ['a', ['b', 10]]
assert json_utf8_decode((b'a', (b'b', 10))) == ['a', ['b', 10]]


def test_describe_method():
Expand Down

0 comments on commit f5d931c

Please sign in to comment.