author | sylvain.thenault@logilab.fr |
Wed, 29 Apr 2009 09:07:26 +0200 | |
branch | tls-sprint |
changeset 1531 | 5fc1e6a15147 |
parent 1528 | 864ae7c15ef5 |
child 1533 | bcd4bfff658b |
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 |
|
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
9 |
class RelationTags(object): |
1152 | 10 |
"""RelationTags instances are 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
|
11 |
|
1152 | 12 |
(subject type, relation type, object type, role) |
13 |
||
14 |
allowing to set tags using wildcard (eg '*') as subject type / object type |
|
15 |
||
16 |
if `use_set` is True, a set of tags is associated to each key, and you |
|
17 |
should use rtags / etype_rtags / add_rtag api. Otherwise, a single tag is |
|
18 |
associated to each key, and you should use rtag / etype_rtag / set_rtag api. |
|
19 |
""" |
|
1451 | 20 |
|
1148
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
21 |
def __init__(self, use_set=False): |
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
22 |
self.use_set = use_set |
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
23 |
self._tagdefs = {} |
1451 | 24 |
|
1528
864ae7c15ef5
other fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1451
diff
changeset
|
25 |
def __repr__(self): |
864ae7c15ef5
other fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1451
diff
changeset
|
26 |
return repr(self._tagdefs) |
864ae7c15ef5
other fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1451
diff
changeset
|
27 |
|
1179
70825477c6ce
fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents:
1152
diff
changeset
|
28 |
def set_rtag(self, tag, rtype, role, stype='*', otype='*'): |
1148
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
29 |
assert not self.use_set |
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
30 |
assert role in ('subject', 'object'), role |
1283
d812bd08c11c
add str() to avoid kilometers of information while printing content (due to __repr__ of yams objects)
sylvain.thenault@logilab.fr
parents:
1179
diff
changeset
|
31 |
self._tagdefs[(str(rtype), role, str(stype), str(otype))] = tag |
1451 | 32 |
|
1356
7b4802822f40
new del_rtag method, for testing purpose (at least)
sylvain.thenault@logilab.fr
parents:
1283
diff
changeset
|
33 |
def del_rtag(self, rtype, role, stype='*', otype='*'): |
7b4802822f40
new del_rtag method, for testing purpose (at least)
sylvain.thenault@logilab.fr
parents:
1283
diff
changeset
|
34 |
assert not self.use_set |
7b4802822f40
new del_rtag method, for testing purpose (at least)
sylvain.thenault@logilab.fr
parents:
1283
diff
changeset
|
35 |
assert role in ('subject', 'object'), role |
7b4802822f40
new del_rtag method, for testing purpose (at least)
sylvain.thenault@logilab.fr
parents:
1283
diff
changeset
|
36 |
del self._tagdefs[(str(rtype), role, str(stype), str(otype))] |
1451 | 37 |
|
1179
70825477c6ce
fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents:
1152
diff
changeset
|
38 |
def rtag(self, rtype, role, stype='*', otype='*'): |
1148
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
39 |
assert not self.use_set |
1179
70825477c6ce
fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents:
1152
diff
changeset
|
40 |
for key in reversed(self._get_keys(rtype, role, stype, otype)): |
1148
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
41 |
try: |
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
42 |
return self._tagdefs[key] |
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
43 |
except KeyError: |
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
44 |
continue |
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
45 |
return None |
1451 | 46 |
|
1179
70825477c6ce
fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents:
1152
diff
changeset
|
47 |
def etype_rtag(self, etype, rtype, role, ttype='*'): |
1148
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
48 |
if role == 'subject': |
1179
70825477c6ce
fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents:
1152
diff
changeset
|
49 |
return self.rtag(rtype, role, etype, ttype) |
70825477c6ce
fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents:
1152
diff
changeset
|
50 |
return self.rtag(rtype, role, ttype, etype) |
1451 | 51 |
|
1179
70825477c6ce
fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents:
1152
diff
changeset
|
52 |
def add_rtag(self, tag, rtype, role, stype='*', otype='*'): |
1148
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
53 |
assert self.use_set |
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
54 |
assert role in ('subject', 'object'), role |
1179
70825477c6ce
fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents:
1152
diff
changeset
|
55 |
rtags = self._tagdefs.setdefault((rtype, role, stype, otype), set()) |
1148
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
56 |
rtags.add(tag) |
1451 | 57 |
|
1179
70825477c6ce
fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents:
1152
diff
changeset
|
58 |
def rtags(self, rtype, role, stype='*', otype='*'): |
1148
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
59 |
assert self.use_set |
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
60 |
rtags = set() |
1179
70825477c6ce
fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents:
1152
diff
changeset
|
61 |
for key in self._get_keys(rtype, role, stype, otype): |
1148
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
62 |
try: |
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
63 |
rtags.update(self._tagdefs[key]) |
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
64 |
except KeyError: |
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
65 |
continue |
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
66 |
return rtags |
1451 | 67 |
|
1179
70825477c6ce
fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents:
1152
diff
changeset
|
68 |
def etype_rtags(self, etype, rtype, role, ttype='*'): |
1148
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
69 |
if role == 'subject': |
1179
70825477c6ce
fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents:
1152
diff
changeset
|
70 |
return self.rtags(rtype, role, etype, ttype) |
70825477c6ce
fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents:
1152
diff
changeset
|
71 |
return self.rtags(rtype, role, ttype, etype) |
1148
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
72 |
|
1451 | 73 |
def _get_keys(self, rtype, role, stype, otype): |
1148
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
74 |
assert role in ('subject', 'object'), role |
1179
70825477c6ce
fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents:
1152
diff
changeset
|
75 |
keys = [(rtype, role, '*', '*'), |
70825477c6ce
fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents:
1152
diff
changeset
|
76 |
(rtype, role, '*', otype), |
70825477c6ce
fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents:
1152
diff
changeset
|
77 |
(rtype, role, stype, '*'), |
70825477c6ce
fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents:
1152
diff
changeset
|
78 |
(rtype, role, stype, otype)] |
1148
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
79 |
if stype == '*' or otype == '*': |
1179
70825477c6ce
fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents:
1152
diff
changeset
|
80 |
keys.remove((rtype, role, '*', '*')) |
1148
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
81 |
if stype == '*': |
1179
70825477c6ce
fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents:
1152
diff
changeset
|
82 |
keys.remove((rtype, role, '*', otype)) |
1148
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
83 |
if otype == '*': |
1451 | 84 |
keys.remove((rtype, role, stype, '*')) |
1148
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
85 |
return keys |
1451 | 86 |
|
1148
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
87 |
# dict compat |
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
88 |
def __getitem__(self, key): |
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
89 |
if isinstance(key, basestring): |
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
90 |
key = (key,) |
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
91 |
return self.rtags(*key) |
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
92 |
|
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
93 |
__contains__ = __getitem__ |