Skip to content

Commit 3a65ba9

Browse files
committed
Refactor filewatching and add tests.
Change API of filewatch.py, update curtsies repl to reflect changes.
1 parent 2698531 commit 3a65ba9

File tree

3 files changed

+56
-24
lines changed

3 files changed

+56
-24
lines changed

bpython/curtsiesfrontend/filewatch.py

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,20 @@ def __init__(self, paths, on_change):
1919
self.observer = Observer()
2020
self.old_dirs = defaultdict(set)
2121
self.started = False
22+
self.activated = False
2223
for path in paths:
23-
self.add_module(path)
24+
self._add_module(path)
2425

2526
def reset(self):
2627
self.dirs = defaultdict(set)
2728
del self.modules_to_add_later[:]
2829
self.old_dirs = defaultdict(set)
2930
self.observer.unschedule_all()
3031

31-
def add_module(self, path):
32-
"""Add a python module to track changes to"""
32+
def _add_module(self, path):
33+
"""Add a python module to track changes to
34+
35+
Can"""
3336
path = os.path.abspath(path)
3437
for suff in importcompletion.SUFFIXES:
3538
if path.endswith(suff):
@@ -40,24 +43,36 @@ def add_module(self, path):
4043
self.observer.schedule(self, dirname, recursive=False)
4144
self.dirs[os.path.dirname(path)].add(path)
4245

43-
def add_module_later(self, path):
46+
def _add_module_later(self, path):
4447
self.modules_to_add_later.append(path)
48+
49+
def track_module(self, path):
50+
"""
51+
Begins tracking this if activated, or remembers to track later.
52+
"""
53+
if self.activated:
54+
self._add_module(path)
55+
else:
56+
self._add_module_later(path)
4557

4658
def activate(self):
59+
if self.activated:
60+
raise ValueError("%r is already activated." % (self,))
4761
if not self.started:
4862
self.started = True
4963
self.observer.start()
50-
self.dirs = self.old_dirs
5164
for dirname in self.dirs:
5265
self.observer.schedule(self, dirname, recursive=False)
5366
for module in self.modules_to_add_later:
54-
self.add_module(module)
67+
self._add_module(module)
5568
del self.modules_to_add_later[:]
69+
self.activated = True
5670

5771
def deactivate(self):
72+
if not self.activated:
73+
raise ValueError("%r is not activated." % (self,))
5874
self.observer.unschedule_all()
59-
self.old_dirs = self.dirs
60-
self.dirs = defaultdict(set)
75+
self.activated = False
6176

6277
def on_any_event(self, event):
6378
dirpath = os.path.dirname(event.src_path)
@@ -66,12 +81,5 @@ def on_any_event(self, event):
6681
self.on_change(files_modified=[event.src_path])
6782

6883
if __name__ == '__main__':
69-
m = ModuleChangedEventHandler([])
70-
m.add_module('./wdtest.py')
71-
try:
72-
while True:
73-
time.sleep(1)
74-
except KeyboardInterrupt:
75-
m.observer.stop()
76-
m.observer.join()
84+
pass
7785

bpython/curtsiesfrontend/repl.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -366,18 +366,12 @@ def new_import(name, globals={}, locals={}, fromlist=[], level=-1):
366366
except:
367367
if name in old_module_locations:
368368
loc = old_module_locations[name]
369-
if self.watching_files:
370-
self.watcher.add_module(loc)
371-
else:
372-
self.watcher.add_module_later(loc)
369+
self.watcher.track_module(loc)
373370
raise
374371
else:
375372
if hasattr(m, "__file__"):
376373
old_module_locations[name] = m.__file__
377-
if self.watching_files:
378-
self.watcher.add_module(m.__file__)
379-
else:
380-
self.watcher.add_module_later(m.__file__)
374+
self.watcher.track_module(m.__file__)
381375
return m
382376
__builtins__['__import__'] = new_import
383377

bpython/test/test_filewatch.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import mock
2+
import os
3+
4+
try:
5+
import unittest2 as unittest
6+
except ImportError:
7+
import unittest
8+
9+
from bpython.curtsiesfrontend.filewatch import ModuleChangedEventHandler
10+
11+
class TestModuleChangeEventHandler(unittest.TestCase):
12+
13+
def setUp(self):
14+
self.module = ModuleChangedEventHandler([], 1)
15+
self.module.observer = mock.Mock()
16+
17+
def test_create_module_handler(self):
18+
self.assertIsInstance(self.module, ModuleChangedEventHandler)
19+
20+
def test_add_module(self):
21+
self.module._add_module('something/test.py')
22+
self.assertIn(os.path.abspath('something/test'),
23+
self.module.dirs[os.path.abspath('something')])
24+
25+
def test_activate_throws_error_when_already_activated(self):
26+
self.module.activated = True
27+
with self.assertRaises(ValueError):
28+
self.module.activate()
29+
30+

0 commit comments

Comments
 (0)