user_data.py
Go to the documentation of this file.
1 #!/user/bin/env python
2 from copy import deepcopy
3 from flexbe_core.core.exceptions import UserDataError
4 
5 
6 class UserData(object):
7 
8  def __init__(self, reference=None, input_keys=None, output_keys=None, remap=None):
9  self._data = dict()
10  self._reference = reference if reference is not None else dict()
11  self._input_keys = input_keys
12  self._output_keys = output_keys
13  self._remap = remap or dict()
14  self._hashes = dict()
15 
16  def __enter__(self):
17  return self
18 
19  def __exit__(self, *args):
20  for key, value in self._hashes.items():
21  if value != hash(repr(self._data[key])):
22  raise UserDataError("Illegally modified input-only key '%s', declare it as output." % key)
23 
24  def __contains__(self, key):
25  if key in self._data:
26  return True
27  elif self._input_keys is not None and key not in self._input_keys:
28  return False
29  else:
30  return self._remap.get(key, key) in self._reference
31 
32  def __getitem__(self, key):
33  if key in self._data:
34  return self._data[key]
35  if key not in self:
36  raise UserDataError("Key '%s' cannot be accessed, declare it as input key for read access." % key
37  if self._input_keys is not None and key not in self._input_keys else
38  "No data found for key '%s'" % key)
39  value = self._reference[self._remap.get(key, key)]
40  if self._output_keys is not None and key not in self._output_keys:
41  self._data[key] = value
42  self._hashes[key] = hash(repr(value))
43  if getattr(value.__class__, "_has_header", False):
44  # This is specific to rospy: If the value here is a message and has a header,
45  # it will automatically be modified during publishing by rospy.
46  # So to avoid hash issues, we need to return a copy.
47  value = deepcopy(value)
48  return value
49 
50  def __setitem__(self, key, value):
51  if self._output_keys is not None and key in self._output_keys:
52  self._reference[self._remap.get(key, key)] = value
53  self._data[key] = value
54 
55  def __getattr__(self, key):
56  if key.startswith('_'):
57  return object.__getattr__(self, key)
58  return self[key]
59 
60  def __setattr__(self, key, value):
61  if key.startswith('_'):
62  return object.__setattr__(self, key, value)
63  if self._output_keys is not None and key not in self._output_keys:
64  raise UserDataError("Key '%s' cannot be set, declare it as output key for write access." % key)
65  self[key] = value
66 
67  def __call__(self, reference=None, add_from=None, update_from=None, remove_key=None):
68  self._reference = reference or self._reference
69  if isinstance(add_from, UserData):
70  for key, value in add_from._data.items():
71  if key not in self._data:
72  self._data[key] = value
73  if isinstance(update_from, UserData):
74  for key, value in update_from._data.items():
75  self._data[key] = value
76  if remove_key is not None and remove_key in self._data:
77  del self._data[remove_key]
78 
79  def __len__(self):
80  return len(self._data) + len(self._reference)
81 
82  def __str__(self):
83  if isinstance(self._reference, UserData):
84  data_str = '\n '.join(str(self._reference).split('\n'))
85  else:
86  data_str = str(self._reference)
87  return ("UserData object with %d data entries:\n"
88  " Input Keys: %s\n Output Keys: %s\n Data: %s\n Remapping: %s\n Reference: %s"
89  % (len(self), str(self._input_keys), str(self._output_keys), str(self._data),
90  str(self._remap), data_str))
def __call__(self, reference=None, add_from=None, update_from=None, remove_key=None)
Definition: user_data.py:67
def __setattr__(self, key, value)
Definition: user_data.py:60
def __setitem__(self, key, value)
Definition: user_data.py:50
def __init__(self, reference=None, input_keys=None, output_keys=None, remap=None)
Definition: user_data.py:8


flexbe_core
Author(s): Philipp Schillinger
autogenerated on Sun Dec 13 2020 04:01:39