mirror of
https://github.com/Grasscutters/mitmproxy.git
synced 2024-11-23 00:01:36 +00:00
Use watchdog to reload scripts automatically.
This commit is contained in:
parent
3739e1fe82
commit
3f6521f912
@ -225,6 +225,7 @@ class ConsoleMaster(flow.FlowMaster):
|
||||
if err:
|
||||
print >> sys.stderr, "Script load error:", err
|
||||
sys.exit(1)
|
||||
script.ObserveScripts(self, i)
|
||||
|
||||
if options.outfile:
|
||||
err = self.start_stream_to_path(
|
||||
|
@ -667,6 +667,10 @@ class FlowMaster(controller.Master):
|
||||
self.add_event("Script error:\n" + str(e), "error")
|
||||
self.scripts.remove(script_obj)
|
||||
|
||||
def reload_scripts(self):
|
||||
for s in self.scripts[:]:
|
||||
s.load()
|
||||
|
||||
def load_script(self, command):
|
||||
"""
|
||||
Loads a script. Returns an error description if something went
|
||||
|
@ -4,6 +4,9 @@ import traceback
|
||||
import threading
|
||||
import shlex
|
||||
import sys
|
||||
from watchdog.observers import Observer
|
||||
from watchdog.events import PatternMatchingEventHandler, FileModifiedEvent
|
||||
from .console import signals
|
||||
|
||||
|
||||
class ScriptError(Exception):
|
||||
@ -192,3 +195,22 @@ def concurrent(fn):
|
||||
return _concurrent
|
||||
raise NotImplementedError(
|
||||
"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()
|
||||
|
Loading…
Reference in New Issue
Block a user