File: delegator.py

package info (click to toggle)
python-atom 0.11.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 1,676 kB
  • sloc: cpp: 9,254; python: 6,181; makefile: 123
file content (155 lines) | stat: -rw-r--r-- 4,920 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# --------------------------------------------------------------------------------------
# Copyright (c) 2013-2024, Nucleic Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file LICENSE, distributed with this software.
# --------------------------------------------------------------------------------------
from .catom import (
    DefaultValue,
    Member,
    PostGetAttr,
    PostSetAttr,
    PostValidate,
    Validate,
)


class Delegator(Member):
    """A member subclass which delegates all work to a wrapped member.

    The only behaviors not delegated are GetAttr and SetAttr. Subclasses
    should override behavior as needed to suit their needs. In order to
    change a particular mode, the relevant change method must be called
    via super(Delegator, ...).

    """

    __slots__ = "delegate"

    def __init__(self, delegate):
        """Initialize a DeclarativeProperty.

        Parameters
        ----------
        delegate : Member
            The Atom Member which provides the behavior for the property.
            The member should use standard slot behavior semantics.

        """
        self.delegate = delegate
        sup = super(Delegator, self)
        sup.set_post_getattr_mode(PostGetAttr.Delegate, delegate)
        sup.set_post_setattr_mode(PostSetAttr.Delegate, delegate)
        sup.set_default_value_mode(DefaultValue.Delegate, delegate)
        sup.set_validate_mode(Validate.Delegate, delegate)
        sup.set_post_validate_mode(PostValidate.Delegate, delegate)

    def add_static_observer(self, observer):
        """Add a static observer to the member.

        This method also adds the static observer to the delegate.

        """
        super(Delegator, self).add_static_observer(observer)
        self.delegate.add_static_observer(observer)

    def remove_static_observer(self, observer):
        """Remove a static observer from the member.

        This method also removes the static observer from the delegate.

        """
        super(Delegator, self).remove_static_observer(observer)
        self.delegate.remove_static_observer(observer)

    def set_name(self, name):
        """Assign the name to this member.

        This method keeps the name of the delegate member in sync.

        """
        super(Delegator, self).set_name(name)
        self.delegate.set_name(name)

    def set_index(self, index):
        """Assign the index to this member.

        This method keeps the index of the delegate member in sync.

        """
        super(Delegator, self).set_index(index)
        self.delegate.set_index(index)

    def set_post_getattr_mode(self, mode, context):
        """Set the post getattr mode for the member.

        This method proxies the change to the delegate member.

        """
        self.delegate.set_post_getattr_mode(mode, context)

    def set_post_setattr_mode(self, mode, context):
        """Set the post getattr mode for the member.

        This method proxies the change to the delegate member.

        """
        self.delegate.set_post_setattr_mode(mode, context)

    def set_default_value_mode(self, mode, context):
        """Set the default value mode for the member.

        This method proxies the change to the delegate member.

        """
        self.delegate.set_default_value_mode(mode, context)

    def set_validate_mode(self, mode, context):
        """Set the default value mode for the member.

        This method proxies the change to the delegate member.

        """
        self.delegate.set_validate_mode(mode, context)

    def set_post_validate_mode(self, mode, context):
        """Set the default value mode for the member.

        This method proxies the change to the delegate member.

        """
        self.delegate.set_post_validate_mode(mode, context)

    def clone(self):
        """Create a clone of the declarative property.

        This method also creates a clone of the internal delegate for
        mode handlers which use the original delegate as the context.

        """
        clone = super(Delegator, self).clone()
        delegate = self.delegate
        clone.delegate = delegate_clone = delegate.clone()

        mode, old = clone.post_getattr_mode
        if old is delegate:
            clone.set_post_getattr_mode(mode, delegate_clone)

        mode, old = clone.post_setattr_mode
        if old is delegate:
            clone.set_post_setattr_mode(mode, delegate_clone)

        mode, old = clone.default_value_mode
        if old is delegate:
            clone.set_default_value_mode(mode, delegate_clone)

        mode, old = clone.validate_mode
        if old is delegate:
            clone.set_validate_mode(mode, delegate_clone)

        mode, old = clone.post_validate_mode
        if old is delegate:
            clone.set_post_validate_mode(mode, delegate_clone)

        return clone