rtags.py
author sylvain.thenault@logilab.fr
Thu, 30 Apr 2009 18:31:43 +0200
branchtls-sprint
changeset 1628 888871a071d9
parent 1548 bd225e776739
child 1721 694f6a50e138
permissions -rw-r--r--
display internal_fields
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1152
c99ef2a2535c cleanup
sylvain.thenault@logilab.fr
parents: 1148
diff changeset
     1
"""relation tags store
c99ef2a2535c cleanup
sylvain.thenault@logilab.fr
parents: 1148
diff changeset
     2
c99ef2a2535c cleanup
sylvain.thenault@logilab.fr
parents: 1148
diff changeset
     3
:organization: Logilab
c99ef2a2535c cleanup
sylvain.thenault@logilab.fr
parents: 1148
diff changeset
     4
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
c99ef2a2535c cleanup
sylvain.thenault@logilab.fr
parents: 1148
diff changeset
     5
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
c99ef2a2535c cleanup
sylvain.thenault@logilab.fr
parents: 1148
diff changeset
     6
"""
c99ef2a2535c cleanup
sylvain.thenault@logilab.fr
parents: 1148
diff changeset
     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
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
     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
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    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
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    13
         (subject type, relation type, object type, tagged)
1152
c99ef2a2535c cleanup
sylvain.thenault@logilab.fr
parents: 1148
diff changeset
    14
c99ef2a2535c cleanup
sylvain.thenault@logilab.fr
parents: 1148
diff changeset
    15
    allowing to set tags using wildcard (eg '*') as subject type / object type
c99ef2a2535c cleanup
sylvain.thenault@logilab.fr
parents: 1148
diff changeset
    16
1533
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    17
    This class associates a single tag to each key.
1152
c99ef2a2535c cleanup
sylvain.thenault@logilab.fr
parents: 1148
diff changeset
    18
    """
1451
982e8616d9a2 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1356
diff changeset
    19
1533
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    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
982e8616d9a2 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1356
diff changeset
    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
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    26
    # dict compat
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    27
    def __getitem__(self, key):
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    28
        return self.get(*key)
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    29
    __contains__ = __getitem__
1451
982e8616d9a2 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1356
diff changeset
    30
1533
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    31
    def _get_keys(self, rtype, tagged, stype, otype):
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    32
        assert tagged in ('subject', 'object'), tagged
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    33
        keys = [(rtype, tagged, '*', '*'),
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    34
                (rtype, tagged, '*', otype),
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    35
                (rtype, tagged, stype, '*'),
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    36
                (rtype, tagged, stype, otype)]
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    37
        if stype == '*' or otype == '*':
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    38
            keys.remove((rtype, tagged, '*', '*'))
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    39
            if stype == '*':
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    40
                keys.remove((rtype, tagged, '*', otype))
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    41
            if otype == '*':
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    42
                keys.remove((rtype, tagged, stype, '*'))
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    43
        return keys
1451
982e8616d9a2 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1356
diff changeset
    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
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    48
    def tag_relation(self, tag, relation, tagged):
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    49
        assert tagged in ('subject', 'object'), tagged
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    50
        stype, rtype, otype = relation
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    51
        self._tagdefs[(str(rtype), tagged, str(stype), str(otype))] = tag
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    52
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    53
    def del_rtag(self, relation, tagged):
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    54
        assert tagged in ('subject', 'object'), tagged
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    55
        stype, rtype, otype = relation
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    56
        del self._tagdefs[(str(rtype), tagged, str(stype), str(otype))]
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    57
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    58
    def get(self, rtype, tagged, stype='*', otype='*'):
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    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
982e8616d9a2 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1356
diff changeset
    65
1533
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    66
    def etype_get(self, etype, rtype, tagged, ttype='*'):
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    67
        if tagged == 'subject':
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    68
            return self.get(rtype, tagged, etype, ttype)
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    69
        return self.get(rtype, tagged, ttype, etype)
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    70
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    71
1451
982e8616d9a2 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1356
diff changeset
    72
1533
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    73
class RelationTagsSet(RelationTags):
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    74
    """This class associates a set of tags to each key."""
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    75
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    76
    def tag_relation(self, tag, relation, tagged):
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    77
        assert tagged in ('subject', 'object'), tagged
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    78
        stype, rtype, otype = relation
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    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
982e8616d9a2 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1356
diff changeset
    81
1533
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    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
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    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