Merge PR92: use 'humanize' to abbreviate filesizes
This commit is contained in:
commit
6b0ebef559
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,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.internet import reactor
|
||||
from twisted.internet.defer import inlineCallbacks, returnValue
|
||||
from twisted.python import log
|
||||
|
@ -194,8 +195,8 @@ class TwistedReceiver:
|
|||
file_data["filename"])
|
||||
self.xfersize = file_data["filesize"]
|
||||
|
||||
self._msg(u"Receiving file (%d bytes) into: %s" %
|
||||
(self.xfersize, os.path.basename(self.abs_destname)))
|
||||
self._msg(u"Receiving file (%s) into: %s" %
|
||||
(naturalsize(self.xfersize), os.path.basename(self.abs_destname)))
|
||||
self._ask_permission()
|
||||
tmp_destname = self.abs_destname + ".tmp"
|
||||
return open(tmp_destname, "wb")
|
||||
|
@ -210,10 +211,10 @@ class TwistedReceiver:
|
|||
file_data["dirname"])
|
||||
self.xfersize = file_data["zipsize"]
|
||||
|
||||
self._msg(u"Receiving directory (%d bytes) into: %s/" %
|
||||
(self.xfersize, os.path.basename(self.abs_destname)))
|
||||
self._msg(u"%d files, %d bytes (uncompressed)" %
|
||||
(file_data["numfiles"], file_data["numbytes"]))
|
||||
self._msg(u"Receiving directory (%s) into: %s/" %
|
||||
(naturalsize(self.xfersize), os.path.basename(self.abs_destname)))
|
||||
self._msg(u"%d files, %s (uncompressed)" %
|
||||
(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
|
||||
|
@ -167,7 +168,7 @@ class Sender:
|
|||
text = six.moves.input("Text to send: ")
|
||||
|
||||
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)
|
||||
offer = { "message": text }
|
||||
fd_to_send = None
|
||||
|
@ -187,7 +188,8 @@ class Sender:
|
|||
"filename": basename,
|
||||
"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)
|
||||
fd_to_send = open(what, "rb")
|
||||
return offer, fd_to_send
|
||||
|
@ -222,8 +224,8 @@ class Sender:
|
|||
"numbytes": num_bytes,
|
||||
"numfiles": num_files,
|
||||
}
|
||||
print(u"Sending directory (%d bytes compressed) named '%s'"
|
||||
% (filesize, basename), file=args.stdout)
|
||||
print(u"Sending directory (%s compressed) named '%s'"
|
||||
% (naturalsize(filesize), basename), file=args.stdout)
|
||||
return offer, fd_to_send
|
||||
|
||||
raise TypeError("'%s' is neither file nor directory" % args.what)
|
||||
|
|
|
@ -2,6 +2,7 @@ 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
|
||||
|
||||
def abbrev(t):
|
||||
|
@ -13,31 +14,6 @@ def abbrev(t):
|
|||
return "%.1fms" % (t*1e3)
|
||||
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):
|
||||
event_type, started, result, total_bytes, waiting_time, total_time = event
|
||||
|
@ -49,7 +25,7 @@ def print_event(event):
|
|||
abbrev(total_time),
|
||||
abbrev(waiting_time),
|
||||
abbrev(followthrough),
|
||||
abbreviate_space(total_bytes),
|
||||
naturalsize(total_bytes),
|
||||
time.ctime(started),
|
||||
))
|
||||
|
||||
|
@ -108,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" % (abbreviate_space(total_transit_bytes),
|
||||
abbreviate_space(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
|
||||
|
@ -367,7 +368,7 @@ class PregeneratedCode(ServerBase, ScriptsBase, unittest.TestCase):
|
|||
|
||||
# check sender
|
||||
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: "
|
||||
"wormhole receive{NL}"
|
||||
"Wormhole code is: {code}{NL}{NL}"
|
||||
|
@ -376,8 +377,9 @@ class PregeneratedCode(ServerBase, ScriptsBase, unittest.TestCase):
|
|||
NL=NL)
|
||||
self.failUnlessEqual(send_stdout, expected)
|
||||
elif mode == "file":
|
||||
self.failUnlessIn("Sending {bytes:d} byte file named '{name}'{NL}"
|
||||
.format(bytes=len(message), name=send_filename,
|
||||
self.failUnlessIn("Sending {size:s} file named '{name}'{NL}"
|
||||
.format(size=naturalsize(len(message)),
|
||||
name=send_filename,
|
||||
NL=NL), send_stdout)
|
||||
self.failUnlessIn("On the other computer, please run: "
|
||||
"wormhole receive{NL}"
|
||||
|
@ -402,8 +404,8 @@ class PregeneratedCode(ServerBase, ScriptsBase, unittest.TestCase):
|
|||
if mode == "text":
|
||||
self.failUnlessEqual(receive_stdout, message+NL)
|
||||
elif mode == "file":
|
||||
self.failUnlessIn("Receiving file ({bytes:d} bytes) into: {name}"
|
||||
.format(bytes=len(message),
|
||||
self.failUnlessIn("Receiving file ({size:s}) into: {name}"
|
||||
.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)
|
||||
|
@ -411,7 +413,7 @@ class PregeneratedCode(ServerBase, ScriptsBase, unittest.TestCase):
|
|||
with open(fn, "r") as f:
|
||||
self.failUnlessEqual(f.read(), message)
|
||||
elif mode == "directory":
|
||||
want = (r"Receiving directory \(\d+ bytes\) into: {name}/"
|
||||
want = (r"Receiving directory \(\d+ \w+\) into: {name}/"
|
||||
.format(name=receive_dirname))
|
||||
self.failUnless(re.search(want, receive_stdout),
|
||||
(want, receive_stdout))
|
||||
|
@ -511,8 +513,9 @@ class PregeneratedCode(ServerBase, ScriptsBase, unittest.TestCase):
|
|||
(receive_stdout, receive_stderr))
|
||||
|
||||
# check sender
|
||||
self.failUnlessIn("Sending {bytes:d} byte file named '{name}'{NL}"
|
||||
.format(bytes=len(message), name=send_filename,
|
||||
self.failUnlessIn("Sending {size:s} file named '{name}'{NL}"
|
||||
.format(size=naturalsize(len(message)),
|
||||
name=send_filename,
|
||||
NL=NL), send_stdout)
|
||||
self.failUnlessIn("On the other computer, please run: "
|
||||
"wormhole receive{NL}"
|
||||
|
|
Loading…
Reference in New Issue
Block a user