author | sylvain.thenault@logilab.fr |
Mon, 04 May 2009 15:49:20 +0200 | |
branch | tls-sprint |
changeset 1653 | 8b82227719f0 |
parent 1548 | bd225e776739 |
child 1721 | 694f6a50e138 |
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 |
|
1533 | 9 |
|
1148
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
10 |
class RelationTags(object): |
1533 | 11 |
"""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
|
12 |
|
1533 | 13 |
(subject type, relation type, object type, tagged) |
1152 | 14 |
|
15 |
allowing to set tags using wildcard (eg '*') as subject type / object type |
|
16 |
||
1533 | 17 |
This class associates a single tag to each key. |
1152 | 18 |
""" |
1451 | 19 |
|
1533 | 20 |
def __init__(self): |
1148
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
21 |
self._tagdefs = {} |
1451 | 22 |
|
1528
864ae7c15ef5
other fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1451
diff
changeset
|
23 |
def __repr__(self): |
864ae7c15ef5
other fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1451
diff
changeset
|
24 |
return repr(self._tagdefs) |
864ae7c15ef5
other fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1451
diff
changeset
|
25 |
|
1533 | 26 |
# dict compat |
27 |
def __getitem__(self, key): |
|
28 |
return self.get(*key) |
|
29 |
__contains__ = __getitem__ |
|
1451 | 30 |
|
1533 | 31 |
def _get_keys(self, rtype, tagged, stype, otype): |
32 |
assert tagged in ('subject', 'object'), tagged |
|
33 |
keys = [(rtype, tagged, '*', '*'), |
|
34 |
(rtype, tagged, '*', otype), |
|
35 |
(rtype, tagged, stype, '*'), |
|
36 |
(rtype, tagged, stype, otype)] |
|
37 |
if stype == '*' or otype == '*': |
|
38 |
keys.remove((rtype, tagged, '*', '*')) |
|
39 |
if stype == '*': |
|
40 |
keys.remove((rtype, tagged, '*', otype)) |
|
41 |
if otype == '*': |
|
42 |
keys.remove((rtype, tagged, stype, '*')) |
|
43 |
return keys |
|
1451 | 44 |
|
1548
bd225e776739
new tag_attribute convenience method
sylvain.thenault@logilab.fr
parents:
1533
diff
changeset
|
45 |
def tag_attribute(self, tag, stype, attr): |
bd225e776739
new tag_attribute convenience method
sylvain.thenault@logilab.fr
parents:
1533
diff
changeset
|
46 |
self._tagdefs[(str(attr), 'subject', str(stype), '*')] = tag |
bd225e776739
new tag_attribute convenience method
sylvain.thenault@logilab.fr
parents:
1533
diff
changeset
|
47 |
|
1533 | 48 |
def tag_relation(self, tag, relation, tagged): |
49 |
assert tagged in ('subject', 'object'), tagged |
|
50 |
stype, rtype, otype = relation |
|
51 |
self._tagdefs[(str(rtype), tagged, str(stype), str(otype))] = tag |
|
52 |
||
53 |
def del_rtag(self, relation, tagged): |
|
54 |
assert tagged in ('subject', 'object'), tagged |
|
55 |
stype, rtype, otype = relation |
|
56 |
del self._tagdefs[(str(rtype), tagged, str(stype), str(otype))] |
|
57 |
||
58 |
def get(self, rtype, tagged, stype='*', otype='*'): |
|
59 |
for key in reversed(self._get_keys(rtype, tagged, stype, otype)): |
|
1148
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
60 |
try: |
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
61 |
return self._tagdefs[key] |
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
62 |
except KeyError: |
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
63 |
continue |
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
64 |
return None |
1451 | 65 |
|
1533 | 66 |
def etype_get(self, etype, rtype, tagged, ttype='*'): |
67 |
if tagged == 'subject': |
|
68 |
return self.get(rtype, tagged, etype, ttype) |
|
69 |
return self.get(rtype, tagged, ttype, etype) |
|
70 |
||
71 |
||
1451 | 72 |
|
1533 | 73 |
class RelationTagsSet(RelationTags): |
74 |
"""This class associates a set of tags to each key.""" |
|
75 |
||
76 |
def tag_relation(self, tag, relation, tagged): |
|
77 |
assert tagged in ('subject', 'object'), tagged |
|
78 |
stype, rtype, otype = relation |
|
79 |
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
|
80 |
rtags.add(tag) |
1451 | 81 |
|
1533 | 82 |
def get(self, rtype, tagged, stype='*', otype='*'): |
1148
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
83 |
rtags = set() |
1533 | 84 |
for key in self._get_keys(rtype, tagged, stype, otype): |
1148
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
85 |
try: |
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
86 |
rtags.update(self._tagdefs[key]) |
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
87 |
except KeyError: |
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
88 |
continue |
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
89 |
return rtags |