author | Florent <florent@secondweb.fr> |
Wed, 29 Apr 2009 18:56:53 +0200 | |
branch | tls-sprint |
changeset 1545 | 53d3d783370f |
parent 1533 | bcd4bfff658b |
child 1548 | bd225e776739 |
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 |
|
1533 | 45 |
def tag_relation(self, tag, relation, tagged): |
46 |
assert tagged in ('subject', 'object'), tagged |
|
47 |
stype, rtype, otype = relation |
|
48 |
self._tagdefs[(str(rtype), tagged, str(stype), str(otype))] = tag |
|
49 |
||
50 |
def del_rtag(self, relation, tagged): |
|
51 |
assert tagged in ('subject', 'object'), tagged |
|
52 |
stype, rtype, otype = relation |
|
53 |
del self._tagdefs[(str(rtype), tagged, str(stype), str(otype))] |
|
54 |
||
55 |
def get(self, rtype, tagged, stype='*', otype='*'): |
|
56 |
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
|
57 |
try: |
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
58 |
return self._tagdefs[key] |
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
59 |
except KeyError: |
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
60 |
continue |
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
61 |
return None |
1451 | 62 |
|
1533 | 63 |
def etype_get(self, etype, rtype, tagged, ttype='*'): |
64 |
if tagged == 'subject': |
|
65 |
return self.get(rtype, tagged, etype, ttype) |
|
66 |
return self.get(rtype, tagged, ttype, etype) |
|
67 |
||
68 |
||
1451 | 69 |
|
1533 | 70 |
class RelationTagsSet(RelationTags): |
71 |
"""This class associates a set of tags to each key.""" |
|
72 |
||
73 |
def tag_relation(self, tag, relation, tagged): |
|
74 |
assert tagged in ('subject', 'object'), tagged |
|
75 |
stype, rtype, otype = relation |
|
76 |
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
|
77 |
rtags.add(tag) |
1451 | 78 |
|
1533 | 79 |
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
|
80 |
rtags = set() |
1533 | 81 |
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
|
82 |
try: |
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
83 |
rtags.update(self._tagdefs[key]) |
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
84 |
except KeyError: |
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
85 |
continue |
55a8238f8f7c
keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
86 |
return rtags |