-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13_eventbus_introspection.py
More file actions
55 lines (41 loc) · 1.48 KB
/
13_eventbus_introspection.py
File metadata and controls
55 lines (41 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
"""Handler Introspection
Demonstrates:
- Using get_handlers() to inspect registered handlers
- Viewing handler metadata (function name, module)
- Understanding what handlers are registered for each event type
"""
import asyncio
import logging
from opensecflow.eventbus.memory_broker import AsyncQueueBroker
from opensecflow.eventbus.eventbus import EventBus
from opensecflow.eventbus.event import ScopedEvent, EventScope
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
async def main():
"""Inspecting registered handlers"""
print("\n=== Handler Introspection ===\n")
process_broker = AsyncQueueBroker()
app_broker = AsyncQueueBroker()
bus = EventBus(process_broker, app_broker)
class TestEvent(ScopedEvent):
type: str = "test.event"
scope: EventScope = EventScope.PROCESS
async def handler1(event_data: dict):
pass
async def handler2(event_data: dict):
pass
bus.subscribe("test.event", handler1)
bus.subscribe("test.event", handler2)
bus.subscribe("another.event", handler1)
# Get all handlers
handlers = bus.get_handlers()
print(" Registered handlers:")
for event_type, handler_list in handlers.items():
print(f" {event_type}:")
for handler_info in handler_list:
print(f" - {handler_info['function_name']} ({handler_info['module']})")
if __name__ == "__main__":
asyncio.run(main())