Mercurial > kallithea
comparison rhodecode/bin/base.py @ 3833:5055dd385118 beta
api: decouple some parts from api CLI script
- also change how we save config file
- method should be always API method
- now we use --save-config for saving api config
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Thu, 09 May 2013 21:44:15 +0200 |
parents | |
children | dc4644865e8b |
comparison
equal
deleted
inserted
replaced
3832:07f8039ef090 | 3833:5055dd385118 |
---|---|
1 """ | |
2 Base utils for shell scripts | |
3 """ | |
4 import os | |
5 import sys | |
6 import random | |
7 import urllib2 | |
8 import pprint | |
9 | |
10 try: | |
11 from rhodecode.lib.ext_json import json | |
12 except ImportError: | |
13 try: | |
14 import simplejson as json | |
15 except ImportError: | |
16 import json | |
17 | |
18 CONFIG_NAME = '.rhodecode' | |
19 FORMAT_PRETTY = 'pretty' | |
20 FORMAT_JSON = 'json' | |
21 | |
22 | |
23 def api_call(apikey, apihost, format, method=None, **kw): | |
24 """ | |
25 Api_call wrapper for RhodeCode | |
26 | |
27 :param apikey: | |
28 :param apihost: | |
29 :param format: formatting, pretty means prints and pprint of json | |
30 json returns unparsed json | |
31 :param method: | |
32 """ | |
33 def _build_data(random_id): | |
34 """ | |
35 Builds API data with given random ID | |
36 | |
37 :param random_id: | |
38 :type random_id: | |
39 """ | |
40 return { | |
41 "id": random_id, | |
42 "api_key": apikey, | |
43 "method": method, | |
44 "args": kw | |
45 } | |
46 | |
47 if not method: | |
48 raise Exception('please specify method name !') | |
49 id_ = random.randrange(1, 9999) | |
50 req = urllib2.Request('%s/_admin/api' % apihost, | |
51 data=json.dumps(_build_data(id_)), | |
52 headers={'content-type': 'text/plain'}) | |
53 if format == FORMAT_PRETTY: | |
54 sys.stdout.write('calling %s to %s \n' % (req.get_data(), apihost)) | |
55 ret = urllib2.urlopen(req) | |
56 raw_json = ret.read() | |
57 json_data = json.loads(raw_json) | |
58 id_ret = json_data['id'] | |
59 _formatted_json = pprint.pformat(json_data) | |
60 if id_ret == id_: | |
61 if format == FORMAT_JSON: | |
62 sys.stdout.write(str(raw_json)) | |
63 else: | |
64 sys.stdout.write('rhodecode returned:\n%s\n' % (_formatted_json)) | |
65 | |
66 else: | |
67 raise Exception('something went wrong. ' | |
68 'ID mismatch got %s, expected %s | %s' % ( | |
69 id_ret, id_, _formatted_json)) | |
70 | |
71 | |
72 class RcConf(object): | |
73 """ | |
74 RhodeCode config for API | |
75 | |
76 conf = RcConf() | |
77 conf['key'] | |
78 | |
79 """ | |
80 | |
81 def __init__(self, config_location=None, autoload=True, autocreate=False, | |
82 config=None): | |
83 self._conf_name = CONFIG_NAME if not config_location else config_location | |
84 self._conf = {} | |
85 if autocreate: | |
86 self.make_config(config) | |
87 if autoload: | |
88 self._conf = self.load_config() | |
89 | |
90 def __getitem__(self, key): | |
91 return self._conf[key] | |
92 | |
93 def __nonzero__(self): | |
94 if self._conf: | |
95 return True | |
96 return False | |
97 | |
98 def __eq__(self): | |
99 return self._conf.__eq__() | |
100 | |
101 def __repr__(self): | |
102 return 'RcConf<%s>' % self._conf.__repr__() | |
103 | |
104 def make_config(self, config): | |
105 """ | |
106 Saves given config as a JSON dump in the _conf_name location | |
107 | |
108 :param config: | |
109 :type config: | |
110 """ | |
111 update = False | |
112 if os.path.exists(self._conf_name): | |
113 update = True | |
114 with open(self._conf_name, 'wb') as f: | |
115 json.dump(config, f, indent=4) | |
116 | |
117 if update: | |
118 sys.stdout.write('Updated config in %s\n' % self._conf_name) | |
119 else: | |
120 sys.stdout.write('Created new config in %s\n' % self._conf_name) | |
121 | |
122 def update_config(self, new_config): | |
123 """ | |
124 Reads the JSON config updates it's values with new_config and | |
125 saves it back as JSON dump | |
126 | |
127 :param new_config: | |
128 """ | |
129 config = {} | |
130 try: | |
131 with open(self._conf_name, 'rb') as conf: | |
132 config = json.load(conf) | |
133 except IOError, e: | |
134 sys.stderr.write(str(e) + '\n') | |
135 | |
136 config.update(new_config) | |
137 self.make_config(config) | |
138 | |
139 def load_config(self): | |
140 """ | |
141 Loads config from file and returns loaded JSON object | |
142 """ | |
143 try: | |
144 with open(self._conf_name, 'rb') as conf: | |
145 return json.load(conf) | |
146 except IOError, e: | |
147 #sys.stderr.write(str(e) + '\n') | |
148 pass |