diff options
Diffstat (limited to 'libmproxy/console/grideditor.py')
-rw-r--r-- | libmproxy/console/grideditor.py | 76 |
1 files changed, 52 insertions, 24 deletions
diff --git a/libmproxy/console/grideditor.py b/libmproxy/console/grideditor.py index 438d0ad7..fe3df509 100644 --- a/libmproxy/console/grideditor.py +++ b/libmproxy/console/grideditor.py @@ -1,6 +1,10 @@ from __future__ import absolute_import -import copy, re, os + +import copy +import re +import os import urwid + from . import common from .. import utils, filt, script from netlib import http_uastrings @@ -15,7 +19,7 @@ footer_editing = [ ] -class SText(common.WWrap): +class SText(urwid.WidgetWrap): def __init__(self, txt, focused, error): txt = txt.encode("string-escape") w = urwid.Text(txt, wrap="any") @@ -26,10 +30,10 @@ class SText(common.WWrap): w = urwid.AttrWrap(w, "focusfield") elif error: w = urwid.AttrWrap(w, "field_error") - common.WWrap.__init__(self, w) + urwid.WidgetWrap.__init__(self, w) def get_text(self): - return self.w.get_text()[0] + return self._w.get_text()[0] def keypress(self, size, key): return key @@ -38,21 +42,21 @@ class SText(common.WWrap): return True -class SEdit(common.WWrap): +class SEdit(urwid.WidgetWrap): def __init__(self, txt): txt = txt.encode("string-escape") w = urwid.Edit(edit_text=txt, wrap="any", multiline=True) w = urwid.AttrWrap(w, "editfield") - common.WWrap.__init__(self, w) + urwid.WidgetWrap.__init__(self, w) def get_text(self): - return self.w.get_text()[0] + return self._w.get_text()[0] def selectable(self): return True -class GridRow(common.WWrap): +class GridRow(urwid.WidgetWrap): def __init__(self, focused, editing, editor, values): self.focused, self.editing, self.editor = focused, editing, editor @@ -76,14 +80,14 @@ class GridRow(common.WWrap): ) if focused is not None: w.set_focus_column(focused) - common.WWrap.__init__(self, w) + urwid.WidgetWrap.__init__(self, w) def get_edit_value(self): return self.editing.get_text() def keypress(self, s, k): if self.editing: - w = self.w.column_widths(s)[self.focused] + w = self._w.column_widths(s)[self.focused] k = self.editing.keypress((w,), k) return k @@ -121,7 +125,9 @@ class GridWalker(urwid.ListWalker): try: val = val.decode("string-escape") except ValueError: - self.editor.master.statusbar.message("Invalid Python-style string encoding.", 1000) + self.editor.master.statusbar.message( + "Invalid Python-style string encoding.", 1000 + ) return errors = self.lst[self.focus][1] emsg = self.editor.is_error(self.focus_col, val) @@ -155,7 +161,9 @@ class GridWalker(urwid.ListWalker): def start_edit(self): if self.lst: - self.editing = GridRow(self.focus_col, True, self.editor, self.lst[self.focus]) + self.editing = GridRow( + self.focus_col, True, self.editor, self.lst[self.focus] + ) self.editor.master.statusbar.update(footer_editing) self._modified() @@ -187,7 +195,12 @@ class GridWalker(urwid.ListWalker): if self.editing: return self.editing, self.focus elif self.lst: - return GridRow(self.focus_col, False, self.editor, self.lst[self.focus]), self.focus + return GridRow( + self.focus_col, + False, + self.editor, + self.lst[self.focus] + ), self.focus else: return None, None @@ -213,10 +226,13 @@ class GridListBox(urwid.ListBox): FIRST_WIDTH_MAX = 40 FIRST_WIDTH_MIN = 20 -class GridEditor(common.WWrap): + + +class GridEditor(urwid.WidgetWrap): title = None columns = None headings = None + def __init__(self, master, value, callback, *cb_args, **cb_kwargs): value = copy.deepcopy(value) self.master, self.value, self.callback = master, value, callback @@ -248,7 +264,7 @@ class GridEditor(common.WWrap): self.walker = GridWalker(self.value, self) self.lb = GridListBox(self.walker) - self.w = urwid.Frame( + self._w = urwid.Frame( self.lb, header = urwid.Pile([title, h]) ) @@ -257,9 +273,9 @@ class GridEditor(common.WWrap): def show_empty_msg(self): if self.walker.lst: - self.w.set_footer(None) + self._w.set_footer(None) else: - self.w.set_footer( + self._w.set_footer( urwid.Text( [ ("highlight", "No values. Press "), @@ -297,7 +313,7 @@ class GridEditor(common.WWrap): if self.walker.focus == pf and self.walker.focus_col != pfc: self.walker.start_edit() else: - self.w.keypress(size, key) + self._w.keypress(size, key) return None key = common.shortcuts(key) @@ -325,7 +341,9 @@ class GridEditor(common.WWrap): self.master.path_prompt("Read file: ", "", self.read_file) elif key == "R": if self.walker.get_current_value() is not None: - self.master.path_prompt("Read unescaped file: ", "", self.read_file, True) + self.master.path_prompt( + "Read unescaped file: ", "", self.read_file, True + ) elif key == "e": o = self.walker.get_current_value() if o is not None: @@ -336,7 +354,7 @@ class GridEditor(common.WWrap): elif key in ["enter"]: self.walker.start_edit() elif not self.handle_key(key): - return self.w.keypress(size, key) + return self._w.keypress(size, key) def is_error(self, col, val): """ @@ -362,7 +380,9 @@ class GridEditor(common.WWrap): ("tab", "next field"), ("enter", "edit field"), ] - text.extend(common.format_keyvals(keys, key="key", val="text", indent=4)) + text.extend( + common.format_keyvals(keys, key="key", val="text", indent=4) + ) text.append( urwid.Text( [ @@ -384,6 +404,7 @@ class HeaderEditor(GridEditor): title = "Editing headers" columns = 2 headings = ("Key", "Value") + def make_help(self): h = GridEditor.make_help(self) text = [] @@ -391,7 +412,9 @@ class HeaderEditor(GridEditor): keys = [ ("U", "add User-Agent header"), ] - text.extend(common.format_keyvals(keys, key="key", val="text", indent=4)) + text.extend( + common.format_keyvals(keys, key="key", val="text", indent=4) + ) text.append(urwid.Text([("text", "\n")])) text.extend(h) return text @@ -426,6 +449,7 @@ class ReplaceEditor(GridEditor): title = "Editing replacement patterns" columns = 3 headings = ("Filter", "Regex", "Replacement") + def is_error(self, col, val): if col == 0: if not filt.parse(val): @@ -442,6 +466,7 @@ class SetHeadersEditor(GridEditor): title = "Editing header set patterns" columns = 3 headings = ("Filter", "Header", "Value") + def is_error(self, col, val): if col == 0: if not filt.parse(val): @@ -455,7 +480,9 @@ class SetHeadersEditor(GridEditor): keys = [ ("U", "add User-Agent header"), ] - text.extend(common.format_keyvals(keys, key="key", val="text", indent=4)) + text.extend( + common.format_keyvals(keys, key="key", val="text", indent=4) + ) text.append(urwid.Text([("text", "\n")])) text.extend(h) return text @@ -491,6 +518,7 @@ class ScriptEditor(GridEditor): title = "Editing scripts" columns = 1 headings = ("Command",) + def is_error(self, col, val): try: script.Script.parse_command(val) @@ -507,4 +535,4 @@ class HostPatternEditor(GridEditor): try: re.compile(val, re.IGNORECASE) except re.error as e: - return "Invalid regex: %s" % str(e)
\ No newline at end of file + return "Invalid regex: %s" % str(e) |