17 # with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
17 # with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
18 """schema definition related entities""" |
18 """schema definition related entities""" |
19 |
19 |
20 __docformat__ = "restructuredtext en" |
20 __docformat__ = "restructuredtext en" |
21 |
21 |
22 import re |
|
23 from socket import gethostname |
|
24 |
|
25 from logilab.common.decorators import cached |
22 from logilab.common.decorators import cached |
26 from logilab.common.textutils import text_to_dict |
|
27 from logilab.common.configuration import OptionError |
|
28 |
23 |
29 from yams.schema import role_name |
24 from yams.schema import role_name |
30 |
25 |
31 from cubicweb import ValidationError |
26 from cubicweb import ValidationError |
32 from cubicweb.schema import ERQLExpression, RRQLExpression |
27 from cubicweb.schema import ERQLExpression, RRQLExpression |
33 |
28 |
34 from cubicweb.entities import AnyEntity, fetch_config |
29 from cubicweb.entities import AnyEntity, fetch_config |
35 |
|
36 |
|
37 class _CWSourceCfgMixIn(object): |
|
38 @property |
|
39 def dictconfig(self): |
|
40 return self.config and text_to_dict(self.config) or {} |
|
41 |
|
42 def update_config(self, skip_unknown=False, **config): |
|
43 from cubicweb.server import SOURCE_TYPES |
|
44 from cubicweb.server.serverconfig import (SourceConfiguration, |
|
45 generate_source_config) |
|
46 cfg = self.dictconfig |
|
47 cfg.update(config) |
|
48 options = SOURCE_TYPES[self.type].options |
|
49 sconfig = SourceConfiguration(self._cw.vreg.config, options=options) |
|
50 for opt, val in cfg.iteritems(): |
|
51 try: |
|
52 sconfig.set_option(opt, val) |
|
53 except OptionError: |
|
54 if skip_unknown: |
|
55 continue |
|
56 raise |
|
57 cfgstr = unicode(generate_source_config(sconfig), self._cw.encoding) |
|
58 self.set_attributes(config=cfgstr) |
|
59 |
|
60 |
|
61 class CWSource(_CWSourceCfgMixIn, AnyEntity): |
|
62 __regid__ = 'CWSource' |
|
63 fetch_attrs, fetch_order = fetch_config(['name', 'type']) |
|
64 |
|
65 @property |
|
66 def host_config(self): |
|
67 dictconfig = self.dictconfig |
|
68 host = gethostname() |
|
69 for hostcfg in self.host_configs: |
|
70 if hostcfg.match(host): |
|
71 self.info('matching host config %s for source %s', |
|
72 hostcfg.match_host, self.name) |
|
73 dictconfig.update(hostcfg.dictconfig) |
|
74 return dictconfig |
|
75 |
|
76 @property |
|
77 def host_configs(self): |
|
78 return self.reverse_cw_host_config_of |
|
79 |
|
80 |
|
81 class CWSourceHostConfig(_CWSourceCfgMixIn, AnyEntity): |
|
82 __regid__ = 'CWSourceHostConfig' |
|
83 fetch_attrs, fetch_order = fetch_config(['match_host', 'config']) |
|
84 |
|
85 def match(self, hostname): |
|
86 return re.match(self.match_host, hostname) |
|
87 |
30 |
88 |
31 |
89 class CWEType(AnyEntity): |
32 class CWEType(AnyEntity): |
90 __regid__ = 'CWEType' |
33 __regid__ = 'CWEType' |
91 fetch_attrs, fetch_order = fetch_config(['name']) |
34 fetch_attrs, fetch_order = fetch_config(['name']) |