8 __docformat__ = "restructuredtext en" |
8 __docformat__ = "restructuredtext en" |
9 |
9 |
10 import os |
10 import os |
11 from os.path import join, exists |
11 from os.path import join, exists |
12 |
12 |
13 from logilab.common.configuration import Method |
13 from logilab.common.configuration import Method, Configuration, \ |
|
14 ini_format_section |
14 from logilab.common.decorators import wproperty, cached, clear_cache |
15 from logilab.common.decorators import wproperty, cached, clear_cache |
15 |
16 |
16 from cubicweb import CW_SOFTWARE_ROOT, RegistryNotFound |
17 from cubicweb import CW_SOFTWARE_ROOT, RegistryNotFound |
17 from cubicweb.toolsutils import env_path, read_config |
18 from cubicweb.toolsutils import env_path, read_config, restrict_perms_to_user |
18 from cubicweb.cwconfig import CubicWebConfiguration, merge_options |
19 from cubicweb.cwconfig import CubicWebConfiguration, merge_options |
|
20 |
|
21 |
|
22 def generate_sources_file(sourcesfile, sourcescfg, keys=None): |
|
23 """serialize repository'sources configuration into a INI like file |
|
24 |
|
25 the `keys` parameter may be used to sort sections |
|
26 """ |
|
27 if keys is None: |
|
28 keys = sourcescfg.keys() |
|
29 else: |
|
30 for key in sourcescfg: |
|
31 if not key in keys: |
|
32 keys.append(key) |
|
33 stream = open(sourcesfile, 'w') |
|
34 for uri in keys: |
|
35 sconfig = sourcescfg[uri] |
|
36 if isinstance(sconfig, dict): |
|
37 # get a Configuration object |
|
38 _sconfig = Configuration(options=SOURCE_TYPES[sconfig['adapter']].options) |
|
39 for attr, val in sconfig.items(): |
|
40 if attr == 'uri': |
|
41 continue |
|
42 if attr == 'adapter': |
|
43 _sconfig.adapter = val |
|
44 else: |
|
45 _sconfig.set_option(attr, val) |
|
46 sconfig = _sconfig |
|
47 optsbysect = list(sconfig.options_by_section()) |
|
48 assert len(optsbysect) == 1, 'all options for a source should be in the same group' |
|
49 ini_format_section(stream, uri, optsbysect[0][1]) |
|
50 if hasattr(sconfig, 'adapter'): |
|
51 print >> stream |
|
52 print >> stream, '# adapter for this source (YOU SHOULD NOT CHANGE THIS)' |
|
53 print >> stream, 'adapter=%s' % sconfig.adapter |
|
54 print >> stream |
19 |
55 |
20 |
56 |
21 class ServerConfiguration(CubicWebConfiguration): |
57 class ServerConfiguration(CubicWebConfiguration): |
22 """standalone RQL server""" |
58 """standalone RQL server""" |
23 name = 'repository' |
59 name = 'repository' |
186 |
222 |
187 # this method has to be cached since when the server is running using a |
223 # this method has to be cached since when the server is running using a |
188 # restricted user, this user usually don't have access to the sources |
224 # restricted user, this user usually don't have access to the sources |
189 # configuration file (#16102) |
225 # configuration file (#16102) |
190 @cached |
226 @cached |
|
227 def read_sources_file(self): |
|
228 return read_config(self.sources_file()) |
|
229 |
191 def sources(self): |
230 def sources(self): |
192 """return a dictionnaries containing sources definitions indexed by |
231 """return a dictionnaries containing sources definitions indexed by |
193 sources'uri |
232 sources'uri |
194 """ |
233 """ |
195 allsources = read_config(self.sources_file()) |
234 allsources = self.read_sources_file() |
196 if self._enabled_sources is None: |
235 if self._enabled_sources is None: |
197 return allsources |
236 return allsources |
198 return dict((uri, config) for uri, config in allsources.items() |
237 return dict((uri, config) for uri, config in allsources.items() |
199 if uri in self._enabled_sources or uri == 'admin') |
238 if uri in self._enabled_sources or uri == 'admin') |
|
239 |
|
240 def write_sources_file(self, sourcescfg): |
|
241 sourcesfile = self.sources_file() |
|
242 generate_sources_file(sourcesfile, sourcescfg, ['admin', 'system']) |
|
243 restrict_perms_to_user(sourcesfile) |
200 |
244 |
201 def pyro_enabled(self): |
245 def pyro_enabled(self): |
202 """pyro is always enabled in standalone repository configuration""" |
246 """pyro is always enabled in standalone repository configuration""" |
203 return True |
247 return True |
204 |
248 |