2014-03-10 21:36:47 +00:00
|
|
|
from __future__ import absolute_import
|
2015-03-13 12:14:37 +00:00
|
|
|
|
|
|
|
import copy
|
|
|
|
import re
|
|
|
|
import os
|
2012-02-07 03:39:37 +00:00
|
|
|
import urwid
|
2015-03-13 12:14:37 +00:00
|
|
|
|
2015-07-15 21:19:01 +00:00
|
|
|
from netlib import odict
|
|
|
|
from netlib.http import user_agents
|
|
|
|
|
2015-03-20 22:19:20 +00:00
|
|
|
from . import common, signals
|
2014-01-13 01:15:17 +00:00
|
|
|
from .. import utils, filt, script
|
2012-02-07 03:39:37 +00:00
|
|
|
|
2012-02-08 08:47:39 +00:00
|
|
|
|
2015-03-22 03:59:11 +00:00
|
|
|
FOOTER = [
|
2012-03-22 01:57:57 +00:00
|
|
|
('heading_key', "enter"), ":edit ",
|
|
|
|
('heading_key', "q"), ":back ",
|
|
|
|
]
|
2015-03-22 03:59:11 +00:00
|
|
|
FOOTER_EDITING = [
|
2012-03-22 01:57:57 +00:00
|
|
|
('heading_key', "esc"), ":stop editing ",
|
|
|
|
]
|
|
|
|
|
2012-02-08 08:47:39 +00:00
|
|
|
|
2015-04-15 22:57:12 +00:00
|
|
|
class TextColumn:
|
|
|
|
subeditor = None
|
|
|
|
|
|
|
|
def __init__(self, heading):
|
|
|
|
self.heading = heading
|
|
|
|
|
|
|
|
def text(self, obj):
|
|
|
|
return SEscaped(obj or "")
|
|
|
|
|
|
|
|
def blank(self):
|
|
|
|
return ""
|
|
|
|
|
2015-04-16 10:01:54 +00:00
|
|
|
def keypress(self, key, editor):
|
|
|
|
if key == "r":
|
|
|
|
if editor.walker.get_current_value() is not None:
|
|
|
|
signals.status_prompt_path.send(
|
|
|
|
self,
|
|
|
|
prompt = "Read file",
|
|
|
|
callback = editor.read_file
|
|
|
|
)
|
|
|
|
elif key == "R":
|
|
|
|
if editor.walker.get_current_value() is not None:
|
|
|
|
signals.status_prompt_path.send(
|
|
|
|
editor,
|
|
|
|
prompt = "Read unescaped file",
|
|
|
|
callback = editor.read_file,
|
|
|
|
args = (True,)
|
|
|
|
)
|
|
|
|
elif key == "e":
|
|
|
|
o = editor.walker.get_current_value()
|
|
|
|
if o is not None:
|
|
|
|
n = editor.master.spawn_editor(o.encode("string-escape"))
|
|
|
|
n = utils.clean_hanging_newline(n)
|
|
|
|
editor.walker.set_current_value(n, False)
|
|
|
|
editor.walker._modified()
|
|
|
|
elif key in ["enter"]:
|
|
|
|
editor.walker.start_edit()
|
|
|
|
else:
|
|
|
|
return key
|
|
|
|
|
2015-04-15 22:57:12 +00:00
|
|
|
|
|
|
|
class SubgridColumn:
|
2016-01-27 09:12:18 +00:00
|
|
|
|
2015-04-15 22:57:12 +00:00
|
|
|
def __init__(self, heading, subeditor):
|
|
|
|
self.heading = heading
|
|
|
|
self.subeditor = subeditor
|
|
|
|
|
|
|
|
def text(self, obj):
|
|
|
|
p = http_cookies._format_pairs(obj, sep="\n")
|
|
|
|
return urwid.Text(p)
|
|
|
|
|
|
|
|
def blank(self):
|
|
|
|
return []
|
|
|
|
|
2015-04-16 10:01:54 +00:00
|
|
|
def keypress(self, key, editor):
|
|
|
|
if key in "rRe":
|
|
|
|
signals.status_message.send(
|
|
|
|
self,
|
|
|
|
message = "Press enter to edit this field.",
|
|
|
|
expire = 1000
|
|
|
|
)
|
|
|
|
return
|
|
|
|
elif key in ["enter"]:
|
|
|
|
editor.master.view_grideditor(
|
|
|
|
self.subeditor(
|
|
|
|
editor.master,
|
|
|
|
editor.walker.get_current_value(),
|
|
|
|
editor.set_subeditor_value,
|
|
|
|
editor.walker.focus,
|
|
|
|
editor.walker.focus_col
|
|
|
|
)
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
return key
|
|
|
|
|
2015-04-15 22:57:12 +00:00
|
|
|
|
|
|
|
class SEscaped(urwid.WidgetWrap):
|
2016-01-27 09:12:18 +00:00
|
|
|
|
2015-04-15 22:57:12 +00:00
|
|
|
def __init__(self, txt):
|
2012-08-25 00:54:34 +00:00
|
|
|
txt = txt.encode("string-escape")
|
2012-02-07 03:39:37 +00:00
|
|
|
w = urwid.Text(txt, wrap="any")
|
2015-03-13 12:03:46 +00:00
|
|
|
urwid.WidgetWrap.__init__(self, w)
|
2012-02-07 03:39:37 +00:00
|
|
|
|
2012-02-08 01:58:48 +00:00
|
|
|
def get_text(self):
|
2015-03-13 12:03:46 +00:00
|
|
|
return self._w.get_text()[0]
|
2012-02-08 01:58:48 +00:00
|
|
|
|
2012-02-07 03:39:37 +00:00
|
|
|
def keypress(self, size, key):
|
|
|
|
return key
|
|
|
|
|
|
|
|
def selectable(self):
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2015-03-13 12:03:46 +00:00
|
|
|
class SEdit(urwid.WidgetWrap):
|
2016-01-27 09:12:18 +00:00
|
|
|
|
2012-02-08 01:07:17 +00:00
|
|
|
def __init__(self, txt):
|
2012-08-25 00:54:34 +00:00
|
|
|
txt = txt.encode("string-escape")
|
2012-02-08 03:43:11 +00:00
|
|
|
w = urwid.Edit(edit_text=txt, wrap="any", multiline=True)
|
|
|
|
w = urwid.AttrWrap(w, "editfield")
|
2015-03-13 12:03:46 +00:00
|
|
|
urwid.WidgetWrap.__init__(self, w)
|
2012-02-08 01:07:17 +00:00
|
|
|
|
2012-02-08 01:58:48 +00:00
|
|
|
def get_text(self):
|
2015-04-15 22:57:12 +00:00
|
|
|
return self._w.get_text()[0].strip()
|
2012-02-08 01:58:48 +00:00
|
|
|
|
2012-02-08 01:07:17 +00:00
|
|
|
def selectable(self):
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2015-03-13 12:03:46 +00:00
|
|
|
class GridRow(urwid.WidgetWrap):
|
2016-01-27 09:12:18 +00:00
|
|
|
|
2012-03-23 00:28:33 +00:00
|
|
|
def __init__(self, focused, editing, editor, values):
|
|
|
|
self.focused, self.editing, self.editor = focused, editing, editor
|
2012-02-08 01:07:17 +00:00
|
|
|
|
2012-03-23 00:28:33 +00:00
|
|
|
errors = values[1]
|
2012-03-17 21:33:11 +00:00
|
|
|
self.fields = []
|
2012-03-23 00:28:33 +00:00
|
|
|
for i, v in enumerate(values[0]):
|
2012-03-17 21:33:11 +00:00
|
|
|
if focused == i and editing:
|
|
|
|
self.editing = SEdit(v)
|
|
|
|
self.fields.append(self.editing)
|
|
|
|
else:
|
2015-04-15 22:57:12 +00:00
|
|
|
w = self.editor.columns[i].text(v)
|
|
|
|
if focused == i:
|
|
|
|
if i in errors:
|
|
|
|
w = urwid.AttrWrap(w, "focusfield_error")
|
|
|
|
else:
|
|
|
|
w = urwid.AttrWrap(w, "focusfield")
|
|
|
|
elif i in errors:
|
|
|
|
w = urwid.AttrWrap(w, "field_error")
|
|
|
|
self.fields.append(w)
|
2012-03-17 21:33:11 +00:00
|
|
|
|
|
|
|
fspecs = self.fields[:]
|
2012-08-19 01:15:54 +00:00
|
|
|
if len(self.fields) > 1:
|
|
|
|
fspecs[0] = ("fixed", self.editor.first_width + 2, fspecs[0])
|
2012-02-08 01:07:17 +00:00
|
|
|
w = urwid.Columns(
|
2012-03-17 21:33:11 +00:00
|
|
|
fspecs,
|
2012-02-08 01:07:17 +00:00
|
|
|
dividechars = 2
|
|
|
|
)
|
|
|
|
if focused is not None:
|
|
|
|
w.set_focus_column(focused)
|
2015-03-13 12:03:46 +00:00
|
|
|
urwid.WidgetWrap.__init__(self, w)
|
2012-02-08 01:07:17 +00:00
|
|
|
|
2012-08-25 00:54:34 +00:00
|
|
|
def get_edit_value(self):
|
|
|
|
return self.editing.get_text()
|
2012-02-08 01:58:48 +00:00
|
|
|
|
2012-02-08 01:07:17 +00:00
|
|
|
def keypress(self, s, k):
|
|
|
|
if self.editing:
|
2015-03-13 12:03:46 +00:00
|
|
|
w = self._w.column_widths(s)[self.focused]
|
2012-03-18 21:57:27 +00:00
|
|
|
k = self.editing.keypress((w,), k)
|
2012-02-08 01:07:17 +00:00
|
|
|
return k
|
|
|
|
|
|
|
|
def selectable(self):
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2012-03-17 20:38:42 +00:00
|
|
|
class GridWalker(urwid.ListWalker):
|
2016-01-27 09:12:18 +00:00
|
|
|
|
2012-03-23 00:28:33 +00:00
|
|
|
"""
|
|
|
|
Stores rows as a list of (rows, errors) tuples, where rows is a list
|
|
|
|
and errors is a set with an entry of each offset in rows that is an
|
|
|
|
error.
|
|
|
|
"""
|
2015-05-30 00:03:28 +00:00
|
|
|
|
2012-02-08 22:30:35 +00:00
|
|
|
def __init__(self, lst, editor):
|
2012-03-23 00:28:33 +00:00
|
|
|
self.lst = [(i, set([])) for i in lst]
|
|
|
|
self.editor = editor
|
2012-02-08 01:07:17 +00:00
|
|
|
self.focus = 0
|
|
|
|
self.focus_col = 0
|
|
|
|
self.editing = False
|
|
|
|
|
2012-02-08 22:30:35 +00:00
|
|
|
def _modified(self):
|
|
|
|
self.editor.show_empty_msg()
|
|
|
|
return urwid.ListWalker._modified(self)
|
|
|
|
|
2012-08-31 01:28:04 +00:00
|
|
|
def add_value(self, lst):
|
|
|
|
self.lst.append((lst[:], set([])))
|
|
|
|
self._modified()
|
|
|
|
|
2012-02-08 05:25:00 +00:00
|
|
|
def get_current_value(self):
|
|
|
|
if self.lst:
|
2012-03-30 06:51:40 +00:00
|
|
|
return self.lst[self.focus][0][self.focus_col]
|
2012-02-08 05:25:00 +00:00
|
|
|
|
2012-08-25 01:23:46 +00:00
|
|
|
def set_current_value(self, val, unescaped):
|
|
|
|
if not unescaped:
|
|
|
|
try:
|
|
|
|
val = val.decode("string-escape")
|
|
|
|
except ValueError:
|
2015-03-20 22:19:20 +00:00
|
|
|
signals.status_message.send(
|
2015-04-15 22:57:12 +00:00
|
|
|
self,
|
|
|
|
message = "Invalid Python-style string encoding.",
|
|
|
|
expire = 1000
|
2015-03-13 12:14:37 +00:00
|
|
|
)
|
2012-08-25 01:23:46 +00:00
|
|
|
return
|
|
|
|
errors = self.lst[self.focus][1]
|
2012-08-25 01:13:16 +00:00
|
|
|
emsg = self.editor.is_error(self.focus_col, val)
|
|
|
|
if emsg:
|
2015-03-20 23:37:00 +00:00
|
|
|
signals.status_message.send(message = emsg, expire = 1)
|
2012-08-25 00:54:34 +00:00
|
|
|
errors.add(self.focus_col)
|
2014-11-13 23:21:47 +00:00
|
|
|
else:
|
|
|
|
errors.discard(self.focus_col)
|
2015-04-15 22:57:12 +00:00
|
|
|
self.set_value(val, self.focus, self.focus_col, errors)
|
|
|
|
|
|
|
|
def set_value(self, val, focus, focus_col, errors=None):
|
|
|
|
if not errors:
|
|
|
|
errors = set([])
|
|
|
|
row = list(self.lst[focus][0])
|
|
|
|
row[focus_col] = val
|
|
|
|
self.lst[focus] = [tuple(row), errors]
|
|
|
|
self._modified()
|
2012-02-08 05:25:00 +00:00
|
|
|
|
2012-02-08 03:55:11 +00:00
|
|
|
def delete_focus(self):
|
|
|
|
if self.lst:
|
|
|
|
del self.lst[self.focus]
|
2015-05-30 00:03:28 +00:00
|
|
|
self.focus = min(len(self.lst) - 1, self.focus)
|
2012-02-08 03:55:11 +00:00
|
|
|
self._modified()
|
|
|
|
|
2012-02-08 04:52:43 +00:00
|
|
|
def _insert(self, pos):
|
|
|
|
self.focus = pos
|
2015-04-15 22:57:12 +00:00
|
|
|
self.lst.insert(
|
2015-04-17 00:54:29 +00:00
|
|
|
self.focus,
|
|
|
|
[
|
|
|
|
[c.blank() for c in self.editor.columns], set([])
|
|
|
|
]
|
2015-04-15 22:57:12 +00:00
|
|
|
)
|
2012-02-08 03:55:11 +00:00
|
|
|
self.focus_col = 0
|
|
|
|
self.start_edit()
|
|
|
|
|
2012-02-08 04:52:43 +00:00
|
|
|
def insert(self):
|
|
|
|
return self._insert(self.focus)
|
|
|
|
|
|
|
|
def add(self):
|
|
|
|
return self._insert(min(self.focus + 1, len(self.lst)))
|
|
|
|
|
2012-02-08 01:58:48 +00:00
|
|
|
def start_edit(self):
|
2015-04-16 10:01:54 +00:00
|
|
|
col = self.editor.columns[self.focus_col]
|
|
|
|
if self.lst and not col.subeditor:
|
2015-03-13 12:14:37 +00:00
|
|
|
self.editing = GridRow(
|
|
|
|
self.focus_col, True, self.editor, self.lst[self.focus]
|
|
|
|
)
|
2015-03-22 03:59:11 +00:00
|
|
|
self.editor.master.loop.widget.footer.update(FOOTER_EDITING)
|
2012-02-08 22:32:29 +00:00
|
|
|
self._modified()
|
2012-02-08 01:07:17 +00:00
|
|
|
|
2012-02-08 01:58:48 +00:00
|
|
|
def stop_edit(self):
|
2012-02-08 03:43:11 +00:00
|
|
|
if self.editing:
|
2015-03-22 03:59:11 +00:00
|
|
|
self.editor.master.loop.widget.footer.update(FOOTER)
|
2012-08-25 01:23:46 +00:00
|
|
|
self.set_current_value(self.editing.get_edit_value(), False)
|
2012-02-08 03:43:11 +00:00
|
|
|
self.editing = False
|
|
|
|
self._modified()
|
2012-02-08 01:58:48 +00:00
|
|
|
|
2012-02-08 01:07:17 +00:00
|
|
|
def left(self):
|
2012-03-17 21:33:11 +00:00
|
|
|
self.focus_col = max(self.focus_col - 1, 0)
|
2012-02-08 01:07:17 +00:00
|
|
|
self._modified()
|
|
|
|
|
|
|
|
def right(self):
|
2015-05-30 00:03:28 +00:00
|
|
|
self.focus_col = min(self.focus_col + 1, len(self.editor.columns) - 1)
|
2012-02-08 01:07:17 +00:00
|
|
|
self._modified()
|
|
|
|
|
|
|
|
def tab_next(self):
|
2012-02-08 03:55:11 +00:00
|
|
|
self.stop_edit()
|
2015-05-30 00:03:28 +00:00
|
|
|
if self.focus_col < len(self.editor.columns) - 1:
|
2012-03-17 21:33:11 +00:00
|
|
|
self.focus_col += 1
|
2015-05-30 00:03:28 +00:00
|
|
|
elif self.focus != len(self.lst) - 1:
|
2012-02-08 01:07:17 +00:00
|
|
|
self.focus_col = 0
|
|
|
|
self.focus += 1
|
|
|
|
self._modified()
|
|
|
|
|
|
|
|
def get_focus(self):
|
|
|
|
if self.editing:
|
|
|
|
return self.editing, self.focus
|
2012-02-08 03:55:11 +00:00
|
|
|
elif self.lst:
|
2015-03-13 12:14:37 +00:00
|
|
|
return GridRow(
|
|
|
|
self.focus_col,
|
|
|
|
False,
|
|
|
|
self.editor,
|
|
|
|
self.lst[self.focus]
|
|
|
|
), self.focus
|
2012-02-08 03:55:11 +00:00
|
|
|
else:
|
|
|
|
return None, None
|
2012-02-08 01:07:17 +00:00
|
|
|
|
|
|
|
def set_focus(self, focus):
|
2012-02-08 03:43:11 +00:00
|
|
|
self.stop_edit()
|
2012-02-08 01:07:17 +00:00
|
|
|
self.focus = focus
|
2015-04-17 01:30:49 +00:00
|
|
|
self._modified()
|
2012-02-08 01:07:17 +00:00
|
|
|
|
|
|
|
def get_next(self, pos):
|
2015-05-30 00:03:28 +00:00
|
|
|
if pos + 1 >= len(self.lst):
|
2012-02-08 01:07:17 +00:00
|
|
|
return None, None
|
2015-05-30 00:03:28 +00:00
|
|
|
return GridRow(None, False, self.editor, self.lst[pos + 1]), pos + 1
|
2012-02-08 01:07:17 +00:00
|
|
|
|
|
|
|
def get_prev(self, pos):
|
2015-05-30 00:03:28 +00:00
|
|
|
if pos - 1 < 0:
|
2012-02-08 01:07:17 +00:00
|
|
|
return None, None
|
2015-05-30 00:03:28 +00:00
|
|
|
return GridRow(None, False, self.editor, self.lst[pos - 1]), pos - 1
|
2012-02-08 01:07:17 +00:00
|
|
|
|
|
|
|
|
2012-03-17 20:38:42 +00:00
|
|
|
class GridListBox(urwid.ListBox):
|
2016-01-27 09:12:18 +00:00
|
|
|
|
2012-02-08 01:07:17 +00:00
|
|
|
def __init__(self, lw):
|
|
|
|
urwid.ListBox.__init__(self, lw)
|
|
|
|
|
|
|
|
|
2012-03-18 21:42:30 +00:00
|
|
|
FIRST_WIDTH_MAX = 40
|
|
|
|
FIRST_WIDTH_MIN = 20
|
2015-03-13 12:14:37 +00:00
|
|
|
|
|
|
|
|
2015-03-13 12:03:46 +00:00
|
|
|
class GridEditor(urwid.WidgetWrap):
|
2012-07-04 23:27:40 +00:00
|
|
|
title = None
|
|
|
|
columns = None
|
2015-03-13 12:14:37 +00:00
|
|
|
|
2012-03-18 21:12:06 +00:00
|
|
|
def __init__(self, master, value, callback, *cb_args, **cb_kwargs):
|
2015-04-17 00:54:29 +00:00
|
|
|
value = self.data_in(copy.deepcopy(value))
|
2012-03-18 21:12:06 +00:00
|
|
|
self.master, self.value, self.callback = master, value, callback
|
2012-02-08 03:43:11 +00:00
|
|
|
self.cb_args, self.cb_kwargs = cb_args, cb_kwargs
|
2012-03-18 21:42:30 +00:00
|
|
|
|
|
|
|
first_width = 20
|
|
|
|
if value:
|
|
|
|
for r in value:
|
2015-04-15 22:57:12 +00:00
|
|
|
assert len(r) == len(self.columns)
|
2012-03-18 21:42:30 +00:00
|
|
|
first_width = max(len(r), first_width)
|
|
|
|
self.first_width = min(first_width, FIRST_WIDTH_MAX)
|
|
|
|
|
|
|
|
title = urwid.Text(self.title)
|
|
|
|
title = urwid.Padding(title, align="left", width=("relative", 100))
|
|
|
|
title = urwid.AttrWrap(title, "heading")
|
|
|
|
|
|
|
|
headings = []
|
2015-04-15 22:57:12 +00:00
|
|
|
for i, col in enumerate(self.columns):
|
|
|
|
c = urwid.Text(col.heading)
|
|
|
|
if i == 0 and len(self.columns) > 1:
|
2012-03-18 21:42:30 +00:00
|
|
|
headings.append(("fixed", first_width + 2, c))
|
|
|
|
else:
|
|
|
|
headings.append(c)
|
|
|
|
h = urwid.Columns(
|
|
|
|
headings,
|
|
|
|
dividechars = 2
|
|
|
|
)
|
|
|
|
h = urwid.AttrWrap(h, "heading")
|
|
|
|
|
2012-03-17 20:38:42 +00:00
|
|
|
self.walker = GridWalker(self.value, self)
|
|
|
|
self.lb = GridListBox(self.walker)
|
2015-03-13 12:03:46 +00:00
|
|
|
self._w = urwid.Frame(
|
2012-03-22 01:57:57 +00:00
|
|
|
self.lb,
|
2012-03-18 21:42:30 +00:00
|
|
|
header = urwid.Pile([title, h])
|
|
|
|
)
|
2015-03-22 03:59:11 +00:00
|
|
|
self.master.loop.widget.footer.update("")
|
2012-02-08 22:30:35 +00:00
|
|
|
self.show_empty_msg()
|
|
|
|
|
|
|
|
def show_empty_msg(self):
|
|
|
|
if self.walker.lst:
|
2015-03-13 12:03:46 +00:00
|
|
|
self._w.set_footer(None)
|
2012-02-08 22:30:35 +00:00
|
|
|
else:
|
2015-03-13 12:03:46 +00:00
|
|
|
self._w.set_footer(
|
2012-02-08 22:30:35 +00:00
|
|
|
urwid.Text(
|
|
|
|
[
|
|
|
|
("highlight", "No values. Press "),
|
|
|
|
("key", "a"),
|
|
|
|
("highlight", " to add some."),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
)
|
2012-02-07 03:39:37 +00:00
|
|
|
|
2012-08-25 00:21:45 +00:00
|
|
|
def encode(self, s):
|
|
|
|
if not self.encoding:
|
|
|
|
return s
|
|
|
|
try:
|
|
|
|
return s.encode(self.encoding)
|
|
|
|
except ValueError:
|
|
|
|
return None
|
|
|
|
|
2012-08-25 01:23:46 +00:00
|
|
|
def read_file(self, p, unescaped=False):
|
2012-08-31 01:33:18 +00:00
|
|
|
if p:
|
|
|
|
try:
|
|
|
|
p = os.path.expanduser(p)
|
2013-06-15 22:23:44 +00:00
|
|
|
d = file(p, "rb").read()
|
2012-08-31 01:33:18 +00:00
|
|
|
self.walker.set_current_value(d, unescaped)
|
|
|
|
self.walker._modified()
|
2015-05-30 00:03:28 +00:00
|
|
|
except IOError as v:
|
2012-08-31 01:33:18 +00:00
|
|
|
return str(v)
|
2012-08-25 00:21:45 +00:00
|
|
|
|
2015-04-15 22:57:12 +00:00
|
|
|
def set_subeditor_value(self, val, focus, focus_col):
|
|
|
|
self.walker.set_value(val, focus, focus_col)
|
|
|
|
|
2012-02-07 03:39:37 +00:00
|
|
|
def keypress(self, size, key):
|
2012-02-08 01:07:17 +00:00
|
|
|
if self.walker.editing:
|
2012-03-18 21:57:27 +00:00
|
|
|
if key in ["esc"]:
|
2012-02-08 01:58:48 +00:00
|
|
|
self.walker.stop_edit()
|
2012-02-08 03:55:11 +00:00
|
|
|
elif key == "tab":
|
2012-02-08 22:36:10 +00:00
|
|
|
pf, pfc = self.walker.focus, self.walker.focus_col
|
2012-02-08 03:55:11 +00:00
|
|
|
self.walker.tab_next()
|
2012-02-08 22:36:10 +00:00
|
|
|
if self.walker.focus == pf and self.walker.focus_col != pfc:
|
2012-02-08 04:55:17 +00:00
|
|
|
self.walker.start_edit()
|
2012-02-07 03:39:37 +00:00
|
|
|
else:
|
2015-03-13 12:03:46 +00:00
|
|
|
self._w.keypress(size, key)
|
2012-02-08 03:43:11 +00:00
|
|
|
return None
|
2012-02-08 05:25:00 +00:00
|
|
|
|
2012-02-08 01:58:48 +00:00
|
|
|
key = common.shortcuts(key)
|
2015-04-15 22:57:12 +00:00
|
|
|
column = self.columns[self.walker.focus_col]
|
2012-02-08 03:43:11 +00:00
|
|
|
if key in ["q", "esc"]:
|
2012-03-18 21:42:30 +00:00
|
|
|
res = []
|
|
|
|
for i in self.walker.lst:
|
2015-04-15 22:57:12 +00:00
|
|
|
if not i[1] and any([x for x in i[0]]):
|
2012-08-25 01:13:16 +00:00
|
|
|
res.append(i[0])
|
2015-04-17 00:54:29 +00:00
|
|
|
self.callback(self.data_out(res), *self.cb_args, **self.cb_kwargs)
|
2015-03-22 04:18:53 +00:00
|
|
|
signals.pop_view_state.send(self)
|
2015-04-17 01:30:49 +00:00
|
|
|
elif key == "g":
|
2015-07-08 15:10:03 +00:00
|
|
|
self.walker.set_focus(0)
|
|
|
|
elif key == "G":
|
2015-05-30 00:03:28 +00:00
|
|
|
self.walker.set_focus(len(self.walker.lst) - 1)
|
2012-02-08 01:58:48 +00:00
|
|
|
elif key in ["h", "left"]:
|
|
|
|
self.walker.left()
|
|
|
|
elif key in ["l", "right"]:
|
|
|
|
self.walker.right()
|
|
|
|
elif key == "tab":
|
|
|
|
self.walker.tab_next()
|
2012-02-08 03:55:11 +00:00
|
|
|
elif key == "a":
|
|
|
|
self.walker.add()
|
2012-02-08 09:55:48 +00:00
|
|
|
elif key == "A":
|
2012-02-08 04:52:43 +00:00
|
|
|
self.walker.insert()
|
2012-02-08 03:55:11 +00:00
|
|
|
elif key == "d":
|
|
|
|
self.walker.delete_focus()
|
2015-04-16 10:01:54 +00:00
|
|
|
elif column.keypress(key, self) and not self.handle_key(key):
|
2015-03-13 12:03:46 +00:00
|
|
|
return self._w.keypress(size, key)
|
2012-03-18 21:12:06 +00:00
|
|
|
|
2015-04-17 00:54:29 +00:00
|
|
|
def data_out(self, data):
|
|
|
|
"""
|
|
|
|
Called on raw list data, before data is returned through the
|
|
|
|
callback.
|
|
|
|
"""
|
|
|
|
return data
|
|
|
|
|
|
|
|
def data_in(self, data):
|
|
|
|
"""
|
|
|
|
Called to prepare provided data.
|
|
|
|
"""
|
|
|
|
return data
|
|
|
|
|
2012-03-23 00:28:33 +00:00
|
|
|
def is_error(self, col, val):
|
2012-08-25 01:13:16 +00:00
|
|
|
"""
|
|
|
|
Return False, or a string error message.
|
|
|
|
"""
|
2012-03-23 00:28:33 +00:00
|
|
|
return False
|
|
|
|
|
2012-08-31 01:28:04 +00:00
|
|
|
def handle_key(self, key):
|
|
|
|
return False
|
|
|
|
|
|
|
|
def make_help(self):
|
|
|
|
text = []
|
|
|
|
text.append(urwid.Text([("text", "Editor control:\n")]))
|
|
|
|
keys = [
|
|
|
|
("A", "insert row before cursor"),
|
|
|
|
("a", "add row after cursor"),
|
|
|
|
("d", "delete row"),
|
|
|
|
("e", "spawn external editor on current field"),
|
2015-04-17 01:30:49 +00:00
|
|
|
("q", "save changes and exit editor"),
|
2012-08-31 01:28:04 +00:00
|
|
|
("r", "read value from file"),
|
|
|
|
("R", "read unescaped value from file"),
|
2015-04-17 01:30:49 +00:00
|
|
|
("esc", "save changes and exit editor"),
|
2012-08-31 01:28:04 +00:00
|
|
|
("tab", "next field"),
|
|
|
|
("enter", "edit field"),
|
|
|
|
]
|
2015-03-13 12:14:37 +00:00
|
|
|
text.extend(
|
|
|
|
common.format_keyvals(keys, key="key", val="text", indent=4)
|
|
|
|
)
|
2012-08-31 01:28:04 +00:00
|
|
|
text.append(
|
|
|
|
urwid.Text(
|
|
|
|
[
|
|
|
|
"\n",
|
|
|
|
("text", "Values are escaped Python-style strings.\n"),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
)
|
|
|
|
return text
|
|
|
|
|
2012-03-18 21:12:06 +00:00
|
|
|
|
|
|
|
class QueryEditor(GridEditor):
|
|
|
|
title = "Editing query"
|
2015-04-15 22:57:12 +00:00
|
|
|
columns = [
|
|
|
|
TextColumn("Key"),
|
|
|
|
TextColumn("Value")
|
|
|
|
]
|
2012-03-18 21:12:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
class HeaderEditor(GridEditor):
|
|
|
|
title = "Editing headers"
|
2015-04-15 22:57:12 +00:00
|
|
|
columns = [
|
|
|
|
TextColumn("Key"),
|
|
|
|
TextColumn("Value")
|
|
|
|
]
|
2015-03-13 12:14:37 +00:00
|
|
|
|
2012-08-31 01:28:04 +00:00
|
|
|
def make_help(self):
|
|
|
|
h = GridEditor.make_help(self)
|
|
|
|
text = []
|
|
|
|
text.append(urwid.Text([("text", "Special keys:\n")]))
|
|
|
|
keys = [
|
|
|
|
("U", "add User-Agent header"),
|
|
|
|
]
|
2015-03-13 12:14:37 +00:00
|
|
|
text.extend(
|
|
|
|
common.format_keyvals(keys, key="key", val="text", indent=4)
|
|
|
|
)
|
2012-08-31 01:28:04 +00:00
|
|
|
text.append(urwid.Text([("text", "\n")]))
|
|
|
|
text.extend(h)
|
|
|
|
return text
|
|
|
|
|
|
|
|
def set_user_agent(self, k):
|
2015-07-15 21:19:01 +00:00
|
|
|
ua = user_agents.get_by_shortcut(k)
|
2012-08-31 01:28:04 +00:00
|
|
|
if ua:
|
|
|
|
self.walker.add_value(
|
|
|
|
[
|
|
|
|
"User-Agent",
|
|
|
|
ua[2]
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
def handle_key(self, key):
|
|
|
|
if key == "U":
|
2015-03-22 00:32:24 +00:00
|
|
|
signals.status_prompt_onekey.send(
|
|
|
|
prompt = "Add User-Agent header:",
|
2015-07-15 21:19:01 +00:00
|
|
|
keys = [(i[0], i[1]) for i in user_agents.UASTRINGS],
|
2015-03-22 00:32:24 +00:00
|
|
|
callback = self.set_user_agent,
|
2012-08-31 01:28:04 +00:00
|
|
|
)
|
|
|
|
return True
|
2012-03-18 21:12:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
class URLEncodedFormEditor(GridEditor):
|
|
|
|
title = "Editing URL-encoded form"
|
2015-04-15 22:57:12 +00:00
|
|
|
columns = [
|
|
|
|
TextColumn("Key"),
|
|
|
|
TextColumn("Value")
|
|
|
|
]
|
2012-03-18 21:12:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ReplaceEditor(GridEditor):
|
|
|
|
title = "Editing replacement patterns"
|
2015-04-15 22:57:12 +00:00
|
|
|
columns = [
|
|
|
|
TextColumn("Filter"),
|
|
|
|
TextColumn("Regex"),
|
|
|
|
TextColumn("Replacement"),
|
|
|
|
]
|
2015-03-13 12:14:37 +00:00
|
|
|
|
2012-03-23 00:28:33 +00:00
|
|
|
def is_error(self, col, val):
|
|
|
|
if col == 0:
|
|
|
|
if not filt.parse(val):
|
2012-08-25 01:13:16 +00:00
|
|
|
return "Invalid filter specification."
|
2012-03-23 00:28:33 +00:00
|
|
|
elif col == 1:
|
|
|
|
try:
|
|
|
|
re.compile(val)
|
|
|
|
except re.error:
|
2012-08-25 01:13:16 +00:00
|
|
|
return "Invalid regular expression."
|
2012-03-23 00:28:33 +00:00
|
|
|
return False
|
2012-03-18 21:12:06 +00:00
|
|
|
|
2012-08-18 11:39:52 +00:00
|
|
|
|
|
|
|
class SetHeadersEditor(GridEditor):
|
|
|
|
title = "Editing header set patterns"
|
2015-04-15 22:57:12 +00:00
|
|
|
columns = [
|
|
|
|
TextColumn("Filter"),
|
|
|
|
TextColumn("Header"),
|
|
|
|
TextColumn("Value"),
|
|
|
|
]
|
2015-03-13 12:14:37 +00:00
|
|
|
|
2012-08-18 11:39:52 +00:00
|
|
|
def is_error(self, col, val):
|
|
|
|
if col == 0:
|
|
|
|
if not filt.parse(val):
|
2012-08-25 01:13:16 +00:00
|
|
|
return "Invalid filter specification"
|
2012-08-18 11:39:52 +00:00
|
|
|
return False
|
|
|
|
|
2012-09-01 23:50:17 +00:00
|
|
|
def make_help(self):
|
|
|
|
h = GridEditor.make_help(self)
|
|
|
|
text = []
|
|
|
|
text.append(urwid.Text([("text", "Special keys:\n")]))
|
|
|
|
keys = [
|
|
|
|
("U", "add User-Agent header"),
|
|
|
|
]
|
2015-03-13 12:14:37 +00:00
|
|
|
text.extend(
|
|
|
|
common.format_keyvals(keys, key="key", val="text", indent=4)
|
|
|
|
)
|
2012-09-01 23:50:17 +00:00
|
|
|
text.append(urwid.Text([("text", "\n")]))
|
|
|
|
text.extend(h)
|
|
|
|
return text
|
|
|
|
|
|
|
|
def set_user_agent(self, k):
|
2015-07-15 21:19:01 +00:00
|
|
|
ua = user_agents.get_by_shortcut(k)
|
2012-09-01 23:50:17 +00:00
|
|
|
if ua:
|
|
|
|
self.walker.add_value(
|
|
|
|
[
|
|
|
|
".*",
|
|
|
|
"User-Agent",
|
|
|
|
ua[2]
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
def handle_key(self, key):
|
|
|
|
if key == "U":
|
2015-03-22 00:32:24 +00:00
|
|
|
signals.status_prompt_onekey.send(
|
|
|
|
prompt = "Add User-Agent header:",
|
2015-07-15 21:19:01 +00:00
|
|
|
keys = [(i[0], i[1]) for i in user_agents.UASTRINGS],
|
2015-03-22 00:32:24 +00:00
|
|
|
callback = self.set_user_agent,
|
2012-09-01 23:50:17 +00:00
|
|
|
)
|
|
|
|
return True
|
|
|
|
|
2012-08-19 01:15:54 +00:00
|
|
|
|
|
|
|
class PathEditor(GridEditor):
|
|
|
|
title = "Editing URL path components"
|
2015-04-15 22:57:12 +00:00
|
|
|
columns = [
|
|
|
|
TextColumn("Component"),
|
|
|
|
]
|
2012-08-19 01:15:54 +00:00
|
|
|
|
2015-04-17 00:54:29 +00:00
|
|
|
def data_in(self, data):
|
|
|
|
return [[i] for i in data]
|
|
|
|
|
|
|
|
def data_out(self, data):
|
|
|
|
return [i[0] for i in data]
|
|
|
|
|
2014-01-03 02:29:32 +00:00
|
|
|
|
|
|
|
class ScriptEditor(GridEditor):
|
|
|
|
title = "Editing scripts"
|
2015-04-15 22:57:12 +00:00
|
|
|
columns = [
|
|
|
|
TextColumn("Command"),
|
|
|
|
]
|
2015-03-13 12:14:37 +00:00
|
|
|
|
2014-01-03 02:29:32 +00:00
|
|
|
def is_error(self, col, val):
|
2014-01-13 01:15:17 +00:00
|
|
|
try:
|
|
|
|
script.Script.parse_command(val)
|
2015-11-14 03:21:38 +00:00
|
|
|
except script.ScriptException as v:
|
2014-01-13 01:15:17 +00:00
|
|
|
return str(v)
|
2014-09-08 10:20:40 +00:00
|
|
|
|
|
|
|
|
2014-10-18 16:29:35 +00:00
|
|
|
class HostPatternEditor(GridEditor):
|
|
|
|
title = "Editing host patterns"
|
2015-04-15 22:57:12 +00:00
|
|
|
columns = [
|
|
|
|
TextColumn("Regex (matched on hostname:port / ip:port)")
|
|
|
|
]
|
2014-09-08 10:20:40 +00:00
|
|
|
|
|
|
|
def is_error(self, col, val):
|
|
|
|
try:
|
|
|
|
re.compile(val, re.IGNORECASE)
|
|
|
|
except re.error as e:
|
2015-03-13 12:03:46 +00:00
|
|
|
return "Invalid regex: %s" % str(e)
|
2015-04-14 21:43:15 +00:00
|
|
|
|
2015-04-17 00:54:29 +00:00
|
|
|
def data_in(self, data):
|
|
|
|
return [[i] for i in data]
|
|
|
|
|
|
|
|
def data_out(self, data):
|
|
|
|
return [i[0] for i in data]
|
|
|
|
|
2015-04-14 21:43:15 +00:00
|
|
|
|
|
|
|
class CookieEditor(GridEditor):
|
|
|
|
title = "Editing request Cookie header"
|
2015-04-15 22:57:12 +00:00
|
|
|
columns = [
|
|
|
|
TextColumn("Name"),
|
|
|
|
TextColumn("Value"),
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class CookieAttributeEditor(GridEditor):
|
|
|
|
title = "Editing Set-Cookie attributes"
|
|
|
|
columns = [
|
|
|
|
TextColumn("Name"),
|
|
|
|
TextColumn("Value"),
|
|
|
|
]
|
2015-04-15 00:56:43 +00:00
|
|
|
|
2015-04-17 00:54:29 +00:00
|
|
|
def data_out(self, data):
|
|
|
|
ret = []
|
|
|
|
for i in data:
|
|
|
|
if not i[1]:
|
|
|
|
ret.append([i[0], None])
|
|
|
|
else:
|
|
|
|
ret.append(i)
|
|
|
|
return ret
|
|
|
|
|
2015-04-15 00:56:43 +00:00
|
|
|
|
|
|
|
class SetCookieEditor(GridEditor):
|
2015-04-15 22:57:12 +00:00
|
|
|
title = "Editing response SetCookie header"
|
|
|
|
columns = [
|
|
|
|
TextColumn("Name"),
|
|
|
|
TextColumn("Value"),
|
|
|
|
SubgridColumn("Attributes", CookieAttributeEditor),
|
|
|
|
]
|
2015-04-17 00:54:29 +00:00
|
|
|
|
|
|
|
def data_in(self, data):
|
|
|
|
flattened = []
|
|
|
|
for k, v in data.items():
|
|
|
|
flattened.append([k, v[0], v[1].lst])
|
|
|
|
return flattened
|
|
|
|
|
|
|
|
def data_out(self, data):
|
|
|
|
vals = []
|
|
|
|
for i in data:
|
|
|
|
vals.append(
|
|
|
|
[
|
|
|
|
i[0],
|
|
|
|
[i[1], odict.ODictCaseless(i[2])]
|
|
|
|
]
|
|
|
|
)
|
|
|
|
return odict.ODict(vals)
|