Mercurial > kallithea
changeset 3875:5a7d52cf084d beta
Added --format into gist CLI
- default --pretty format will give a nice message about gist creation
- api_call shouldn't do any printing let the CLI decide about that
- return pure json with --json format
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Sun, 19 May 2013 19:46:54 +0200 |
parents | 237a8e3727a2 |
children | c5dd289ca96e |
files | rhodecode/bin/base.py rhodecode/bin/rhodecode_api.py rhodecode/bin/rhodecode_gist.py |
diffstat | 3 files changed, 34 insertions(+), 17 deletions(-) [+] |
line wrap: on
line diff
--- a/rhodecode/bin/base.py Sun May 19 18:21:42 2013 +0200 +++ b/rhodecode/bin/base.py Sun May 19 19:46:54 2013 +0200 @@ -20,15 +20,16 @@ FORMAT_JSON = 'json' -def api_call(apikey, apihost, format, method=None, **kw): +def api_call(apikey, apihost, method=None, **kw): """ - Api_call wrapper for RhodeCode + Api_call wrapper for RhodeCode. :param apikey: :param apihost: :param format: formatting, pretty means prints and pprint of json json returns unparsed json :param method: + :returns: json response from server """ def _build_data(random_id): """ @@ -45,24 +46,20 @@ if not method: raise Exception('please specify method name !') + id_ = random.randrange(1, 9999) req = urllib2.Request('%s/_admin/api' % apihost, data=json.dumps(_build_data(id_)), headers={'content-type': 'text/plain'}) - if format == FORMAT_PRETTY: - sys.stdout.write('calling %s to %s \n' % (req.get_data(), apihost)) ret = urllib2.urlopen(req) raw_json = ret.read() json_data = json.loads(raw_json) id_ret = json_data['id'] - _formatted_json = pprint.pformat(json_data) if id_ret == id_: - if format == FORMAT_JSON: - sys.stdout.write(str(raw_json)) - else: - sys.stdout.write('rhodecode returned:\n%s\n' % (_formatted_json)) + return json_data else: + _formatted_json = pprint.pformat(json_data) raise Exception('something went wrong. ' 'ID mismatch got %s, expected %s | %s' % ( id_ret, id_, _formatted_json))
--- a/rhodecode/bin/rhodecode_api.py Sun May 19 18:21:42 2013 +0200 +++ b/rhodecode/bin/rhodecode_api.py Sun May 19 19:46:54 2013 +0200 @@ -27,7 +27,7 @@ import sys import argparse -from rhodecode.bin.base import api_call, RcConf, FORMAT_JSON, FORMAT_PRETTY +from rhodecode.bin.base import json, api_call, RcConf, FORMAT_JSON, FORMAT_PRETTY def argparser(argv): @@ -53,8 +53,8 @@ help='API method name to call followed by key:value attributes', ) group.add_argument('--format', dest='format', type=str, - help='output format default: `pretty` can ' - 'be also `%s`' % FORMAT_JSON, + help='output format default: `%s` can ' + 'be also `%s`' % (FORMAT_PRETTY, FORMAT_JSON), default=FORMAT_PRETTY ) args, other = parser.parse_known_args() @@ -90,16 +90,27 @@ '--apikey or --apihost in params') apikey = args.apikey or conf['apikey'] - host = args.apihost or conf['apihost'] + apihost = args.apihost or conf['apihost'] method = args.method + # if we don't have method here it's an error + if not method: + parser.error('Please specify method name') + try: margs = dict(map(lambda s: s.split(':', 1), other)) except Exception: sys.stderr.write('Error parsing arguments \n') sys.exit() + if args.format == FORMAT_PRETTY: + print 'Calling method %s => %s' % (method, apihost) - api_call(apikey, host, args.format, method, **margs) + json_data = api_call(apikey, apihost, method, **margs)['result'] + if args.format == FORMAT_JSON: + print json.dumps(json_data) + elif args.format == FORMAT_PRETTY: + print 'Server response \n%s' % ( + json.dumps(json_data, indent=4, sort_keys=True)) return 0 if __name__ == '__main__':
--- a/rhodecode/bin/rhodecode_gist.py Sun May 19 18:21:42 2013 +0200 +++ b/rhodecode/bin/rhodecode_gist.py Sun May 19 19:46:54 2013 +0200 @@ -30,7 +30,7 @@ import argparse import fileinput -from rhodecode.bin.base import api_call, RcConf +from rhodecode.bin.base import json, api_call, RcConf, FORMAT_JSON, FORMAT_PRETTY def argparser(argv): @@ -59,7 +59,11 @@ group.add_argument('-d', '--description', help='Gist description') group.add_argument('-l', '--lifetime', metavar='MINUTES', help='Gist lifetime in minutes, -1 (Default) is forever') - + group.add_argument('--format', dest='format', type=str, + help='output format default: `%s` can ' + 'be also `%s`' % (FORMAT_PRETTY, FORMAT_JSON), + default=FORMAT_PRETTY + ) args, other = parser.parse_known_args() return parser, args, other @@ -135,7 +139,12 @@ files=files ) - api_call(apikey, host, 'json', 'create_gist', **margs) + json_data = api_call(apikey, host, 'create_gist', **margs)['result'] + if args.format == FORMAT_JSON: + print json.dumps(json_data) + elif args.format == FORMAT_PRETTY: + print 'Created %s gist %s' % (json_data['gist_type'], + json_data['gist_url']) return 0