Merge PR92: use 'humanize' to abbreviate filesizes

This commit is contained in:
Brian Warner 2016-11-17 14:34:29 -08:00
commit 6b0ebef559
5 changed files with 29 additions and 46 deletions

View File

@ -33,6 +33,7 @@ setup(name="magic-wormhole",
"autobahn[twisted] >= 0.14.1", "autobahn[twisted] >= 0.14.1",
"hkdf", "tqdm", "hkdf", "tqdm",
"click", "click",
"humanize",
], ],
extras_require={ extras_require={
':sys_platform=="win32"': ["pypiwin32"], ':sys_platform=="win32"': ["pypiwin32"],

View File

@ -1,6 +1,7 @@
from __future__ import print_function from __future__ import print_function
import os, sys, six, tempfile, zipfile, hashlib import os, sys, six, tempfile, zipfile, hashlib
from tqdm import tqdm from tqdm import tqdm
from humanize import naturalsize
from twisted.internet import reactor from twisted.internet import reactor
from twisted.internet.defer import inlineCallbacks, returnValue from twisted.internet.defer import inlineCallbacks, returnValue
from twisted.python import log from twisted.python import log
@ -194,8 +195,8 @@ class TwistedReceiver:
file_data["filename"]) file_data["filename"])
self.xfersize = file_data["filesize"] self.xfersize = file_data["filesize"]
self._msg(u"Receiving file (%d bytes) into: %s" % self._msg(u"Receiving file (%s) into: %s" %
(self.xfersize, os.path.basename(self.abs_destname))) (naturalsize(self.xfersize), os.path.basename(self.abs_destname)))
self._ask_permission() self._ask_permission()
tmp_destname = self.abs_destname + ".tmp" tmp_destname = self.abs_destname + ".tmp"
return open(tmp_destname, "wb") return open(tmp_destname, "wb")
@ -210,10 +211,10 @@ class TwistedReceiver:
file_data["dirname"]) file_data["dirname"])
self.xfersize = file_data["zipsize"] self.xfersize = file_data["zipsize"]
self._msg(u"Receiving directory (%d bytes) into: %s/" % self._msg(u"Receiving directory (%s) into: %s/" %
(self.xfersize, os.path.basename(self.abs_destname))) (naturalsize(self.xfersize), os.path.basename(self.abs_destname)))
self._msg(u"%d files, %d bytes (uncompressed)" % self._msg(u"%d files, %s (uncompressed)" %
(file_data["numfiles"], file_data["numbytes"])) (file_data["numfiles"], naturalsize(file_data["numbytes"])))
self._ask_permission() self._ask_permission()
return tempfile.SpooledTemporaryFile() return tempfile.SpooledTemporaryFile()

View File

@ -1,6 +1,7 @@
from __future__ import print_function from __future__ import print_function
import os, sys, six, tempfile, zipfile, hashlib import os, sys, six, tempfile, zipfile, hashlib
from tqdm import tqdm from tqdm import tqdm
from humanize import naturalsize
from twisted.python import log from twisted.python import log
from twisted.protocols import basic from twisted.protocols import basic
from twisted.internet import reactor from twisted.internet import reactor
@ -167,7 +168,7 @@ class Sender:
text = six.moves.input("Text to send: ") text = six.moves.input("Text to send: ")
if text is not None: if text is not None:
print(u"Sending text message (%d bytes)" % len(text), print(u"Sending text message (%s)" % naturalsize(len(text)),
file=args.stdout) file=args.stdout)
offer = { "message": text } offer = { "message": text }
fd_to_send = None fd_to_send = None
@ -187,7 +188,8 @@ class Sender:
"filename": basename, "filename": basename,
"filesize": filesize, "filesize": filesize,
} }
print(u"Sending %d byte file named '%s'" % (filesize, basename), print(u"Sending %s file named '%s'"
% (naturalsize(filesize), basename),
file=args.stdout) file=args.stdout)
fd_to_send = open(what, "rb") fd_to_send = open(what, "rb")
return offer, fd_to_send return offer, fd_to_send
@ -222,8 +224,8 @@ class Sender:
"numbytes": num_bytes, "numbytes": num_bytes,
"numfiles": num_files, "numfiles": num_files,
} }
print(u"Sending directory (%d bytes compressed) named '%s'" print(u"Sending directory (%s compressed) named '%s'"
% (filesize, basename), file=args.stdout) % (naturalsize(filesize), basename), file=args.stdout)
return offer, fd_to_send return offer, fd_to_send
raise TypeError("'%s' is neither file nor directory" % args.what) raise TypeError("'%s' is neither file nor directory" % args.what)

View File

@ -2,6 +2,7 @@ from __future__ import print_function, unicode_literals
import os, time, json import os, time, json
from collections import defaultdict from collections import defaultdict
import click import click
from humanize import naturalsize
from .database import get_db from .database import get_db
def abbrev(t): def abbrev(t):
@ -13,31 +14,6 @@ def abbrev(t):
return "%.1fms" % (t*1e3) return "%.1fms" % (t*1e3)
return "%.1fus" % (t*1e6) return "%.1fus" % (t*1e6)
def abbreviate_space(s, SI=True):
if s is None:
return "-"
if SI:
U = 1000.0
isuffix = "B"
else:
U = 1024.0
isuffix = "iB"
def r(count, suffix):
return "%.2f %s%s" % (count, suffix, isuffix)
if s < 1024: # 1000-1023 get emitted as bytes, even in SI mode
return "%d B" % s
if s < U*U:
return r(s/U, "k")
if s < U*U*U:
return r(s/(U*U), "M")
if s < U*U*U*U:
return r(s/(U*U*U), "G")
if s < U*U*U*U*U:
return r(s/(U*U*U*U), "T")
if s < U*U*U*U*U*U:
return r(s/(U*U*U*U*U), "P")
return r(s/(U*U*U*U*U*U), "E")
def print_event(event): def print_event(event):
event_type, started, result, total_bytes, waiting_time, total_time = event event_type, started, result, total_bytes, waiting_time, total_time = event
@ -49,7 +25,7 @@ def print_event(event):
abbrev(total_time), abbrev(total_time),
abbrev(waiting_time), abbrev(waiting_time),
abbrev(followthrough), abbrev(followthrough),
abbreviate_space(total_bytes), naturalsize(total_bytes),
time.ctime(started), time.ctime(started),
)) ))
@ -108,8 +84,8 @@ def show_usage(args):
print(" %d events in %s (%.2f per hour)" % (total, abbrev(elapsed), print(" %d events in %s (%.2f per hour)" % (total, abbrev(elapsed),
(3600 * total / elapsed))) (3600 * total / elapsed)))
rate = total_transit_bytes / elapsed rate = total_transit_bytes / elapsed
print(" %s total bytes, %sps" % (abbreviate_space(total_transit_bytes), print(" %s total bytes, %sps" % (naturalsize(total_transit_bytes),
abbreviate_space(rate))) naturalsize(rate)))
print("", ", ".join(["%s=%d (%d%%)" % print("", ", ".join(["%s=%d (%d%%)" %
(k, counters[k], (100.0 * counters[k] / total)) (k, counters[k], (100.0 * counters[k] / total))
for k in sorted(counters) for k in sorted(counters)

View File

@ -1,5 +1,6 @@
from __future__ import print_function, unicode_literals from __future__ import print_function, unicode_literals
import os, sys, re, io, zipfile, six, stat import os, sys, re, io, zipfile, six, stat
from humanize import naturalsize
import mock import mock
from twisted.trial import unittest from twisted.trial import unittest
from twisted.python import procutils, log from twisted.python import procutils, log
@ -367,7 +368,7 @@ class PregeneratedCode(ServerBase, ScriptsBase, unittest.TestCase):
# check sender # check sender
if mode == "text": if mode == "text":
expected = ("Sending text message ({bytes:d} bytes){NL}" expected = ("Sending text message ({bytes:d} Bytes){NL}"
"On the other computer, please run: " "On the other computer, please run: "
"wormhole receive{NL}" "wormhole receive{NL}"
"Wormhole code is: {code}{NL}{NL}" "Wormhole code is: {code}{NL}{NL}"
@ -376,8 +377,9 @@ class PregeneratedCode(ServerBase, ScriptsBase, unittest.TestCase):
NL=NL) NL=NL)
self.failUnlessEqual(send_stdout, expected) self.failUnlessEqual(send_stdout, expected)
elif mode == "file": elif mode == "file":
self.failUnlessIn("Sending {bytes:d} byte file named '{name}'{NL}" self.failUnlessIn("Sending {size:s} file named '{name}'{NL}"
.format(bytes=len(message), name=send_filename, .format(size=naturalsize(len(message)),
name=send_filename,
NL=NL), send_stdout) NL=NL), send_stdout)
self.failUnlessIn("On the other computer, please run: " self.failUnlessIn("On the other computer, please run: "
"wormhole receive{NL}" "wormhole receive{NL}"
@ -402,8 +404,8 @@ class PregeneratedCode(ServerBase, ScriptsBase, unittest.TestCase):
if mode == "text": if mode == "text":
self.failUnlessEqual(receive_stdout, message+NL) self.failUnlessEqual(receive_stdout, message+NL)
elif mode == "file": elif mode == "file":
self.failUnlessIn("Receiving file ({bytes:d} bytes) into: {name}" self.failUnlessIn("Receiving file ({size:s}) into: {name}"
.format(bytes=len(message), .format(size=naturalsize(len(message)),
name=receive_filename), receive_stdout) name=receive_filename), receive_stdout)
self.failUnlessIn("Received file written to ", receive_stdout) self.failUnlessIn("Received file written to ", receive_stdout)
fn = os.path.join(receive_dir, receive_filename) fn = os.path.join(receive_dir, receive_filename)
@ -411,7 +413,7 @@ class PregeneratedCode(ServerBase, ScriptsBase, unittest.TestCase):
with open(fn, "r") as f: with open(fn, "r") as f:
self.failUnlessEqual(f.read(), message) self.failUnlessEqual(f.read(), message)
elif mode == "directory": elif mode == "directory":
want = (r"Receiving directory \(\d+ bytes\) into: {name}/" want = (r"Receiving directory \(\d+ \w+\) into: {name}/"
.format(name=receive_dirname)) .format(name=receive_dirname))
self.failUnless(re.search(want, receive_stdout), self.failUnless(re.search(want, receive_stdout),
(want, receive_stdout)) (want, receive_stdout))
@ -511,8 +513,9 @@ class PregeneratedCode(ServerBase, ScriptsBase, unittest.TestCase):
(receive_stdout, receive_stderr)) (receive_stdout, receive_stderr))
# check sender # check sender
self.failUnlessIn("Sending {bytes:d} byte file named '{name}'{NL}" self.failUnlessIn("Sending {size:s} file named '{name}'{NL}"
.format(bytes=len(message), name=send_filename, .format(size=naturalsize(len(message)),
name=send_filename,
NL=NL), send_stdout) NL=NL), send_stdout)
self.failUnlessIn("On the other computer, please run: " self.failUnlessIn("On the other computer, please run: "
"wormhole receive{NL}" "wormhole receive{NL}"