-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaconfig.py
53 lines (43 loc) · 1.74 KB
/
aconfig.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
# Copyright 2024.
# This file is part of Amity.
# Amity is free software: you can redistribute it and/or modify it under the terms of the
# GNU General Public License as published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
import tools
log = tools.logger(__name__)
import asyncio, os
from watchdog.observers import Observer
from config import Config
class Watcher(object):
def __init__(self, filename, callback):
self.loop = None
self.observer = None
self.watch = None
self.full_path = filename
path = os.path.dirname(self.full_path)
if path == '':
path = '.'
self.dir_path = path
self.filename = os.path.basename(self.full_path)
self.callback = callback
def start(self):
if self.loop is None:
self.loop = asyncio.get_event_loop()
if self.observer is None:
log.info(f'Start watch of {self.full_path}')
self.observer = Observer()
self.observer.start()
self.watch = self.observer.schedule(self, self.dir_path, recursive=False)
def dispatch(self, event):
if (not event.is_directory and
(event.src_path == self.full_path and
event.event_type in ('created', 'modified')) or
(event.src_path == self.full_path or
event.dest_path == self.full_path) and
event.event_type in ('moved', )):
log.info(f'Event {event}')
self.loop.call_soon_threadsafe(self.callback)
class ConfigWatcher(Watcher):
def __init__(self, config, callback):
super().__init__(config.filename, callback)
config = Config('var/config/config.yaml')