Skip to content

Commit 8e10646

Browse files
Move reloading code into curtsies Repl object
1 parent 9e6015c commit 8e10646

File tree

3 files changed

+33
-19
lines changed

3 files changed

+33
-19
lines changed

bpython/curtsies.py

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
from bpython.translations import _
2121
from bpython.importcompletion import find_iterator
2222

23-
import wdtest
24-
2523
repl = None # global for `from bpython.curtsies import repl`
2624
#WARNING Will be a problem if more than one repl is ever instantiated this way
2725

@@ -83,17 +81,6 @@ def request_reload(desc):
8381
def request_refresh(when='now'):
8482
refresh_requests.append(curtsies.events.RefreshRequestEvent(when=when))
8583

86-
watcher = wdtest.ModuleChangedEventHandler([], request_reload)
87-
88-
orig_import = __builtins__['__import__']
89-
@wraps(orig_import)
90-
def new_import(name, globals={}, locals={}, fromlist=[], level=-1):
91-
m = orig_import(name, globals=globals, locals=locals, fromlist=fromlist)
92-
if hasattr(m, "__file__"):
93-
watcher.add_module(m.__file__)
94-
return m
95-
__builtins__['__import__'] = new_import
96-
9784
def event_or_refresh(timeout=None):
9885
while True:
9986
t = time.time()
@@ -112,6 +99,7 @@ def event_or_refresh(timeout=None):
11299
with Repl(config=config,
113100
locals_=locals_,
114101
request_refresh=request_refresh,
102+
request_reload=request_reload,
115103
get_term_hw=window.get_term_hw,
116104
get_cursor_vertical_diff=window.get_cursor_vertical_diff,
117105
banner=banner,

wdtest.py renamed to bpython/curtsiesfrontend/filewatch.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,17 @@
99

1010
class ModuleChangedEventHandler(FileSystemEventHandler):
1111
def __init__(self, paths, on_change):
12-
self.dirs = defaultdict(list)
12+
self.dirs = defaultdict(set)
1313
self.on_change = on_change
1414
self.observer = Observer()
1515
for path in paths:
1616
self.add_module(path)
1717
self.observer.start()
1818

19+
def reset(self):
20+
self.dirs = defaultdict(set)
21+
self.observer.unschedule_all()
22+
1923
def add_module(self, path):
2024
"""Add a python module to track changes to"""
2125
path = os.path.abspath(path)
@@ -26,7 +30,7 @@ def add_module(self, path):
2630
dirname = os.path.dirname(path)
2731
if dirname not in self.dirs:
2832
self.observer.schedule(self, dirname, recursive=False)
29-
self.dirs[os.path.dirname(path)].append(path)
33+
self.dirs[os.path.dirname(path)].add(path)
3034

3135
def on_any_event(self, event):
3236
dirpath = os.path.dirname(event.src_path)

bpython/curtsiesfrontend/repl.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import code
22
import contextlib
33
import errno
4+
import functools
45
import greenlet
56
import logging
67
import os
@@ -38,6 +39,7 @@
3839
from bpython.curtsiesfrontend import sitefix; sitefix.monkeypatch_quit()
3940
import bpython.curtsiesfrontend.replpainter as paint
4041
from bpython.curtsiesfrontend.coderunner import CodeRunner, FakeOutput
42+
from bpython.curtsiesfrontend.filewatch import ModuleChangedEventHandler
4143

4244
#TODO other autocomplete modes (also fix in other bpython implementations)
4345

@@ -174,10 +176,10 @@ class Repl(BpythonRepl):
174176
"""
175177

176178
## initialization, cleanup
177-
def __init__(self, locals_=None, config=None,
178-
request_refresh=lambda: None, get_term_hw=lambda:(50, 10),
179-
get_cursor_vertical_diff=lambda: 0, banner=None, interp=None, interactive=True,
180-
orig_tcattrs=None):
179+
def __init__(self, locals_=None, config=None, request_refresh=lambda: None,
180+
request_reload=lambda desc: None, get_term_hw=lambda:(50, 10),
181+
get_cursor_vertical_diff=lambda: 0, banner=None, interp=None,
182+
interactive=True, orig_tcattrs=None):
181183
"""
182184
locals_ is a mapping of locals to pass into the interpreter
183185
config is a bpython config.Struct with config attributes
@@ -217,6 +219,12 @@ def smarter_request_refresh(when='now'):
217219
else:
218220
request_refresh(when=when)
219221
self.request_refresh = smarter_request_refresh
222+
def smarter_request_reload(desc):
223+
if self.watching_files:
224+
request_reload(desc)
225+
else:
226+
pass
227+
self.request_reload = smarter_request_reload
220228
self.get_term_hw = get_term_hw
221229
self.get_cursor_vertical_diff = get_cursor_vertical_diff
222230

@@ -271,6 +279,8 @@ def smarter_request_refresh(when='now'):
271279
self.width = None # will both be set by a window resize event
272280
self.height = None
273281

282+
self.watcher = ModuleChangedEventHandler([], smarter_request_reload)
283+
274284
def __enter__(self):
275285
self.orig_stdout = sys.stdout
276286
self.orig_stderr = sys.stderr
@@ -280,13 +290,24 @@ def __enter__(self):
280290
sys.stdin = self.stdin
281291
self.orig_sigwinch_handler = signal.getsignal(signal.SIGWINCH)
282292
signal.signal(signal.SIGWINCH, self.sigwinch_handler)
293+
294+
self.orig_import = __builtins__['__import__']
295+
@functools.wraps(self.orig_import)
296+
def new_import(name, globals={}, locals={}, fromlist=[], level=-1):
297+
m = self.orig_import(name, globals=globals, locals=locals, fromlist=fromlist)
298+
if hasattr(m, "__file__"):
299+
self.watcher.add_module(m.__file__)
300+
return m
301+
__builtins__['__import__'] = new_import
302+
283303
return self
284304

285305
def __exit__(self, *args):
286306
sys.stdin = self.orig_stdin
287307
sys.stdout = self.orig_stdout
288308
sys.stderr = self.orig_stderr
289309
signal.signal(signal.SIGWINCH, self.orig_sigwinch_handler)
310+
__builtins__['__import__'] = self.orig_import
290311

291312
def sigwinch_handler(self, signum, frame):
292313
old_rows, old_columns = self.height, self.width
@@ -373,6 +394,7 @@ def process_event(self, e):
373394
elif e in key_dispatch[self.config.toggle_file_watch_key]:
374395
msg = "Auto-reloading active, watching for file changes..."
375396
if self.watching_files:
397+
self.watcher.reset()
376398
self.watching_files = False
377399
self.status_bar.pop_permanent_message(msg)
378400
else:

0 commit comments

Comments
 (0)