Mercurial > kallithea
comparison rhodecode/bin/base.py @ 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 | dc4644865e8b |
children | ffd45b185016 |
comparison
equal
deleted
inserted
replaced
3874:237a8e3727a2 | 3875:5a7d52cf084d |
---|---|
18 CONFIG_NAME = '.rhodecode' | 18 CONFIG_NAME = '.rhodecode' |
19 FORMAT_PRETTY = 'pretty' | 19 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 """ |
35 Builds API data with given random ID | 36 Builds API data with given random ID |
36 | 37 |
43 "args": kw | 44 "args": kw |
44 } | 45 } |
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)) |
69 | 66 |
70 | 67 |