##// END OF EJS Templates
cleanup: remove unused code RcConf.update_config
Thomas De Schampheleire -
r8388:cecb8eeb stable
parent child Browse files
Show More
@@ -1,166 +1,149 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 # This program is free software: you can redistribute it and/or modify
2 # This program is free software: you can redistribute it and/or modify
3 # it under the terms of the GNU General Public License as published by
3 # it under the terms of the GNU General Public License as published by
4 # the Free Software Foundation, either version 3 of the License, or
4 # the Free Software Foundation, either version 3 of the License, or
5 # (at your option) any later version.
5 # (at your option) any later version.
6 #
6 #
7 # This program is distributed in the hope that it will be useful,
7 # This program is distributed in the hope that it will be useful,
8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 # GNU General Public License for more details.
10 # GNU General Public License for more details.
11 #
11 #
12 # You should have received a copy of the GNU General Public License
12 # You should have received a copy of the GNU General Public License
13 # along with this program. If not, see <http://www.gnu.org/licenses/>.
13 # along with this program. If not, see <http://www.gnu.org/licenses/>.
14 """
14 """
15 kallithea.bin.base
15 kallithea.bin.base
16 ~~~~~~~~~~~~~~~~~~
16 ~~~~~~~~~~~~~~~~~~
17
17
18 Base utils for shell scripts
18 Base utils for shell scripts
19
19
20 This file was forked by the Kallithea project in July 2014.
20 This file was forked by the Kallithea project in July 2014.
21 Original author and date, and relevant copyright and licensing information is below:
21 Original author and date, and relevant copyright and licensing information is below:
22 :created_on: May 09, 2013
22 :created_on: May 09, 2013
23 :author: marcink
23 :author: marcink
24 :copyright: (c) 2013 RhodeCode GmbH, and others.
24 :copyright: (c) 2013 RhodeCode GmbH, and others.
25 :license: GPLv3, see LICENSE.md for more details.
25 :license: GPLv3, see LICENSE.md for more details.
26 """
26 """
27
27
28 import os
28 import os
29 import pprint
29 import pprint
30 import random
30 import random
31 import sys
31 import sys
32 import urllib.request
32 import urllib.request
33
33
34 from kallithea.lib import ext_json
34 from kallithea.lib import ext_json
35 from kallithea.lib.utils2 import ascii_bytes
35 from kallithea.lib.utils2 import ascii_bytes
36
36
37
37
38 CONFIG_NAME = '.config/kallithea'
38 CONFIG_NAME = '.config/kallithea'
39 FORMAT_PRETTY = 'pretty'
39 FORMAT_PRETTY = 'pretty'
40 FORMAT_JSON = 'json'
40 FORMAT_JSON = 'json'
41
41
42
42
43 def api_call(apikey, apihost, method=None, **kw):
43 def api_call(apikey, apihost, method=None, **kw):
44 """
44 """
45 Api_call wrapper for Kallithea.
45 Api_call wrapper for Kallithea.
46
46
47 :param apikey:
47 :param apikey:
48 :param apihost:
48 :param apihost:
49 :param format: formatting, pretty means prints and pprint of json
49 :param format: formatting, pretty means prints and pprint of json
50 json returns unparsed json
50 json returns unparsed json
51 :param method:
51 :param method:
52 :returns: json response from server
52 :returns: json response from server
53 """
53 """
54 def _build_data(random_id):
54 def _build_data(random_id):
55 """
55 """
56 Builds API data with given random ID
56 Builds API data with given random ID
57
57
58 :param random_id:
58 :param random_id:
59 """
59 """
60 return {
60 return {
61 "id": random_id,
61 "id": random_id,
62 "api_key": apikey,
62 "api_key": apikey,
63 "method": method,
63 "method": method,
64 "args": kw
64 "args": kw
65 }
65 }
66
66
67 if not method:
67 if not method:
68 raise Exception('please specify method name !')
68 raise Exception('please specify method name !')
69 apihost = apihost.rstrip('/')
69 apihost = apihost.rstrip('/')
70 id_ = random.randrange(1, 9999)
70 id_ = random.randrange(1, 9999)
71 req = urllib.request.Request('%s/_admin/api' % apihost,
71 req = urllib.request.Request('%s/_admin/api' % apihost,
72 data=ascii_bytes(ext_json.dumps(_build_data(id_))),
72 data=ascii_bytes(ext_json.dumps(_build_data(id_))),
73 headers={'content-type': 'text/plain'})
73 headers={'content-type': 'text/plain'})
74 ret = urllib.request.urlopen(req)
74 ret = urllib.request.urlopen(req)
75 raw_json = ret.read()
75 raw_json = ret.read()
76 json_data = ext_json.loads(raw_json)
76 json_data = ext_json.loads(raw_json)
77 id_ret = json_data['id']
77 id_ret = json_data['id']
78 if id_ret == id_:
78 if id_ret == id_:
79 return json_data
79 return json_data
80
80
81 else:
81 else:
82 _formatted_json = pprint.pformat(json_data)
82 _formatted_json = pprint.pformat(json_data)
83 raise Exception('something went wrong. '
83 raise Exception('something went wrong. '
84 'ID mismatch got %s, expected %s | %s' % (
84 'ID mismatch got %s, expected %s | %s' % (
85 id_ret, id_, _formatted_json))
85 id_ret, id_, _formatted_json))
86
86
87
87
88 class RcConf(object):
88 class RcConf(object):
89 """
89 """
90 Kallithea config for API
90 Kallithea config for API
91
91
92 conf = RcConf()
92 conf = RcConf()
93 conf['key']
93 conf['key']
94
94
95 """
95 """
96
96
97 def __init__(self, config_location=None, autoload=True, autocreate=False,
97 def __init__(self, config_location=None, autoload=True, autocreate=False,
98 config=None):
98 config=None):
99 HOME = os.getenv('HOME', os.getenv('USERPROFILE')) or ''
99 HOME = os.getenv('HOME', os.getenv('USERPROFILE')) or ''
100 HOME_CONF = os.path.abspath(os.path.join(HOME, CONFIG_NAME))
100 HOME_CONF = os.path.abspath(os.path.join(HOME, CONFIG_NAME))
101 self._conf_name = HOME_CONF if not config_location else config_location
101 self._conf_name = HOME_CONF if not config_location else config_location
102 self._conf = {}
102 self._conf = {}
103 if autocreate:
103 if autocreate:
104 self.make_config(config)
104 self.make_config(config)
105 if autoload:
105 if autoload:
106 self._conf = self.load_config()
106 self._conf = self.load_config()
107
107
108 def __getitem__(self, key):
108 def __getitem__(self, key):
109 return self._conf[key]
109 return self._conf[key]
110
110
111 def __bool__(self):
111 def __bool__(self):
112 if self._conf:
112 if self._conf:
113 return True
113 return True
114 return False
114 return False
115
115
116 def __eq__(self, other):
116 def __eq__(self, other):
117 return self._conf.__eq__(other)
117 return self._conf.__eq__(other)
118
118
119 def __repr__(self):
119 def __repr__(self):
120 return 'RcConf<%s>' % self._conf.__repr__()
120 return 'RcConf<%s>' % self._conf.__repr__()
121
121
122 def make_config(self, config):
122 def make_config(self, config):
123 """
123 """
124 Saves given config as a JSON dump in the _conf_name location
124 Saves given config as a JSON dump in the _conf_name location
125
125
126 :param config:
126 :param config:
127 """
127 """
128 update = False
128 update = False
129 if os.path.exists(self._conf_name):
129 if os.path.exists(self._conf_name):
130 update = True
130 update = True
131 with open(self._conf_name, 'w') as f:
131 with open(self._conf_name, 'w') as f:
132 ext_json.dump(config, f, indent=4)
132 ext_json.dump(config, f, indent=4)
133 f.write('\n')
133 f.write('\n')
134
134
135 if update:
135 if update:
136 sys.stdout.write('Updated config in %s\n' % self._conf_name)
136 sys.stdout.write('Updated config in %s\n' % self._conf_name)
137 else:
137 else:
138 sys.stdout.write('Created new config in %s\n' % self._conf_name)
138 sys.stdout.write('Created new config in %s\n' % self._conf_name)
139
139
140 def update_config(self, new_config):
141 """
142 Reads the JSON config updates it's values with new_config and
143 saves it back as JSON dump
144
145 :param new_config:
146 """
147 config = {}
148 try:
149 with open(self._conf_name, 'rb') as conf:
150 config = ext_json.load(conf)
151 except IOError as e:
152 sys.stderr.write(str(e) + '\n')
153
154 config.update(new_config)
155 self.make_config(config)
156
157 def load_config(self):
140 def load_config(self):
158 """
141 """
159 Loads config from file and returns loaded JSON object
142 Loads config from file and returns loaded JSON object
160 """
143 """
161 try:
144 try:
162 with open(self._conf_name, 'r') as conf:
145 with open(self._conf_name, 'r') as conf:
163 return ext_json.load(conf)
146 return ext_json.load(conf)
164 except IOError as e:
147 except IOError as e:
165 #sys.stderr.write(str(e) + '\n')
148 #sys.stderr.write(str(e) + '\n')
166 pass
149 pass
General Comments 0
You need to be logged in to leave comments. Login now