13 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
13 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
14 # details. |
14 # details. |
15 # |
15 # |
16 # You should have received a copy of the GNU Lesser General Public License along |
16 # You should have received a copy of the GNU Lesser General Public License along |
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 """ |
|
21 __docformat__ = "restructuredtext en" |
20 __docformat__ = "restructuredtext en" |
22 |
21 |
|
22 import re |
|
23 from socket import gethostname |
|
24 |
23 from logilab.common.decorators import cached |
25 from logilab.common.decorators import cached |
|
26 from logilab.common.textutils import text_to_dict |
24 |
27 |
25 from yams.schema import role_name |
28 from yams.schema import role_name |
26 |
29 |
27 from cubicweb import ValidationError |
30 from cubicweb import ValidationError |
28 from cubicweb.schema import ERQLExpression, RRQLExpression |
31 from cubicweb.schema import ERQLExpression, RRQLExpression |
29 |
32 |
30 from cubicweb.entities import AnyEntity, fetch_config |
33 from cubicweb.entities import AnyEntity, fetch_config |
|
34 |
|
35 |
|
36 |
|
37 class CWSource(AnyEntity): |
|
38 __regid__ = 'CWSource' |
|
39 fetch_attrs, fetch_order = fetch_config(['name', 'type']) |
|
40 |
|
41 @property |
|
42 def dictconfig(self): |
|
43 return self.config and text_to_dict(self.config) or {} |
|
44 |
|
45 @property |
|
46 def host_config(self): |
|
47 dictconfig = self.dictconfig |
|
48 host = gethostname() |
|
49 for hostcfg in self.host_configs: |
|
50 if hostcfg.match(hostname): |
|
51 dictconfig.update(hostcfg.dictconfig) |
|
52 return dictconfig |
|
53 |
|
54 @property |
|
55 def host_configs(self): |
|
56 return self.reverse_cw_host_config_of |
|
57 |
|
58 |
|
59 class CWSourceHostConfig(AnyEntity): |
|
60 __regid__ = 'CWSourceHostConfig' |
|
61 fetch_attrs, fetch_order = fetch_config(['match_host', 'config']) |
|
62 |
|
63 @property |
|
64 def dictconfig(self): |
|
65 return self.config and text_to_dict(self.config) or {} |
|
66 |
|
67 def match(self, hostname): |
|
68 return re.match(self.match_host, hostname) |
31 |
69 |
32 |
70 |
33 class CWEType(AnyEntity): |
71 class CWEType(AnyEntity): |
34 __regid__ = 'CWEType' |
72 __regid__ = 'CWEType' |
35 fetch_attrs, fetch_order = fetch_config(['name']) |
73 fetch_attrs, fetch_order = fetch_config(['name']) |