Skip to content

Commit

Permalink
use humanize library instead of custom implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
anarcat committed Nov 12, 2016
1 parent 047af4b commit 342bebb
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 63 deletions.
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"autobahn[twisted] >= 0.14.1",
"hkdf", "tqdm",
"click",
"humanize",
],
extras_require={
':sys_platform=="win32"': ["pypiwin32"],
Expand Down
9 changes: 5 additions & 4 deletions src/wormhole/cli/cmd_receive.py
Original file line number Diff line number Diff line change
@@ -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"

Expand Down Expand Up @@ -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")
Expand All @@ -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()

Expand Down
9 changes: 5 additions & 4 deletions src/wormhole/cli/cmd_send.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
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
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"

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions src/wormhole/server/cmd_usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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),
))

Expand Down Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions src/wormhole/test/test_scripts.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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: "
Expand All @@ -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)
Expand Down Expand Up @@ -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: "
Expand Down
28 changes: 0 additions & 28 deletions src/wormhole/test/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
19 changes: 0 additions & 19 deletions src/wormhole/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 342bebb

Please sign in to comment.