rtags.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Wed, 24 Mar 2010 14:11:01 +0100
branchstable
changeset 4992 398cc8b39aec
parent 4931 92c9d0a5dc11
child 5314 86e5abbebfaf
permissions -rw-r--r--
backport pending_operations on hi-jacked session, see comment
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4931
92c9d0a5dc11 improve documentation for uicfg and rtags
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4391
diff changeset
     1
#:organization: Logilab
92c9d0a5dc11 improve documentation for uicfg and rtags
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4391
diff changeset
     2
#:copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2.
92c9d0a5dc11 improve documentation for uicfg and rtags
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4391
diff changeset
     3
#:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
92c9d0a5dc11 improve documentation for uicfg and rtags
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4391
diff changeset
     4
#:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses
92c9d0a5dc11 improve documentation for uicfg and rtags
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4391
diff changeset
     5
92c9d0a5dc11 improve documentation for uicfg and rtags
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4391
diff changeset
     6
"""
92c9d0a5dc11 improve documentation for uicfg and rtags
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4391
diff changeset
     7
A RelationTag object is an object which allows to link a configuration information to a relation definition. For instance, the standard primary view uses a RelationTag object (uicfg.primaryview_section) to get the section to display relations.
92c9d0a5dc11 improve documentation for uicfg and rtags
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4391
diff changeset
     8
92c9d0a5dc11 improve documentation for uicfg and rtags
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4391
diff changeset
     9
.. sourcecode:: python
1152
c99ef2a2535c cleanup
sylvain.thenault@logilab.fr
parents: 1148
diff changeset
    10
4931
92c9d0a5dc11 improve documentation for uicfg and rtags
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4391
diff changeset
    11
   # display ``entry_of`` relations in the ``relations`` section in the ``BlogEntry`` primary view
92c9d0a5dc11 improve documentation for uicfg and rtags
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4391
diff changeset
    12
   uicfg.primaryview_section.tag_subject_of(('BlogEntry', 'entry_of', '*'),
92c9d0a5dc11 improve documentation for uicfg and rtags
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4391
diff changeset
    13
                                             'relations')
92c9d0a5dc11 improve documentation for uicfg and rtags
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4391
diff changeset
    14
92c9d0a5dc11 improve documentation for uicfg and rtags
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4391
diff changeset
    15
   # hide every relation ``entry_of`` in the ``Blog`` primary view
92c9d0a5dc11 improve documentation for uicfg and rtags
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4391
diff changeset
    16
   uicfg.primaryview_section.tag_object_of(('*', 'entry_of', 'Blog'), 'hidden')
92c9d0a5dc11 improve documentation for uicfg and rtags
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4391
diff changeset
    17
92c9d0a5dc11 improve documentation for uicfg and rtags
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4391
diff changeset
    18
Three primitives are defined:
92c9d0a5dc11 improve documentation for uicfg and rtags
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4391
diff changeset
    19
   * ``tag_subject_of`` tag a relation in the subject's context
92c9d0a5dc11 improve documentation for uicfg and rtags
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4391
diff changeset
    20
   * ``tag_object_of`` tag a relation in the object's context
92c9d0a5dc11 improve documentation for uicfg and rtags
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4391
diff changeset
    21
   * ``tag_attribute`` shortcut for tag_subject_of
92c9d0a5dc11 improve documentation for uicfg and rtags
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4391
diff changeset
    22
92c9d0a5dc11 improve documentation for uicfg and rtags
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4391
diff changeset
    23
1152
c99ef2a2535c cleanup
sylvain.thenault@logilab.fr
parents: 1148
diff changeset
    24
"""
c99ef2a2535c cleanup
sylvain.thenault@logilab.fr
parents: 1148
diff changeset
    25
__docformat__ = "restructuredtext en"
1148
55a8238f8f7c keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff changeset
    26
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
    27
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
    28
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
    29
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
    30
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
    31
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
    32
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
    33
    RTAGS.append(rtag)
1533
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    34
3472
5345570eefc6 [rtags] make internal key order consistent with api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2689
diff changeset
    35
def _ensure_str_key(key):
5345570eefc6 [rtags] make internal key order consistent with api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2689
diff changeset
    36
    return tuple(str(k) for k in key)
5345570eefc6 [rtags] make internal key order consistent with api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2689
diff changeset
    37
1148
55a8238f8f7c keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff changeset
    38
class RelationTags(object):
1533
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    39
    """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
    40
1533
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    41
         (subject type, relation type, object type, tagged)
1152
c99ef2a2535c cleanup
sylvain.thenault@logilab.fr
parents: 1148
diff changeset
    42
c99ef2a2535c cleanup
sylvain.thenault@logilab.fr
parents: 1148
diff changeset
    43
    allowing to set tags using wildcard (eg '*') as subject type / object type
c99ef2a2535c cleanup
sylvain.thenault@logilab.fr
parents: 1148
diff changeset
    44
1533
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    45
    This class associates a single tag to each key.
1152
c99ef2a2535c cleanup
sylvain.thenault@logilab.fr
parents: 1148
diff changeset
    46
    """
1739
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    47
    _allowed_values = None
3473
d8bc02158efc [rtags] ease overriding
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3472
diff changeset
    48
    _initfunc = None
1849
1901fa97f521 give a name to rtags instance to ease debugging
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1769
diff changeset
    49
    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
    50
        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
    51
        self._tagdefs = {}
1739
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    52
        if allowed_values is not None:
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    53
            self._allowed_values = allowed_values
3473
d8bc02158efc [rtags] ease overriding
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3472
diff changeset
    54
        if initfunc is not None:
d8bc02158efc [rtags] ease overriding
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3472
diff changeset
    55
            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
    56
        register_rtag(self)
1769
fb91d2b8a441 fix some rtags pb on i18n catalog generation
sylvain.thenault@logilab.fr
parents: 1752
diff changeset
    57
1528
864ae7c15ef5 other fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1451
diff changeset
    58
    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
    59
        return '%s: %s' % (self._name, repr(self._tagdefs))
1528
864ae7c15ef5 other fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1451
diff changeset
    60
1533
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    61
    # dict compat
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    62
    def __getitem__(self, key):
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    63
        return self.get(*key)
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    64
    __contains__ = __getitem__
1451
982e8616d9a2 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1356
diff changeset
    65
2689
44f041222d0f [autoreload] handle uicfg reloading properly with the new event / callback mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2369
diff changeset
    66
    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
    67
        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
    68
1739
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    69
    def _get_keys(self, stype, rtype, otype, tagged):
3472
5345570eefc6 [rtags] make internal key order consistent with api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2689
diff changeset
    70
        keys = [('*', rtype, '*', tagged),
5345570eefc6 [rtags] make internal key order consistent with api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2689
diff changeset
    71
                ('*', rtype, otype, tagged),
5345570eefc6 [rtags] make internal key order consistent with api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2689
diff changeset
    72
                (stype, rtype, '*', tagged),
5345570eefc6 [rtags] make internal key order consistent with api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2689
diff changeset
    73
                (stype, rtype, otype, tagged)]
1533
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    74
        if stype == '*' or otype == '*':
3472
5345570eefc6 [rtags] make internal key order consistent with api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2689
diff changeset
    75
            keys.remove( ('*', rtype, '*', tagged) )
1533
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    76
            if stype == '*':
3472
5345570eefc6 [rtags] make internal key order consistent with api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2689
diff changeset
    77
                keys.remove( ('*', rtype, otype, tagged) )
1533
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    78
            if otype == '*':
3472
5345570eefc6 [rtags] make internal key order consistent with api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2689
diff changeset
    79
                keys.remove( (stype, rtype, '*', tagged) )
1533
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
    80
        return keys
1451
982e8616d9a2 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1356
diff changeset
    81
1769
fb91d2b8a441 fix some rtags pb on i18n catalog generation
sylvain.thenault@logilab.fr
parents: 1752
diff changeset
    82
    def init(self, schema, check=True):
1739
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    83
        # XXX check existing keys against schema
1769
fb91d2b8a441 fix some rtags pb on i18n catalog generation
sylvain.thenault@logilab.fr
parents: 1752
diff changeset
    84
        if check:
3472
5345570eefc6 [rtags] make internal key order consistent with api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2689
diff changeset
    85
            for (stype, rtype, otype, tagged), value in self._tagdefs.items():
1769
fb91d2b8a441 fix some rtags pb on i18n catalog generation
sylvain.thenault@logilab.fr
parents: 1752
diff changeset
    86
                for ertype in (stype, rtype, otype):
fb91d2b8a441 fix some rtags pb on i18n catalog generation
sylvain.thenault@logilab.fr
parents: 1752
diff changeset
    87
                    if ertype != '*' and not ertype in schema:
fb91d2b8a441 fix some rtags pb on i18n catalog generation
sylvain.thenault@logilab.fr
parents: 1752
diff changeset
    88
                        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
    89
                                     (stype, rtype, otype, tagged), value, ertype)
fb91d2b8a441 fix some rtags pb on i18n catalog generation
sylvain.thenault@logilab.fr
parents: 1752
diff changeset
    90
                        self.del_rtag(stype, rtype, otype, tagged)
fb91d2b8a441 fix some rtags pb on i18n catalog generation
sylvain.thenault@logilab.fr
parents: 1752
diff changeset
    91
                        break
1739
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    92
        if self._initfunc is not None:
4364
766359c69f2f [uicfg] fix autoform_section rtags initialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4339
diff changeset
    93
            self.apply(schema, self._initfunc)
766359c69f2f [uicfg] fix autoform_section rtags initialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4339
diff changeset
    94
766359c69f2f [uicfg] fix autoform_section rtags initialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4339
diff changeset
    95
    def apply(self, schema, func):
766359c69f2f [uicfg] fix autoform_section rtags initialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4339
diff changeset
    96
        for eschema in schema.entities():
766359c69f2f [uicfg] fix autoform_section rtags initialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4339
diff changeset
    97
            for rschema, tschemas, role in eschema.relation_definitions(True):
766359c69f2f [uicfg] fix autoform_section rtags initialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4339
diff changeset
    98
                for tschema in tschemas:
766359c69f2f [uicfg] fix autoform_section rtags initialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4339
diff changeset
    99
                    if role == 'subject':
766359c69f2f [uicfg] fix autoform_section rtags initialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4339
diff changeset
   100
                        sschema, oschema = eschema, tschema
766359c69f2f [uicfg] fix autoform_section rtags initialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4339
diff changeset
   101
                    else:
766359c69f2f [uicfg] fix autoform_section rtags initialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4339
diff changeset
   102
                        sschema, oschema = tschema, eschema
766359c69f2f [uicfg] fix autoform_section rtags initialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4339
diff changeset
   103
                    func(self, sschema, rschema, oschema, role)
1739
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   104
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   105
    # rtag declaration api ####################################################
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   106
3473
d8bc02158efc [rtags] ease overriding
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3472
diff changeset
   107
    def tag_attribute(self, key, *args, **kwargs):
1739
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   108
        key = list(key)
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   109
        key.append('*')
4339
fe93b670343d some fixes so that deprecation warning are properly localized
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   110
        key.append('subject')
fe93b670343d some fixes so that deprecation warning are properly localized
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   111
        self.tag_relation(key, *args, **kwargs)
1548
bd225e776739 new tag_attribute convenience method
sylvain.thenault@logilab.fr
parents: 1533
diff changeset
   112
3473
d8bc02158efc [rtags] ease overriding
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3472
diff changeset
   113
    def tag_subject_of(self, key, *args, **kwargs):
1739
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   114
        key = list(key)
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   115
        key.append('subject')
3473
d8bc02158efc [rtags] ease overriding
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3472
diff changeset
   116
        self.tag_relation(key, *args, **kwargs)
1739
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   117
3473
d8bc02158efc [rtags] ease overriding
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3472
diff changeset
   118
    def tag_object_of(self, key, *args, **kwargs):
1739
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   119
        key = list(key)
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   120
        key.append('object')
3473
d8bc02158efc [rtags] ease overriding
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3472
diff changeset
   121
        self.tag_relation(key, *args, **kwargs)
1533
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
   122
1739
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   123
    def tag_relation(self, key, tag):
4391
bd8480824e0b ensure we get a correct key
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4364
diff changeset
   124
        assert len(key) == 4, 'bad key: %s' % list(key)
1739
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   125
        if self._allowed_values is not None:
1859
b068abd45a1c nicer error message
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1849
diff changeset
   126
            assert tag in self._allowed_values, \
b068abd45a1c nicer error message
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1849
diff changeset
   127
                   '%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
   128
                tag, self._allowed_values)
3472
5345570eefc6 [rtags] make internal key order consistent with api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2689
diff changeset
   129
        self._tagdefs[_ensure_str_key(key)] = tag
2369
5a2b8ed266ca new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   130
        return tag
1533
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
   131
1739
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   132
    # rtag runtime api ########################################################
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   133
3472
5345570eefc6 [rtags] make internal key order consistent with api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2689
diff changeset
   134
    def del_rtag(self, *key):
5345570eefc6 [rtags] make internal key order consistent with api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2689
diff changeset
   135
        del self._tagdefs[key]
1739
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   136
3473
d8bc02158efc [rtags] ease overriding
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3472
diff changeset
   137
    def get(self, *key):
d8bc02158efc [rtags] ease overriding
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3472
diff changeset
   138
        for key in reversed(self._get_keys(*key)):
1148
55a8238f8f7c keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff changeset
   139
            try:
55a8238f8f7c keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff changeset
   140
                return self._tagdefs[key]
55a8238f8f7c keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff changeset
   141
            except KeyError:
55a8238f8f7c keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff changeset
   142
                continue
55a8238f8f7c keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff changeset
   143
        return None
1451
982e8616d9a2 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1356
diff changeset
   144
1721
694f6a50e138 final rtags api (eventually :$)
sylvain.thenault@logilab.fr
parents: 1548
diff changeset
   145
    def etype_get(self, etype, rtype, role, ttype='*'):
694f6a50e138 final rtags api (eventually :$)
sylvain.thenault@logilab.fr
parents: 1548
diff changeset
   146
        if role == 'subject':
694f6a50e138 final rtags api (eventually :$)
sylvain.thenault@logilab.fr
parents: 1548
diff changeset
   147
            return self.get(etype, rtype, ttype, role)
694f6a50e138 final rtags api (eventually :$)
sylvain.thenault@logilab.fr
parents: 1548
diff changeset
   148
        return self.get(ttype, rtype, etype, role)
1533
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
   149
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
   150
1451
982e8616d9a2 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1356
diff changeset
   151
1533
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
   152
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
   153
    """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
   154
    """
5a2b8ed266ca new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   155
    tag_container_cls = set
1533
bcd4bfff658b update rtags api
sylvain.thenault@logilab.fr
parents: 1528
diff changeset
   156
1739
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   157
    def tag_relation(self, key, tag):
3472
5345570eefc6 [rtags] make internal key order consistent with api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2689
diff changeset
   158
        rtags = self._tagdefs.setdefault(_ensure_str_key(key),
2369
5a2b8ed266ca new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   159
                                         self.tag_container_cls())
1148
55a8238f8f7c keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff changeset
   160
        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
   161
        return rtags
1451
982e8616d9a2 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1356
diff changeset
   162
1739
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   163
    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
   164
        rtags = self.tag_container_cls()
1721
694f6a50e138 final rtags api (eventually :$)
sylvain.thenault@logilab.fr
parents: 1548
diff changeset
   165
        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
   166
            try:
55a8238f8f7c keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff changeset
   167
                rtags.update(self._tagdefs[key])
55a8238f8f7c keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff changeset
   168
            except KeyError:
55a8238f8f7c keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff changeset
   169
                continue
55a8238f8f7c keep notion of relation tags, tough with simplier implementation and usage
sylvain.thenault@logilab.fr
parents:
diff changeset
   170
        return rtags
1739
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   171
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   172
2369
5a2b8ed266ca new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   173
class RelationTagsDict(RelationTagsSet):
5a2b8ed266ca new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   174
    """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
   175
    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
   176
5a2b8ed266ca new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   177
    def tag_relation(self, key, tag):
3472
5345570eefc6 [rtags] make internal key order consistent with api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2689
diff changeset
   178
        key = _ensure_str_key(key)
2369
5a2b8ed266ca new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   179
        try:
3472
5345570eefc6 [rtags] make internal key order consistent with api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2689
diff changeset
   180
            rtags = self._tagdefs[key]
2369
5a2b8ed266ca new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   181
            rtags.update(tag)
5a2b8ed266ca new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   182
            return rtags
5a2b8ed266ca new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   183
        except KeyError:
3472
5345570eefc6 [rtags] make internal key order consistent with api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2689
diff changeset
   184
            self._tagdefs[key] = tag
2369
5a2b8ed266ca new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   185
            return tag
5a2b8ed266ca new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   186
5a2b8ed266ca new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   187
    def setdefault(self, key, tagkey, tagvalue):
3472
5345570eefc6 [rtags] make internal key order consistent with api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2689
diff changeset
   188
        key = _ensure_str_key(key)
2369
5a2b8ed266ca new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   189
        try:
3472
5345570eefc6 [rtags] make internal key order consistent with api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2689
diff changeset
   190
            rtags = self._tagdefs[key]
2369
5a2b8ed266ca new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   191
            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
   192
            return rtags
5a2b8ed266ca new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   193
        except KeyError:
3472
5345570eefc6 [rtags] make internal key order consistent with api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2689
diff changeset
   194
            self._tagdefs[key] = {tagkey: tagvalue}
5345570eefc6 [rtags] make internal key order consistent with api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2689
diff changeset
   195
            return self._tagdefs[key]
2369
5a2b8ed266ca new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   196
5a2b8ed266ca new RelationTagsDict class, use it in uicfg where necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   197
1739
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   198
class RelationTagsBool(RelationTags):
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   199
    _allowed_values = frozenset((True, False))
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   200
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   201
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
   202
set_log_methods(RelationTags, logging.getLogger('cubicweb.rtags'))