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