# HG changeset patch # User Marcin Kuzminski # Date 2013-05-19 19:46:54 # Node ID 5a7d52cf084d89ff52475b56a50faad9512dddd1 # Parent 237a8e3727a23c02b1f2180adce9fd1de73a60d7 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 diff --git a/rhodecode/bin/base.py b/rhodecode/bin/base.py --- a/rhodecode/bin/base.py +++ b/rhodecode/bin/base.py @@ -20,15 +20,16 @@ FORMAT_PRETTY = 'pretty' 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 @@ def api_call(apikey, apihost, format, me 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)) diff --git a/rhodecode/bin/rhodecode_api.py b/rhodecode/bin/rhodecode_api.py --- a/rhodecode/bin/rhodecode_api.py +++ b/rhodecode/bin/rhodecode_api.py @@ -27,7 +27,7 @@ from __future__ import with_statement 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 @@ def argparser(argv): 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 @@ def main(argv=None): '--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__': diff --git a/rhodecode/bin/rhodecode_gist.py b/rhodecode/bin/rhodecode_gist.py --- a/rhodecode/bin/rhodecode_gist.py +++ b/rhodecode/bin/rhodecode_gist.py @@ -30,7 +30,7 @@ import stat 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 @@ def argparser(argv): 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 @@ def _run(argv): 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