remove urwid from contentviews

This commit is contained in:
Maximilian Hils 2015-09-04 17:33:21 +02:00
parent 018c693dee
commit b62498e125

View File

@ -53,8 +53,8 @@ def format_dict(d):
key += ":" key += ":"
key = key.ljust(max_key_len + 2) key = key.ljust(max_key_len + 2)
yield ( yield (
("key", key), ("header", key),
("value", value) ("text", value)
) )
@ -110,12 +110,16 @@ def trailer(clen, txt, limit):
""" """
class View: class View(object):
name = None name = None
prompt = () prompt = ()
content_types = [] content_types = []
def __call__(self, hdrs, content, limit): def __call__(self, hdrs, content, limit):
"""
Returns:
A (mode name, content generator) tuple.
"""
raise NotImplementedError() raise NotImplementedError()
@ -132,8 +136,8 @@ class ViewAuto(View):
if ct in content_types_map: if ct in content_types_map:
return content_types_map[ct][0](hdrs, content, limit) return content_types_map[ct][0](hdrs, content, limit)
elif utils.isXML(content): elif utils.isXML(content):
return ViewXML(hdrs, content, limit) return get("XML")(hdrs, content, limit)
return ViewRaw(hdrs, content, limit) return get("Raw")(hdrs, content, limit)
class ViewRaw(View): class ViewRaw(View):
@ -217,7 +221,7 @@ class ViewJSON(View):
return "JSON", format_text(pretty_json, limit) return "JSON", format_text(pretty_json, limit)
class ViewHTML: class ViewHTML(View):
name = "HTML" name = "HTML"
prompt = ("html", "h") prompt = ("html", "h")
content_types = ["text/html"] content_types = ["text/html"]
@ -238,7 +242,7 @@ class ViewHTML:
return "HTML", format_text(s, limit) return "HTML", format_text(s, limit)
class ViewHTMLOutline: class ViewHTMLOutline(View):
name = "HTML Outline" name = "HTML Outline"
prompt = ("html outline", "o") prompt = ("html outline", "o")
content_types = ["text/html"] content_types = ["text/html"]
@ -249,43 +253,34 @@ class ViewHTMLOutline:
h.ignore_images = True h.ignore_images = True
h.body_width = 0 h.body_width = 0
content = h.handle(content) content = h.handle(content)
txt = _view_text(content[:limit], len(content), limit) return "HTML Outline", format_text(content, limit)
return "HTML Outline", txt
class ViewURLEncoded: class ViewURLEncoded(View):
name = "URL-encoded" name = "URL-encoded"
prompt = ("urlencoded", "u") prompt = ("urlencoded", "u")
content_types = ["application/x-www-form-urlencoded"] content_types = ["application/x-www-form-urlencoded"]
def __call__(self, hdrs, content, limit): def __call__(self, hdrs, content, limit):
lines = netlib.utils.urldecode(content) d = netlib.utils.urldecode(content)
if lines: return "URLEncoded form", format_dict(d)
body = format_keyvals(
[(k + ":", v) for (k, v) in lines],
key="header",
val="text"
)
return "URLEncoded form", body
class ViewMultipart: class ViewMultipart(View):
name = "Multipart Form" name = "Multipart Form"
prompt = ("multipart", "m") prompt = ("multipart", "m")
content_types = ["multipart/form-data"] content_types = ["multipart/form-data"]
@staticmethod
def _format(v):
yield (("highlight", "Form data:\n"))
for message in format_dict({key:val for key,val in v}):
yield message
def __call__(self, hdrs, content, limit): def __call__(self, hdrs, content, limit):
v = netlib.utils.multipartdecode(hdrs, content) v = netlib.utils.multipartdecode(hdrs, content)
if v: if v:
r = [ return "Multipart form", self._format(v)
urwid.Text(("highlight", "Form data:\n")),
]
r.extend(format_keyvals(
v,
key="header",
val="text"
))
return "Multipart form", r
if pyamf: if pyamf:
@ -310,7 +305,7 @@ if pyamf:
pyamf.register_class_loader(pyamf_class_loader) pyamf.register_class_loader(pyamf_class_loader)
class ViewAMF: class ViewAMF(View):
name = "AMF" name = "AMF"
prompt = ("amf", "f") prompt = ("amf", "f")
content_types = ["application/x-amf"] content_types = ["application/x-amf"]
@ -337,31 +332,32 @@ if pyamf:
else: else:
return b return b
def _format(self, envelope, limit):
for target, message in iter(envelope):
if isinstance(message, pyamf.remoting.Request):
yield (
("header", "Request: "),
("text", str(target)),
)
else:
yield (
("header", "Response: "),
("text", "%s, code %s" % (target, message.status)),
)
s = json.dumps(self.unpack(message), indent=4)
for msg in format_text(s, limit):
yield msg
def __call__(self, hdrs, content, limit): def __call__(self, hdrs, content, limit):
envelope = remoting.decode(content, strict=False) envelope = remoting.decode(content, strict=False)
if not envelope: if not envelope:
return None return None
txt = [] return "AMF v%s" % envelope.amfVersion, self._format(envelope, limit)
for target, message in iter(envelope):
if isinstance(message, pyamf.remoting.Request):
txt.append(urwid.Text([
("header", "Request: "),
("text", str(target)),
]))
else:
txt.append(urwid.Text([
("header", "Response: "),
("text", "%s, code %s" % (target, message.status)),
]))
s = json.dumps(self.unpack(message), indent=4)
txt.extend(_view_text(s[:limit], len(s), limit))
return "AMF v%s" % envelope.amfVersion, txt
class ViewJavaScript: class ViewJavaScript(View):
name = "JavaScript" name = "JavaScript"
prompt = ("javascript", "j") prompt = ("javascript", "j")
content_types = [ content_types = [
@ -374,10 +370,11 @@ class ViewJavaScript:
opts = jsbeautifier.default_options() opts = jsbeautifier.default_options()
opts.indent_size = 2 opts.indent_size = 2
res = jsbeautifier.beautify(content[:limit], opts) res = jsbeautifier.beautify(content[:limit], opts)
return "JavaScript", _view_text(res, len(res), limit) cutoff = max(0, len(content) - limit)
return "JavaScript", format_text(res, limit - cutoff)
class ViewCSS: class ViewCSS(View):
name = "CSS" name = "CSS"
prompt = ("css", "c") prompt = ("css", "c")
content_types = [ content_types = [
@ -391,10 +388,10 @@ class ViewCSS:
else: else:
beautified = content beautified = content
return "CSS", _view_text(beautified, len(beautified), limit) return "CSS", format_text(beautified, limit)
class ViewImage: class ViewImage(View):
name = "Image" name = "Image"
prompt = ("image", "i") prompt = ("image", "i")
content_types = [ content_types = [
@ -433,15 +430,11 @@ class ViewImage:
clean.append( clean.append(
[netlib.utils.cleanBin(i[0]), netlib.utils.cleanBin(i[1])] [netlib.utils.cleanBin(i[0]), netlib.utils.cleanBin(i[1])]
) )
fmt = format_keyvals( fmt = format_dict({k:v for k,v in clean})
clean,
key="header",
val="text"
)
return "%s image" % img.format, fmt return "%s image" % img.format, fmt
class ViewProtobuf: class ViewProtobuf(View):
"""Human friendly view of protocol buffers """Human friendly view of protocol buffers
The view uses the protoc compiler to decode the binary The view uses the protoc compiler to decode the binary
""" """
@ -480,11 +473,10 @@ class ViewProtobuf:
def __call__(self, hdrs, content, limit): def __call__(self, hdrs, content, limit):
decoded = self.decode_protobuf(content) decoded = self.decode_protobuf(content)
txt = _view_text(decoded[:limit], len(decoded), limit) return "Protobuf", format_text(decoded, limit)
return "Protobuf", txt
class ViewWBXML: class ViewWBXML(View):
name = "WBXML" name = "WBXML"
prompt = ("wbxml", "w") prompt = ("wbxml", "w")
content_types = [ content_types = [
@ -497,8 +489,7 @@ class ViewWBXML:
try: try:
parser = ASCommandResponse(content) parser = ASCommandResponse(content)
parsedContent = parser.xmlString parsedContent = parser.xmlString
txt = _view_text(parsedContent, len(parsedContent), limit) return "WBXML", format_text(parsedContent, limit)
return "WBXML", txt
except: except:
return None return None