-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrecreate_on_changes.py
More file actions
executable file
·72 lines (57 loc) · 1.62 KB
/
recreate_on_changes.py
File metadata and controls
executable file
·72 lines (57 loc) · 1.62 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
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
#!/usr/bin/env python
import logging
import time
import os
import sys
from prepare import apply_rules
try:
import watchdog
except ImportError as e:
print('need to install watchdog')
print(e)
def watch_main():
args = sys.argv[1:]
if len(args) != 2:
msg = 'I expect 2 arguments.'
raise ValueError(msg)
filename1 = args[0]
filename2 = args[1]
class Storage:
last = None
def go0():
with open(filename1) as f:
s = f.read()
if Storage.last != s:
print('Recreating file.')
out = apply_rules(s)
with open(filename2, 'w') as f:
f.write(out)
Storage.last = s
go0()
dirname = os.path.dirname(filename1)
if dirname == '': dirname = '.'
watch(dirname, go0)
def watch(path, handler):
print('Watching path %r' % path)
from watchdog.events import FileSystemEventHandler
from watchdog.observers import Observer
class MyWatcher(FileSystemEventHandler):
def on_modified(self, event):
src_path = event.src_path
print('Change detected: %s' % src_path)
handler.__call__()
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
event_handler = MyWatcher()
observer = Observer()
observer.schedule(event_handler, path, recursive=True)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
if __name__ == '__main__':
watch_main()