##// END OF EJS Templates
Added --format into gist CLI...
marcink -
r3875:5a7d52cf beta
parent child Browse files
Show More
@@ -20,15 +20,16 b" FORMAT_PRETTY = 'pretty'"
20 FORMAT_JSON = 'json'
20 FORMAT_JSON = 'json'
21
21
22
22
23 def api_call(apikey, apihost, format, method=None, **kw):
23 def api_call(apikey, apihost, method=None, **kw):
24 """
24 """
25 Api_call wrapper for RhodeCode
25 Api_call wrapper for RhodeCode.
26
26
27 :param apikey:
27 :param apikey:
28 :param apihost:
28 :param apihost:
29 :param format: formatting, pretty means prints and pprint of json
29 :param format: formatting, pretty means prints and pprint of json
30 json returns unparsed json
30 json returns unparsed json
31 :param method:
31 :param method:
32 :returns: json response from server
32 """
33 """
33 def _build_data(random_id):
34 def _build_data(random_id):
34 """
35 """
@@ -45,24 +46,20 b' def api_call(apikey, apihost, format, me'
45
46
46 if not method:
47 if not method:
47 raise Exception('please specify method name !')
48 raise Exception('please specify method name !')
49
48 id_ = random.randrange(1, 9999)
50 id_ = random.randrange(1, 9999)
49 req = urllib2.Request('%s/_admin/api' % apihost,
51 req = urllib2.Request('%s/_admin/api' % apihost,
50 data=json.dumps(_build_data(id_)),
52 data=json.dumps(_build_data(id_)),
51 headers={'content-type': 'text/plain'})
53 headers={'content-type': 'text/plain'})
52 if format == FORMAT_PRETTY:
53 sys.stdout.write('calling %s to %s \n' % (req.get_data(), apihost))
54 ret = urllib2.urlopen(req)
54 ret = urllib2.urlopen(req)
55 raw_json = ret.read()
55 raw_json = ret.read()
56 json_data = json.loads(raw_json)
56 json_data = json.loads(raw_json)
57 id_ret = json_data['id']
57 id_ret = json_data['id']
58 _formatted_json = pprint.pformat(json_data)
59 if id_ret == id_:
58 if id_ret == id_:
60 if format == FORMAT_JSON:
59 return json_data
61 sys.stdout.write(str(raw_json))
62 else:
63 sys.stdout.write('rhodecode returned:\n%s\n' % (_formatted_json))
64
60
65 else:
61 else:
62 _formatted_json = pprint.pformat(json_data)
66 raise Exception('something went wrong. '
63 raise Exception('something went wrong. '
67 'ID mismatch got %s, expected %s | %s' % (
64 'ID mismatch got %s, expected %s | %s' % (
68 id_ret, id_, _formatted_json))
65 id_ret, id_, _formatted_json))
@@ -27,7 +27,7 b' from __future__ import with_statement'
27 import sys
27 import sys
28 import argparse
28 import argparse
29
29
30 from rhodecode.bin.base import api_call, RcConf, FORMAT_JSON, FORMAT_PRETTY
30 from rhodecode.bin.base import json, api_call, RcConf, FORMAT_JSON, FORMAT_PRETTY
31
31
32
32
33 def argparser(argv):
33 def argparser(argv):
@@ -53,8 +53,8 b' def argparser(argv):'
53 help='API method name to call followed by key:value attributes',
53 help='API method name to call followed by key:value attributes',
54 )
54 )
55 group.add_argument('--format', dest='format', type=str,
55 group.add_argument('--format', dest='format', type=str,
56 help='output format default: `pretty` can '
56 help='output format default: `%s` can '
57 'be also `%s`' % FORMAT_JSON,
57 'be also `%s`' % (FORMAT_PRETTY, FORMAT_JSON),
58 default=FORMAT_PRETTY
58 default=FORMAT_PRETTY
59 )
59 )
60 args, other = parser.parse_known_args()
60 args, other = parser.parse_known_args()
@@ -90,16 +90,27 b' def main(argv=None):'
90 '--apikey or --apihost in params')
90 '--apikey or --apihost in params')
91
91
92 apikey = args.apikey or conf['apikey']
92 apikey = args.apikey or conf['apikey']
93 host = args.apihost or conf['apihost']
93 apihost = args.apihost or conf['apihost']
94 method = args.method
94 method = args.method
95
95
96 # if we don't have method here it's an error
97 if not method:
98 parser.error('Please specify method name')
99
96 try:
100 try:
97 margs = dict(map(lambda s: s.split(':', 1), other))
101 margs = dict(map(lambda s: s.split(':', 1), other))
98 except Exception:
102 except Exception:
99 sys.stderr.write('Error parsing arguments \n')
103 sys.stderr.write('Error parsing arguments \n')
100 sys.exit()
104 sys.exit()
105 if args.format == FORMAT_PRETTY:
106 print 'Calling method %s => %s' % (method, apihost)
101
107
102 api_call(apikey, host, args.format, method, **margs)
108 json_data = api_call(apikey, apihost, method, **margs)['result']
109 if args.format == FORMAT_JSON:
110 print json.dumps(json_data)
111 elif args.format == FORMAT_PRETTY:
112 print 'Server response \n%s' % (
113 json.dumps(json_data, indent=4, sort_keys=True))
103 return 0
114 return 0
104
115
105 if __name__ == '__main__':
116 if __name__ == '__main__':
@@ -30,7 +30,7 b' import stat'
30 import argparse
30 import argparse
31 import fileinput
31 import fileinput
32
32
33 from rhodecode.bin.base import api_call, RcConf
33 from rhodecode.bin.base import json, api_call, RcConf, FORMAT_JSON, FORMAT_PRETTY
34
34
35
35
36 def argparser(argv):
36 def argparser(argv):
@@ -59,7 +59,11 b' def argparser(argv):'
59 group.add_argument('-d', '--description', help='Gist description')
59 group.add_argument('-d', '--description', help='Gist description')
60 group.add_argument('-l', '--lifetime', metavar='MINUTES',
60 group.add_argument('-l', '--lifetime', metavar='MINUTES',
61 help='Gist lifetime in minutes, -1 (Default) is forever')
61 help='Gist lifetime in minutes, -1 (Default) is forever')
62
62 group.add_argument('--format', dest='format', type=str,
63 help='output format default: `%s` can '
64 'be also `%s`' % (FORMAT_PRETTY, FORMAT_JSON),
65 default=FORMAT_PRETTY
66 )
63 args, other = parser.parse_known_args()
67 args, other = parser.parse_known_args()
64 return parser, args, other
68 return parser, args, other
65
69
@@ -135,7 +139,12 b' def _run(argv):'
135 files=files
139 files=files
136 )
140 )
137
141
138 api_call(apikey, host, 'json', 'create_gist', **margs)
142 json_data = api_call(apikey, host, 'create_gist', **margs)['result']
143 if args.format == FORMAT_JSON:
144 print json.dumps(json_data)
145 elif args.format == FORMAT_PRETTY:
146 print 'Created %s gist %s' % (json_data['gist_type'],
147 json_data['gist_url'])
139 return 0
148 return 0
140
149
141
150
General Comments 0
You need to be logged in to leave comments. Login now