From 8730705515848c205e7120855f67a3b278b1b34c Mon Sep 17 00:00:00 2001 From: Ale Almuna Date: Sun, 20 Dec 2015 19:55:36 -0300 Subject: [PATCH] Allow passing options to speedtest() in arguments The same options available for the cli interface are available for passing as function arguments. --- speedtest_cli.py | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/speedtest_cli.py b/speedtest_cli.py index d0a98fd..1b27d50 100755 --- a/speedtest_cli.py +++ b/speedtest_cli.py @@ -547,7 +547,7 @@ def version(): raise SystemExit(__version__) -def speedtest(): +def speedtest(**kwargs): """Run the full speedtest.net test""" global shutdown_event, source, scheme @@ -593,12 +593,30 @@ def speedtest(): parser.add_argument('--version', action='store_true', help='Show the version number and exit') - options = parser.parse_args() - if isinstance(options, tuple): - args = options[0] + if kwargs == {}: + options = parser.parse_args() + if isinstance(options, tuple): + args = options[0] + else: + args = options + del options else: - args = options - del options + opts_dict = {} + opts_dict['units'] = ('byte', 1) if 'bytes' in kwargs else ( + 'bit', 8) + opts_dict['share'] = kwargs['share'] if 'share' in kwargs else False + opts_dict['list'] = kwargs['list'] if 'list' in kwargs else False + opts_dict['server'] = str( + kwargs['server']) if 'server' in kwargs else None + opts_dict['mini'] = kwargs['mini'] if 'mini' in kwargs else None + opts_dict['source'] = kwargs['source'] if 'source' in kwargs else None + opts_dict['timeout'] = kwargs['timeout'] if 'timeout' in kwargs else 10 + opts_dict['secure'] = True if 'secure' in kwargs else False + opts_dict['version'] = True if 'version' in kwargs else False + opts_dict['simple'] = kwargs['simple'] if 'simple' in kwargs else False + + # Object to access dictionary values by attribute lookup + args = type('TestOptions', (object,), opts_dict) # Print the version and exit if args.version: