rtags.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Wed, 16 Sep 2009 17:52:54 +0200
branch3.5
changeset 3268 e9f4ea71e696
parent 2689 44f041222d0f
child 3472 5345570eefc6
child 4212 ab6573088b4a
permissions -rw-r--r--
3.5
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
1977
606923dff11b big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1859
diff changeset
     4
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2.
1152
c99ef2a2535c cleanup
sylvain.thenault@logilab.fr
parents: 1148
diff changeset
     5
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
1977
606923dff11b big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1859
diff changeset
     6
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses
1152
c99ef2a2535c cleanup
sylvain.thenault@logilab.fr
parents: 1148
diff changeset
     7
"""
c99ef2a2535c cleanup
sylvain.thenault@logilab.fr
parents: 1148
diff changeset
     8
__docformat__ = "restructuredtext en"
1148
55a8238f8f7c keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff changeset
     9
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
    10
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
    11
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
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
    13
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
    14
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
    15
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
    16
    RTAGS.append(rtag)
1533
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    17
1148
55a8238f8f7c keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff changeset
    18
class RelationTags(object):
1533
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    19
    """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
    20
1533
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    21
         (subject type, relation type, object type, tagged)
1152
c99ef2a2535c cleanup
sylvain.thenault@logilab.fr
parents: 1148
diff changeset
    22
c99ef2a2535c cleanup
sylvain.thenault@logilab.fr
parents: 1148
diff changeset
    23
    allowing to set tags using wildcard (eg '*') as subject type / object type
c99ef2a2535c cleanup
sylvain.thenault@logilab.fr
parents: 1148
diff changeset
    24
1533
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    25
    This class associates a single tag to each key.
1152
c99ef2a2535c cleanup
sylvain.thenault@logilab.fr
parents: 1148
diff changeset
    26
    """
1739
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    27
    _allowed_values = None
1849
1901fa97f521 give a name to rtags instance to ease debugging
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1769
diff changeset
    28
    def __init__(self, name=None, initfunc=None, allowed_values=None):
1901fa97f521 give a name to rtags instance to ease debugging
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1769
diff changeset
    29
        self._name = name or '<unknown>'
1148
55a8238f8f7c keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff changeset
    30
        self._tagdefs = {}
1739
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    31
        if allowed_values is not None:
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    32
            self._allowed_values = allowed_values
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    33
        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
    34
        register_rtag(self)
1769
fb91d2b8a441 fix some rtags pb on i18n catalog generation
sylvain.thenault@logilab.fr
parents: 1752
diff changeset
    35
1528
864ae7c15ef5 other fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1451
diff changeset
    36
    def __repr__(self):
1849
1901fa97f521 give a name to rtags instance to ease debugging
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1769
diff changeset
    37
        return '%s: %s' % (self._name, repr(self._tagdefs))
1528
864ae7c15ef5 other fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1451
diff changeset
    38
1533
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    39
    # dict compat
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    40
    def __getitem__(self, key):
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    41
        return self.get(*key)
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    42
    __contains__ = __getitem__
1451
982e8616d9a2 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1356
diff changeset
    43
2689
44f041222d0f [autoreload] handle uicfg reloading properly with the new event / callback mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2369
diff changeset
    44
    def clear(self):
44f041222d0f [autoreload] handle uicfg reloading properly with the new event / callback mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2369
diff changeset
    45
        self._tagdefs.clear()
44f041222d0f [autoreload] handle uicfg reloading properly with the new event / callback mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2369
diff changeset
    46
1739
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    47
    def _get_keys(self, stype, rtype, otype, tagged):
1533
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    48
        keys = [(rtype, tagged, '*', '*'),
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    49
                (rtype, tagged, '*', otype),
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    50
                (rtype, tagged, stype, '*'),
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    51
                (rtype, tagged, stype, otype)]
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    52
        if stype == '*' or otype == '*':
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    53
            keys.remove((rtype, tagged, '*', '*'))
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    54
            if stype == '*':
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    55
                keys.remove((rtype, tagged, '*', otype))
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    56
            if otype == '*':
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    57
                keys.remove((rtype, tagged, stype, '*'))
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    58
        return keys
1451
982e8616d9a2 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1356
diff changeset
    59
1769
fb91d2b8a441 fix some rtags pb on i18n catalog generation
sylvain.thenault@logilab.fr
parents: 1752
diff changeset
    60
    def init(self, schema, check=True):
1739
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    61
        # XXX check existing keys against schema
1769
fb91d2b8a441 fix some rtags pb on i18n catalog generation
sylvain.thenault@logilab.fr
parents: 1752
diff changeset
    62
        if check:
fb91d2b8a441 fix some rtags pb on i18n catalog generation
sylvain.thenault@logilab.fr
parents: 1752
diff changeset
    63
            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
    64
                for ertype in (stype, rtype, otype):
fb91d2b8a441 fix some rtags pb on i18n catalog generation
sylvain.thenault@logilab.fr
parents: 1752
diff changeset
    65
                    if ertype != '*' and not ertype in schema:
fb91d2b8a441 fix some rtags pb on i18n catalog generation
sylvain.thenault@logilab.fr
parents: 1752
diff changeset
    66
                        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
    67
                                     (stype, rtype, otype, tagged), value, ertype)
fb91d2b8a441 fix some rtags pb on i18n catalog generation
sylvain.thenault@logilab.fr
parents: 1752
diff changeset
    68
                        self.del_rtag(stype, rtype, otype, tagged)
fb91d2b8a441 fix some rtags pb on i18n catalog generation
sylvain.thenault@logilab.fr
parents: 1752
diff changeset
    69
                        break
1739
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    70
        if self._initfunc is not None:
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    71
            for eschema in schema.entities():
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    72
                for rschema, tschemas, role in eschema.relation_definitions(True):
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    73
                    for tschema in tschemas:
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    74
                        if role == 'subject':
1744
056ed8fca8d3 fix naming to avoid errors...
sylvain.thenault@logilab.fr
parents: 1742
diff changeset
    75
                            sschema, oschema = eschema, tschema
1739
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    76
                        else:
1744
056ed8fca8d3 fix naming to avoid errors...
sylvain.thenault@logilab.fr
parents: 1742
diff changeset
    77
                            sschema, oschema = tschema, eschema
056ed8fca8d3 fix naming to avoid errors...
sylvain.thenault@logilab.fr
parents: 1742
diff changeset
    78
                        self._initfunc(self, sschema, rschema, oschema, role)
1739
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    79
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    80
    # rtag declaration api ####################################################
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    81
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    82
    def tag_attribute(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('*')
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    85
        self.tag_subject_of(key, tag)
1548
bd225e776739 new tag_attribute convenience method
sylvain.thenault@logilab.fr
parents: 1533
diff changeset
    86
1739
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    87
    def tag_subject_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('subject')
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    90
        self.tag_relation(key, tag)
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    91
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    92
    def tag_object_of(self, key, tag):
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    93
        key = list(key)
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    94
        key.append('object')
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    95
        self.tag_relation(key, tag)
1533
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    96
1739
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    97
    def tag_relation(self, key, tag):
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    98
        #if isinstance(key, basestring):
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    99
        #    stype, rtype, otype = key.split()
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   100
        #else:
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   101
        stype, rtype, otype, tagged = [str(k) for k in key]
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   102
        if self._allowed_values is not None:
1859
b068abd45a1c nicer error message
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1849
diff changeset
   103
            assert tag in self._allowed_values, \
b068abd45a1c nicer error message
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1849
diff changeset
   104
                   '%r is not an allowed tag (should be in %s)' % (
b068abd45a1c nicer error message
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1849
diff changeset
   105
                tag, self._allowed_values)
1739
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   106
        self._tagdefs[(rtype, tagged, stype, otype)] = tag
2369
5a2b8ed266ca new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   107
        return tag
1533
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
   108
1739
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   109
    # rtag runtime api ########################################################
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   110
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   111
    def del_rtag(self, stype, rtype, otype, tagged):
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   112
        del self._tagdefs[(rtype, tagged, stype, otype)]
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   113
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   114
    def get(self, stype, rtype, otype, tagged):
1721
694f6a50e138 final rtags api (eventually :$)
sylvain.thenault@logilab.fr
parents: 1548
diff changeset
   115
        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
   116
            try:
55a8238f8f7c keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff changeset
   117
                return self._tagdefs[key]
55a8238f8f7c keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff changeset
   118
            except KeyError:
55a8238f8f7c keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff changeset
   119
                continue
55a8238f8f7c keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff changeset
   120
        return None
1451
982e8616d9a2 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1356
diff changeset
   121
1721
694f6a50e138 final rtags api (eventually :$)
sylvain.thenault@logilab.fr
parents: 1548
diff changeset
   122
    def etype_get(self, etype, rtype, role, ttype='*'):
694f6a50e138 final rtags api (eventually :$)
sylvain.thenault@logilab.fr
parents: 1548
diff changeset
   123
        if role == 'subject':
694f6a50e138 final rtags api (eventually :$)
sylvain.thenault@logilab.fr
parents: 1548
diff changeset
   124
            return self.get(etype, rtype, ttype, role)
694f6a50e138 final rtags api (eventually :$)
sylvain.thenault@logilab.fr
parents: 1548
diff changeset
   125
        return self.get(ttype, rtype, etype, role)
1533
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
   126
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
   127
1451
982e8616d9a2 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1356
diff changeset
   128
1533
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
   129
class RelationTagsSet(RelationTags):
2369
5a2b8ed266ca new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   130
    """This class associates a set of tags to each key.
5a2b8ed266ca new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   131
    """
5a2b8ed266ca new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   132
    tag_container_cls = set
1533
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
   133
1739
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   134
    def tag_relation(self, key, tag):
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   135
        stype, rtype, otype, tagged = [str(k) for k in key]
2369
5a2b8ed266ca new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   136
        rtags = self._tagdefs.setdefault((rtype, tagged, stype, otype),
5a2b8ed266ca new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   137
                                         self.tag_container_cls())
1148
55a8238f8f7c keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff changeset
   138
        rtags.add(tag)
2369
5a2b8ed266ca new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   139
        return rtags
1451
982e8616d9a2 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1356
diff changeset
   140
1739
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   141
    def get(self, stype, rtype, otype, tagged):
2369
5a2b8ed266ca new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   142
        rtags = self.tag_container_cls()
1721
694f6a50e138 final rtags api (eventually :$)
sylvain.thenault@logilab.fr
parents: 1548
diff changeset
   143
        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
   144
            try:
55a8238f8f7c keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff changeset
   145
                rtags.update(self._tagdefs[key])
55a8238f8f7c keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff changeset
   146
            except KeyError:
55a8238f8f7c keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff changeset
   147
                continue
55a8238f8f7c keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff changeset
   148
        return rtags
1739
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   149
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   150
2369
5a2b8ed266ca new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   151
class RelationTagsDict(RelationTagsSet):
5a2b8ed266ca new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   152
    """This class associates a set of tags to each key."""
5a2b8ed266ca new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   153
    tag_container_cls = dict
5a2b8ed266ca new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   154
5a2b8ed266ca new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   155
    def tag_relation(self, key, tag):
5a2b8ed266ca new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   156
        stype, rtype, otype, tagged = [str(k) for k in key]
5a2b8ed266ca new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   157
        try:
5a2b8ed266ca new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   158
            rtags = self._tagdefs[(rtype, tagged, stype, otype)]
5a2b8ed266ca new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   159
            rtags.update(tag)
5a2b8ed266ca new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   160
            return rtags
5a2b8ed266ca new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   161
        except KeyError:
5a2b8ed266ca new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   162
            self._tagdefs[(rtype, tagged, stype, otype)] = tag
5a2b8ed266ca new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   163
            return tag
5a2b8ed266ca new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   164
5a2b8ed266ca new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   165
    def setdefault(self, key, tagkey, tagvalue):
5a2b8ed266ca new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   166
        stype, rtype, otype, tagged = [str(k) for k in key]
5a2b8ed266ca new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   167
        try:
5a2b8ed266ca new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   168
            rtags = self._tagdefs[(rtype, tagged, stype, otype)]
5a2b8ed266ca new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   169
            rtags.setdefault(tagkey, tagvalue)
5a2b8ed266ca new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   170
            return rtags
5a2b8ed266ca new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   171
        except KeyError:
5a2b8ed266ca new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   172
            self._tagdefs[(rtype, tagged, stype, otype)] = {tagkey: tagvalue}
5a2b8ed266ca new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   173
            return self._tagdefs[(rtype, tagged, stype, otype)]
5a2b8ed266ca new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   174
5a2b8ed266ca new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   175
1739
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   176
class RelationTagsBool(RelationTags):
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   177
    _allowed_values = frozenset((True, False))
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   178
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   179
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
   180
set_log_methods(RelationTags, logging.getLogger('cubicweb.rtags'))