pretty_size now lives in netlib.utils

This commit is contained in:
Aldo Cortesi 2015-04-30 12:18:01 +12:00
parent 90dff4a8a1
commit 1c26516b18
7 changed files with 16 additions and 31 deletions

View File

@ -7,6 +7,7 @@ import os
from .. import utils from .. import utils
from ..protocol.http import CONTENT_MISSING, decoded from ..protocol.http import CONTENT_MISSING, decoded
from . import signals from . import signals
import netlib.utils
try: try:
import pyperclip import pyperclip
@ -379,7 +380,7 @@ def format_flow(f, focus, extended=False, hostheader=False, padding=2):
) )
if f.response: if f.response:
if f.response.content: if f.response.content:
contentdesc = utils.pretty_size(len(f.response.content)) contentdesc = netlib.utils.pretty_size(len(f.response.content))
elif f.response.content == CONTENT_MISSING: elif f.response.content == CONTENT_MISSING:
contentdesc = "[content missing]" contentdesc = "[content missing]"
else: else:

View File

@ -60,7 +60,7 @@ def trailer(clen, txt, limit):
txt.append( txt.append(
urwid.Text( urwid.Text(
[ [
("highlight", "... %s of data not shown. Press "%utils.pretty_size(rem)), ("highlight", "... %s of data not shown. Press "%netlib.utils.pretty_size(rem)),
("key", "f"), ("key", "f"),
("highlight", " to load all data.") ("highlight", " to load all data.")
] ]

View File

@ -1,8 +1,8 @@
import time
import os.path import os.path
import urwid import urwid
import netlib.utils
from . import pathedit, signals, common from . import pathedit, signals, common
from .. import utils from .. import utils
@ -22,7 +22,6 @@ class ActionBar(urwid.WidgetWrap):
self.onekey = False self.onekey = False
self.pathprompt = False self.pathprompt = False
def sig_message(self, sender, message, expire=None): def sig_message(self, sender, message, expire=None):
w = urwid.Text(message) w = urwid.Text(message)
self._w = w self._w = w
@ -191,7 +190,7 @@ class StatusBar(urwid.WidgetWrap):
opts.append("following") opts.append("following")
if self.master.stream_large_bodies: if self.master.stream_large_bodies:
opts.append( opts.append(
"stream:%s" % utils.pretty_size( "stream:%s" % netlib.utils.pretty_size(
self.master.stream_large_bodies.max_size self.master.stream_large_bodies.max_size
) )
) )

View File

@ -207,7 +207,7 @@ class DumpMaster(flow.FlowMaster):
if f.response.content == http.CONTENT_MISSING: if f.response.content == http.CONTENT_MISSING:
sz = "(content missing)" sz = "(content missing)"
else: else:
sz = utils.pretty_size(len(f.response.content)) sz = netlib.utils.pretty_size(len(f.response.content))
print(" << %s %s" % (str_response(f.response), sz), file=self.outfile) print(" << %s %s" % (str_response(f.response), sz), file=self.outfile)
self._print_message(f.response) self._print_message(f.response)

View File

@ -691,7 +691,10 @@ class HTTPResponse(HTTPMessage):
return f return f
def __repr__(self): def __repr__(self):
size = utils.pretty_size(len(self.content)) if self.content else "content missing" if self.content:
size = netlib.utils.pretty_size(len(self.content))
else:
size = "content missing"
return "<HTTPResponse: {code} {msg} ({contenttype}, {size})>".format( return "<HTTPResponse: {code} {msg} ({contenttype}, {size})>".format(
code=self.code, code=self.code,
msg=self.msg, msg=self.msg,

View File

@ -96,20 +96,6 @@ def multipartdecode(hdrs, content):
return r return r
return [] return []
def pretty_size(size):
suffixes = [
("B", 2**10),
("kB", 2**20),
("MB", 2**30),
]
for suf, lim in suffixes:
if size >= lim:
continue
else:
x = round(size/float(lim/2**10), 2)
if x == int(x):
x = int(x)
return str(x) + suf
def pretty_duration(secs): def pretty_duration(secs):
formatters = [ formatters = [
@ -124,6 +110,7 @@ def pretty_duration(secs):
#less than 1 sec #less than 1 sec
return "{:.0f}ms".format(secs*1000) return "{:.0f}ms".format(secs*1000)
class Data: class Data:
def __init__(self, name): def __init__(self, name):
m = __import__(name) m = __import__(name)

View File

@ -1,5 +1,5 @@
import json import json
from libmproxy import utils, flow from libmproxy import utils
from netlib import odict from netlib import odict
import tutils import tutils
@ -9,9 +9,11 @@ utils.CERT_SLEEP_TIME = 0
def test_format_timestamp(): def test_format_timestamp():
assert utils.format_timestamp(utils.timestamp()) assert utils.format_timestamp(utils.timestamp())
def test_format_timestamp_with_milli(): def test_format_timestamp_with_milli():
assert utils.format_timestamp_with_milli(utils.timestamp()) assert utils.format_timestamp_with_milli(utils.timestamp())
def test_isBin(): def test_isBin():
assert not utils.isBin("testing\n\r") assert not utils.isBin("testing\n\r")
assert utils.isBin("testing\x01") assert utils.isBin("testing\x01")
@ -31,13 +33,6 @@ def test_clean_hanging_newline():
assert utils.clean_hanging_newline("foo") == "foo" assert utils.clean_hanging_newline("foo") == "foo"
def test_pretty_size():
assert utils.pretty_size(100) == "100B"
assert utils.pretty_size(1024) == "1kB"
assert utils.pretty_size(1024 + (1024/2.0)) == "1.5kB"
assert utils.pretty_size(1024*1024) == "1MB"
def test_pkg_data(): def test_pkg_data():
assert utils.pkg_data.path("console") assert utils.pkg_data.path("console")
tutils.raises("does not exist", utils.pkg_data.path, "nonexistent") tutils.raises("does not exist", utils.pkg_data.path, "nonexistent")
@ -135,7 +130,7 @@ def test_parse_size():
def test_parse_content_type(): def test_parse_content_type():
p = utils.parse_content_type p = utils.parse_content_type
assert p("text/html") == ("text", "html", {}) assert p("text/html") == ("text", "html", {})
assert p("text") == None assert p("text") is None
v = p("text/html; charset=UTF-8") v = p("text/html; charset=UTF-8")
assert v == ('text', 'html', {'charset': 'UTF-8'}) assert v == ('text', 'html', {'charset': 'UTF-8'})