-
Notifications
You must be signed in to change notification settings - Fork 1
/
watcher.py
63 lines (51 loc) · 2.42 KB
/
watcher.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
import time
from watchdog.observers import Observer
import watchdog.events
class Watcher:
def __init__(self, generations, individuals):
self.event_handler = None
self.observer = Observer()
self.generations = generations
self.generation = 0
self.individuals = individuals
self.individual = 0
self.pending_generations = True
self.pending_individuals = True
def on_any_event(self, event):
if event.is_directory:
return None
elif event.event_type == 'modified':
file_full_name = event.src_path.split("/")
file_name = file_full_name[-1].split(".")
file_generation = file_name[0].split("_")
self.generation = int(file_generation[1])
with open(event.src_path, "r") as gen_file:
lines = gen_file.readlines()
if len(lines) <= 1:
self.individual = 0
else:
last_line = lines[-1]
line_fields = last_line.split(";")
self.individual = int(line_fields[0])
self.individuals = int(line_fields[1])
def run(self, watched_dir, gen_progress_bar, indi_progress_bar):
self.event_handler = watchdog.events.PatternMatchingEventHandler(patterns=['*.csv'],
ignore_directories=True, case_sensitive=False)
self.event_handler.on_any_event = self.on_any_event
self.observer.schedule(self.event_handler, watched_dir, recursive=True)
self.observer.start()
try:
while self.pending_generations or self.pending_individuals:
if self.generation == self.generations:
self.pending_generations = False
if self.individual == self.individuals:
self.pending_individuals = False
else:
self.pending_individuals = True
gen_label = 'Generation ' + str(self.generation)
gen_progress_bar.progress(int(100 * self.generation / self.generations), gen_label)
indi_label = 'Individual ' + str(self.individual) + '/' + str(self.individuals)
indi_progress_bar.progress(int(100 * self.individual / self.individuals), indi_label)
time.sleep(0.5)
except:
self.observer.stop()