-
Notifications
You must be signed in to change notification settings - Fork 6
/
enhancer.py
73 lines (55 loc) · 2.2 KB
/
enhancer.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
import json
import time
import logging
import logging.config
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
from amarillo.configuration import configure_enhancer_services
from amarillo.utils.container import container
from amarillo.models.Carpool import Carpool
from amarillo.utils.utils import agency_carpool_ids_from_filename
logging.config.fileConfig('logging.conf', disable_existing_loggers=False)
logger = logging.getLogger("enhancer")
logger.info("Hello Enhancer")
configure_enhancer_services()
observer = Observer() # Watch Manager
class EventHandler(FileSystemEventHandler):
# TODO FG HB should watch for both carpools and agencies
# in data/agency, data/agencyconf, see AgencyConfService
def on_modified(self, event):
if not event.src_path.endswith(".json"):
return
logger.info("MODIFIED: Created %s", event.src_path)
try:
with open(event.src_path, 'r', encoding='utf-8') as f:
dict = json.load(f)
carpool = Carpool(**dict)
container['carpools'].put(carpool.agency, carpool.id, carpool)
except FileNotFoundError as e:
logger.error("Carpool could not be added, as already deleted (%s)", event.src_path)
except:
logger.exception("Eventhandler on_closed encountered exception")
def on_deleted(self, event):
try:
logger.info("DELETE: Removing %s", event.src_path)
(agency_id, carpool_id) = agency_carpool_ids_from_filename(event.src_path)
container['carpools'].delete(agency_id, carpool_id)
except:
logger.exception("Eventhandler on_deleted encountered exception")
observer.schedule(EventHandler(), 'data/carpool', recursive=True)
observer.start()
import time
try:
# TODO FG Is this really needed?
cnt = 0
ENHANCER_LOG_INTERVAL_IN_S = 600
while True:
if cnt == ENHANCER_LOG_INTERVAL_IN_S:
logger.debug("Currently stored carpool ids: %s", container['carpools'].get_all_ids())
cnt = 0
time.sleep(1)
cnt += 1
finally:
observer.stop()
observer.join()
logger.info("Goodbye Enhancer")