diff options
author | 2011-07-26 03:52:55 +0530 | |
---|---|---|
committer | 2011-07-26 03:52:55 +0530 | |
commit | 9fc558e2719e01c5ad25e55124e45a662ebb5fc0 (patch) | |
tree | d3cf9f7c97499161ce19768dd2f9410b608b6543 | |
parent | gentoostats-cli first commit (diff) | |
download | gentoostats-9fc558e2719e01c5ad25e55124e45a662ebb5fc0.tar.gz gentoostats-9fc558e2719e01c5ad25e55124e45a662ebb5fc0.tar.bz2 gentoostats-9fc558e2719e01c5ad25e55124e45a662ebb5fc0.zip |
misc fixes
-rwxr-xr-x | client/gentoostats-cli | 9 | ||||
-rw-r--r-- | client/gentoostats/list.py | 109 | ||||
-rw-r--r-- | client/gentoostats/utils.py | 22 |
3 files changed, 76 insertions, 64 deletions
diff --git a/client/gentoostats-cli b/client/gentoostats-cli index cee0da1..c8a5222 100755 --- a/client/gentoostats-cli +++ b/client/gentoostats-cli @@ -3,9 +3,12 @@ import sys import importlib -actions = {'list':'gentoostats.list', 'search':'gentooostats.search'} +actions = { + 'list': 'gentoostats.list', + 'search': 'gentooostats.search' + } -def print_usage(): +def print_usage(actions): print 'Usage:', sys.argv[0], '<action>' print 'Available actions:' for action in actions.keys(): @@ -14,7 +17,7 @@ def print_usage(): def main(): argc = len(sys.argv) if argc == 1: - print_usage() + print_usage(actions) sys.exit(1) try: loaded_module = importlib.import_module(actions[sys.argv[1]]) diff --git a/client/gentoostats/list.py b/client/gentoostats/list.py index 480587c..3cf60cf 100644 --- a/client/gentoostats/list.py +++ b/client/gentoostats/list.py @@ -2,34 +2,38 @@ import sys import json import httplib +import utils -objects={ -'arch':'list_arch', -'feature':'list_feature', -'lang':'list_lang', -'mirror':'list_mirror', -'repo':'list_repo', -'category':'list_cat', -} +objects = { + 'arch': 'list_arch', + 'feature': 'list_feature', + 'lang': 'list_lang', + 'mirror': 'list_mirror', + 'repo': 'list_repo', + 'cat': 'list_cat', + 'cp': 'list_cp', + 'cpv': 'list_cpv' + } server = 'soc.dev.gentoo.org' url = '/gentoostats' -headers = {'Accept':'application/json'} +headers = {'Accept': 'application/json'} -def print_usage(): +def print_usage(objects): print 'Usage: list <object>' print 'Available objects:' for obj in objects.keys(): print obj -def pprint(object): +def pprint(title, object): + print title import pprint pprint.pprint(object) def main(opts): l = len(opts) if l == 0: - print_usage() + print_usage(objects) sys.exit(1) if opts[0] not in objects: @@ -37,61 +41,44 @@ def main(opts): sys.exit(1) try: - globals()[objects[opts[0]]]() + globals()[objects[opts[0]]](server, url, headers) except KeyError: sys.stderr.write('Unimplemented') sys.exit(1) -def list_arch(): - conn = httplib.HTTPSConnection(server) - conn.request('GET', url=url+'/arch', headers=headers) - try: - arch_data = json.JSONDecoder().decode(conn.getresponse().read()) - except ValueError: - sys.exit(1) - pprint(arch_data) +def list(server, url_base, url_extra, headers): + get_data = utils.GET(server=server, url=url_base+url_extra, headers=headers) + data = utils.deserialize(get_data) + return data -def list_feature(): - conn = httplib.HTTPSConnection(server) - conn.request('GET', url=url+'/feature', headers=headers) - try: - feature_data = json.JSONDecoder().decode(conn.getresponse().read()) - except ValueError: - sys.exit(1) - pprint(feature_data) +def list_arch(server, url, headers): + data = list(server, url, '/arch', headers) + pprint('Arch', data) -def list_lang(): - conn = httplib.HTTPSConnection(server) - conn.request('GET', url=url+'/lang', headers=headers) - try: - lang_data = json.JSONDecoder().decode(conn.getresponse().read()) - except ValueError: - sys.exit(1) - pprint(lang_data) +def list_feature(server, url, headers): + data = list(server, url, '/feature', headers) + pprint('Feature', data) -def list_mirror(): - conn = httplib.HTTPSConnection(server) - conn.request('GET', url=url+'/mirror', headers=headers) - try: - mirror_data = json.JSONDecoder().decode(conn.getresponse().read()) - except ValueError: - sys.exit(1) - pprint(mirror_data) +def list_lang(server, url, headers): + data = list(server, url, '/lang', headers) + pprint('Lang', data) -def list_repo(): - conn = httplib.HTTPSConnection(server) - conn.request('GET', url=url+'/mirror', headers=headers) - try: - repo_data = json.JSONDecoder().decode(conn.getresponse().read()) - except ValueError: - sys.exit(1) - pprint(repo_data) +def list_mirror(server, url, headers): + data = list(server, url, '/mirror', headers) + pprint('Mirror', data) -def list_cat(): - conn = httplib.HTTPSConnection(server) - conn.request('GET', url=url+'/package', headers=headers) - try: - cat_data = json.JSONDecoder().decode(conn.getresponse().read()) - except ValueError: - sys.exit(1) - pprint(cat_data) +def list_repo(server, url, headers): + data = list(server, url, '/repo', headers) + pprint('Repo', data) + +def list_cat(server, url, headers): + data = list(server, url, '/package', headers) + pprint('Category', data) + +def list_cp(server, url, headers): + data = list(server, url, '/package', headers) + pprint('Category/Package', data) + +def list_cpv(server, url, headers): + data = list(server, url, '/package', headers) + pprint('Category/Package-Version', data) diff --git a/client/gentoostats/utils.py b/client/gentoostats/utils.py new file mode 100644 index 0000000..ae520f2 --- /dev/null +++ b/client/gentoostats/utils.py @@ -0,0 +1,22 @@ + +import json +import httplib + +def GET(server, url, headers, https=True): + if https: + conn = httplib.HTTPSConnection(server) + else: + conn = httplib.HTTPConnection(server) + try: + conn.request('GET', url=url, headers=headers) + data = conn.getresponse().read() + except httplib.HTTPException: + return None + return data + +def deserialize(object): + try: + decoded = json.JSONDecoder().decode(object) + except (ValueError, TypeError): + return None + return decoded |