From 342bebbd0ee3b7c0d5057762b8a39460dda40192 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antoine=20Beaupr=C3=A9?= Date: Fri, 11 Nov 2016 22:01:21 -0500 Subject: [PATCH] use humanize library instead of custom implementation --- setup.py | 1 + src/wormhole/cli/cmd_receive.py | 9 +++++---- src/wormhole/cli/cmd_send.py | 9 +++++---- src/wormhole/server/cmd_usage.py | 8 ++++---- src/wormhole/test/test_scripts.py | 8 ++++---- src/wormhole/test/test_util.py | 28 ---------------------------- src/wormhole/util.py | 19 ------------------- 7 files changed, 19 insertions(+), 63 deletions(-) diff --git a/setup.py b/setup.py index 0a23c6d3..d945d6c7 100644 --- a/setup.py +++ b/setup.py @@ -33,6 +33,7 @@ "autobahn[twisted] >= 0.14.1", "hkdf", "tqdm", "click", + "humanize", ], extras_require={ ':sys_platform=="win32"': ["pypiwin32"], diff --git a/src/wormhole/cli/cmd_receive.py b/src/wormhole/cli/cmd_receive.py index a2cd89df..c759ed00 100644 --- a/src/wormhole/cli/cmd_receive.py +++ b/src/wormhole/cli/cmd_receive.py @@ -1,13 +1,14 @@ from __future__ import print_function import os, sys, six, tempfile, zipfile, hashlib from tqdm import tqdm +from humanize import naturalsize from twisted.internet import reactor from twisted.internet.defer import inlineCallbacks, returnValue from twisted.python import log from ..wormhole import wormhole from ..transit import TransitReceiver from ..errors import TransferError, WormholeClosedError -from ..util import dict_to_bytes, bytes_to_dict, bytes_to_hexstr, sizeof_fmt_iec +from ..util import dict_to_bytes, bytes_to_dict, bytes_to_hexstr APPID = u"lothar.com/wormhole/text-or-file-xfer" @@ -195,7 +196,7 @@ def _handle_file(self, them_d): self.xfersize = file_data["filesize"] self._msg(u"Receiving file (%s) into: %s" % - (sizeof_fmt_iec(self.xfersize), os.path.basename(self.abs_destname))) + (naturalsize(self.xfersize), os.path.basename(self.abs_destname))) self._ask_permission() tmp_destname = self.abs_destname + ".tmp" return open(tmp_destname, "wb") @@ -211,9 +212,9 @@ def _handle_directory(self, them_d): self.xfersize = file_data["zipsize"] self._msg(u"Receiving directory (%s) into: %s/" % - (sizeof_fmt_iec(self.xfersize), os.path.basename(self.abs_destname))) + (naturalsize(self.xfersize), os.path.basename(self.abs_destname))) self._msg(u"%d files, %s (uncompressed)" % - (file_data["numfiles"], sizeof_fmt_iec(file_data["numbytes"]))) + (file_data["numfiles"], naturalsize(file_data["numbytes"]))) self._ask_permission() return tempfile.SpooledTemporaryFile() diff --git a/src/wormhole/cli/cmd_send.py b/src/wormhole/cli/cmd_send.py index de6d3045..6fa74462 100644 --- a/src/wormhole/cli/cmd_send.py +++ b/src/wormhole/cli/cmd_send.py @@ -1,6 +1,7 @@ from __future__ import print_function import os, sys, six, tempfile, zipfile, hashlib from tqdm import tqdm +from humanize import naturalsize from twisted.python import log from twisted.protocols import basic from twisted.internet import reactor @@ -8,7 +9,7 @@ from ..errors import TransferError, WormholeClosedError from ..wormhole import wormhole from ..transit import TransitSender -from ..util import dict_to_bytes, bytes_to_dict, bytes_to_hexstr, sizeof_fmt_iec +from ..util import dict_to_bytes, bytes_to_dict, bytes_to_hexstr APPID = u"lothar.com/wormhole/text-or-file-xfer" @@ -167,7 +168,7 @@ def _build_offer(self): text = six.moves.input("Text to send: ") if text is not None: - print(u"Sending text message (%s)" % sizeof_fmt_iec(len(text), suffix='bytes'), + print(u"Sending text message (%s)" % naturalsize(len(text)), file=args.stdout) offer = { "message": text } fd_to_send = None @@ -188,7 +189,7 @@ def _build_offer(self): "filesize": filesize, } print(u"Sending %s file named '%s'" - % (sizeof_fmt_iec(filesize), basename), + % (naturalsize(filesize), basename), file=args.stdout) fd_to_send = open(what, "rb") return offer, fd_to_send @@ -224,7 +225,7 @@ def _build_offer(self): "numfiles": num_files, } print(u"Sending directory (%s compressed) named '%s'" - % (sizeof_fmt_iec(filesize), basename), file=args.stdout) + % (naturalsize(filesize), basename), file=args.stdout) return offer, fd_to_send raise TypeError("'%s' is neither file nor directory" % args.what) diff --git a/src/wormhole/server/cmd_usage.py b/src/wormhole/server/cmd_usage.py index 6b2f9adf..227a220d 100644 --- a/src/wormhole/server/cmd_usage.py +++ b/src/wormhole/server/cmd_usage.py @@ -2,8 +2,8 @@ import os, time, json from collections import defaultdict import click +from humanize import naturalsize from .database import get_db -from ..util import sizeof_fmt_iec def abbrev(t): if t is None: @@ -25,7 +25,7 @@ def print_event(event): abbrev(total_time), abbrev(waiting_time), abbrev(followthrough), - sizeof_fmt_iec(total_bytes), + naturalsize(total_bytes), time.ctime(started), )) @@ -84,8 +84,8 @@ def show_usage(args): print(" %d events in %s (%.2f per hour)" % (total, abbrev(elapsed), (3600 * total / elapsed))) rate = total_transit_bytes / elapsed - print(" %s total bytes, %sps" % (sizeof_fmt_iec(total_transit_bytes), - sizeof_fmt_iec(rate))) + print(" %s total bytes, %sps" % (naturalsize(total_transit_bytes), + naturalsize(rate))) print("", ", ".join(["%s=%d (%d%%)" % (k, counters[k], (100.0 * counters[k] / total)) for k in sorted(counters) diff --git a/src/wormhole/test/test_scripts.py b/src/wormhole/test/test_scripts.py index ecf0dee7..da68bed0 100644 --- a/src/wormhole/test/test_scripts.py +++ b/src/wormhole/test/test_scripts.py @@ -1,5 +1,6 @@ from __future__ import print_function, unicode_literals import os, sys, re, io, zipfile, six, stat +from humanize import naturalsize import mock from twisted.trial import unittest from twisted.python import procutils, log @@ -9,7 +10,6 @@ from .common import ServerBase, config from ..cli import cmd_send, cmd_receive from ..errors import TransferError, WrongPasswordError, WelcomeError -from ..util import sizeof_fmt_iec def build_offer(args): @@ -378,7 +378,7 @@ def message(i): self.failUnlessEqual(send_stdout, expected) elif mode == "file": self.failUnlessIn("Sending {size:s} file named '{name}'{NL}" - .format(size=sizeof_fmt_iec(len(message)), + .format(size=naturalsize(len(message)), name=send_filename, NL=NL), send_stdout) self.failUnlessIn("On the other computer, please run: " @@ -405,7 +405,7 @@ def message(i): self.failUnlessEqual(receive_stdout, message+NL) elif mode == "file": self.failUnlessIn("Receiving file ({size:s}) into: {name}" - .format(size=sizeof_fmt_iec(len(message)), + .format(size=naturalsize(len(message)), name=receive_filename), receive_stdout) self.failUnlessIn("Received file written to ", receive_stdout) fn = os.path.join(receive_dir, receive_filename) @@ -514,7 +514,7 @@ def test_file_noclobber(self): # check sender self.failUnlessIn("Sending {size:s} file named '{name}'{NL}" - .format(size=sizeof_fmt_iec(len(message)), + .format(size=naturalsize(len(message)), name=send_filename, NL=NL), send_stdout) self.failUnlessIn("On the other computer, please run: " diff --git a/src/wormhole/test/test_util.py b/src/wormhole/test/test_util.py index 7e377a61..fb58adcc 100644 --- a/src/wormhole/test/test_util.py +++ b/src/wormhole/test/test_util.py @@ -38,31 +38,3 @@ def test_bytes_to_dict(self): d = util.bytes_to_dict(b) self.assertIsInstance(d, dict) self.assertEqual(d, {"a": "b", "c": 2}) - - def test_size_fmt_decimal(self): - """test the size formatting routines""" - si_size_map = { - 0: '0 B', # no rounding necessary for those - 1: '1 B', - 142: '142 B', - 999: '999 B', - 1000: '1.00 kB', # rounding starts here - 1001: '1.00 kB', # should be rounded away - 1234: '1.23 kB', # should be rounded down - 1235: '1.24 kB', # should be rounded up - 1010: '1.01 kB', # rounded down as well - 999990000: '999.99 MB', # rounded down - 999990001: '999.99 MB', # rounded down - 999995000: '1.00 GB', # rounded up to next unit - 10**6: '1.00 MB', # and all the remaining units, megabytes - 10**9: '1.00 GB', # gigabytes - 10**12: '1.00 TB', # terabytes - 10**15: '1.00 PB', # petabytes - 10**18: '1.00 EB', # exabytes - 10**21: '1.00 ZB', # zottabytes - 10**24: '1.00 YB', # yottabytes - -1: '-1 B', # negative value - -1010: '-1.01 kB', # negative value with rounding - } - for size, fmt in si_size_map.items(): - self.assertEqual(util.sizeof_fmt_decimal(size), fmt) diff --git a/src/wormhole/util.py b/src/wormhole/util.py index a34005ff..967243ee 100644 --- a/src/wormhole/util.py +++ b/src/wormhole/util.py @@ -24,22 +24,3 @@ def bytes_to_dict(b): d = json.loads(b.decode("utf-8")) assert isinstance(d, dict) return d - - -def sizeof_fmt(num, suffix='B', units=None, power=None, sep=' ', precision=2): - for unit in units[:-1]: - if abs(round(num, precision)) < power: - if isinstance(num, int): - return "{}{}{}{}".format(num, sep, unit, suffix) - else: - return "{:3.{}f}{}{}{}".format(num, precision, sep, unit, suffix) - num /= float(power) - return "{:.{}f}{}{}{}".format(num, precision, sep, units[-1], suffix) - - -def sizeof_fmt_iec(num, suffix='B', sep=' ', precision=2): - return sizeof_fmt(num, suffix=suffix, sep=sep, precision=precision, units=['', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi', 'Yi'], power=1024) - - -def sizeof_fmt_decimal(num, suffix='B', sep=' ', precision=2): - return sizeof_fmt(num, suffix=suffix, sep=sep, precision=precision, units=['', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'], power=1000)