Skip to content

Commit

Permalink
Update message definitions, use protobuf 3.0 to support Python 3.
Browse files Browse the repository at this point in the history
  • Loading branch information
peplin committed Sep 10, 2016
1 parent f65ec77 commit f9b240d
Show file tree
Hide file tree
Showing 16 changed files with 339 additions and 255 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ v0.14.0-dev
----------

* Remove support for Python 2.6
* Restore support for Python 3.x (3.5 specifically)

v0.13.0
----------
Expand Down
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ OpenXC for Python
:target: http://python.openxcplatform.com
:alt: Documentation Status

The OpenXC Python library (for Python 2.7) provides an interface to
The OpenXC Python library (for Python 2.7 and 3.5) provides an interface to
vehicle data from the OpenXC Platform. The primary platform for OpenXC
applications is Android, but for prototyping and testing, often it is
preferrable to use a low-overhead environment like Python when developing.
Expand Down
2 changes: 1 addition & 1 deletion docs/contributing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Test Suite
----------

The ``openxc-python`` repository contains a test suite that can be run with the
``tox`` tool, which attemps to run the test suite in Python 2.7. If
``tox`` tool, which attemps to run the test suite in Python 2.7 and 3.5. If
you wish to just run the test suite in your primary Python version, run

.. code-block:: sh
Expand Down
4 changes: 2 additions & 2 deletions docs/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ Install Python and Pip
----------------------

This library (obviously) requires a Python language runtime - the OpenXC library
currently works with Python 2.7, but not Python 3.x.
currently works with Python 2.7 and 3.5.

- **Mac OS X and Linux**

Mac OS X and most Linux distributions already have a compatible Python
installed. Run ``python --version`` from a terminal to check - you need a
2.7.x version, such as 2.7.8.
2.7.x version, such as 2.7.8 or Python 3.5.

- **Windows**

Expand Down
4 changes: 2 additions & 2 deletions openxc/controllers/usb.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ class UsbControllerMixin(Controller):

def write_bytes(self, data):
if self.out_endpoint is None:
LOG.warn("Can't write \"%s\" to USB, OUT endpoint is %x", data,
self.out_endpoint)
LOG.warning("Can't write \"%s\" to USB, OUT endpoint is %x", data,
self.out_endpoint)
return 0
else:
return self.out_endpoint.write(data)
Expand Down
14 changes: 10 additions & 4 deletions openxc/formats/binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@

LOG = logging.getLogger(__name__)

try:
string_type = unicode
except NameError:
string_type = str


class UnrecognizedBinaryCommandError(Exception): pass

class ProtobufStreamer(VehicleMessageStreamer):
Expand Down Expand Up @@ -66,7 +72,7 @@ def deserialize(cls, data):
except google.protobuf.message.DecodeError as e:
pass
except UnicodeDecodeError as e:
LOG.warn("Unable to parse protobuf: %s", e)
LOG.warning("Unable to parse protobuf: %s", e)
else:
return cls._protobuf_to_dict(message)

Expand Down Expand Up @@ -209,7 +215,7 @@ def _protobuf_to_dict(cls, message):
if can_message.HasField('id'):
parsed_message['id'] = can_message.id
if can_message.HasField('data'):
parsed_message['data'] = "0x%s" % binascii.hexlify(can_message.data)
parsed_message['data'] = "0x%s" % string_type(binascii.hexlify(can_message.data), 'ascii')
if can_message.HasField('frame_format'):
if can_message.frame_format == openxc_pb2.RawMessage.STANDARD:
parsed_message['frame_format'] = "standard"
Expand All @@ -232,7 +238,7 @@ def _protobuf_to_dict(cls, message):
if diagnostic_message.HasField('negative_response_code'):
parsed_message['negative_response_code'] = diagnostic_message.negative_response_code
if diagnostic_message.HasField('payload'):
parsed_message['payload'] = "0x%s" % binascii.hexlify(diagnostic_message.payload)
parsed_message['payload'] = "0x%s" % string_type(binascii.hexlify(diagnostic_message.payload), 'ascii')
elif message.type == message.SIMPLE:
simple_message = message.simple_message
parsed_message['name'] = simple_message.name
Expand Down Expand Up @@ -286,7 +292,7 @@ def _protobuf_to_dict(cls, message):
if request.HasField('pid'):
parsed_message['request']['pid'] = request.pid
if request.HasField('payload'):
parsed_message['request']['payload'] = "0x%s" % binascii.hexlify(request.payload)
parsed_message['request']['payload'] = "0x%s" % string_type(binascii.hexlify(request.payload), 'ascii')
elif command.type == openxc_pb2.ControlCommand.PASSTHROUGH:
parsed_message['command'] = "passthrough"
parsed_message['bus'] = command.passthrough_mode_request.bus
Expand Down
4 changes: 3 additions & 1 deletion openxc/formats/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ class JsonFormatter(object):

@classmethod
def deserialize(cls, message):
return json.loads(message.decode("utf8"))
if isinstance(message, bytes):
message = message.decode("utf8")
return json.loads(message)

@classmethod
def serialize(cls, data):
Expand Down
4 changes: 0 additions & 4 deletions openxc/generator/structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ def __init__(self, message_set, enabled=True, **kwargs):
self.id = kwargs['id']

self.bus_name = kwargs['bus']
if not isinstance(self.bus_name, unicode):
raise ConfigurationError("Bus name must be name of bus in "
"config, was '%s'" % self.bus_name)

self.bus = self.message_set.lookup_bus_index(self.bus_name)
if self.bus is None:
raise ConfigurationError("Unable to find bus with name '%s'"
Expand Down
Loading

0 comments on commit f9b240d

Please sign in to comment.