Use **kwargs with EventHandlers and Processor
This commit is contained in:
parent
5f1d7cf90f
commit
f9f9c08f2e
4 changed files with 33 additions and 14 deletions
|
@ -86,7 +86,12 @@ class EventHandler():
|
|||
Provides handling functions for some events, depending on the name.
|
||||
"""
|
||||
|
||||
def __init__(self, handler_map: HandlerMap) -> None:
|
||||
def __init__(
|
||||
self,
|
||||
*,
|
||||
handler_map: HandlerMap,
|
||||
**kwargs,
|
||||
) -> None:
|
||||
"""
|
||||
Constructor.
|
||||
|
||||
|
@ -111,7 +116,7 @@ class EventHandler():
|
|||
return []
|
||||
|
||||
@classmethod
|
||||
def process(cls, events: List[DictEvent]) -> 'EventHandler':
|
||||
def process(cls, events: List[DictEvent], **kwargs) -> 'EventHandler':
|
||||
"""
|
||||
Create a `Processor` and process an instance of the class.
|
||||
|
||||
|
@ -119,8 +124,8 @@ class EventHandler():
|
|||
:return: the processor object after processing
|
||||
"""
|
||||
assert cls != EventHandler, 'only call process() from inheriting classes'
|
||||
handler_object = cls() # pylint: disable=no-value-for-parameter
|
||||
processor = Processor(handler_object)
|
||||
handler_object = cls(**kwargs) # pylint: disable=no-value-for-parameter
|
||||
processor = Processor(handler_object, **kwargs)
|
||||
processor.process(events)
|
||||
return handler_object
|
||||
|
||||
|
@ -128,7 +133,11 @@ class EventHandler():
|
|||
class Processor():
|
||||
"""Base processor class."""
|
||||
|
||||
def __init__(self, *handlers: EventHandler) -> None:
|
||||
def __init__(
|
||||
self,
|
||||
*handlers: EventHandler,
|
||||
**kwargs,
|
||||
) -> None:
|
||||
"""
|
||||
Constructor.
|
||||
|
||||
|
@ -136,13 +145,15 @@ class Processor():
|
|||
"""
|
||||
self._handlers = list(handlers)
|
||||
print('handlers before:', [type(handler).__name__ for handler in self._handlers])
|
||||
self._add_dependant_handlers(self._handlers)
|
||||
self._add_handler_dependencies(self._handlers, **kwargs)
|
||||
print('handlers after:', [type(handler).__name__ for handler in self._handlers])
|
||||
self._register(self._handlers)
|
||||
|
||||
def _add_dependant_handlers(self, handlers: List[EventHandler]) -> None:
|
||||
def _add_handler_dependencies(self, handlers: List[EventHandler], **kwargs) -> None:
|
||||
"""
|
||||
Check handlers and add dependant handlers if not included. Ordered.
|
||||
Check handlers and add handler dependencies if not included.
|
||||
|
||||
Ordered.
|
||||
|
||||
:param handlers: the list of primary `EventHandler`s
|
||||
"""
|
||||
|
|
|
@ -30,13 +30,16 @@ class CpuTimeHandler(EventHandler):
|
|||
It extracts timestamps from sched_switch events to later compute CPU time per thread.
|
||||
"""
|
||||
|
||||
def __init__(self) -> None:
|
||||
def __init__(
|
||||
self,
|
||||
**kwargs,
|
||||
) -> None:
|
||||
# Link event to handling method
|
||||
handler_map = {
|
||||
'sched_switch':
|
||||
self._handle_sched_switch,
|
||||
}
|
||||
super().__init__(handler_map)
|
||||
super().__init__(handler_map=handler_map, **kwargs)
|
||||
|
||||
self._data = CpuTimeDataModel()
|
||||
|
||||
|
|
|
@ -49,7 +49,9 @@ class ProfileHandler(EventHandler):
|
|||
|
||||
def __init__(
|
||||
self,
|
||||
functions: Dict[str, List[str]] = FUNCTIONS
|
||||
*,
|
||||
functions: Dict[str, List[str]] = FUNCTIONS,
|
||||
**kwargs,
|
||||
) -> None:
|
||||
handler_map = {
|
||||
'lttng_ust_cyg_profile_fast:func_entry':
|
||||
|
@ -57,7 +59,7 @@ class ProfileHandler(EventHandler):
|
|||
'lttng_ust_cyg_profile_fast:func_exit':
|
||||
self._handle_function_exit,
|
||||
}
|
||||
super().__init__(handler_map)
|
||||
super().__init__(handler_map=handler_map, **kwargs)
|
||||
|
||||
self._data = ProfileDataModel()
|
||||
self.functions = functions
|
||||
|
|
|
@ -30,7 +30,10 @@ class Ros2Handler(EventHandler):
|
|||
Handles a trace's events and builds a model with the data.
|
||||
"""
|
||||
|
||||
def __init__(self) -> None:
|
||||
def __init__(
|
||||
self,
|
||||
**kwargs,
|
||||
) -> None:
|
||||
# Link a ROS trace event to its corresponding handling method
|
||||
handler_map = {
|
||||
'ros2:rcl_init':
|
||||
|
@ -60,7 +63,7 @@ class Ros2Handler(EventHandler):
|
|||
'ros2:callback_end':
|
||||
self._handle_callback_end,
|
||||
}
|
||||
super().__init__(handler_map)
|
||||
super().__init__(handler_map=handler_map, **kwargs)
|
||||
|
||||
self._data = RosDataModel()
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue