-
Notifications
You must be signed in to change notification settings - Fork 1
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
Fix several issues #12
Changes from 7 commits
465d9e2
c071549
0cd2f9a
a629e7e
9febe9f
3ff0463
d7dae88
f7997db
cf83b16
e735fab
b0d2e4a
d2caf2c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,5 @@ | ||
# Python 2/3 compatibility import list | ||
try: | ||
from collections import UserDict | ||
except ImportError: | ||
from UserDict import UserDict | ||
from compas.data import json_dumps | ||
from compas.data import json_loads | ||
|
||
DEFAULT_TRANSPORT = None | ||
|
||
|
@@ -61,21 +58,34 @@ def unadvertise(self, topic): | |
pass | ||
|
||
|
||
class Message(UserDict): | ||
class Message(object): | ||
"""Message objects used for publishing and subscribing to/from topics. | ||
|
||
A message is fundamentally a dictionary and behaves as one.""" | ||
|
||
def __init__(self, *args, **kwargs): | ||
super(Message, self).__init__() | ||
self.data = {} | ||
self.data.update(*args, **kwargs) | ||
|
||
def ToString(self): | ||
return str(self) | ||
|
||
def __str__(self): | ||
return str(self.data) | ||
|
||
def __getattr__(self, name): | ||
return self.__dict__["data"][name] | ||
return self.data[name] | ||
|
||
def __getitem__(self, key): | ||
return self.data[key] | ||
|
||
def __setitem__(self, key, value): | ||
self.data[key] = value | ||
|
||
@classmethod | ||
def parse(cls, value): | ||
instance = cls() | ||
instance.update(value) | ||
instance = cls(**value) | ||
return instance | ||
|
||
|
||
|
@@ -103,6 +113,26 @@ def __init__(self, name, message_type, **options): | |
self.message_type = message_type | ||
self.options = options | ||
|
||
def _message_to_json(self, message): | ||
"""Convert a message to a JSON string. | ||
|
||
Normally, this method expects sub-classes of ``Message`` as input. | ||
However, it can deal with regular dictionaries as well as classes | ||
implementing the COMPAS data framework. | ||
""" | ||
try: | ||
data = message.data | ||
except (KeyError, AttributeError): | ||
try: | ||
data = dict(message) | ||
except Exception: | ||
data = message.__to_data__() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe catch more specific errors like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. true, I was too lazy to think which one is the correct exception here :P There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed at d2caf2c |
||
return json_dumps(data) | ||
gonzalocasas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def _message_from_json(self, json_message): | ||
"""Converts a jJSOn string back into a message instance.""" | ||
return self.message_type.parse(json_loads(json_message)) | ||
|
||
|
||
class Publisher(object): | ||
"""Publisher interface.""" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should this (and
_message_from_json
) maybe made public? I see they are only used externally.more generally, aren't these two doing something very similar to
compas.data
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
they are doing something similar to
compas.data
but not exactly, a message can be a simple dictionary, an instance ofMessage
, or a subclass ofcompas.data.Data
and needs to support compas 1.x and compas 2.x