From fe1e85637209432836a068acb85b9ce8dd81ab92 Mon Sep 17 00:00:00 2001 From: Christophe Bedard Date: Tue, 31 Dec 2019 20:16:05 -0500 Subject: [PATCH 1/7] Add getter for Processor in EventHandler --- .../tracetools_analysis/processor/__init__.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/tracetools_analysis/tracetools_analysis/processor/__init__.py b/tracetools_analysis/tracetools_analysis/processor/__init__.py index 4605132..7de0ae0 100644 --- a/tracetools_analysis/tracetools_analysis/processor/__init__.py +++ b/tracetools_analysis/tracetools_analysis/processor/__init__.py @@ -21,6 +21,7 @@ from typing import Dict from typing import List from typing import Set from typing import Type +from typing import Union from tracetools_read import DictEvent from tracetools_read import get_event_name @@ -125,7 +126,7 @@ class EventHandler(Dependant): f'empty map: {self.__class__.__name__}' assert all(required_name in handler_map.keys() for required_name in self.required_events()) self._handler_map = handler_map - self.processor = None + self._processor = None @property def handler_map(self) -> HandlerMap: @@ -137,6 +138,10 @@ class EventHandler(Dependant): """Get the data model.""" return None + @property + def processor(self) -> Processor: + return self._processor + @staticmethod def required_events() -> Set[str]: """ @@ -147,9 +152,12 @@ class EventHandler(Dependant): """ return {} - def register_processor(self, processor: 'Processor') -> None: + def register_processor( + self, + processor: 'Processor', + ) -> None: """Register processor with this `EventHandler` so that it can query other handlers.""" - self.processor = processor + self._processor = processor @staticmethod def int_to_hex_str(addr: int) -> str: From 06efe1a169564e5f883b1f0162e99349375d8725 Mon Sep 17 00:00:00 2001 From: Christophe Bedard Date: Tue, 31 Dec 2019 20:16:24 -0500 Subject: [PATCH 2/7] Add get_handler_by_type() method in Processor --- .../tracetools_analysis/processor/__init__.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tracetools_analysis/tracetools_analysis/processor/__init__.py b/tracetools_analysis/tracetools_analysis/processor/__init__.py index 7de0ae0..c55e8e1 100644 --- a/tracetools_analysis/tracetools_analysis/processor/__init__.py +++ b/tracetools_analysis/tracetools_analysis/processor/__init__.py @@ -332,6 +332,18 @@ class Processor(): for handler in handlers: handler.register_processor(self) + def get_handler_by_type( + self, + handler_type: Type, + ) -> Union[EventHandler, None]: + """ + Get an existing EventHandler instance from its type. + + :param handler_type: the type of EventHandler subclass to find + :return: the EventHandler instance if found, otherwise `None` + """ + return next((handler for handler in self._expanded_handlers if type(handler) is handler_type), None) + @staticmethod def get_event_names( events: List[DictEvent], From 088b555ae0915a5c8b21a9697c68741125bb413a Mon Sep 17 00:00:00 2001 From: Christophe Bedard Date: Tue, 31 Dec 2019 20:17:09 -0500 Subject: [PATCH 3/7] Cleanup EventHandler.process() method and use AssertionError --- .../tracetools_analysis/processor/__init__.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/tracetools_analysis/tracetools_analysis/processor/__init__.py b/tracetools_analysis/tracetools_analysis/processor/__init__.py index c55e8e1..63dcfbb 100644 --- a/tracetools_analysis/tracetools_analysis/processor/__init__.py +++ b/tracetools_analysis/tracetools_analysis/processor/__init__.py @@ -165,14 +165,19 @@ class EventHandler(Dependant): return f'0x{addr:X}' @classmethod - def process(cls, events: List[DictEvent], **kwargs) -> 'EventHandler': + def process( + cls, + events: List[DictEvent], + **kwargs, + ) -> 'EventHandler': """ Create a `Processor` and process an instance of the class. :param events: the list of events :return: the processor object after processing """ - assert cls != EventHandler, 'only call process() from inheriting classes' + if cls == EventHandler: + raise AssertionError('only call EventHandler.process() from inheriting classes') handler_object = cls(**kwargs) # pylint: disable=all processor = Processor(handler_object, **kwargs) processor.process(events) From 19652f165e726396b6970df8cb8304ca20f9d437 Mon Sep 17 00:00:00 2001 From: Christophe Bedard Date: Tue, 31 Dec 2019 20:17:26 -0500 Subject: [PATCH 4/7] Add test for get_handler_by_type() --- .../test/tracetools_analysis/test_processor.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tracetools_analysis/test/tracetools_analysis/test_processor.py b/tracetools_analysis/test/tracetools_analysis/test_processor.py index 2798f73..818bdad 100644 --- a/tracetools_analysis/test/tracetools_analysis/test_processor.py +++ b/tracetools_analysis/test/tracetools_analysis/test_processor.py @@ -159,6 +159,13 @@ class TestProcessor(unittest.TestCase): # Passes check Processor(EventHandlerWithRequiredEvent()).process([required_mock_event, mock_event]) + def test_get_handler_by_type(self) -> None: + handler1 = StubHandler1() + handler2 = StubHandler2() + processor = Processor(handler1, handler2) + result = processor.get_handler_by_type(StubHandler1) + self.assertTrue(result is handler1) + if __name__ == '__main__': unittest.main() From e9705a71ab1b6b6fa63e3a92af7286b9b1ce3b8f Mon Sep 17 00:00:00 2001 From: Christophe Bedard Date: Tue, 31 Dec 2019 20:18:28 -0500 Subject: [PATCH 5/7] Add test for direct EventHandler.process() call --- .../test/tracetools_analysis/test_processor.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tracetools_analysis/test/tracetools_analysis/test_processor.py b/tracetools_analysis/test/tracetools_analysis/test_processor.py index 818bdad..af7f6c9 100644 --- a/tracetools_analysis/test/tracetools_analysis/test_processor.py +++ b/tracetools_analysis/test/tracetools_analysis/test_processor.py @@ -113,6 +113,11 @@ class TestProcessor(unittest.TestCase): *args, ) + def test_event_handler_process(self) -> None: + # Should not be called directly + with self.assertRaises(AssertionError): + EventHandler.process([]) + def test_handler_wrong_signature(self) -> None: handler = WrongHandler() mock_event = { From 40700570474f1aae8a0b936eb2bc07e81d890864 Mon Sep 17 00:00:00 2001 From: Christophe Bedard Date: Tue, 31 Dec 2019 20:18:41 -0500 Subject: [PATCH 6/7] Fix type hint --- tracetools_analysis/tracetools_analysis/processor/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tracetools_analysis/tracetools_analysis/processor/__init__.py b/tracetools_analysis/tracetools_analysis/processor/__init__.py index 63dcfbb..698359f 100644 --- a/tracetools_analysis/tracetools_analysis/processor/__init__.py +++ b/tracetools_analysis/tracetools_analysis/processor/__init__.py @@ -139,7 +139,7 @@ class EventHandler(Dependant): return None @property - def processor(self) -> Processor: + def processor(self) -> 'Processor': return self._processor @staticmethod From e7e831c506b1a42306d4180fd4cae1a3aa7ba347 Mon Sep 17 00:00:00 2001 From: Christophe Bedard Date: Tue, 31 Dec 2019 20:19:56 -0500 Subject: [PATCH 7/7] Fix linter errors --- .../tracetools_analysis/processor/__init__.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tracetools_analysis/tracetools_analysis/processor/__init__.py b/tracetools_analysis/tracetools_analysis/processor/__init__.py index 698359f..a0fedda 100644 --- a/tracetools_analysis/tracetools_analysis/processor/__init__.py +++ b/tracetools_analysis/tracetools_analysis/processor/__init__.py @@ -346,8 +346,11 @@ class Processor(): :param handler_type: the type of EventHandler subclass to find :return: the EventHandler instance if found, otherwise `None` - """ - return next((handler for handler in self._expanded_handlers if type(handler) is handler_type), None) + """ + return next( + (handler for handler in self._expanded_handlers if type(handler) is handler_type), + None, + ) @staticmethod def get_event_names(