From 0ba30d5eb751ba0727fa7ec48a5dfa0869bc88c6 Mon Sep 17 00:00:00 2001 From: dzollo Date: Tue, 19 May 2015 11:24:52 -0700 Subject: [PATCH 1/4] break the loop when we get a SystemExit --- python/sbp/client/handler.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/python/sbp/client/handler.py b/python/sbp/client/handler.py index c8bda8ec35..0f5d8ddc7d 100644 --- a/python/sbp/client/handler.py +++ b/python/sbp/client/handler.py @@ -138,6 +138,8 @@ def run(self): if msg is not None: if msg.msg_type: self.call(msg) + except SystemExit: + break except: import traceback traceback.print_exc() From 92535526cd8f70284517252d3fe9b63ebefd2b30 Mon Sep 17 00:00:00 2001 From: dzollo Date: Tue, 19 May 2015 11:25:28 -0700 Subject: [PATCH 2/4] update imports for pickle logger --- python/sbp/client/loggers/pickle_logger.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/sbp/client/loggers/pickle_logger.py b/python/sbp/client/loggers/pickle_logger.py index a0fa5d50c4..e9b075736b 100644 --- a/python/sbp/client/loggers/pickle_logger.py +++ b/python/sbp/client/loggers/pickle_logger.py @@ -7,7 +7,7 @@ # THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, # EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED # WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. - +from ... import SBP from .base_logger import BaseLogger, LogIterator import cPickle as pickle From 7a48d44123d250abaa9dfb47026f5aadb5006c63 Mon Sep 17 00:00:00 2001 From: dzollo Date: Tue, 19 May 2015 12:44:22 -0700 Subject: [PATCH 3/4] Create a device_logger iterator interface which Uses a Queue to communicate between callback and iterator --- python/sbp/client/loggers/device_iterator.py | 88 ++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 python/sbp/client/loggers/device_iterator.py diff --git a/python/sbp/client/loggers/device_iterator.py b/python/sbp/client/loggers/device_iterator.py new file mode 100644 index 0000000000..b2b9eded73 --- /dev/null +++ b/python/sbp/client/loggers/device_iterator.py @@ -0,0 +1,88 @@ +# Copyright (C) 2015 Swift Navigation Inc. +# Contact: Mark Fine +# +# This source is subject to the license found in the file 'LICENSE' which must +# be be distributed together with this source. All other rights reserved. +# +# THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, +# EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED +# WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. + +from base_logger import LogIterator +from ...client.handler import Handler +from ...table import dispatch +import calendar +import time +from Queue import Queue, Empty + +class DeviceIterator(LogIterator): + """ + LogIterator + + The :class: `DeviceIterator` provides an abstract interface for reading + from a handle and appending messages to a queue, and reading the queue + in an iterable fashion + + Parameters + ---------- + handler : Handler + SBP handler + timeout : float + number of seconds to block when reading queue before + raising stop iteration + + """ + + def _enqueue(self, msg): + """ + Called with each receipt of the message + Note: dispatch is necessary but perhaps should be handled in handler + """ + try: + data = self.dispatcher(msg) + except KeyError: + data = msg + self.queue.put(data, True) + + def __init__(self, handler, timeout=0, maxsize=0, dispatcher=dispatch): + + self.handler = handler + self.queue = Queue(maxsize) + self.handler.add_callback(self._enqueue) + self.timeout = timeout + self.base_time = time.time() + self.dispatcher = dispatcher + + def __iter__(self): + return self + + def delta(self): + return time.time() - self.base_time + + def timestamp(self): + """ + Timestamp generator. + """ + return calendar.timegm(time.gmtime()) + + def next(self): + """Return the next record tuple from the log file. If an unknown SBP + message type is found, it'll return the raw SBP. If the queue is empty, + It throws the StopIteration exception + + Returns + ------- + (float, float, object) + (elapsed msec since beginning of log, UTC timestamp, msg) + + """ + try: + msg = self.queue.get(True, self.timeout) + return (self.delta(), self.timestamp(), msg) + except Empty: + raise StopIteration + + + + + From 07b806969799e96b4010c2b132a11ff87e1bb6ab Mon Sep 17 00:00:00 2001 From: dzollo Date: Tue, 19 May 2015 12:45:05 -0700 Subject: [PATCH 4/4] add simple unit test for device_iterator --- python/data/one_msg.bin | Bin 0 -> 54 bytes python/tests/sbp/client/test_logger.py | 17 +++++++++++++++++ 2 files changed, 17 insertions(+) create mode 100644 python/data/one_msg.bin diff --git a/python/data/one_msg.bin b/python/data/one_msg.bin new file mode 100644 index 0000000000000000000000000000000000000000..f7b985a883a200224360ba32dc612d4d29a3ffc3 GIT binary patch literal 54 zcmWF)Ww^!4J8Nd6BZI?k=fKWeE_|>3L&boCPK}O=|F 0 + assert timestamp >0 + assert type(msg) == MsgAcqResult