Skip to content

Commit 8d990b7

Browse files
Delegate legacy instance attributes
Signed-off-by: Thomas Sedlmayer <tsedlmayer@pmsfit.de>
1 parent 4c1d2a0 commit 8d990b7

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

osi3trace/osi_trace.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,42 @@ def message_types():
5050
"""Message types that OSITrace supports."""
5151
return list(MESSAGES_TYPE.keys())
5252

53+
_legacy_ositrace_attributes = {
54+
"type",
55+
"file",
56+
"current_index",
57+
"message_offsets",
58+
"read_complete",
59+
"message_cache",
60+
}
61+
62+
def __getattr__(self, name):
63+
"""
64+
This method forwards the getattr call for unsuccessful legacy attribute
65+
name lookups to the reader in case it is an OSITraceSingle instance.
66+
"""
67+
if name in self._legacy_ositrace_attributes and isinstance(self.reader, OSITraceSingle):
68+
return getattr(self.reader, name)
69+
raise AttributeError(f"'{type(self).__name__}' object has no attribute '{name}'")
70+
71+
def __setattr__(self, name, value):
72+
"""
73+
This method overwrites the default setter and forwards setattr calls for
74+
legacy attribute names to the reader in case the reader is an
75+
OSITraceSingle instance. Otherwise it uses the default setter.
76+
"""
77+
reader = super().__getattribute__("reader") if "reader" in self.__dict__ else None
78+
if name in self._legacy_ositrace_attributes and isinstance(reader, OSITraceSingle):
79+
setattr(reader, name, value)
80+
else:
81+
super().__setattr__(name, value)
82+
83+
def __dir__(self):
84+
attrs = super().__dir__()
85+
if isinstance(self.reader, OSITraceSingle):
86+
attrs += list(self._legacy_ositrace_attributes)
87+
return attrs
88+
5389
def __init__(self, path=None, type_name="SensorView", cache_messages=False, topic=None):
5490
"""
5591
Initializes the trace reader depending on the trace file format.

0 commit comments

Comments
 (0)