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

Expand JSON messages before they go out #123

Merged
merged 3 commits into from
Apr 17, 2015
Merged
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion python/sbp/client/loggers/base_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ class BaseLogger(object):
File to log to.

"""
def __init__(self, filename):
def __init__(self, filename, dispatcher=dispatch):
self.handle = open(filename, "w+")
self.dispatcher = dispatcher
self.base_time = time.time()

def __enter__(self):
Expand Down
6 changes: 5 additions & 1 deletion python/sbp/client/loggers/json_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@ def __call__(self, msg):
self.call(msg)

def fmt_msg(self, msg):
try:
data = self.dispatcher(msg).to_json_dict()
except KeyError:
data = msg.to_json_dict()
return {"delta": self.delta(),
"timestamp": self.timestamp(),
"data": msg.to_json_dict()}
"data": data}

def call(self, msg):
self.handle.write(json.dumps(self.fmt_msg(msg)) + "\n")
Expand Down
9 changes: 7 additions & 2 deletions python/tests/sbp/client/test_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,12 @@ def test_pickle_log_missing():
new_table.pop(SBP_MSG_PRINT, None)
d = lambda msg: dispatch(msg, table=new_table)
with PickleLogIterator(FileDriver(log_datafile), dispatcher=d) as log:
with pytest.raises(InvalidSBPMessageType) as exc_info:
with warnings.catch_warnings(record=True) as w:
for delta, timestamp, msg in log.next():
pass
assert str(exc_info.value).find("No message found for msg_type id 16*")
warnings.simplefilter("always")
assert len(w) == 13
for x in w:
assert issubclass(x.category, RuntimeWarning)
assert str(x.message).find("No message found for msg_type id 16*")