diff options
author | Aldo Cortesi <aldo@nullcube.com> | 2016-05-31 19:32:08 +1200 |
---|---|---|
committer | Aldo Cortesi <aldo@nullcube.com> | 2016-05-31 19:32:08 +1200 |
commit | b2f63458fcda7878d5cf674c2f1e9ca7db5bf3ce (patch) | |
tree | 639b7e8408a52dc8828f579f324dab065e4b4cb2 /netlib | |
parent | ec34cae6181d6af0150ac730d70b96104a07e9d5 (diff) | |
download | mitmproxy-b2f63458fcda7878d5cf674c2f1e9ca7db5bf3ce.tar.gz mitmproxy-b2f63458fcda7878d5cf674c2f1e9ca7db5bf3ce.tar.bz2 mitmproxy-b2f63458fcda7878d5cf674c2f1e9ca7db5bf3ce.zip |
Move human-friendly format functions to netlib.human, remove redundant implementations
Diffstat (limited to 'netlib')
-rw-r--r-- | netlib/http/response.py | 4 | ||||
-rw-r--r-- | netlib/human.py | 51 | ||||
-rw-r--r-- | netlib/utils.py | 16 | ||||
-rw-r--r-- | netlib/websockets/frame.py | 3 |
4 files changed, 55 insertions, 19 deletions
diff --git a/netlib/http/response.py b/netlib/http/response.py index a6a5bf47..858b3aea 100644 --- a/netlib/http/response.py +++ b/netlib/http/response.py @@ -7,7 +7,7 @@ from . import cookies from .headers import Headers from .message import Message, _native, _always_bytes, MessageData from ..multidict import MultiDictView -from .. import utils +from .. import human class ResponseData(MessageData): @@ -36,7 +36,7 @@ class Response(Message): if self.content: details = "{}, {}".format( self.headers.get("content-type", "unknown content type"), - utils.pretty_size(len(self.content)) + human.pretty_size(len(self.content)) ) else: details = "no content" diff --git a/netlib/human.py b/netlib/human.py new file mode 100644 index 00000000..f4640c00 --- /dev/null +++ b/netlib/human.py @@ -0,0 +1,51 @@ +SIZE_UNITS = dict( + b=1024 ** 0, + k=1024 ** 1, + m=1024 ** 2, + g=1024 ** 3, + t=1024 ** 4, +) + + +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 parse_size(s): + try: + return int(s) + except ValueError: + pass + for i in SIZE_UNITS.keys(): + if s.endswith(i): + try: + return int(s[:-1]) * SIZE_UNITS[i] + except ValueError: + break + raise ValueError("Invalid size specification.") + + +def pretty_duration(secs): + formatters = [ + (100, "{:.0f}s"), + (10, "{:2.1f}s"), + (1, "{:1.2f}s"), + ] + + for limit, formatter in formatters: + if secs >= limit: + return formatter.format(secs) + # less than 1 sec + return "{:.0f}ms".format(secs * 1000) diff --git a/netlib/utils.py b/netlib/utils.py index a0150e77..6be1c17f 100644 --- a/netlib/utils.py +++ b/netlib/utils.py @@ -116,22 +116,6 @@ class BiDi(object): return self.values.get(n, default) -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 - - class Data(object): def __init__(self, name): diff --git a/netlib/websockets/frame.py b/netlib/websockets/frame.py index da5a97f3..cf8917c1 100644 --- a/netlib/websockets/frame.py +++ b/netlib/websockets/frame.py @@ -9,6 +9,7 @@ import six from .protocol import Masker from netlib import tcp from netlib import utils +from netlib import human MAX_16_BIT_INT = (1 << 16) @@ -98,7 +99,7 @@ class FrameHeader(object): if self.masking_key: vals.append(":key=%s" % repr(self.masking_key)) if self.payload_length: - vals.append(" %s" % utils.pretty_size(self.payload_length)) + vals.append(" %s" % human.pretty_size(self.payload_length)) return "".join(vals) def human_readable(self): |