4040
4141class OSITrace :
4242 """This class can import and decode OSI single- and multi-channel trace files."""
43-
43+
4444 @staticmethod
4545 def map_message_type (type_name ):
4646 """Map the type name to the protobuf message type."""
@@ -50,7 +50,7 @@ def map_message_type(type_name):
5050 def message_types ():
5151 """Message types that OSITrace supports."""
5252 return list (MESSAGES_TYPE .keys ())
53-
53+
5454 _legacy_ositrace_attributes = {
5555 "type" ,
5656 "file" ,
@@ -65,18 +65,26 @@ def __getattr__(self, name):
6565 This method forwards the getattr call for unsuccessful legacy attribute
6666 name lookups to the reader in case it is an _OSITraceSingle instance.
6767 """
68- if name in self ._legacy_ositrace_attributes and isinstance (self .reader , _OSITraceSingle ):
68+ if name in self ._legacy_ositrace_attributes and isinstance (
69+ self .reader , _OSITraceSingle
70+ ):
6971 return getattr (self .reader , name )
70- raise AttributeError (f"'{ type (self ).__name__ } ' object has no attribute '{ name } '" )
71-
72+ raise AttributeError (
73+ f"'{ type (self ).__name__ } ' object has no attribute '{ name } '"
74+ )
75+
7276 def __setattr__ (self , name , value ):
7377 """
7478 This method overwrites the default setter and forwards setattr calls for
7579 legacy attribute names to the reader in case the reader is an
7680 _OSITraceSingle instance. Otherwise it uses the default setter.
7781 """
78- reader = super ().__getattribute__ ("reader" ) if "reader" in self .__dict__ else None
79- if name in self ._legacy_ositrace_attributes and isinstance (reader , _OSITraceSingle ):
82+ reader = (
83+ super ().__getattribute__ ("reader" ) if "reader" in self .__dict__ else None
84+ )
85+ if name in self ._legacy_ositrace_attributes and isinstance (
86+ reader , _OSITraceSingle
87+ ):
8088 setattr (reader , name , value )
8189 else :
8290 super ().__setattr__ (name , value )
@@ -86,11 +94,13 @@ def __dir__(self):
8694 if isinstance (self .reader , _OSITraceSingle ):
8795 attrs += list (self ._legacy_ositrace_attributes )
8896 return attrs
89-
90- def __init__ (self , path = None , type_name = "SensorView" , cache_messages = False , topic = None ):
97+
98+ def __init__ (
99+ self , path = None , type_name = "SensorView" , cache_messages = False , topic = None
100+ ):
91101 """
92102 Initializes the trace reader depending on the trace file format.
93-
103+
94104 Args:
95105 path (str): The path to the trace file.
96106 type_name (str): The type name of the messages in the trace; check supported message types with `OSITrace.message_types()`.
@@ -100,8 +110,10 @@ def __init__(self, path=None, type_name="SensorView", cache_messages=False, topi
100110 self .reader = None
101111
102112 if path is not None :
103- self .reader = self ._init_reader (Path (path ), type_name , cache_messages , topic )
104-
113+ self .reader = self ._init_reader (
114+ Path (path ), type_name , cache_messages , topic
115+ )
116+
105117 def _init_reader (self , path , type_name , cache_messages , topic ):
106118 if not path .exists ():
107119 raise FileNotFoundError ("File not found" )
@@ -116,7 +128,7 @@ def _init_reader(self, path, type_name, cache_messages, topic):
116128 def from_file (self , path , type_name = "SensorView" , cache_messages = False , topic = None ):
117129 """
118130 Initializes the trace reader depending on the trace file format.
119-
131+
120132 Args:
121133 path (str): The path to the trace file.
122134 type_name (str): The type name of the messages in the trace; check supported message types with `OSITrace.message_types()`.
@@ -140,47 +152,63 @@ def __iter__(self):
140152 def close (self ):
141153 return self .reader .close ()
142154
143- @deprecated ("This is a legacy interface only supported for single-channel traces, which will be removed in future versions." )
155+ @deprecated (
156+ "This is a legacy interface only supported for single-channel traces, which will be removed in future versions."
157+ )
144158 def retrieve_offsets (self , limit = None ):
145159 if isinstance (self .reader , _OSITraceSingle ):
146160 return self .reader .retrieve_offsets (limit )
147- raise NotImplementedError ("Offsets are only supported for single-channel traces." )
161+ raise NotImplementedError (
162+ "Offsets are only supported for single-channel traces."
163+ )
148164
149- @deprecated ("This is a legacy interface only supported for single-channel traces, which will be removed in future versions." )
165+ @deprecated (
166+ "This is a legacy interface only supported for single-channel traces, which will be removed in future versions."
167+ )
150168 def retrieve_message (self , index = None , skip = False ):
151169 if isinstance (self .reader , _OSITraceSingle ):
152170 return self .reader .retrieve_message (index , skip )
153- raise NotImplementedError ("Index-based message retrieval is only supported for single-channel traces." )
171+ raise NotImplementedError (
172+ "Index-based message retrieval is only supported for single-channel traces."
173+ )
154174
155- @deprecated ("This is a legacy interface only supported for single-channel traces, which will be removed in future versions." )
175+ @deprecated (
176+ "This is a legacy interface only supported for single-channel traces, which will be removed in future versions."
177+ )
156178 def get_message_by_index (self , index ):
157179 if isinstance (self .reader , _OSITraceSingle ):
158180 return self .reader .get_message_by_index (index )
159- raise NotImplementedError ("Index-based message retrieval is only supported for single-channel traces." )
181+ raise NotImplementedError (
182+ "Index-based message retrieval is only supported for single-channel traces."
183+ )
160184
161- @deprecated ("This is a legacy interface only supported for single-channel traces, which will be removed in future versions." )
185+ @deprecated (
186+ "This is a legacy interface only supported for single-channel traces, which will be removed in future versions."
187+ )
162188 def get_messages_in_index_range (self , begin , end ):
163189 if isinstance (self .reader , _OSITraceSingle ):
164190 return self .reader .get_messages_in_index_range (begin , end )
165- raise NotImplementedError ("Index-based message retrieval is only supported for single-channel traces." )
191+ raise NotImplementedError (
192+ "Index-based message retrieval is only supported for single-channel traces."
193+ )
166194
167195 def get_available_topics (self ):
168196 return self .reader .get_available_topics ()
169-
197+
170198 def get_file_metadata (self ):
171199 return self .reader .get_file_metadata ()
172-
200+
173201 def get_channel_metadata (self ):
174202 return self .reader .get_channel_metadata ()
175203
176204
177205class _ReaderBase (ABC ):
178206 """Common interface for trace readers"""
179-
207+
180208 @abstractmethod
181209 def restart (self , index = None ):
182210 pass
183-
211+
184212 @abstractmethod
185213 def __iter__ (self ):
186214 pass
@@ -204,7 +232,7 @@ def get_channel_metadata(self):
204232
205233class _OSITraceSingle (_ReaderBase ):
206234 """OSI single-channel trace reader"""
207-
235+
208236 def __init__ (self , path = None , type_name = "SensorView" , cache_messages = False ):
209237 self .type = OSITrace .map_message_type (type_name )
210238 self .file = None
@@ -344,13 +372,19 @@ def close(self):
344372 self .type = None
345373
346374 def get_available_topics (self ):
347- raise NotImplementedError ("Getting available topics is only supported for multi-channel traces." )
375+ raise NotImplementedError (
376+ "Getting available topics is only supported for multi-channel traces."
377+ )
348378
349379 def get_file_metadata (self ):
350- raise NotImplementedError ("Getting file metadata is only supported for multi-channel traces." )
380+ raise NotImplementedError (
381+ "Getting file metadata is only supported for multi-channel traces."
382+ )
351383
352384 def get_channel_metadata (self ):
353- raise NotImplementedError ("Getting channel metadata is only supported for multi-channel traces." )
385+ raise NotImplementedError (
386+ "Getting channel metadata is only supported for multi-channel traces."
387+ )
354388
355389
356390class _OSITraceMulti (_ReaderBase ):
@@ -365,12 +399,16 @@ def __init__(self, path, type_name, topic):
365399 if topic == None :
366400 topic = next (iter (available_topics ), None )
367401 if topic not in available_topics :
368- raise ValueError (f"The requested topic '{ topic } ' is not present in the trace file or is not of type '{ type_name } '." )
402+ raise ValueError (
403+ f"The requested topic '{ topic } ' is not present in the trace file or is not of type '{ type_name } '."
404+ )
369405 self .topic = topic
370-
406+
371407 def restart (self , index = None ):
372408 if index != None :
373- raise NotImplementedError ("Restarting from a given index is not supported for multi-channel traces." )
409+ raise NotImplementedError (
410+ "Restarting from a given index is not supported for multi-channel traces."
411+ )
374412 self ._iter = None
375413
376414 def __iter__ (self ):
@@ -384,7 +422,7 @@ def __iter__(self):
384422 msg = message_class ()
385423 msg .ParseFromString (message .data )
386424 yield msg
387-
425+
388426 def close (self ):
389427 if self ._file :
390428 self ._file .close ()
@@ -393,8 +431,12 @@ def close(self):
393431 self ._summary = None
394432 self ._iter = None
395433
396- def get_available_topics (self , type_name = None ):
397- return [channel .topic for channel in self ._summary .channels .values () if self ._channel_is_of_type (channel , type_name )]
434+ def get_available_topics (self , type_name = None ):
435+ return [
436+ channel .topic
437+ for channel in self ._summary .channels .values ()
438+ if self ._channel_is_of_type (channel , type_name )
439+ ]
398440
399441 def get_file_metadata (self ):
400442 metadata = []
@@ -415,9 +457,11 @@ def get_message_type(self):
415457 if schema .name .startswith ("osi3." ):
416458 return schema .name [len ("osi3." ) :]
417459 else :
418- raise ValueError (f"Schema '{ schema .name } ' is not an 'osi3.' schema." )
460+ raise ValueError (
461+ f"Schema '{ schema .name } ' is not an 'osi3.' schema."
462+ )
419463 return None
420464
421465 def _channel_is_of_type (self , channel , type_name ):
422- schema = self ._summary .schemas [channel .schema_id ]
466+ schema = self ._summary .schemas [channel .schema_id ]
423467 return type_name is None or schema .name == f"osi3.{ type_name } "
0 commit comments