Merge pull request #1691 from cortesi/consoleclean1

console: various cleanups
This commit is contained in:
Aldo Cortesi 2016-10-30 17:21:12 +13:00 committed by GitHub
commit 897d5ddc87
5 changed files with 34 additions and 43 deletions

View File

@ -80,13 +80,13 @@ Logging and the context
Scripts should not output straight to stderr or stdout. Instead, the `log Scripts should not output straight to stderr or stdout. Instead, the `log
<api.html#mitmproxy.controller.Log>`_ object on the ``ctx`` contexzt module <api.html#mitmproxy.controller.Log>`_ object on the ``ctx`` contexzt module
should be used, so that the mitmproxy host program can handle output should be used, so that the mitmproxy host program can handle output
appropriately. So, mitmdump can print colorised sript output to the terminal, appropriately. So, mitmdump can print colorised script output to the terminal,
and mitmproxy console can place script output in the event buffer. and mitmproxy console can place script output in the event buffer.
Here's how this looks: Here's how this looks:
.. literalinclude:: ../../examples/logging.py .. literalinclude:: ../../examples/context_logging.py
:caption: :src:`examples/logging.py` :caption: :src:`examples/context_logging.py`
:language: python :language: python
The ``ctx`` module also exposes the mitmproxy master object at ``ctx.master`` The ``ctx`` module also exposes the mitmproxy master object at ``ctx.master``

View File

@ -211,6 +211,7 @@ class Focus:
def __init__(self, v: View) -> None: def __init__(self, v: View) -> None:
self.view = v self.view = v
self._flow = None self._flow = None
self.sig_change = blinker.Signal()
if len(self.view): if len(self.view):
self.flow = self.view[0] self.flow = self.view[0]
v.sig_add.connect(self._sig_add) v.sig_add.connect(self._sig_add)
@ -226,6 +227,7 @@ class Focus:
if f is not None and f not in self.view: if f is not None and f not in self.view:
raise ValueError("Attempt to set focus to flow not in view") raise ValueError("Attempt to set focus to flow not in view")
self._flow = f self._flow = f
self.sig_change.send(self)
@property @property
def index(self) -> typing.Optional[int]: def index(self) -> typing.Optional[int]:

View File

@ -107,11 +107,10 @@ class BodyPile(urwid.Pile):
return self.focus_item.keypress(tsize, key) return self.focus_item.keypress(tsize, key)
class ConnectionItem(urwid.WidgetWrap): class FlowItem(urwid.WidgetWrap):
def __init__(self, master, view, flow, focus): def __init__(self, master, flow):
self.master, self.view, self.flow = master, view, flow self.master, self.flow = master, flow
self.f = focus
w = self.get_text() w = self.get_text()
urwid.WidgetWrap.__init__(self, w) urwid.WidgetWrap.__init__(self, w)
@ -119,7 +118,7 @@ class ConnectionItem(urwid.WidgetWrap):
cols, _ = self.master.ui.get_cols_rows() cols, _ = self.master.ui.get_cols_rows()
return common.format_flow( return common.format_flow(
self.flow, self.flow,
self.f, self.flow is self.master.view.focus.flow,
hostheader=self.master.options.showhost, hostheader=self.master.options.showhost,
max_url_len=cols, max_url_len=cols,
) )
@ -163,13 +162,11 @@ class ConnectionItem(urwid.WidgetWrap):
elif key == "d": elif key == "d":
if self.flow.killable: if self.flow.killable:
self.flow.kill(self.master) self.flow.kill(self.master)
self.view.remove(self.view.focus.flow) self.master.view.remove(self.master.view.focus.flow)
signals.flowlist_change.send(self)
elif key == "D": elif key == "D":
cp = self.flow.copy() cp = self.flow.copy()
self.master.view.add(cp) self.master.view.add(cp)
self.master.view.focus.flow = cp self.master.view.focus.flow = cp
signals.flowlist_change.send(self)
elif key == "m": elif key == "m":
self.flow.marked = not self.flow.marked self.flow.marked = not self.flow.marked
signals.flowlist_change.send(self) signals.flowlist_change.send(self)
@ -205,7 +202,7 @@ class ConnectionItem(urwid.WidgetWrap):
callback = self.server_replay_prompt, callback = self.server_replay_prompt,
) )
elif key == "U": elif key == "U":
for f in self.view: for f in self.master.view:
f.marked = False f.marked = False
signals.flowlist_change.send(self) signals.flowlist_change.send(self)
elif key == "V": elif key == "V":
@ -261,49 +258,49 @@ class ConnectionItem(urwid.WidgetWrap):
class FlowListWalker(urwid.ListWalker): class FlowListWalker(urwid.ListWalker):
def __init__(self, master, view): def __init__(self, master):
self.master, self.view = master, view self.master = master
self.view.sig_refresh.connect(self.sig_mod) self.master.view.sig_refresh.connect(self.sig_mod)
self.view.sig_add.connect(self.sig_mod) self.master.view.sig_add.connect(self.sig_mod)
self.view.sig_remove.connect(self.sig_mod) self.master.view.sig_remove.connect(self.sig_mod)
self.view.sig_update.connect(self.sig_mod) self.master.view.sig_update.connect(self.sig_mod)
self.master.view.focus.sig_change.connect(self.sig_mod)
signals.flowlist_change.connect(self.sig_mod) signals.flowlist_change.connect(self.sig_mod)
def sig_mod(self, *args, **kwargs): def sig_mod(self, *args, **kwargs):
self._modified() self._modified()
def get_focus(self): def get_focus(self):
if not self.view.focus.flow: if not self.master.view.focus.flow:
return None, 0 return None, 0
return ConnectionItem( f = FlowItem(self.master, self.master.view.focus.flow)
self.master, self.view, self.view.focus.flow, True return f, self.master.view.focus.index
), self.view.focus.index
def set_focus(self, index): def set_focus(self, index):
if self.view.inbounds(index): if self.master.view.inbounds(index):
self.view.focus.index = index self.master.view.focus.index = index
signals.flowlist_change.send(self) signals.flowlist_change.send(self)
def get_next(self, pos): def get_next(self, pos):
pos = pos + 1 pos = pos + 1
if not self.view.inbounds(pos): if not self.master.view.inbounds(pos):
return None, None return None, None
f = ConnectionItem(self.master, self.view, self.view[pos], False) f = FlowItem(self.master, self.master.view[pos])
return f, pos return f, pos
def get_prev(self, pos): def get_prev(self, pos):
pos = pos - 1 pos = pos - 1
if not self.view.inbounds(pos): if not self.master.view.inbounds(pos):
return None, None return None, None
f = ConnectionItem(self.master, self.view, self.view[pos], False) f = FlowItem(self.master, self.master.view[pos])
return f, pos return f, pos
class FlowListBox(urwid.ListBox): class FlowListBox(urwid.ListBox):
def __init__(self, master: "mitmproxy.console.master.ConsoleMaster"): def __init__(self, master):
self.master = master self.master = master
super().__init__(FlowListWalker(master, master.view)) super().__init__(FlowListWalker(master))
def get_method_raw(self, k): def get_method_raw(self, k):
if k: if k:
@ -340,7 +337,6 @@ class FlowListBox(urwid.ListBox):
scheme, host, port, path = parts scheme, host, port, path = parts
f = self.master.create_request(method, scheme, host, port, path) f = self.master.create_request(method, scheme, host, port, path)
self.master.view.focus.flow = f self.master.view.focus.flow = f
signals.flowlist_change.send(self)
def keypress(self, size, key): def keypress(self, size, key):
key = common.shortcuts(key) key = common.shortcuts(key)
@ -351,15 +347,12 @@ class FlowListBox(urwid.ListBox):
signals.flowlist_change.send(self) signals.flowlist_change.send(self)
elif key == "z": elif key == "z":
self.master.view.clear() self.master.view.clear()
signals.flowlist_change.send(self)
elif key == "e": elif key == "e":
self.master.toggle_eventlog() self.master.toggle_eventlog()
elif key == "g": elif key == "g":
self.master.view.focus.index = 0 self.master.view.focus.index = 0
signals.flowlist_change.send(self)
elif key == "G": elif key == "G":
self.master.view.focus.index = len(self.master.view) - 1 self.master.view.focus.index = len(self.master.view) - 1
signals.flowlist_change.send(self)
elif key == "f": elif key == "f":
signals.status_prompt.send( signals.status_prompt.send(
prompt = "Filter View", prompt = "Filter View",

View File

@ -436,17 +436,10 @@ class ConsoleMaster(master.Master):
signals.flowlist_change.send(self) signals.flowlist_change.send(self)
return reterr return reterr
def edit_scripts(self, scripts):
self.options.scripts = [x[0] for x in scripts]
def quit(self, a): def quit(self, a):
if a != "n": if a != "n":
raise urwid.ExitMainLoop raise urwid.ExitMainLoop
def refresh_focus(self):
if self.view.focus.flow:
signals.flow_change.send(self, flow = self.view.focus.flow)
def clear_events(self): def clear_events(self):
self.logbuffer[:] = [] self.logbuffer[:] = []

View File

@ -217,11 +217,13 @@ class Options(urwid.WidgetWrap):
) )
def scripts(self): def scripts(self):
def edit_scripts(scripts):
self.master.options.scripts = [x[0] for x in scripts]
self.master.view_grideditor( self.master.view_grideditor(
grideditor.ScriptEditor( grideditor.ScriptEditor(
self.master, self.master,
[[i] for i in self.master.options.scripts], [[i] for i in self.master.options.scripts],
self.master.edit_scripts edit_scripts
) )
) )
@ -235,7 +237,8 @@ class Options(urwid.WidgetWrap):
def change_default_display_mode(self, t): def change_default_display_mode(self, t):
v = contentviews.get_by_shortcut(t) v = contentviews.get_by_shortcut(t)
self.master.options.default_contentview = v.name self.master.options.default_contentview = v.name
self.master.refresh_focus() if self.master.view.focus.flow:
signals.flow_change.send(self, flow = self.master.view.focus.flow)
def sticky_auth(self): def sticky_auth(self):
signals.status_prompt.send( signals.status_prompt.send(