use humanize library instead of custom implementation
This commit is contained in:
parent
047af4b27d
commit
342bebbd0e
1
setup.py
1
setup.py
|
@ -33,6 +33,7 @@ setup(name="magic-wormhole",
|
|||
"autobahn[twisted] >= 0.14.1",
|
||||
"hkdf", "tqdm",
|
||||
"click",
|
||||
"humanize",
|
||||
],
|
||||
extras_require={
|
||||
':sys_platform=="win32"': ["pypiwin32"],
|
||||
|
|
|
@ -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 @@ class TwistedReceiver:
|
|||
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 @@ class TwistedReceiver:
|
|||
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()
|
||||
|
||||
|
|
|
@ -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 twisted.internet.defer import inlineCallbacks, returnValue
|
|||
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 @@ class Sender:
|
|||
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 @@ class Sender:
|
|||
"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 @@ class Sender:
|
|||
"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)
|
||||
|
|
|
@ -2,8 +2,8 @@ from __future__ import print_function, unicode_literals
|
|||
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)
|
||||
|
|
|
@ -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 .. import __version__
|
|||
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 @@ class PregeneratedCode(ServerBase, ScriptsBase, unittest.TestCase):
|
|||
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 @@ class PregeneratedCode(ServerBase, ScriptsBase, unittest.TestCase):
|
|||
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 @@ class PregeneratedCode(ServerBase, ScriptsBase, unittest.TestCase):
|
|||
|
||||
# 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: "
|
||||
|
|
|
@ -38,31 +38,3 @@ class Utils(unittest.TestCase):
|
|||
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)
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue
Block a user