diff options
author | 2011-08-02 03:53:54 +0530 | |
---|---|---|
committer | 2011-08-02 03:53:54 +0530 | |
commit | 8755eeaee5daad8a51ffab361ce2e01422272f69 (patch) | |
tree | 2902c6cc0a765488566180ed8d865c14ab8b0b9a | |
parent | misc fixes (diff) | |
download | gentoostats-8755eeaee5daad8a51ffab361ce2e01422272f69.tar.gz gentoostats-8755eeaee5daad8a51ffab361ce2e01422272f69.tar.bz2 gentoostats-8755eeaee5daad8a51ffab361ce2e01422272f69.zip |
reworked gentoostats-cli code
-rwxr-xr-x | client/gentoostats-cli | 35 | ||||
-rw-r--r-- | client/gentoostats/list.py | 139 | ||||
-rw-r--r-- | client/gentoostats/utils.py | 2 |
3 files changed, 89 insertions, 87 deletions
diff --git a/client/gentoostats-cli b/client/gentoostats-cli index c8a5222..6a237fc 100755 --- a/client/gentoostats-cli +++ b/client/gentoostats-cli @@ -1,31 +1,22 @@ #!/usr/bin/env python -import sys -import importlib +import argparse -actions = { - 'list': 'gentoostats.list', - 'search': 'gentooostats.search' - } - -def print_usage(actions): - print 'Usage:', sys.argv[0], '<action>' - print 'Available actions:' - for action in actions.keys(): - print action +from gentoostats import list +#from gentoostats import search def main(): - argc = len(sys.argv) - if argc == 1: - print_usage(actions) - sys.exit(1) - try: - loaded_module = importlib.import_module(actions[sys.argv[1]]) - loaded_module.main(opts=sys.argv[2:]) - except KeyError, ImportError: - sys.stderr.writelines('Unknown action') - sys.exit(1) + parser = argparse.ArgumentParser() + parser.add_argument('-s', '--server', default='soc.dev.gentoo.org') + parser.add_argument('-u', '--url', default='/gentoostats') + + subparsers = parser.add_subparsers() + + list.add_parser(subparsers) +# search.add_parser(subparsers) + args = parser.parse_args() + args.func(args) if __name__ == "__main__": main() diff --git a/client/gentoostats/list.py b/client/gentoostats/list.py index 3cf60cf..6ee62cb 100644 --- a/client/gentoostats/list.py +++ b/client/gentoostats/list.py @@ -1,84 +1,93 @@ -import sys -import json -import httplib import utils -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'} - -def print_usage(objects): - print 'Usage: list <object>' - print 'Available objects:' - for obj in objects.keys(): - print obj - def pprint(title, object): - print title + # TODO: write a custom pretty printer here import pprint + print title pprint.pprint(object) -def main(opts): - l = len(opts) - if l == 0: - print_usage(objects) - sys.exit(1) - - if opts[0] not in objects: - sys.stderr.write('Unknown object') - sys.exit(1) - - try: - globals()[objects[opts[0]]](server, url, headers) - except KeyError: - sys.stderr.write('Unimplemented') - sys.exit(1) - -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_arch(server, url, headers): - data = list(server, url, '/arch', headers) +def add_parser(subparsers): + # TODO: add help and descriptions for all opts + list_parser = subparsers.add_parser('list') + list_subparsers = list_parser.add_subparsers() + + objects = { + 'arch': ['parser_arch', list_arch], + 'feature': ['parser_feature', list_feature], + 'lang': ['parser_lang', list_lang], + 'mirror': ['parser_mirror', list_mirror], + 'repo': ['parser_repo', list_repo], + 'package': ['parser_package', list_package], + 'use': ['parser_use', list_use] + } + for obj in objects.keys(): + parser = vars()[objects[obj][0]] = list_subparsers.add_parser(obj) + parser.set_defaults(func=objects[obj][1]) + + # need separate arguments for package + parser = vars()[objects['package'][0]] + parser.add_argument('-t', '--top', type=int) + parser.add_argument('-c', '--category') + parser.add_argument('-p', '--package') + parser.add_argument('-v', '--version') + + # need separate arguments for use + parser = vars()[objects['use'][0]] + parser.add_argument('--use') + +def list_arch(args): + data = list(args.server, args.url, '/arch', utils.headers) pprint('Arch', data) -def list_feature(server, url, headers): - data = list(server, url, '/feature', headers) +def list_feature(args): + data = list(args.server, args.url, '/feature', utils.headers) pprint('Feature', data) -def list_lang(server, url, headers): - data = list(server, url, '/lang', headers) +def list_lang(args): + data = list(args.server, args.url, '/lang', utils.headers) pprint('Lang', data) -def list_mirror(server, url, headers): - data = list(server, url, '/mirror', headers) +def list_mirror(args): + data = list(args.server, args.url, '/mirror', utils.headers) pprint('Mirror', data) -def list_repo(server, url, headers): - data = list(server, url, '/repo', headers) +def list_repo(args): + data = list(args.server, args.url, '/repo', utils.headers) pprint('Repo', data) -def list_cat(server, url, headers): - data = list(server, url, '/package', headers) - pprint('Category', data) +def list_package(args): + url_top = '' + if args.top: + url_top = '?top=' + str(args.top) + + title = 'Categories' + url_pkg = '/package' + if args.category: + title = 'Category: ' + args.category + url_pkg += '/' + args.category + if args.package: + title = 'Category-Package: ' + args.category + '/' + args.package + url_pkg += '/' + args.package + if args.version: + title = 'Category-Package-Version: ' + args.category + '/' + args.package + '-' + args.version + url_pkg += '-' + args.version + + data = list(args.server, args.url, url_pkg + url_top, utils.headers) + pprint(title, data) + +def list_use(args): + url_use = '/use' + title = 'Useflags' + if args.use: + title = 'Useflag: ' + args.use + url_use += '/' + args.use + + data = list(args.server, args.url, url_use, utils.headers) + pprint(title, 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) +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 diff --git a/client/gentoostats/utils.py b/client/gentoostats/utils.py index ae520f2..98f8375 100644 --- a/client/gentoostats/utils.py +++ b/client/gentoostats/utils.py @@ -2,6 +2,8 @@ import json import httplib +headers = {'Accept': 'application/json'} + def GET(server, url, headers, https=True): if https: conn = httplib.HTTPSConnection(server) |