-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathetcWatch.py
97 lines (71 loc) · 2.84 KB
/
etcWatch.py
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import threading
from typing import Dict, Any, List
import queue
# ------------------ Event/Message Class ------------------ #
class Message:
def __init__(self, event_type: str, data: Dict[str, Any]):
self.event_type = event_type
self.data = data
# ------------------ Core Interfaces ------------------ #
class EventProcessor:
def process_event(self, event: Message):
raise NotImplementedError
class EventPersister:
def persist_event(self, event: Message):
raise NotImplementedError
class Subscriber:
def handle_event(self, event: Message):
raise NotImplementedError
# ------------------ Concrete Implementations ------------------ #
class BatchingEventProcessor(EventProcessor):
def process_event(self, event: Message):
# Batching logic
pass
class DatabaseEventPersister(EventPersister):
def persist_event(self, event: Message):
# Database persistence logic
pass
class RealTimeEventProcessor(EventProcessor):
def process_event(self, event: Message):
# Real-time processing logic
pass
class EmailNotifierSubscriber(Subscriber):
def handle_event(self, event: Message):
# Handle and send email based on event data
pass
# ------------------ Pub-Sub Mechanism ------------------ #
class PubSub:
def __init__(self):
self.subscribers: Dict[str, List[Subscriber]] = {}
def subscribe(self, event_type: str, subscriber: Subscriber):
if event_type not in self.subscribers:
self.subscribers[event_type] = []
self.subscribers[event_type].append(subscriber)
def publish(self, event: Message):
if event.event_type in self.subscribers:
for subscriber in self.subscribers[event.event_type]:
# Using threading to handle events concurrently
threading.Thread(target=subscriber.handle_event, args=(event,)).start()
# ------------------ Sample Usage ------------------ #
def main():
# Configurations (could come from a file or other sources)
config = {
"processor_type": "batching",
"persister_type": "database"
}
# Instantiate PubSub mechanism
pubsub = PubSub()
# Factory method for event processors based on config
if config["processor_type"] == "batching":
processor = BatchingEventProcessor()
elif config["processor_type"] == "real_time":
processor = RealTimeEventProcessor()
else:
raise ValueError(f"Unsupported processor_type: {config['processor_type']}")
# Example: Subscribing to an event type for notifications
pubsub.subscribe("file_changed", EmailNotifierSubscriber())
# Example: Publishing an event
message = Message(event_type="file_changed", data={"file_name": "sample.txt", "action": "updated"})
pubsub.publish(message)
if __name__ == "__main__":
main()