From 120c8db8a413018bde60d156f480ade001b492ef Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Sun, 22 Mar 2015 16:59:11 +1300 Subject: [PATCH] console: refactor the way we keep global view state --- libmproxy/console/__init__.py | 99 ++++++++++++++--------------- libmproxy/console/flowdetailview.py | 5 +- libmproxy/console/grideditor.py | 10 +-- libmproxy/console/help.py | 5 +- libmproxy/console/statusbar.py | 7 +- 5 files changed, 60 insertions(+), 66 deletions(-) diff --git a/libmproxy/console/__init__.py b/libmproxy/console/__init__.py index d988ba841..f6f8e7210 100644 --- a/libmproxy/console/__init__.py +++ b/libmproxy/console/__init__.py @@ -188,8 +188,6 @@ class ConsoleMaster(flow.FlowMaster): self.eventlog = options.eventlog self.eventlist = urwid.SimpleListWalker([]) - self.statusbar = None - if options.client_replay: self.client_playback_path(options.client_replay) @@ -287,12 +285,7 @@ class ConsoleMaster(flow.FlowMaster): try: return flow.read_flows_from_paths([path]) except flow.FlowReadError as e: - if not self.statusbar: - print >> sys.stderr, e.strerror - sys.exit(1) - else: - signals.status_message.send(message=e.strerror) - return None + signals.status_message.send(message=e.strerror) def client_playback_path(self, path): flows = self._readflows(path) @@ -326,7 +319,9 @@ class ConsoleMaster(flow.FlowMaster): try: subprocess.call(cmd) except: - signals.status_message.send(message="Can't start editor: %s" % " ".join(c)) + signals.status_message.send( + message = "Can't start editor: %s" % " ".join(c) + ) else: data = open(name, "rb").read() self.ui.start() @@ -386,17 +381,11 @@ class ConsoleMaster(flow.FlowMaster): self.ui.set_terminal_properties(256) self.ui.register_palette(self.palette.palette()) self.flow_list_walker = flowlist.FlowListWalker(self, self.state) - self.view = None - self.statusbar = None - self.header = None - self.body = None self.help_context = None - self.onekey = False self.loop = urwid.MainLoop( - self.view, + urwid.SolidFill("x"), screen = self.ui, ) - self.view_flowlist() self.server.start_slave( controller.Slave, @@ -425,6 +414,11 @@ class ConsoleMaster(flow.FlowMaster): raise urwid.ExitMainLoop signal.signal(signal.SIGINT, exit) + self.loop.set_alarm_in( + 0.0001, + lambda *args: self.view_flowlist() + ) + try: self.loop.run() except Exception: @@ -438,43 +432,38 @@ class ConsoleMaster(flow.FlowMaster): sys.stderr.flush() self.shutdown() - def make_view(self): - self.view = window.Window( - self, - self.body, - header = self.header, - footer = self.statusbar - ) - return self.view - def view_help(self): - h = help.HelpView( + self.loop.widget = window.Window( self, - self.help_context, - (self.statusbar, self.body, self.header) + help.HelpView( + self, + self.help_context, + self.loop.widget, + ), + None, + statusbar.StatusBar(self, help.footer) ) - self.statusbar = statusbar.StatusBar(self, help.footer) - self.body = h - self.header = None - self.loop.widget = self.make_view() def view_flowdetails(self, flow): - h = flowdetailview.FlowDetailsView( + self.loop.widget = window.Window( self, - flow, - (self.statusbar, self.body, self.header) + flowdetailview.FlowDetailsView( + self, + flow, + self.loop.widget + ), + None, + statusbar.StatusBar(self, flowdetailview.footer) ) - self.statusbar = statusbar.StatusBar(self, flowdetailview.footer) - self.body = h - self.header = None - self.loop.widget = self.make_view() def view_grideditor(self, ge): - self.body = ge - self.header = None self.help_context = ge.make_help() - self.statusbar = statusbar.StatusBar(self, grideditor.footer) - self.loop.widget = self.make_view() + self.loop.widget = window.Window( + self, + ge, + None, + statusbar.StatusBar(self, grideditor.FOOTER) + ) def view_flowlist(self): if self.ui.started: @@ -483,24 +472,30 @@ class ConsoleMaster(flow.FlowMaster): self.state.set_focus(self.state.flow_count()) if self.eventlog: - self.body = flowlist.BodyPile(self) + body = flowlist.BodyPile(self) else: - self.body = flowlist.FlowListBox(self) - self.statusbar = statusbar.StatusBar(self, flowlist.footer) - self.header = None + body = flowlist.FlowListBox(self) self.state.view_mode = common.VIEW_LIST - self.loop.widget = self.make_view() self.help_context = flowlist.help_context + self.loop.widget = window.Window( + self, + body, + None, + statusbar.StatusBar(self, flowlist.footer) + ) + self.loop.draw_screen() def view_flow(self, flow): - self.body = flowview.FlowView(self, self.state, flow) - self.header = flowview.FlowViewHeader(self, flow) - self.statusbar = statusbar.StatusBar(self, flowview.footer) self.state.set_focus_flow(flow) self.state.view_mode = common.VIEW_FLOW - self.loop.widget = self.make_view() self.help_context = flowview.help_context + self.loop.widget = window.Window( + self, + flowview.FlowView(self, self.state, flow), + flowview.FlowViewHeader(self, flow), + statusbar.StatusBar(self, flowview.footer) + ) def _write_flows(self, path, flows): if not path: diff --git a/libmproxy/console/flowdetailview.py b/libmproxy/console/flowdetailview.py index f351bff10..15350ea19 100644 --- a/libmproxy/console/flowdetailview.py +++ b/libmproxy/console/flowdetailview.py @@ -18,10 +18,7 @@ class FlowDetailsView(urwid.ListBox): def keypress(self, size, key): key = common.shortcuts(key) if key == "q": - self.master.statusbar = self.state[0] - self.master.body = self.state[1] - self.master.header = self.state[2] - self.master.loop.widget = self.master.make_view() + self.master.loop.widget = self.state return None elif key == "?": key = None diff --git a/libmproxy/console/grideditor.py b/libmproxy/console/grideditor.py index dc3bad0eb..a1d662c8c 100644 --- a/libmproxy/console/grideditor.py +++ b/libmproxy/console/grideditor.py @@ -10,11 +10,11 @@ from .. import utils, filt, script from netlib import http_uastrings -footer = [ +FOOTER = [ ('heading_key', "enter"), ":edit ", ('heading_key', "q"), ":back ", ] -footer_editing = [ +FOOTER_EDITING = [ ('heading_key', "esc"), ":stop editing ", ] @@ -164,12 +164,12 @@ class GridWalker(urwid.ListWalker): self.editing = GridRow( self.focus_col, True, self.editor, self.lst[self.focus] ) - self.editor.master.statusbar.update(footer_editing) + self.editor.master.loop.widget.footer.update(FOOTER_EDITING) self._modified() def stop_edit(self): if self.editing: - self.editor.master.statusbar.update(footer) + self.editor.master.loop.widget.footer.update(FOOTER) self.set_current_value(self.editing.get_edit_value(), False) self.editing = False self._modified() @@ -268,7 +268,7 @@ class GridEditor(urwid.WidgetWrap): self.lb, header = urwid.Pile([title, h]) ) - self.master.statusbar.update("") + self.master.loop.widget.footer.update("") self.show_empty_msg() def show_empty_msg(self): diff --git a/libmproxy/console/help.py b/libmproxy/console/help.py index 6bb49a921..109a97920 100644 --- a/libmproxy/console/help.py +++ b/libmproxy/console/help.py @@ -180,10 +180,7 @@ class HelpView(urwid.ListBox): def keypress(self, size, key): key = common.shortcuts(key) if key == "q": - self.master.statusbar = self.state[0] - self.master.body = self.state[1] - self.master.header = self.state[2] - self.master.loop.widget = self.master.make_view() + self.master.loop.widget = self.state return None elif key == "?": key = None diff --git a/libmproxy/console/statusbar.py b/libmproxy/console/statusbar.py index 7663ee44b..7fb15aa60 100644 --- a/libmproxy/console/statusbar.py +++ b/libmproxy/console/statusbar.py @@ -114,6 +114,7 @@ class StatusBar(urwid.WidgetWrap): self.ib = urwid.WidgetWrap(urwid.Text("")) self._w = urwid.Pile([self.ib, self.ab]) signals.update_settings.connect(self.sig_update_settings) + self.redraw() def sig_update_settings(self, sender): self.redraw() @@ -188,7 +189,11 @@ class StatusBar(urwid.WidgetWrap): if self.master.state.follow_focus: opts.append("following") if self.master.stream_large_bodies: - opts.append("stream:%s" % utils.pretty_size(self.master.stream_large_bodies.max_size)) + opts.append( + "stream:%s" % utils.pretty_size( + self.master.stream_large_bodies.max_size + ) + ) if opts: r.append("[%s]"%(":".join(opts)))