Added the --onlyvalues option

In combination with --tabbed provides only the values skipping the descriptions of the fields; very useful to provide a table with a fancy layout
This commit is contained in:
Francesco Piraneo G. 2014-04-11 08:56:02 +02:00
parent 18408ee938
commit d6220f40e2
1 changed files with 47 additions and 25 deletions

View File

@ -29,6 +29,7 @@ import threading
import re import re
import signal import signal
import socket import socket
import datetime
# Used for bound_interface # Used for bound_interface
socket_socket = socket.socket socket_socket = socket.socket
@ -450,20 +451,15 @@ def speedtest():
parser.add_argument = parser.add_option parser.add_argument = parser.add_option
except AttributeError: except AttributeError:
pass pass
parser.add_argument('--share', action='store_true', parser.add_argument('--share', action='store_true', help='Generate and provide a URL to the speedtest.net share results image')
help='Generate and provide a URL to the speedtest.net ' parser.add_argument('--simple', action='store_true', help='Suppress verbose output, only show basic information')
'share results image') parser.add_argument('--tabbed', action='store_true', help='Return results in a tabbed fashion i.e. ready to be written on a file')
parser.add_argument('--simple', action='store_true', parser.add_argument('--onlyvalues', action='store_true', help='When --tabbed write only the values and suppress descriptions')
help='Suppress verbose output, only show basic ' parser.add_argument('--list', action='store_true', help='Display a list of speedtest.net servers sorted by distance')
'information')
parser.add_argument('--list', action='store_true',
help='Display a list of speedtest.net servers '
'sorted by distance')
parser.add_argument('--server', help='Specify a server ID to test against') parser.add_argument('--server', help='Specify a server ID to test against')
parser.add_argument('--mini', help='URL of the Speedtest Mini server') parser.add_argument('--mini', help='URL of the Speedtest Mini server')
parser.add_argument('--source', help='Source IP address to bind to') parser.add_argument('--source', help='Source IP address to bind to')
parser.add_argument('--version', action='store_true', parser.add_argument('--version', action='store_true', help='Show the version number and exit')
help='Show the version number and exit')
options = parser.parse_args() options = parser.parse_args()
if isinstance(options, tuple): if isinstance(options, tuple):
@ -481,7 +477,7 @@ def speedtest():
source = args.source source = args.source
socket.socket = bound_socket socket.socket = bound_socket
if not args.simple: if not (args.simple or args.tabbed):
print_('Retrieving speedtest.net configuration...') print_('Retrieving speedtest.net configuration...')
try: try:
config = getConfig() config = getConfig()
@ -489,7 +485,7 @@ def speedtest():
print_('Cannot retrieve speedtest configuration') print_('Cannot retrieve speedtest configuration')
sys.exit(1) sys.exit(1)
if not args.simple: if not (args.simple or args.tabbed):
print_('Retrieving speedtest.net server list...') print_('Retrieving speedtest.net server list...')
if args.list or args.server: if args.list or args.server:
servers = closestServers(config['client'], True) servers = closestServers(config['client'], True)
@ -513,7 +509,7 @@ def speedtest():
else: else:
servers = closestServers(config['client']) servers = closestServers(config['client'])
if not args.simple: if not (args.simple or args.tabbed):
print_('Testing from %(isp)s (%(ip)s)...' % config['client']) print_('Testing from %(isp)s (%(ip)s)...' % config['client'])
if args.server: if args.server:
@ -555,11 +551,11 @@ def speedtest():
except: except:
best = servers[0] best = servers[0]
else: else:
if not args.simple: if not (args.simple or args.tabbed):
print_('Selecting best server based on ping...') print_('Selecting best server based on ping...')
best = getBestServer(servers) best = getBestServer(servers)
if not args.simple: if not (args.simple or args.tabbed):
# Python 2.7 and newer seem to be ok with the resultant encoding # Python 2.7 and newer seem to be ok with the resultant encoding
# from parsing the XML, but older versions have some issues. # from parsing the XML, but older versions have some issues.
# This block should detect whether we need to encode or not # This block should detect whether we need to encode or not
@ -570,20 +566,41 @@ def speedtest():
except NameError: except NameError:
print_('Hosted by %(sponsor)s (%(name)s) [%(d)0.2f km]: ' print_('Hosted by %(sponsor)s (%(name)s) [%(d)0.2f km]: '
'%(latency)s ms' % best) '%(latency)s ms' % best)
else: elif not args.tabbed:
print_('Ping: %(latency)s ms' % best) print_('Ping: %(latency)s ms' % best)
if args.tabbed:
ts = time.time()
st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
if not args.onlyvalues:
print_('Timestamp: ', end='')
print_('%s, ' % (st), end='')
if args.onlyvalues:
print_('%(sponsor)s, %(name)s, %(d)0.2f, %(latency)s, ' % (best), end='')
else:
print_('Target: %(sponsor)s, City: %(name)s, Distance: %(d)0.2f km, Latency: %(latency)s ms, ' % (best), end='')
sizes = [350, 500, 750, 1000, 1500, 2000, 2500, 3000, 3500, 4000] sizes = [350, 500, 750, 1000, 1500, 2000, 2500, 3000, 3500, 4000]
urls = [] urls = []
for size in sizes: for size in sizes:
for i in range(0, 4): for i in range(0, 4):
urls.append('%s/random%sx%s.jpg' % urls.append('%s/random%sx%s.jpg' %
(os.path.dirname(best['url']), size, size)) (os.path.dirname(best['url']), size, size))
if not args.simple: if not (args.simple or args.tabbed):
print_('Testing download speed', end='') print_('Testing download speed', end='')
dlspeed = downloadSpeed(urls, args.simple)
if not args.simple: dlspeed = downloadSpeed(urls, (args.simple or args.tabbed))
if not (args.simple or args.tabbed):
print_() print_()
if args.tabbed:
if args.onlyvalues:
print_('%0.2f, ' % ((dlspeed / 1000 / 1000) * 8), end='')
else:
print_('Download: %0.2f Mbit/s, ' % ((dlspeed / 1000 / 1000) * 8), end='')
else:
print_('Download: %0.2f Mbit/s' % ((dlspeed / 1000 / 1000) * 8)) print_('Download: %0.2f Mbit/s' % ((dlspeed / 1000 / 1000) * 8))
sizesizes = [int(.25 * 1000 * 1000), int(.5 * 1000 * 1000)] sizesizes = [int(.25 * 1000 * 1000), int(.5 * 1000 * 1000)]
@ -591,11 +608,16 @@ def speedtest():
for size in sizesizes: for size in sizesizes:
for i in range(0, 25): for i in range(0, 25):
sizes.append(size) sizes.append(size)
if not args.simple: if not (args.simple or args.tabbed):
print_('Testing upload speed', end='') print_('Testing upload speed', end='')
ulspeed = uploadSpeed(best['url'], sizes, args.simple) ulspeed = uploadSpeed(best['url'], sizes, (args.simple or args.tabbed))
if not args.simple:
if not (args.simple or args.tabbed):
print_() print_()
if args.tabbed and args.onlyvalues:
print_('%0.2f' % ((ulspeed / 1000 / 1000) * 8))
else:
print_('Upload: %0.2f Mbit/s' % ((ulspeed / 1000 / 1000) * 8)) print_('Upload: %0.2f Mbit/s' % ((ulspeed / 1000 / 1000) * 8))
if args.share and args.mini: if args.share and args.mini: