Skip to content

Commit dff75e7

Browse files
all watchdog to be missing, require new curtsies
1 parent 8c41de0 commit dff75e7

File tree

3 files changed

+77
-69
lines changed

3 files changed

+77
-69
lines changed

bpython/curtsiesfrontend/filewatch.py

Lines changed: 51 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,59 +4,63 @@
44

55
from bpython import importcompletion
66

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()
924

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()
2030

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)
2642

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)
3845

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[:]
4153

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)
4958

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)
6064

6165
if __name__ == '__main__':
6266
m = ModuleChangedEventHandler([])

bpython/curtsiesfrontend/repl.py

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
from curtsies import fmtfuncs
3535
from curtsies import events
3636
import curtsies
37+
import blessings
3738

3839
from bpython.curtsiesfrontend.manual_readline import char_sequences as rl_char_sequences
3940
from bpython.curtsiesfrontend.manual_readline import get_updated_char_sequences
@@ -314,16 +315,17 @@ def __enter__(self):
314315
signal.signal(signal.SIGWINCH, self.sigwinch_handler)
315316

316317
self.orig_import = __builtins__['__import__']
317-
@functools.wraps(self.orig_import)
318-
def new_import(name, globals={}, locals={}, fromlist=[], level=-1):
319-
m = self.orig_import(name, globals=globals, locals=locals, fromlist=fromlist)
320-
if hasattr(m, "__file__"):
321-
if self.watching_files:
322-
self.watcher.add_module(m.__file__)
323-
else:
324-
self.watcher.add_module_later(m.__file__)
325-
return m
326-
__builtins__['__import__'] = new_import
318+
if self.watcher:
319+
@functools.wraps(self.orig_import)
320+
def new_import(name, globals={}, locals={}, fromlist=[], level=-1):
321+
m = self.orig_import(name, globals=globals, locals=locals, fromlist=fromlist)
322+
if hasattr(m, "__file__"):
323+
if self.watching_files:
324+
self.watcher.add_module(m.__file__)
325+
else:
326+
self.watcher.add_module_later(m.__file__)
327+
return m
328+
__builtins__['__import__'] = new_import
327329

328330
return self
329331

@@ -417,15 +419,18 @@ def process_event(self, e):
417419
self.status_bar.message('Reloaded at ' + time.strftime('%H:%M:%S') + ' because ' + ' & '.join(e.files_modified) + ' modified')
418420

419421
elif e in key_dispatch[self.config.toggle_file_watch_key]:
420-
msg = "Auto-reloading active, watching for file changes..."
421-
if self.watching_files:
422-
self.watcher.deactivate()
423-
self.watching_files = False
424-
self.status_bar.pop_permanent_message(msg)
422+
if self.watcher:
423+
msg = "Auto-reloading active, watching for file changes..."
424+
if self.watching_files:
425+
self.watcher.deactivate()
426+
self.watching_files = False
427+
self.status_bar.pop_permanent_message(msg)
428+
else:
429+
self.watching_files = True
430+
self.status_bar.push_permanent_message(msg)
431+
self.watcher.activate()
425432
else:
426-
self.watching_files = True
427-
self.status_bar.push_permanent_message(msg)
428-
self.watcher.activate()
433+
self.status_bar.message('Autoreloading not available because watchdog not installed')
429434

430435
elif e in key_dispatch[self.config.reimport_key]:
431436
self.clear_modules_and_reevaluate()
@@ -606,7 +611,7 @@ def send_session_to_external_editor(self, filename=None):
606611
self.cursor_offset = len(self.current_line)
607612

608613
def clear_modules_and_reevaluate(self):
609-
self.watcher.reset()
614+
if self.watcher: self.watcher.reset()
610615
cursor, line = self.cursor_offset, self.current_line
611616
for modname in sys.modules.keys():
612617
if modname not in self.original_modules:
@@ -1045,7 +1050,7 @@ def reprint_line(self, lineno, tokens):
10451050
self.display_buffer[lineno] = bpythonparse(format(tokens, self.formatter))
10461051
def reevaluate(self, insert_into_history=False):
10471052
"""bpython.Repl.undo calls this"""
1048-
self.watcher.reset()
1053+
if self.watcher: self.watcher.reset()
10491054
old_logical_lines = self.history
10501055
self.history = []
10511056
self.display_lines = []
@@ -1079,7 +1084,6 @@ def getstdout(self):
10791084
return s
10801085

10811086
def focus_on_subprocess(self, args):
1082-
import blessings
10831087
prev_sigwinch_handler = signal.getsignal(signal.SIGWINCH)
10841088
try:
10851089
signal.signal(signal.SIGWINCH, self.orig_sigwinch_handler)

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ def initialize_options(self):
151151

152152
if sys.version_info[:2] >= (2, 6):
153153
# curtsies only supports 2.6 and onwards
154-
extras_require['curtsies'] = ['curtsies >=0.1.6, <0.2.0', 'greenlet']
154+
extras_require['curtsies'] = ['curtsies >=0.1.7, <0.2.0', 'greenlet']
155155
packages.append("bpython.curtsiesfrontend")
156156
entry_points['console_scripts'].append(
157157
'bpython-curtsies = bpython.curtsies:main [curtsies]')

0 commit comments

Comments
 (0)