author | Adrien Di Mascio <Adrien.DiMascio@logilab.fr> |
Tue, 26 May 2009 09:03:24 +0200 | |
changeset 1933 | f40ee76ecdf1 |
parent 1859 | b068abd45a1c |
child 1977 | 606923dff11b |
permissions | -rw-r--r-- |
1152 | 1 |
"""relation tags store |
2 |
||
3 |
:organization: Logilab |
|
4 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
|
5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
|
6 |
""" |
|
7 |
__docformat__ = "restructuredtext en" |
|
1148
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
8 |
|
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
|
9 |
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
|
10 |
|
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 |
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
|
12 |
|
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
|
13 |
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
|
14 |
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
|
15 |
RTAGS.append(rtag) |
1533 | 16 |
|
1148
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
17 |
class RelationTags(object): |
1533 | 18 |
"""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
|
19 |
|
1533 | 20 |
(subject type, relation type, object type, tagged) |
1152 | 21 |
|
22 |
allowing to set tags using wildcard (eg '*') as subject type / object type |
|
23 |
||
1533 | 24 |
This class associates a single tag to each key. |
1152 | 25 |
""" |
1739 | 26 |
_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
|
27 |
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
|
28 |
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
|
29 |
self._tagdefs = {} |
1739 | 30 |
if allowed_values is not None: |
31 |
self._allowed_values = allowed_values |
|
32 |
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
|
33 |
register_rtag(self) |
1769
fb91d2b8a441
fix some rtags pb on i18n catalog generation
sylvain.thenault@logilab.fr
parents:
1752
diff
changeset
|
34 |
|
1528
864ae7c15ef5
other fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1451
diff
changeset
|
35 |
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
|
36 |
return '%s: %s' % (self._name, repr(self._tagdefs)) |
1528
864ae7c15ef5
other fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1451
diff
changeset
|
37 |
|
1533 | 38 |
# dict compat |
39 |
def __getitem__(self, key): |
|
40 |
return self.get(*key) |
|
41 |
__contains__ = __getitem__ |
|
1451 | 42 |
|
1739 | 43 |
def _get_keys(self, stype, rtype, otype, tagged): |
1533 | 44 |
keys = [(rtype, tagged, '*', '*'), |
45 |
(rtype, tagged, '*', otype), |
|
46 |
(rtype, tagged, stype, '*'), |
|
47 |
(rtype, tagged, stype, otype)] |
|
48 |
if stype == '*' or otype == '*': |
|
49 |
keys.remove((rtype, tagged, '*', '*')) |
|
50 |
if stype == '*': |
|
51 |
keys.remove((rtype, tagged, '*', otype)) |
|
52 |
if otype == '*': |
|
53 |
keys.remove((rtype, tagged, stype, '*')) |
|
54 |
return keys |
|
1451 | 55 |
|
1769
fb91d2b8a441
fix some rtags pb on i18n catalog generation
sylvain.thenault@logilab.fr
parents:
1752
diff
changeset
|
56 |
def init(self, schema, check=True): |
1739 | 57 |
# XXX check existing keys against schema |
1769
fb91d2b8a441
fix some rtags pb on i18n catalog generation
sylvain.thenault@logilab.fr
parents:
1752
diff
changeset
|
58 |
if check: |
fb91d2b8a441
fix some rtags pb on i18n catalog generation
sylvain.thenault@logilab.fr
parents:
1752
diff
changeset
|
59 |
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
|
60 |
for ertype in (stype, rtype, otype): |
fb91d2b8a441
fix some rtags pb on i18n catalog generation
sylvain.thenault@logilab.fr
parents:
1752
diff
changeset
|
61 |
if ertype != '*' and not ertype in schema: |
fb91d2b8a441
fix some rtags pb on i18n catalog generation
sylvain.thenault@logilab.fr
parents:
1752
diff
changeset
|
62 |
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
|
63 |
(stype, rtype, otype, tagged), value, ertype) |
fb91d2b8a441
fix some rtags pb on i18n catalog generation
sylvain.thenault@logilab.fr
parents:
1752
diff
changeset
|
64 |
self.del_rtag(stype, rtype, otype, tagged) |
fb91d2b8a441
fix some rtags pb on i18n catalog generation
sylvain.thenault@logilab.fr
parents:
1752
diff
changeset
|
65 |
break |
1739 | 66 |
if self._initfunc is not None: |
67 |
for eschema in schema.entities(): |
|
68 |
for rschema, tschemas, role in eschema.relation_definitions(True): |
|
69 |
for tschema in tschemas: |
|
70 |
if role == 'subject': |
|
1744 | 71 |
sschema, oschema = eschema, tschema |
1739 | 72 |
else: |
1744 | 73 |
sschema, oschema = tschema, eschema |
74 |
self._initfunc(self, sschema, rschema, oschema, role) |
|
1739 | 75 |
|
76 |
# rtag declaration api #################################################### |
|
77 |
||
78 |
def tag_attribute(self, key, tag): |
|
79 |
key = list(key) |
|
80 |
key.append('*') |
|
81 |
self.tag_subject_of(key, tag) |
|
1548
bd225e776739
new tag_attribute convenience method
sylvain.thenault@logilab.fr
parents:
1533
diff
changeset
|
82 |
|
1739 | 83 |
def tag_subject_of(self, key, tag): |
84 |
key = list(key) |
|
85 |
key.append('subject') |
|
86 |
self.tag_relation(key, tag) |
|
87 |
||
88 |
def tag_object_of(self, key, tag): |
|
89 |
key = list(key) |
|
90 |
key.append('object') |
|
91 |
self.tag_relation(key, tag) |
|
1533 | 92 |
|
1739 | 93 |
def tag_relation(self, key, tag): |
94 |
#if isinstance(key, basestring): |
|
95 |
# stype, rtype, otype = key.split() |
|
96 |
#else: |
|
97 |
stype, rtype, otype, tagged = [str(k) for k in key] |
|
98 |
if self._allowed_values is not None: |
|
1859
b068abd45a1c
nicer error message
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1849
diff
changeset
|
99 |
assert tag in self._allowed_values, \ |
b068abd45a1c
nicer error message
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1849
diff
changeset
|
100 |
'%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
|
101 |
tag, self._allowed_values) |
1739 | 102 |
self._tagdefs[(rtype, tagged, stype, otype)] = tag |
1533 | 103 |
|
1739 | 104 |
# rtag runtime api ######################################################## |
105 |
||
106 |
def del_rtag(self, stype, rtype, otype, tagged): |
|
107 |
del self._tagdefs[(rtype, tagged, stype, otype)] |
|
108 |
||
109 |
def get(self, stype, rtype, otype, tagged): |
|
1721
694f6a50e138
final rtags api (eventually :$)
sylvain.thenault@logilab.fr
parents:
1548
diff
changeset
|
110 |
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
|
111 |
try: |
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
112 |
return self._tagdefs[key] |
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
113 |
except KeyError: |
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
114 |
continue |
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
115 |
return None |
1451 | 116 |
|
1721
694f6a50e138
final rtags api (eventually :$)
sylvain.thenault@logilab.fr
parents:
1548
diff
changeset
|
117 |
def etype_get(self, etype, rtype, role, ttype='*'): |
694f6a50e138
final rtags api (eventually :$)
sylvain.thenault@logilab.fr
parents:
1548
diff
changeset
|
118 |
if role == 'subject': |
694f6a50e138
final rtags api (eventually :$)
sylvain.thenault@logilab.fr
parents:
1548
diff
changeset
|
119 |
return self.get(etype, rtype, ttype, role) |
694f6a50e138
final rtags api (eventually :$)
sylvain.thenault@logilab.fr
parents:
1548
diff
changeset
|
120 |
return self.get(ttype, rtype, etype, role) |
1533 | 121 |
|
122 |
||
1451 | 123 |
|
1533 | 124 |
class RelationTagsSet(RelationTags): |
125 |
"""This class associates a set of tags to each key.""" |
|
126 |
||
1739 | 127 |
def tag_relation(self, key, tag): |
128 |
stype, rtype, otype, tagged = [str(k) for k in key] |
|
1533 | 129 |
rtags = self._tagdefs.setdefault((rtype, tagged, stype, otype), set()) |
1148
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
130 |
rtags.add(tag) |
1451 | 131 |
|
1739 | 132 |
def get(self, stype, rtype, otype, tagged): |
1148
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
133 |
rtags = set() |
1721
694f6a50e138
final rtags api (eventually :$)
sylvain.thenault@logilab.fr
parents:
1548
diff
changeset
|
134 |
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
|
135 |
try: |
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
136 |
rtags.update(self._tagdefs[key]) |
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
137 |
except KeyError: |
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
138 |
continue |
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
139 |
return rtags |
1739 | 140 |
|
141 |
||
142 |
class RelationTagsBool(RelationTags): |
|
143 |
_allowed_values = frozenset((True, False)) |
|
144 |
||
145 |
||
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
|
146 |
set_log_methods(RelationTags, logging.getLogger('cubicweb.rtags')) |