annotate web/watch.py @ 222:fb742ee61342

screenshot of parameter UI
author Matt Johnston <matt@ucc.asn.au>
date Fri, 19 Dec 2014 22:50:37 +0800
parents 9c2de3f977e4
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
189
101c66da848d watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 #!/usr/bin/env python3.3
101c66da848d watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2
101c66da848d watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3 import pyinotify
101c66da848d watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4 import glob
101c66da848d watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5 import sys
101c66da848d watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6 import fnmatch
101c66da848d watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7 import os
101c66da848d watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8
101c66da848d watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9 def GlobWatcher(object):
101c66da848d watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10 def __init__(self, g, watcher):
101c66da848d watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11 self.glob = g
101c66da848d watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12 self.watches = []
101c66da848d watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13 self.watcher = watcher
101c66da848d watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14
101c66da848d watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15 def add_glob(watcher, g):
101c66da848d watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16 d = os.path.dirname(g)
101c66da848d watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17
101c66da848d watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18 file_watches = add_glob_files
101c66da848d watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19
101c66da848d watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20 def main():
101c66da848d watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 touchf = sys.argv[1]
101c66da848d watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22
101c66da848d watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 watcher = pyinotify.WatchManager()
101c66da848d watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24 dirpatterns = {}
101c66da848d watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 for g in sys.argv[2:]:
101c66da848d watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 d = os.path.dirname(g)
101c66da848d watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 pattern = os.path.basename(g)
101c66da848d watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 dirpatterns.setdefault(d, []).append(pattern)
101c66da848d watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29
101c66da848d watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30 watchpatterns = {}
101c66da848d watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 for d, patterns in dirpatterns.items():
101c66da848d watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32
101c66da848d watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 w = watcher.add_watch(d,
101c66da848d watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 (pyinotify.IN_MODIFY
101c66da848d watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 |pyinotify.IN_CREATE
101c66da848d watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36 |pyinotify.IN_DELETE
101c66da848d watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37 |pyinotify.IN_MOVED_FROM
101c66da848d watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38 |pyinotify.IN_MOVED_TO))
101c66da848d watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39
101c66da848d watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40 wd = w[d]
101c66da848d watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41 watchpatterns[wd] = patterns
101c66da848d watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42
101c66da848d watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43 def triggered(event):
101c66da848d watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44 if event.name is None:
101c66da848d watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45 return
101c66da848d watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46
101c66da848d watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47 patterns = watchpatterns[event.wd]
101c66da848d watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48 for p in patterns:
101c66da848d watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49 if fnmatch.fnmatch(event.name, p):
207
9c2de3f977e4 - fermentation heat prediction kind of works?
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
50 print("matched %s %s %s" % (event.name, p, event.maskname))
189
101c66da848d watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51 os.utime(touchf, None)
101c66da848d watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
52
101c66da848d watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
53 n = pyinotify.Notifier(watcher, triggered)
101c66da848d watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54 n.loop()
101c66da848d watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55
101c66da848d watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56 if __name__ == '__main__':
101c66da848d watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57 main()
101c66da848d watcher script
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58