Skip to content

Commit 7623288

Browse files
committed
refactor zeromq && event handles and tests
1 parent 5465e16 commit 7623288

73 files changed

Lines changed: 4535 additions & 1162 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

objwatch/event_handls.py

Lines changed: 0 additions & 520 deletions
This file was deleted.

objwatch/events.py

Lines changed: 0 additions & 71 deletions
This file was deleted.

objwatch/events/__init__.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# MIT License
2+
# Copyright (c) 2025 aeeeeeep
3+
4+
"""
5+
ObjWatch Events Module
6+
7+
Provides a modular event handling system with clear separation of concerns:
8+
- models: Event data structures
9+
- formatters: Event formatting logic
10+
- handlers: Event processing and output
11+
- dispatcher: Event routing and distribution
12+
"""
13+
14+
from .models.event_type import EventType
15+
from .models.base_event import BaseEvent
16+
from .models.function_event import FunctionEvent
17+
from .models.variable_event import VariableEvent
18+
from .models.collection_event import CollectionEvent
19+
from .dispatcher import EventDispatcher
20+
21+
__all__ = [
22+
'EventType',
23+
'BaseEvent',
24+
'FunctionEvent',
25+
'VariableEvent',
26+
'CollectionEvent',
27+
'EventDispatcher',
28+
]

objwatch/events/dispatcher.py

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
# MIT License
2+
# Copyright (c) 2025 aeeeeeep
3+
4+
from typing import List, Optional
5+
6+
from .models.base_event import BaseEvent
7+
from .handlers.abc_handler import ABCEventHandler
8+
from .handlers.logging_handler import LoggingEventHandler
9+
from .handlers.json_output_handler import JsonOutputHandler
10+
from ..config import ObjWatchConfig
11+
12+
13+
class EventDispatcher:
14+
"""
15+
Central dispatcher for routing events to registered handlers.
16+
17+
The dispatcher maintains a list of event handlers and routes
18+
incoming events to all handlers that can process them.
19+
20+
This class follows the Observer pattern, allowing multiple
21+
handlers to process the same event.
22+
"""
23+
24+
def __init__(self, config: Optional[ObjWatchConfig] = None):
25+
"""
26+
Initialize the event dispatcher.
27+
28+
Args:
29+
config: Optional configuration for auto-configuring handlers
30+
"""
31+
self._handlers: List[ABCEventHandler] = []
32+
self._config = config
33+
34+
# Auto-configure default handlers if config is provided
35+
if config:
36+
self._setup_default_handlers(config)
37+
38+
def _setup_default_handlers(self, config: ObjWatchConfig) -> None:
39+
"""
40+
Set up default handlers based on configuration.
41+
42+
Args:
43+
config: The configuration to use
44+
"""
45+
# Always add logging handler
46+
self.register_handler(LoggingEventHandler())
47+
48+
# Add JSON output handler if output_json is configured
49+
if config.output_json:
50+
self.register_handler(JsonOutputHandler(config=config))
51+
52+
def register_handler(self, handler: ABCEventHandler) -> None:
53+
"""
54+
Register an event handler.
55+
56+
Args:
57+
handler: The handler to register
58+
"""
59+
if handler not in self._handlers:
60+
self._handlers.append(handler)
61+
handler.start()
62+
63+
def unregister_handler(self, handler: ABCEventHandler) -> None:
64+
"""
65+
Unregister an event handler.
66+
67+
Args:
68+
handler: The handler to unregister
69+
"""
70+
if handler in self._handlers:
71+
handler.stop()
72+
self._handlers.remove(handler)
73+
74+
def dispatch(self, event: BaseEvent) -> None:
75+
"""
76+
Dispatch an event to all registered handlers that can handle it.
77+
78+
Args:
79+
event: The event to dispatch
80+
"""
81+
for handler in self._handlers:
82+
try:
83+
if handler.can_handle(event):
84+
handler.handle(event)
85+
except Exception as e:
86+
# Log error but continue processing with other handlers
87+
# Import here to avoid circular imports
88+
from ..utils.logger import log_error
89+
90+
log_error(f"Handler {type(handler).__name__} failed to process event: {e}")
91+
92+
def start(self) -> None:
93+
"""
94+
Start all registered handlers.
95+
"""
96+
for handler in self._handlers:
97+
handler.start()
98+
99+
def stop(self) -> None:
100+
"""
101+
Stop all registered handlers and perform cleanup.
102+
"""
103+
for handler in self._handlers:
104+
try:
105+
handler.stop()
106+
except Exception as e:
107+
from ..utils.logger import log_error
108+
109+
log_error(f"Error stopping handler {type(handler).__name__}: {e}")
110+
111+
def clear_handlers(self) -> None:
112+
"""
113+
Clear all registered handlers.
114+
"""
115+
self.stop()
116+
self._handlers.clear()
117+
118+
@property
119+
def handlers(self) -> List[ABCEventHandler]:
120+
"""
121+
Get the list of registered handlers.
122+
123+
Returns:
124+
List[ABCEventHandler]: Copy of the handlers list
125+
"""
126+
return self._handlers.copy()
127+
128+
@property
129+
def handler_count(self) -> int:
130+
"""
131+
Get the number of registered handlers.
132+
133+
Returns:
134+
int: Number of handlers
135+
"""
136+
return len(self._handlers)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# MIT License
2+
# Copyright (c) 2025 aeeeeeep
3+
4+
"""
5+
Event Formatters Module
6+
7+
Provides formatting logic for converting events to various output formats.
8+
"""
9+
10+
from .abc_formatter import ABCEventFormatter
11+
from .log_formatter import LogEventFormatter
12+
13+
__all__ = [
14+
'ABCEventFormatter',
15+
'LogEventFormatter',
16+
]
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# MIT License
2+
# Copyright (c) 2025 aeeeeeep
3+
4+
from abc import ABC, abstractmethod
5+
6+
from objwatch.events.models.base_event import BaseEvent
7+
8+
9+
class ABCEventFormatter(ABC):
10+
"""
11+
Abstract base class for event formatters.
12+
13+
Formatters are responsible for converting event objects into
14+
specific output formats (log strings, JSON, etc.).
15+
"""
16+
17+
@abstractmethod
18+
def format(self, event: BaseEvent) -> str:
19+
"""
20+
Format an event into a string representation.
21+
22+
Args:
23+
event: The event to format
24+
25+
Returns:
26+
str: Formatted string representation of the event
27+
"""
28+
pass
29+
30+
@abstractmethod
31+
def can_format(self, event: BaseEvent) -> bool:
32+
"""
33+
Check if this formatter can handle the given event.
34+
35+
Args:
36+
event: The event to check
37+
38+
Returns:
39+
bool: True if this formatter can format the event
40+
"""
41+
pass
42+
43+
def format_prefix(self, lineno: int, call_depth: int) -> str:
44+
"""
45+
Generate the standard prefix for log messages.
46+
47+
Args:
48+
lineno: Line number where the event occurred
49+
call_depth: Current call stack depth
50+
51+
Returns:
52+
str: Formatted prefix like " 42 " (with indentation)
53+
"""
54+
return f"{lineno:>5} " + " " * call_depth

0 commit comments

Comments
 (0)