Use **kwargs with EventHandlers and Processor

This commit is contained in:
Christophe Bedard 2019-08-02 14:23:48 +02:00
parent 5f1d7cf90f
commit f9f9c08f2e
4 changed files with 33 additions and 14 deletions

View file

@ -86,7 +86,12 @@ class EventHandler():
Provides handling functions for some events, depending on the name. 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. Constructor.
@ -111,7 +116,7 @@ class EventHandler():
return [] return []
@classmethod @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. Create a `Processor` and process an instance of the class.
@ -119,8 +124,8 @@ class EventHandler():
:return: the processor object after processing :return: the processor object after processing
""" """
assert cls != EventHandler, 'only call process() from inheriting classes' assert cls != EventHandler, 'only call process() from inheriting classes'
handler_object = cls() # pylint: disable=no-value-for-parameter handler_object = cls(**kwargs) # pylint: disable=no-value-for-parameter
processor = Processor(handler_object) processor = Processor(handler_object, **kwargs)
processor.process(events) processor.process(events)
return handler_object return handler_object
@ -128,7 +133,11 @@ class EventHandler():
class Processor(): class Processor():
"""Base processor class.""" """Base processor class."""
def __init__(self, *handlers: EventHandler) -> None: def __init__(
self,
*handlers: EventHandler,
**kwargs,
) -> None:
""" """
Constructor. Constructor.
@ -136,13 +145,15 @@ class Processor():
""" """
self._handlers = list(handlers) self._handlers = list(handlers)
print('handlers before:', [type(handler).__name__ for handler in self._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]) print('handlers after:', [type(handler).__name__ for handler in self._handlers])
self._register(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 :param handlers: the list of primary `EventHandler`s
""" """

View file

@ -30,13 +30,16 @@ class CpuTimeHandler(EventHandler):
It extracts timestamps from sched_switch events to later compute CPU time per thread. 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 # Link event to handling method
handler_map = { handler_map = {
'sched_switch': 'sched_switch':
self._handle_sched_switch, self._handle_sched_switch,
} }
super().__init__(handler_map) super().__init__(handler_map=handler_map, **kwargs)
self._data = CpuTimeDataModel() self._data = CpuTimeDataModel()

View file

@ -49,7 +49,9 @@ class ProfileHandler(EventHandler):
def __init__( def __init__(
self, self,
functions: Dict[str, List[str]] = FUNCTIONS *,
functions: Dict[str, List[str]] = FUNCTIONS,
**kwargs,
) -> None: ) -> None:
handler_map = { handler_map = {
'lttng_ust_cyg_profile_fast:func_entry': 'lttng_ust_cyg_profile_fast:func_entry':
@ -57,7 +59,7 @@ class ProfileHandler(EventHandler):
'lttng_ust_cyg_profile_fast:func_exit': 'lttng_ust_cyg_profile_fast:func_exit':
self._handle_function_exit, self._handle_function_exit,
} }
super().__init__(handler_map) super().__init__(handler_map=handler_map, **kwargs)
self._data = ProfileDataModel() self._data = ProfileDataModel()
self.functions = functions self.functions = functions

View file

@ -30,7 +30,10 @@ class Ros2Handler(EventHandler):
Handles a trace's events and builds a model with the data. 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 # Link a ROS trace event to its corresponding handling method
handler_map = { handler_map = {
'ros2:rcl_init': 'ros2:rcl_init':
@ -60,7 +63,7 @@ class Ros2Handler(EventHandler):
'ros2:callback_end': 'ros2:callback_end':
self._handle_callback_end, self._handle_callback_end,
} }
super().__init__(handler_map) super().__init__(handler_map=handler_map, **kwargs)
self._data = RosDataModel() self._data = RosDataModel()