Merge branch 'master' into master

This commit is contained in:
Sandor Nemes 2016-01-18 13:35:27 +01:00
commit b892957b90
2 changed files with 20 additions and 14 deletions

View File

@ -666,7 +666,7 @@ class FlowMaster(controller.Master):
script.reloader.unwatch(script_obj) script.reloader.unwatch(script_obj)
self.scripts.remove(script_obj) self.scripts.remove(script_obj)
def load_script(self, command, use_reloader=False): def load_script(self, command, use_reloader=True):
""" """
Loads a script. Returns an error description if something went Loads a script. Returns an error description if something went
wrong. wrong.
@ -1040,14 +1040,14 @@ class FlowMaster(controller.Master):
s.unload() s.unload()
except script.ScriptException as e: except script.ScriptException as e:
ok = False ok = False
self.add_event('Error reloading "{}": {}'.format(s.filename, str(e))) self.add_event('Error reloading "{}": {}'.format(s.filename, str(e)), 'error')
try: try:
s.load() s.load()
except script.ScriptException as e: except script.ScriptException as e:
ok = False ok = False
self.add_event('Error reloading "{}": {}'.format(s.filename, str(e))) self.add_event('Error reloading "{}": {}'.format(s.filename, str(e)), 'error')
else: else:
self.add_event('"{}" reloaded.'.format(s.filename)) self.add_event('"{}" reloaded.'.format(s.filename), 'info')
return ok return ok
def handle_tcp_message(self, m): def handle_tcp_message(self, m):

View File

@ -1,6 +1,12 @@
import os import os
from watchdog.events import PatternMatchingEventHandler import sys
from watchdog.observers import Observer 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 = {} _observers = {}
@ -9,7 +15,8 @@ def watch(script, callback):
if script in _observers: if script in _observers:
raise RuntimeError("Script already observed") raise RuntimeError("Script already observed")
script_dir = os.path.dirname(os.path.abspath(script.args[0])) 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 = Observer()
observer.schedule(event_handler, script_dir) observer.schedule(event_handler, script_dir)
observer.start() observer.start()
@ -23,18 +30,17 @@ def unwatch(script):
observer.join() observer.join()
class _ScriptModificationHandler(PatternMatchingEventHandler): class _ScriptModificationHandler(RegexMatchingEventHandler):
def __init__(self, callback): def __init__(self, callback, filename='.*'):
# 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.
super(_ScriptModificationHandler, self).__init__( super(_ScriptModificationHandler, self).__init__(
ignore_directories=True, ignore_directories=True,
patterns=["*.py"] regexes=['.*'+filename]
) )
self.callback = callback self.callback = callback
def on_modified(self, event): def on_modified(self, event):
self.callback() self.callback()
__all__ = ["watch", "unwatch"] __all__ = ["watch", "unwatch"]