diff options
Diffstat (limited to 'libmproxy/script')
-rw-r--r-- | libmproxy/script/reloader.py | 26 |
1 files changed, 16 insertions, 10 deletions
diff --git a/libmproxy/script/reloader.py b/libmproxy/script/reloader.py index 26691fa3..e81bdef6 100644 --- a/libmproxy/script/reloader.py +++ b/libmproxy/script/reloader.py @@ -1,6 +1,12 @@ import os -from watchdog.events import PatternMatchingEventHandler -from watchdog.observers import Observer +import sys +from watchdog.events import RegexMatchingEventHandler +if sys.platform == 'darwin': + from watchdog.observers.polling import PollingObserver as Observer +else: + from watchdog.observers import Observer +# The OSX reloader in watchdog 0.8.3 breaks when unobserving paths. +# We use the PollingObserver instead. _observers = {} @@ -9,7 +15,8 @@ def watch(script, callback): if script in _observers: raise RuntimeError("Script already observed") script_dir = os.path.dirname(os.path.abspath(script.args[0])) - event_handler = _ScriptModificationHandler(callback) + script_name = os.path.basename(script.args[0]) + event_handler = _ScriptModificationHandler(callback, filename=script_name) observer = Observer() observer.schedule(event_handler, script_dir) observer.start() @@ -23,18 +30,17 @@ def unwatch(script): observer.join() -class _ScriptModificationHandler(PatternMatchingEventHandler): - def __init__(self, callback): - # We could enumerate all relevant *.py files (as werkzeug does it), - # but our case looks like it isn't as simple as enumerating sys.modules. - # This should be good enough for now. +class _ScriptModificationHandler(RegexMatchingEventHandler): + def __init__(self, callback, filename='.*'): + super(_ScriptModificationHandler, self).__init__( ignore_directories=True, - patterns=["*.py"] + regexes=['.*'+filename] ) self.callback = callback def on_modified(self, event): self.callback() -__all__ = ["watch", "unwatch"]
\ No newline at end of file +__all__ = ["watch", "unwatch"] + |