Merge pull request #2489 from MatthewShao/options-into-own-addon

Move console-related options into their own addon
This commit is contained in:
Maximilian Hils 2017-08-16 22:52:02 +02:00 committed by GitHub
commit a006cab5ce
11 changed files with 97 additions and 78 deletions

View File

@ -202,6 +202,10 @@ class AddonManager:
def __str__(self):
return pprint.pformat([str(i) for i in self.chain])
def __contains__(self, item):
name = _get_name(item)
return name in self.lookup
def handle_lifecycle(self, name, message):
"""
Handle a lifecycle event.

View File

@ -145,6 +145,21 @@ class View(collections.Sequence):
self.focus = Focus(self)
self.settings = Settings(self)
def load(self, loader):
loader.add_option(
"view_order", str, "time",
"Flow sort order.",
choices=list(map(lambda c: c[1], orders)),
)
loader.add_option(
"view_order_reversed", bool, False,
"Reverse the sorting order."
)
loader.add_option(
"console_focus_follow", bool, False,
"Focus follows new flows."
)
def store_count(self):
return len(self._store)
@ -442,14 +457,14 @@ class View(collections.Sequence):
"Invalid interception filter: %s" % ctx.options.view_filter
)
self.set_filter(filt)
if "console_order" in updated:
if ctx.options.console_order not in self.orders:
if "view_order" in updated:
if ctx.options.view_order not in self.orders:
raise exceptions.OptionsError(
"Unknown flow order: %s" % ctx.options.console_order
"Unknown flow order: %s" % ctx.options.view_order
)
self.set_order(self.orders[ctx.options.console_order])
if "console_order_reversed" in updated:
self.set_reversed(ctx.options.console_order_reversed)
self.set_order(self.orders[ctx.options.view_order])
if "view_order_reversed" in updated:
self.set_reversed(ctx.options.view_order_reversed)
if "console_focus_follow" in updated:
self.focus_follow = ctx.options.console_focus_follow

View File

@ -4,29 +4,6 @@ from mitmproxy import optmanager
from mitmproxy import contentviews
from mitmproxy.net import tcp
# We redefine these here for now to avoid importing Urwid-related guff on
# platforms that don't support it, and circular imports. We can do better using
# a lazy checker down the track.
console_palettes = [
"lowlight",
"lowdark",
"light",
"dark",
"solarized_light",
"solarized_dark"
]
view_orders = [
"time",
"method",
"url",
"size",
]
console_layouts = [
"single",
"vertical",
"horizontal",
]
log_verbosity = [
"error",
"warn",
@ -475,43 +452,6 @@ class Options(optmanager.OptManager):
"Intercept filter expression."
)
# Console options
self.add_option(
"console_layout", str, "single",
"Console layout.",
choices=sorted(console_layouts),
)
self.add_option(
"console_layout_headers", bool, True,
"Show layout comonent headers",
)
self.add_option(
"console_focus_follow", bool, False,
"Focus follows new flows."
)
self.add_option(
"console_palette", str, "solarized_dark",
"Color palette.",
choices=sorted(console_palettes),
)
self.add_option(
"console_palette_transparent", bool, False,
"Set transparent background for palette."
)
self.add_option(
"console_mouse", bool, True,
"Console mouse interaction."
)
self.add_option(
"console_order", str, "time",
"Flow sort order.",
choices=view_orders,
)
self.add_option(
"console_order_reversed", bool, False,
"Reverse the sorting order."
)
self.add_option(
"view_filter", Optional[str], None,
"Limit which flows are displayed."

View File

@ -105,6 +105,8 @@ class context:
Options object with the given keyword arguments, then calls the
configure method on the addon with the updated value.
"""
if addon not in self.master.addons:
self.master.addons.register(addon)
with self.options.rollback(kwargs.keys(), reraise=True):
self.options.update(**kwargs)
self.master.addons.invoke_addon(

View File

@ -48,7 +48,7 @@ def common_options(parser, opts):
)
parser.add_argument(
"-v", "--verbose",
action="store_const", dest="verbose", const=3,
action="store_const", dest="verbose", const='debug',
help="Increase log verbosity."
)

View File

@ -11,6 +11,26 @@ from mitmproxy.tools.console import overlay
from mitmproxy.tools.console import signals
from mitmproxy.tools.console import keymap
console_palettes = [
"lowlight",
"lowdark",
"light",
"dark",
"solarized_light",
"solarized_dark"
]
view_orders = [
"time",
"method",
"url",
"size",
]
console_layouts = [
"single",
"vertical",
"horizontal",
]
class Logger:
def log(self, evt):
@ -60,6 +80,34 @@ class ConsoleAddon:
self.master = master
self.started = False
def load(self, loader):
loader.add_option(
"console_layout", str, "single",
"Console layout.",
choices=sorted(console_layouts),
)
loader.add_option(
"console_layout_headers", bool, True,
"Show layout comonent headers",
)
loader.add_option(
"console_focus_follow", bool, False,
"Focus follows new flows."
)
loader.add_option(
"console_palette", str, "solarized_dark",
"Color palette.",
choices=sorted(console_palettes),
)
loader.add_option(
"console_palette_transparent", bool, False,
"Set transparent background for palette."
)
loader.add_option(
"console_mouse", bool, True,
"Console mouse interaction."
)
@command.command("console.layout.options")
def layout_options(self) -> typing.Sequence[str]:
"""

View File

@ -22,6 +22,12 @@ class EventLog(urwid.ListBox, layoutwidget.LayoutWidget):
signals.sig_add_log.connect(self.sig_add_log)
signals.sig_clear_log.connect(self.sig_clear_log)
def load(self, loader):
loader.add_option(
"console_focus_follow", bool, False,
"Focus follows new flows."
)
def set_focus(self, index):
if 0 <= index < len(self.walker):
super().set_focus(index)

View File

@ -199,10 +199,10 @@ class StatusBar(urwid.WidgetWrap):
r.append("[")
r.append(("heading_key", "M"))
r.append(":%s]" % self.master.options.default_contentview)
if self.master.options.has_changed("console_order"):
if self.master.options.has_changed("view_order"):
r.append("[")
r.append(("heading_key", "o"))
r.append(":%s]" % self.master.options.console_order)
r.append(":%s]" % self.master.options.view_order)
opts = []
if self.master.options.anticache:
@ -244,7 +244,7 @@ class StatusBar(urwid.WidgetWrap):
else:
offset = self.master.view.focus.index + 1
if self.master.options.console_order_reversed:
if self.master.options.view_order_reversed:
arrow = common.SYMBOL_UP
else:
arrow = common.SYMBOL_DOWN

View File

@ -7,6 +7,7 @@ from mitmproxy import flowfilter
from mitmproxy import exceptions
from mitmproxy import io
from mitmproxy.test import taddons
from mitmproxy.tools.console import consoleaddons
def tft(*, method="get", start=0):
@ -27,7 +28,7 @@ def test_order_refresh():
tf = tflow.tflow(resp=True)
with taddons.context() as tctx:
tctx.configure(v, console_order="time")
tctx.configure(v, view_order="time")
v.add([tf])
tf.request.timestamp_start = 1
assert not sargs
@ -300,12 +301,12 @@ def test_order():
v.request(tft(method="put", start=4))
assert [i.request.timestamp_start for i in v] == [1, 2, 3, 4]
tctx.configure(v, console_order="method")
tctx.configure(v, view_order="method")
assert [i.request.method for i in v] == ["GET", "GET", "PUT", "PUT"]
v.set_reversed(True)
assert [i.request.method for i in v] == ["PUT", "PUT", "GET", "GET"]
tctx.configure(v, console_order="time")
tctx.configure(v, view_order="time")
assert [i.request.timestamp_start for i in v] == [4, 3, 2, 1]
v.set_reversed(False)
@ -425,6 +426,8 @@ def test_signals():
def test_focus_follow():
v = view.View()
with taddons.context() as tctx:
console_addon = consoleaddons.ConsoleAddon(tctx.master)
tctx.configure(console_addon)
tctx.configure(v, console_focus_follow=True, view_filter="~m get")
v.add([tft(start=5)])
@ -546,11 +549,11 @@ def test_configure():
with pytest.raises(Exception, match="Invalid interception filter"):
tctx.configure(v, view_filter="~~")
tctx.configure(v, console_order="method")
tctx.configure(v, view_order="method")
with pytest.raises(Exception, match="Unknown flow order"):
tctx.configure(v, console_order="no")
tctx.configure(v, view_order="no")
tctx.configure(v, console_order_reversed=True)
tctx.configure(v, view_order_reversed=True)
tctx.configure(v, console_focus_follow=True)
assert v.focus_follow

View File

@ -137,6 +137,8 @@ def test_simple():
a.trigger("custom")
assert ta.custom_called
assert ta in a
def test_load_option():
o = options.Options()

View File

@ -13,20 +13,19 @@ def test_statusbar(monkeypatch):
stickycookie="~dst example.com",
stickyauth="~dst example.com",
default_contentview="javascript",
console_order="url",
anticache=True,
anticomp=True,
showhost=True,
refresh_server_playback=False,
replay_kill_extra=True,
upstream_cert=False,
console_focus_follow=True,
stream_large_bodies="3m",
mode="transparent",
scripts=["nonexistent"],
save_stream_file="foo",
)
m = master.ConsoleMaster(o)
m.options.update(view_order='url', console_focus_follow=True)
monkeypatch.setattr(m.addons.get("clientplayback"), "count", lambda: 42)
monkeypatch.setattr(m.addons.get("serverplayback"), "count", lambda: 42)