Move the observer initalization to the constructor of Script

class.Should work with both mitmproxy and mitmdump now.
Change the names in PEP8 style.
This commit is contained in:
Matthew Shao 2015-11-11 20:59:12 +08:00
parent 3f6521f912
commit 2522ba69fa
2 changed files with 9 additions and 9 deletions

View File

@ -225,7 +225,6 @@ 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(

View File

@ -6,7 +6,6 @@ import shlex
import sys
from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler, FileModifiedEvent
from .console import signals
class ScriptError(Exception):
@ -71,6 +70,7 @@ class Script:
self.ctx = ScriptContext(master)
self.ns = None
self.load()
observe_scripts(master, self.args[0])
@classmethod
def parse_command(cls, command):
@ -199,18 +199,19 @@ def concurrent(fn):
class ScriptModified(PatternMatchingEventHandler):
def __init__(self, FlowMaster):
self.FlowMaster = FlowMaster
def __init__(self, flow_master):
self.flow_master = flow_master
PatternMatchingEventHandler.__init__(self, ignore_directories=True, patterns=["*.py"])
self.context = ScriptContext(self.flow_master)
def on_modified(self, event=FileModifiedEvent):
self.FlowMaster.reload_scripts()
signals.status_message.send(message="script: <{0}> reloaded.".format(event.src_path))
self.flow_master.reload_scripts()
self.context.log("script: <{}> reloaded.".format(event.src_path))
def ObserveScripts(FlowMaster, path):
def observe_scripts(flow_master, path):
script_dir = os.path.dirname(path)
event_handler = ScriptModified(FlowMaster)
event_handler = ScriptModified(flow_master)
observer = Observer()
observer.schedule(event_handler, script_dir)
observer.start()