Use watchdog to reload scripts automatically.

This commit is contained in:
Matthew Shao 2015-11-11 09:03:05 +08:00
parent 3739e1fe82
commit 3f6521f912
3 changed files with 27 additions and 0 deletions

View File

@ -225,6 +225,7 @@ class ConsoleMaster(flow.FlowMaster):
if err: if err:
print >> sys.stderr, "Script load error:", err print >> sys.stderr, "Script load error:", err
sys.exit(1) sys.exit(1)
script.ObserveScripts(self, i)
if options.outfile: if options.outfile:
err = self.start_stream_to_path( err = self.start_stream_to_path(

View File

@ -667,6 +667,10 @@ class FlowMaster(controller.Master):
self.add_event("Script error:\n" + str(e), "error") self.add_event("Script error:\n" + str(e), "error")
self.scripts.remove(script_obj) self.scripts.remove(script_obj)
def reload_scripts(self):
for s in self.scripts[:]:
s.load()
def load_script(self, command): def load_script(self, command):
""" """
Loads a script. Returns an error description if something went Loads a script. Returns an error description if something went

View File

@ -4,6 +4,9 @@ import traceback
import threading import threading
import shlex import shlex
import sys import sys
from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler, FileModifiedEvent
from .console import signals
class ScriptError(Exception): class ScriptError(Exception):
@ -192,3 +195,22 @@ def concurrent(fn):
return _concurrent return _concurrent
raise NotImplementedError( raise NotImplementedError(
"Concurrent decorator not supported for '%s' method." % fn.func_name) "Concurrent decorator not supported for '%s' method." % fn.func_name)
class ScriptModified(PatternMatchingEventHandler):
def __init__(self, FlowMaster):
self.FlowMaster = FlowMaster
PatternMatchingEventHandler.__init__(self, ignore_directories=True, patterns=["*.py"])
def on_modified(self, event=FileModifiedEvent):
self.FlowMaster.reload_scripts()
signals.status_message.send(message="script: <{0}> reloaded.".format(event.src_path))
def ObserveScripts(FlowMaster, path):
script_dir = os.path.dirname(path)
event_handler = ScriptModified(FlowMaster)
observer = Observer()
observer.schedule(event_handler, script_dir)
observer.start()