-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObserver.py
75 lines (49 loc) · 2.11 KB
/
Observer.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
from typing import List
from abc import ABC, abstractmethod
class Publisher:
observers: dict = {}
def __init__(self, operations: List[str]) -> None:
for operation in operations:
self.observers[operation] = []
def subscribe(self, eventType: str, observer: "Observer") -> None:
self.observers[eventType].append(observer)
def unsubscribe(self, eventType: str, observer: "Observer") -> None:
self.observers[eventType].remove(observer)
def notify(self, eventType: str, filePath: str) -> None:
for observer in self.observers[eventType]:
observer.update(eventType, filePath)
class Editor:
publisher: Publisher = None
_file: str = None
def __init__(self) -> None:
self.publisher = Publisher(["open", "save"])
def openFile(self, filePath: str) -> None:
self._file = filePath
self.publisher.notify("open", self._file)
def saveFile(self) -> None:
if self._file:
self.publisher.notify("save", self._file)
else:
raise FileNotFoundError("Please first open a file!")
class Observer(ABC):
@abstractmethod
def update(self, eventType: str, filePath: str) -> None:
pass
class EmailNotificationObserver(Observer):
_email: str = None
def __init__(self, email: str):
self._email = email
def update(self, eventType: str, filePath: str) -> None:
print(f"Email to {self._email}:\n\tSomeone has performed {eventType} operation with the following file: {filePath}")
class LogObserver(Observer):
_logPath: str = None
def __init__(self, logPath: str) -> None:
self._logPath = logPath
def update(self, eventType: str, filePath: str) -> None:
print(f"Save to log {self._logPath}:\n\tSomeone has performed {eventType} operation with the following file: {filePath}")
if __name__ == "__main__":
editor = Editor()
editor.publisher.subscribe("open", LogObserver("/path/to/log/file.txt"))
editor.publisher.subscribe("save", EmailNotificationObserver("[email protected]"))
editor.openFile("test.txt")
editor.saveFile()