author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Fri, 24 Jul 2009 12:09:04 +0200 | |
changeset 2468 | 93853b6f3bad |
parent 2369 | 5a2b8ed266ca |
child 2689 | 44f041222d0f |
permissions | -rw-r--r-- |
1152 | 1 |
"""relation tags store |
2 |
||
3 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1859
diff
changeset
|
4 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
1152 | 5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1859
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
1152 | 7 |
""" |
8 |
__docformat__ = "restructuredtext en" |
|
1148
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
9 |
|
1740
2292ae32c98f
warn and drop rtags not in schema, this may be intentional; drop possibility to not specify value for bool rtags
sylvain.thenault@logilab.fr
parents:
1739
diff
changeset
|
10 |
import logging |
2292ae32c98f
warn and drop rtags not in schema, this may be intentional; drop possibility to not specify value for bool rtags
sylvain.thenault@logilab.fr
parents:
1739
diff
changeset
|
11 |
|
2292ae32c98f
warn and drop rtags not in schema, this may be intentional; drop possibility to not specify value for bool rtags
sylvain.thenault@logilab.fr
parents:
1739
diff
changeset
|
12 |
from logilab.common.logging_ext import set_log_methods |
2292ae32c98f
warn and drop rtags not in schema, this may be intentional; drop possibility to not specify value for bool rtags
sylvain.thenault@logilab.fr
parents:
1739
diff
changeset
|
13 |
|
1752
4b0b912ff5b7
fix rtags initialization: do it at the registry level to avoid multiple initialization of the same rtag
sylvain.thenault@logilab.fr
parents:
1748
diff
changeset
|
14 |
RTAGS = [] |
4b0b912ff5b7
fix rtags initialization: do it at the registry level to avoid multiple initialization of the same rtag
sylvain.thenault@logilab.fr
parents:
1748
diff
changeset
|
15 |
def register_rtag(rtag): |
4b0b912ff5b7
fix rtags initialization: do it at the registry level to avoid multiple initialization of the same rtag
sylvain.thenault@logilab.fr
parents:
1748
diff
changeset
|
16 |
RTAGS.append(rtag) |
1533 | 17 |
|
1148
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
18 |
class RelationTags(object): |
1533 | 19 |
"""a tag store for full relation definitions : |
1148
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
20 |
|
1533 | 21 |
(subject type, relation type, object type, tagged) |
1152 | 22 |
|
23 |
allowing to set tags using wildcard (eg '*') as subject type / object type |
|
24 |
||
1533 | 25 |
This class associates a single tag to each key. |
1152 | 26 |
""" |
1739 | 27 |
_allowed_values = None |
1849
1901fa97f521
give a name to rtags instance to ease debugging
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1769
diff
changeset
|
28 |
def __init__(self, name=None, initfunc=None, allowed_values=None): |
1901fa97f521
give a name to rtags instance to ease debugging
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1769
diff
changeset
|
29 |
self._name = name or '<unknown>' |
1148
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
30 |
self._tagdefs = {} |
1739 | 31 |
if allowed_values is not None: |
32 |
self._allowed_values = allowed_values |
|
33 |
self._initfunc = initfunc |
|
1752
4b0b912ff5b7
fix rtags initialization: do it at the registry level to avoid multiple initialization of the same rtag
sylvain.thenault@logilab.fr
parents:
1748
diff
changeset
|
34 |
register_rtag(self) |
1769
fb91d2b8a441
fix some rtags pb on i18n catalog generation
sylvain.thenault@logilab.fr
parents:
1752
diff
changeset
|
35 |
|
1528
864ae7c15ef5
other fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1451
diff
changeset
|
36 |
def __repr__(self): |
1849
1901fa97f521
give a name to rtags instance to ease debugging
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1769
diff
changeset
|
37 |
return '%s: %s' % (self._name, repr(self._tagdefs)) |
1528
864ae7c15ef5
other fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1451
diff
changeset
|
38 |
|
1533 | 39 |
# dict compat |
40 |
def __getitem__(self, key): |
|
41 |
return self.get(*key) |
|
42 |
__contains__ = __getitem__ |
|
1451 | 43 |
|
1739 | 44 |
def _get_keys(self, stype, rtype, otype, tagged): |
1533 | 45 |
keys = [(rtype, tagged, '*', '*'), |
46 |
(rtype, tagged, '*', otype), |
|
47 |
(rtype, tagged, stype, '*'), |
|
48 |
(rtype, tagged, stype, otype)] |
|
49 |
if stype == '*' or otype == '*': |
|
50 |
keys.remove((rtype, tagged, '*', '*')) |
|
51 |
if stype == '*': |
|
52 |
keys.remove((rtype, tagged, '*', otype)) |
|
53 |
if otype == '*': |
|
54 |
keys.remove((rtype, tagged, stype, '*')) |
|
55 |
return keys |
|
1451 | 56 |
|
1769
fb91d2b8a441
fix some rtags pb on i18n catalog generation
sylvain.thenault@logilab.fr
parents:
1752
diff
changeset
|
57 |
def init(self, schema, check=True): |
1739 | 58 |
# XXX check existing keys against schema |
1769
fb91d2b8a441
fix some rtags pb on i18n catalog generation
sylvain.thenault@logilab.fr
parents:
1752
diff
changeset
|
59 |
if check: |
fb91d2b8a441
fix some rtags pb on i18n catalog generation
sylvain.thenault@logilab.fr
parents:
1752
diff
changeset
|
60 |
for (rtype, tagged, stype, otype), value in self._tagdefs.items(): |
fb91d2b8a441
fix some rtags pb on i18n catalog generation
sylvain.thenault@logilab.fr
parents:
1752
diff
changeset
|
61 |
for ertype in (stype, rtype, otype): |
fb91d2b8a441
fix some rtags pb on i18n catalog generation
sylvain.thenault@logilab.fr
parents:
1752
diff
changeset
|
62 |
if ertype != '*' and not ertype in schema: |
fb91d2b8a441
fix some rtags pb on i18n catalog generation
sylvain.thenault@logilab.fr
parents:
1752
diff
changeset
|
63 |
self.warning('removing rtag %s: %s, %s undefined in schema', |
fb91d2b8a441
fix some rtags pb on i18n catalog generation
sylvain.thenault@logilab.fr
parents:
1752
diff
changeset
|
64 |
(stype, rtype, otype, tagged), value, ertype) |
fb91d2b8a441
fix some rtags pb on i18n catalog generation
sylvain.thenault@logilab.fr
parents:
1752
diff
changeset
|
65 |
self.del_rtag(stype, rtype, otype, tagged) |
fb91d2b8a441
fix some rtags pb on i18n catalog generation
sylvain.thenault@logilab.fr
parents:
1752
diff
changeset
|
66 |
break |
1739 | 67 |
if self._initfunc is not None: |
68 |
for eschema in schema.entities(): |
|
69 |
for rschema, tschemas, role in eschema.relation_definitions(True): |
|
70 |
for tschema in tschemas: |
|
71 |
if role == 'subject': |
|
1744 | 72 |
sschema, oschema = eschema, tschema |
1739 | 73 |
else: |
1744 | 74 |
sschema, oschema = tschema, eschema |
75 |
self._initfunc(self, sschema, rschema, oschema, role) |
|
1739 | 76 |
|
77 |
# rtag declaration api #################################################### |
|
78 |
||
79 |
def tag_attribute(self, key, tag): |
|
80 |
key = list(key) |
|
81 |
key.append('*') |
|
82 |
self.tag_subject_of(key, tag) |
|
1548
bd225e776739
new tag_attribute convenience method
sylvain.thenault@logilab.fr
parents:
1533
diff
changeset
|
83 |
|
1739 | 84 |
def tag_subject_of(self, key, tag): |
85 |
key = list(key) |
|
86 |
key.append('subject') |
|
87 |
self.tag_relation(key, tag) |
|
88 |
||
89 |
def tag_object_of(self, key, tag): |
|
90 |
key = list(key) |
|
91 |
key.append('object') |
|
92 |
self.tag_relation(key, tag) |
|
1533 | 93 |
|
1739 | 94 |
def tag_relation(self, key, tag): |
95 |
#if isinstance(key, basestring): |
|
96 |
# stype, rtype, otype = key.split() |
|
97 |
#else: |
|
98 |
stype, rtype, otype, tagged = [str(k) for k in key] |
|
99 |
if self._allowed_values is not None: |
|
1859
b068abd45a1c
nicer error message
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1849
diff
changeset
|
100 |
assert tag in self._allowed_values, \ |
b068abd45a1c
nicer error message
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1849
diff
changeset
|
101 |
'%r is not an allowed tag (should be in %s)' % ( |
b068abd45a1c
nicer error message
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1849
diff
changeset
|
102 |
tag, self._allowed_values) |
1739 | 103 |
self._tagdefs[(rtype, tagged, stype, otype)] = tag |
2369
5a2b8ed266ca
new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
104 |
return tag |
1533 | 105 |
|
1739 | 106 |
# rtag runtime api ######################################################## |
107 |
||
108 |
def del_rtag(self, stype, rtype, otype, tagged): |
|
109 |
del self._tagdefs[(rtype, tagged, stype, otype)] |
|
110 |
||
111 |
def get(self, stype, rtype, otype, tagged): |
|
1721
694f6a50e138
final rtags api (eventually :$)
sylvain.thenault@logilab.fr
parents:
1548
diff
changeset
|
112 |
for key in reversed(self._get_keys(stype, rtype, otype, tagged)): |
1148
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
113 |
try: |
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
114 |
return self._tagdefs[key] |
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
115 |
except KeyError: |
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
116 |
continue |
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
117 |
return None |
1451 | 118 |
|
1721
694f6a50e138
final rtags api (eventually :$)
sylvain.thenault@logilab.fr
parents:
1548
diff
changeset
|
119 |
def etype_get(self, etype, rtype, role, ttype='*'): |
694f6a50e138
final rtags api (eventually :$)
sylvain.thenault@logilab.fr
parents:
1548
diff
changeset
|
120 |
if role == 'subject': |
694f6a50e138
final rtags api (eventually :$)
sylvain.thenault@logilab.fr
parents:
1548
diff
changeset
|
121 |
return self.get(etype, rtype, ttype, role) |
694f6a50e138
final rtags api (eventually :$)
sylvain.thenault@logilab.fr
parents:
1548
diff
changeset
|
122 |
return self.get(ttype, rtype, etype, role) |
1533 | 123 |
|
124 |
||
1451 | 125 |
|
1533 | 126 |
class RelationTagsSet(RelationTags): |
2369
5a2b8ed266ca
new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
127 |
"""This class associates a set of tags to each key. |
5a2b8ed266ca
new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
128 |
""" |
5a2b8ed266ca
new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
129 |
tag_container_cls = set |
1533 | 130 |
|
1739 | 131 |
def tag_relation(self, key, tag): |
132 |
stype, rtype, otype, tagged = [str(k) for k in key] |
|
2369
5a2b8ed266ca
new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
133 |
rtags = self._tagdefs.setdefault((rtype, tagged, stype, otype), |
5a2b8ed266ca
new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
134 |
self.tag_container_cls()) |
1148
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
135 |
rtags.add(tag) |
2369
5a2b8ed266ca
new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
136 |
return rtags |
1451 | 137 |
|
1739 | 138 |
def get(self, stype, rtype, otype, tagged): |
2369
5a2b8ed266ca
new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
139 |
rtags = self.tag_container_cls() |
1721
694f6a50e138
final rtags api (eventually :$)
sylvain.thenault@logilab.fr
parents:
1548
diff
changeset
|
140 |
for key in self._get_keys(stype, rtype, otype, tagged): |
1148
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
141 |
try: |
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
142 |
rtags.update(self._tagdefs[key]) |
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
143 |
except KeyError: |
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
144 |
continue |
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
145 |
return rtags |
1739 | 146 |
|
147 |
||
2369
5a2b8ed266ca
new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
148 |
class RelationTagsDict(RelationTagsSet): |
5a2b8ed266ca
new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
149 |
"""This class associates a set of tags to each key.""" |
5a2b8ed266ca
new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
150 |
tag_container_cls = dict |
5a2b8ed266ca
new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
151 |
|
5a2b8ed266ca
new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
152 |
def tag_relation(self, key, tag): |
5a2b8ed266ca
new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
153 |
stype, rtype, otype, tagged = [str(k) for k in key] |
5a2b8ed266ca
new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
154 |
try: |
5a2b8ed266ca
new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
155 |
rtags = self._tagdefs[(rtype, tagged, stype, otype)] |
5a2b8ed266ca
new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
156 |
rtags.update(tag) |
5a2b8ed266ca
new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
157 |
return rtags |
5a2b8ed266ca
new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
158 |
except KeyError: |
5a2b8ed266ca
new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
159 |
self._tagdefs[(rtype, tagged, stype, otype)] = tag |
5a2b8ed266ca
new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
160 |
return tag |
5a2b8ed266ca
new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
161 |
|
5a2b8ed266ca
new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
162 |
def setdefault(self, key, tagkey, tagvalue): |
5a2b8ed266ca
new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
163 |
stype, rtype, otype, tagged = [str(k) for k in key] |
5a2b8ed266ca
new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
164 |
try: |
5a2b8ed266ca
new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
165 |
rtags = self._tagdefs[(rtype, tagged, stype, otype)] |
5a2b8ed266ca
new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
166 |
rtags.setdefault(tagkey, tagvalue) |
5a2b8ed266ca
new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
167 |
return rtags |
5a2b8ed266ca
new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
168 |
except KeyError: |
5a2b8ed266ca
new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
169 |
self._tagdefs[(rtype, tagged, stype, otype)] = {tagkey: tagvalue} |
5a2b8ed266ca
new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
170 |
return self._tagdefs[(rtype, tagged, stype, otype)] |
5a2b8ed266ca
new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
171 |
|
5a2b8ed266ca
new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
172 |
|
1739 | 173 |
class RelationTagsBool(RelationTags): |
174 |
_allowed_values = frozenset((True, False)) |
|
175 |
||
176 |
||
1740
2292ae32c98f
warn and drop rtags not in schema, this may be intentional; drop possibility to not specify value for bool rtags
sylvain.thenault@logilab.fr
parents:
1739
diff
changeset
|
177 |
set_log_methods(RelationTags, logging.getLogger('cubicweb.rtags')) |