-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitor.py
More file actions
46 lines (34 loc) · 1.13 KB
/
monitor.py
File metadata and controls
46 lines (34 loc) · 1.13 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
import os
import time
from abstracts.monitor import IFileObserver, IFolderMonitor
class FolderMonitor(IFolderMonitor):
def __init__(self, src_path=""):
self.__observers:list[IFileObserver] = []
self.__src_path = src_path if src_path else os.getcwd()
def attach(self, observer=None):
try:
if observer:
self.__observers.append(observer)
except Exception as e:
raise e
def detach(self, observer=None):
try:
if observer:
self.__observers.remove(observer)
except Exception as e:
raise e
def notify(self, files=[]):
try:
for obs in self.__observers:
obs.update(files)
except Exception as e:
raise e
def start(self, period=60):
try:
while True:
files = [f for f in os.listdir(self.__src_path) if os.path.isfile(os.path.join(self.__src_path, f))]
if files:
self.notify(files)
time.sleep(period)
except Exception as e:
raise e