message_collector.py
Go to the documentation of this file.
1 #
2 # Copyright (C) 2014-2015 UAVCAN Development Team <uavcan.org>
3 #
4 # This software is distributed under the terms of the MIT License.
5 #
6 # Author: Ben Dyer <ben_dyer@mac.com>
7 # Pavel Kirienko <pavel.kirienko@zubax.com>
8 #
9 
10 from __future__ import division, absolute_import, print_function, unicode_literals
11 import time
12 from logging import getLogger
13 
14 import collections
15 try:
16  # noinspection PyUnresolvedReferences
17  collections_abc = collections.abc
18 except AttributeError:
19  collections_abc = collections
20 
21 
22 logger = getLogger(__name__)
23 
24 
25 class MessageCollector(collections_abc.Mapping):
26  """This class keeps the latest TransferEvent of a given message type categorized by specified key.
27  The stored items can be automatically removed if they were not updated in a specified time interval.
28 
29  Defaults are as follows:
30  - Categorization key: source node ID
31  - Entry timeout: infinite
32  """
33 
34  def __init__(self, node, data_type, key=None, timeout=None):
35  """
36  :param node: Node instance.
37 
38  :param data_type: Data type to subscribe to.
39 
40  :param key: A callable that accepts a TransferEvent instance and returns a hashable.
41  The returned hashable will be used as categorization key.
42  If this argument is not provided, the messages will be categorized by source node ID.
43 
44  :param timeout: Entry timeout. If an entry was not updated in this time, it will be removed.
45  By default entry lifetime is not limited.
46  """
47  self._handle = node.add_handler(data_type, lambda e: self._storage.update({self._key_function(e): e}))
48  self._storage = {}
49  self._key_function = key or (lambda e: e.transfer.source_node_id)
50  self._timeout = timeout
51 
52  def close(self):
53  self._handle.remove()
54 
55  def __getitem__(self, key):
56  if self._timeout is not None:
57  if (self._storage[key].transfer.ts_monotonic + self._timeout) < time.monotonic():
58  del self._storage[key]
59  return self._storage[key]
60 
61  def __iter__(self):
62  for x in list(self._storage.keys())[:]:
63  try:
64  self[x] # __getitem__ is mutating here - it removes outdated entries
65  except KeyError:
66  pass
67  return iter(self._storage)
68 
69  def __len__(self):
70  return len(self._storage)
pyuavcan_v0.app.message_collector.MessageCollector.__getitem__
def __getitem__(self, key)
Definition: message_collector.py:55
pyuavcan_v0.app.message_collector.MessageCollector._handle
_handle
Definition: message_collector.py:47
pyuavcan_v0.app.message_collector.MessageCollector.__len__
def __len__(self)
Definition: message_collector.py:69
pyuavcan_v0.app.message_collector.MessageCollector.close
def close(self)
Definition: message_collector.py:52
pyuavcan_v0.app.message_collector.MessageCollector.__iter__
def __iter__(self)
Definition: message_collector.py:61
pyuavcan_v0.app.message_collector.MessageCollector.__init__
def __init__(self, node, data_type, key=None, timeout=None)
Definition: message_collector.py:34
pyuavcan_v0.app.message_collector.MessageCollector
Definition: message_collector.py:25
pyuavcan_v0.app.message_collector.MessageCollector._timeout
_timeout
Definition: message_collector.py:50
pyuavcan_v0.app.message_collector.MessageCollector._storage
_storage
Definition: message_collector.py:48
pyuavcan_v0.app.message_collector.MessageCollector._key_function
_key_function
Definition: message_collector.py:49


uavcan_communicator
Author(s):
autogenerated on Fri Dec 13 2024 03:10:02