rtags.py
author Sylvain <syt@logilab.fr>
Thu, 14 May 2009 14:51:52 +0200
changeset 1819 7d3e8adcd559
parent 1769 fb91d2b8a441
child 1849 1901fa97f521
permissions -rw-r--r--
remove 2.5 construction
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
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
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    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
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    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
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    20
         (subject type, relation type, object type, tagged)
1152
c99ef2a2535c cleanup
sylvain.thenault@logilab.fr
parents: 1148
diff changeset
    21
c99ef2a2535c cleanup
sylvain.thenault@logilab.fr
parents: 1148
diff changeset
    22
    allowing to set tags using wildcard (eg '*') as subject type / object type
c99ef2a2535c cleanup
sylvain.thenault@logilab.fr
parents: 1148
diff changeset
    23
1533
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    24
    This class associates a single tag to each key.
1152
c99ef2a2535c cleanup
sylvain.thenault@logilab.fr
parents: 1148
diff changeset
    25
    """
1739
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    26
    _allowed_values = None
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    27
    def __init__(self, initfunc=None, allowed_values=None):
1148
55a8238f8f7c keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff changeset
    28
        self._tagdefs = {}
1739
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    29
        if allowed_values is not None:
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    30
            self._allowed_values = allowed_values
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    31
        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
    32
        register_rtag(self)
1769
fb91d2b8a441 fix some rtags pb on i18n catalog generation
sylvain.thenault@logilab.fr
parents: 1752
diff changeset
    33
1528
864ae7c15ef5 other fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1451
diff changeset
    34
    def __repr__(self):
864ae7c15ef5 other fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1451
diff changeset
    35
        return repr(self._tagdefs)
864ae7c15ef5 other fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1451
diff changeset
    36
1533
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    37
    # dict compat
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    38
    def __getitem__(self, key):
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    39
        return self.get(*key)
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    40
    __contains__ = __getitem__
1451
982e8616d9a2 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1356
diff changeset
    41
1739
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    42
    def _get_keys(self, stype, rtype, otype, tagged):
1533
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    43
        keys = [(rtype, tagged, '*', '*'),
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    44
                (rtype, tagged, '*', otype),
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    45
                (rtype, tagged, stype, '*'),
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    46
                (rtype, tagged, stype, otype)]
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    47
        if stype == '*' or otype == '*':
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    48
            keys.remove((rtype, tagged, '*', '*'))
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    49
            if stype == '*':
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    50
                keys.remove((rtype, tagged, '*', otype))
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    51
            if otype == '*':
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    52
                keys.remove((rtype, tagged, stype, '*'))
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    53
        return keys
1451
982e8616d9a2 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1356
diff changeset
    54
1769
fb91d2b8a441 fix some rtags pb on i18n catalog generation
sylvain.thenault@logilab.fr
parents: 1752
diff changeset
    55
    def init(self, schema, check=True):
1739
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    56
        # XXX check existing keys against schema
1769
fb91d2b8a441 fix some rtags pb on i18n catalog generation
sylvain.thenault@logilab.fr
parents: 1752
diff changeset
    57
        if check:
fb91d2b8a441 fix some rtags pb on i18n catalog generation
sylvain.thenault@logilab.fr
parents: 1752
diff changeset
    58
            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
    59
                for ertype in (stype, rtype, otype):
fb91d2b8a441 fix some rtags pb on i18n catalog generation
sylvain.thenault@logilab.fr
parents: 1752
diff changeset
    60
                    if ertype != '*' and not ertype in schema:
fb91d2b8a441 fix some rtags pb on i18n catalog generation
sylvain.thenault@logilab.fr
parents: 1752
diff changeset
    61
                        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
    62
                                     (stype, rtype, otype, tagged), value, ertype)
fb91d2b8a441 fix some rtags pb on i18n catalog generation
sylvain.thenault@logilab.fr
parents: 1752
diff changeset
    63
                        self.del_rtag(stype, rtype, otype, tagged)
fb91d2b8a441 fix some rtags pb on i18n catalog generation
sylvain.thenault@logilab.fr
parents: 1752
diff changeset
    64
                        break
1739
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    65
        if self._initfunc is not None:
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    66
            for eschema in schema.entities():
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    67
                for rschema, tschemas, role in eschema.relation_definitions(True):
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    68
                    for tschema in tschemas:
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    69
                        if role == 'subject':
1744
056ed8fca8d3 fix naming to avoid errors...
sylvain.thenault@logilab.fr
parents: 1742
diff changeset
    70
                            sschema, oschema = eschema, tschema
1739
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    71
                        else:
1744
056ed8fca8d3 fix naming to avoid errors...
sylvain.thenault@logilab.fr
parents: 1742
diff changeset
    72
                            sschema, oschema = tschema, eschema
056ed8fca8d3 fix naming to avoid errors...
sylvain.thenault@logilab.fr
parents: 1742
diff changeset
    73
                        self._initfunc(self, sschema, rschema, oschema, role)
1739
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    74
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    75
    # rtag declaration api ####################################################
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    76
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    77
    def tag_attribute(self, key, tag):
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    78
        key = list(key)
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    79
        key.append('*')
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    80
        self.tag_subject_of(key, tag)
1548
bd225e776739 new tag_attribute convenience method
sylvain.thenault@logilab.fr
parents: 1533
diff changeset
    81
1739
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    82
    def tag_subject_of(self, key, tag):
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    83
        key = list(key)
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    84
        key.append('subject')
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    85
        self.tag_relation(key, tag)
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    86
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    87
    def tag_object_of(self, key, tag):
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    88
        key = list(key)
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    89
        key.append('object')
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    90
        self.tag_relation(key, tag)
1533
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    91
1739
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    92
    def tag_relation(self, key, tag):
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    93
        #if isinstance(key, basestring):
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    94
        #    stype, rtype, otype = key.split()
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    95
        #else:
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    96
        stype, rtype, otype, tagged = [str(k) for k in key]
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    97
        if self._allowed_values is not None:
1742
25a765e756c4 fix self on initfunc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1740
diff changeset
    98
            assert tag in self._allowed_values, '%r is not an allowed tag' % tag
1739
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    99
        self._tagdefs[(rtype, tagged, stype, otype)] = tag
1533
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
   100
1739
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   101
    # rtag runtime api ########################################################
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   102
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   103
    def del_rtag(self, stype, rtype, otype, tagged):
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   104
        del self._tagdefs[(rtype, tagged, stype, otype)]
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   105
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   106
    def get(self, stype, rtype, otype, tagged):
1721
694f6a50e138 final rtags api (eventually :$)
sylvain.thenault@logilab.fr
parents: 1548
diff changeset
   107
        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
   108
            try:
55a8238f8f7c keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff changeset
   109
                return self._tagdefs[key]
55a8238f8f7c keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff changeset
   110
            except KeyError:
55a8238f8f7c keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff changeset
   111
                continue
55a8238f8f7c keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff changeset
   112
        return None
1451
982e8616d9a2 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1356
diff changeset
   113
1721
694f6a50e138 final rtags api (eventually :$)
sylvain.thenault@logilab.fr
parents: 1548
diff changeset
   114
    def etype_get(self, etype, rtype, role, ttype='*'):
694f6a50e138 final rtags api (eventually :$)
sylvain.thenault@logilab.fr
parents: 1548
diff changeset
   115
        if role == 'subject':
694f6a50e138 final rtags api (eventually :$)
sylvain.thenault@logilab.fr
parents: 1548
diff changeset
   116
            return self.get(etype, rtype, ttype, role)
694f6a50e138 final rtags api (eventually :$)
sylvain.thenault@logilab.fr
parents: 1548
diff changeset
   117
        return self.get(ttype, rtype, etype, role)
1533
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
   118
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
   119
1451
982e8616d9a2 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1356
diff changeset
   120
1533
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
   121
class RelationTagsSet(RelationTags):
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
   122
    """This class associates a set of tags to each key."""
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
   123
1739
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   124
    def tag_relation(self, key, tag):
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   125
        stype, rtype, otype, tagged = [str(k) for k in key]
1533
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
   126
        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
   127
        rtags.add(tag)
1451
982e8616d9a2 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1356
diff changeset
   128
1739
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   129
    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
   130
        rtags = set()
1721
694f6a50e138 final rtags api (eventually :$)
sylvain.thenault@logilab.fr
parents: 1548
diff changeset
   131
        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
   132
            try:
55a8238f8f7c keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff changeset
   133
                rtags.update(self._tagdefs[key])
55a8238f8f7c keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff changeset
   134
            except KeyError:
55a8238f8f7c keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff changeset
   135
                continue
55a8238f8f7c keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff changeset
   136
        return rtags
1739
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   137
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   138
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   139
class RelationTagsBool(RelationTags):
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   140
    _allowed_values = frozenset((True, False))
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   141
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   142
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
   143
set_log_methods(RelationTags, logging.getLogger('cubicweb.rtags'))