|
4 | 4 |
|
5 | 5 | from bpython import importcompletion |
6 | 6 |
|
7 | | -from watchdog.observers import Observer |
8 | | -from watchdog.events import FileSystemEventHandler |
| 7 | +try: |
| 8 | + from watchdog.observers import Observer |
| 9 | + from watchdog.events import FileSystemEventHandler |
| 10 | +except ImportError: |
| 11 | + def ModuleChangedEventHandler(*args): |
| 12 | + return None |
| 13 | +else: |
| 14 | + class ModuleChangedEventHandler(FileSystemEventHandler): |
| 15 | + def __init__(self, paths, on_change): |
| 16 | + self.dirs = defaultdict(set) |
| 17 | + self.on_change = on_change |
| 18 | + self.modules_to_add_later = [] |
| 19 | + self.observer = Observer() |
| 20 | + self.old_dirs = defaultdict(set) |
| 21 | + for path in paths: |
| 22 | + self.add_module(path) |
| 23 | + self.observer.start() |
9 | 24 |
|
10 | | -class ModuleChangedEventHandler(FileSystemEventHandler): |
11 | | - def __init__(self, paths, on_change): |
12 | | - self.dirs = defaultdict(set) |
13 | | - self.on_change = on_change |
14 | | - self.modules_to_add_later = [] |
15 | | - self.observer = Observer() |
16 | | - self.old_dirs = defaultdict(set) |
17 | | - for path in paths: |
18 | | - self.add_module(path) |
19 | | - self.observer.start() |
| 25 | + def reset(self): |
| 26 | + self.dirs = defaultdict(set) |
| 27 | + del self.modules_to_add_later[:] |
| 28 | + self.old_dirs = defaultdict(set) |
| 29 | + self.observer.unschedule_all() |
20 | 30 |
|
21 | | - def reset(self): |
22 | | - self.dirs = defaultdict(set) |
23 | | - del self.modules_to_add_later[:] |
24 | | - self.old_dirs = defaultdict(set) |
25 | | - self.observer.unschedule_all() |
| 31 | + def add_module(self, path): |
| 32 | + """Add a python module to track changes to""" |
| 33 | + path = os.path.abspath(path) |
| 34 | + for suff in importcompletion.SUFFIXES: |
| 35 | + if path.endswith(suff): |
| 36 | + path = path[:-len(suff)] |
| 37 | + break |
| 38 | + dirname = os.path.dirname(path) |
| 39 | + if dirname not in self.dirs: |
| 40 | + self.observer.schedule(self, dirname, recursive=False) |
| 41 | + self.dirs[os.path.dirname(path)].add(path) |
26 | 42 |
|
27 | | - def add_module(self, path): |
28 | | - """Add a python module to track changes to""" |
29 | | - path = os.path.abspath(path) |
30 | | - for suff in importcompletion.SUFFIXES: |
31 | | - if path.endswith(suff): |
32 | | - path = path[:-len(suff)] |
33 | | - break |
34 | | - dirname = os.path.dirname(path) |
35 | | - if dirname not in self.dirs: |
36 | | - self.observer.schedule(self, dirname, recursive=False) |
37 | | - self.dirs[os.path.dirname(path)].add(path) |
| 43 | + def add_module_later(self, path): |
| 44 | + self.modules_to_add_later.append(path) |
38 | 45 |
|
39 | | - def add_module_later(self, path): |
40 | | - self.modules_to_add_later.append(path) |
| 46 | + def activate(self): |
| 47 | + self.dirs = self.old_dirs |
| 48 | + for dirname in self.dirs: |
| 49 | + self.observer.schedule(self, dirname, recursive=False) |
| 50 | + for module in self.modules_to_add_later: |
| 51 | + self.add_module(module) |
| 52 | + del self.modules_to_add_later[:] |
41 | 53 |
|
42 | | - def activate(self): |
43 | | - self.dirs = self.old_dirs |
44 | | - for dirname in self.dirs: |
45 | | - self.observer.schedule(self, dirname, recursive=False) |
46 | | - for module in self.modules_to_add_later: |
47 | | - self.add_module(module) |
48 | | - del self.modules_to_add_later[:] |
| 54 | + def deactivate(self): |
| 55 | + self.observer.unschedule_all() |
| 56 | + self.old_dirs = self.dirs |
| 57 | + self.dirs = defaultdict(set) |
49 | 58 |
|
50 | | - def deactivate(self): |
51 | | - self.observer.unschedule_all() |
52 | | - self.old_dirs = self.dirs |
53 | | - self.dirs = defaultdict(set) |
54 | | - |
55 | | - def on_any_event(self, event): |
56 | | - dirpath = os.path.dirname(event.src_path) |
57 | | - paths = [path + '.py' for path in self.dirs[dirpath]] |
58 | | - if event.src_path in paths: |
59 | | - self.on_change(event.src_path) |
| 59 | + def on_any_event(self, event): |
| 60 | + dirpath = os.path.dirname(event.src_path) |
| 61 | + paths = [path + '.py' for path in self.dirs[dirpath]] |
| 62 | + if event.src_path in paths: |
| 63 | + self.on_change(event.src_path) |
60 | 64 |
|
61 | 65 | if __name__ == '__main__': |
62 | 66 | m = ModuleChangedEventHandler([]) |
|
0 commit comments