This commit is contained in:
Jonas Malaco Filho 2014-04-27 10:36:49 +00:00
commit 40e1d6d6fa
1 changed files with 34 additions and 13 deletions

View File

@ -102,6 +102,11 @@ except ImportError:
from optparse import OptionParser as ArgParser
PARSER_TYPE_INT = 'int'
try:
import json
except ImportError:
import simplejson as json
try:
import builtins
except ImportError:
@ -388,23 +393,31 @@ class SpeedtestResults(object):
self.upload = DataDescriptor(upload)
self.ping = DataDescriptor(ping)
self.server = DataDescriptor(server)
self.share = DataDescriptor(None)
self._share = None
def dict(self):
def dict(self, share=False):
"""Return dictionary of result data"""
return dict(download=self.download,
output = dict(download=self.download,
upload=self.upload,
ping=self.ping,
server=int(self.server['id']))
def csv(self):
if share:
output["share"] = self.share()
return output
def csv(self, share=False):
"""Return data in CSV format in the order of:
Speedtest.net Server ID, Latency/Ping, Download Speed, Upload Speed
Speedtest.net Server ID, Latency/Ping, Download Speed, Upload Speed and
Share Link
"""
return '%(server)s,%(ping)s,%(download)s,%(upload)s' % self.dict()
output = '%(server)s,%(ping)s,%(download)s,%(upload)s' % self.dict()
return "%s,%s" % (output, self.share()) if share else output
def share(self):
"""POST data to the speedtest.net API to obtain a share results
@ -462,14 +475,17 @@ class SpeedtestResults(object):
return self._share
def simple(self, units):
return """Ping: %s ms
def simple(self, units, share=False):
results = """Ping: %s ms
Download: %0.2f M%s/s
Upload: %0.2f M%s/s""" % (self.ping,
(self.download / 1000 / 1000) * units[1],
units[0],
(self.upload / 1000 / 1000) * units[1],
units[0])
if share:
results = "%s\nShare results: %s" % (results, self.share())
return results
class Speedtest(object):
@ -784,7 +800,8 @@ class Speedtest(object):
while thread.isAlive():
thread.join(timeout=0.1)
finished.append(sum(thread.result))
callback(thread.i, url_count, end=True)
if callback:
callback(thread.i, url_count, end=True)
del thread
q = Queue(self.config['threads']['download'])
@ -834,7 +851,8 @@ class Speedtest(object):
while thread.isAlive():
thread.join(timeout=0.1)
finished.append(thread.result)
callback(thread.i, size_count, end=True)
if callback:
callback(thread.i, size_count, end=True)
del thread
q = Queue(self.config['threads']['upload'])
@ -1044,12 +1062,15 @@ def shell():
((results.upload / 1000 / 1000) * args.units[1], args.units[0]),
quiet)
if args.share:
printer('Share results: %s' % results.share(), quiet)
if args.simple:
print_(results.simple(args.units))
print_(results.simple(args.units, share=args.share))
elif args.csv:
print_(results.csv())
print_(results.csv(share=args.share))
elif args.json:
print_(repr(results.dict()).replace("'", '"'))
print_(json.dumps(results.dict(share=args.share)))
def main():