1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 __docformat__ = 'reStructuredText'
20
21 from pytilities import AttributeCollection, mangle
22 from pytilities.overloading import overloaded, Overload, Param
23 from pytilities.types import SequenceType
24 from . import Profile
27
28 """
29 An AttributeCollection that delegates attributes of one object to another.
30
31 Instance properties:
32
33 - `target`: Write-only, target of delegation
34 - `profile`: Read-write, profile to use for delegation
35 """
36
37 - def __init__(self, profile=None, target=None):
38 """
39 Constructs a delegator.
40
41 Parameters:
42 `profile` :: Profile = None
43 delegation profile. If None, an empty profile is created for
44 you.
45
46 `target` = None
47 target of delegation
48 """
49 self.__profile = profile or Profile()
50 self.__object = target
51 self.__variable_name = None
52
53 @property
55 """
56 Write-only, target for delegation.
57
58 Delegated attributes will be delegated to this object.
59
60 Overloaded, setter parameters:
61
62 :a:
63 `target_object`
64 the object to delegate to
65 :b:
66 `args` :: (object, variable_name::string)
67 target object will be resolved on every get/set/del by doing
68 getattr(object, variable_name)
69 """
70
71 if self.__variable_name:
72 return getattr(self.__object, self.__variable_name)
73 else:
74 return self.__object
75
77
78 self.__object = args[0]
79 self.__variable_name = args[1]
80
81
82 self.__variable_name = mangle(self.__object, self.__variable_name)
83
85 (self.__object, self.__variable_name) = (target_object, None)
86
87 @target.setter
88 @overloaded((
89 Overload(__set_target_args,
90 Param("args", SequenceType)),
91 Overload(__set_target_object,
92 Param("target_object"))))
95
96 @property
98 """
99 Read-write, the profile to use for attribute mappings.
100
101 Returns ::Profile
102
103 Setter parameters:
104
105 `value` :: Profile
106 the profile
107 """
108 return self.__profile
109
110 @profile.setter
112 self.__profile = value
113
127
142
156