| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 8c41de0 commit dff75e7
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,59 +4,63 @@ | |||
| 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([]) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -34,6 +34,7 @@ | |||
| 34 | 34 | from curtsies import fmtfuncs | |
| 35 | 35 | from curtsies import events | |
| 36 | 36 | import curtsies | |
| 37 | + import blessings | ||
| 37 | 38 | ||
| 38 | 39 | from bpython.curtsiesfrontend.manual_readline import char_sequences as rl_char_sequences | |
| 39 | 40 | from bpython.curtsiesfrontend.manual_readline import get_updated_char_sequences | |
@@ -314,16 +315,17 @@ def __enter__(self): | |||
| 314 | 315 | signal.signal(signal.SIGWINCH, self.sigwinch_handler) | |
| 315 | 316 | ||
| 316 | 317 | 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 | ||
| 327 | 329 | ||
| 328 | 330 | return self | |
| 329 | 331 | ||
@@ -417,15 +419,18 @@ def process_event(self, e): | |||
| 417 | 419 | self.status_bar.message('Reloaded at ' + time.strftime('%H:%M:%S') + ' because ' + ' & '.join(e.files_modified) + ' modified') | |
| 418 | 420 | ||
| 419 | 421 | 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() | ||
| 425 | 432 | 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') | ||
| 429 | 434 | ||
| 430 | 435 | elif e in key_dispatch[self.config.reimport_key]: | |
| 431 | 436 | self.clear_modules_and_reevaluate() | |
@@ -606,7 +611,7 @@ def send_session_to_external_editor(self, filename=None): | |||
| 606 | 611 | self.cursor_offset = len(self.current_line) | |
| 607 | 612 | ||
| 608 | 613 | def clear_modules_and_reevaluate(self): | |
| 609 | - self.watcher.reset() | ||
| 614 | + if self.watcher: self.watcher.reset() | ||
| 610 | 615 | cursor, line = self.cursor_offset, self.current_line | |
| 611 | 616 | for modname in sys.modules.keys(): | |
| 612 | 617 | if modname not in self.original_modules: | |
@@ -1045,7 +1050,7 @@ def reprint_line(self, lineno, tokens): | |||
| 1045 | 1050 | self.display_buffer[lineno] = bpythonparse(format(tokens, self.formatter)) | |
| 1046 | 1051 | def reevaluate(self, insert_into_history=False): | |
| 1047 | 1052 | """bpython.Repl.undo calls this""" | |
| 1048 | - self.watcher.reset() | ||
| 1053 | + if self.watcher: self.watcher.reset() | ||
| 1049 | 1054 | old_logical_lines = self.history | |
| 1050 | 1055 | self.history = [] | |
| 1051 | 1056 | self.display_lines = [] | |
@@ -1079,7 +1084,6 @@ def getstdout(self): | |||
| 1079 | 1084 | return s | |
| 1080 | 1085 | ||
| 1081 | 1086 | def focus_on_subprocess(self, args): | |
| 1082 | - import blessings | ||
| 1083 | 1087 | prev_sigwinch_handler = signal.getsignal(signal.SIGWINCH) | |
| 1084 | 1088 | try: | |
| 1085 | 1089 | signal.signal(signal.SIGWINCH, self.orig_sigwinch_handler) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -151,7 +151,7 @@ def initialize_options(self): | |||
| 151 | 151 | ||
| 152 | 152 | if sys.version_info[:2] >= (2, 6): | |
| 153 | 153 | # 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'] | ||
| 155 | 155 | packages.append("bpython.curtsiesfrontend") | |
| 156 | 156 | entry_points['console_scripts'].append( | |
| 157 | 157 | 'bpython-curtsies = bpython.curtsies:main [curtsies]') | |
| Back | FazBrowse Home | New Git URL |
0 commit comments