1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 __docformat__ = 'reStructuredText'
20
22
23 """
24 States what attributes to delegate, and to which target attributes.
25
26 Instance methods:
27
28 - `has_readable`: Check for read-access mapping of attribute
29 - `has_writable`: Check for read-access mapping of attribute
30 - `has_deletable`: Check for read-access mapping of attribute
31 - `get_readable`: Get read-access mapping for attribute
32 - `get_writable`: Get read-access mapping for attribute
33 - `get_deletable`: Get read-access mapping for attribute
34 - `remove_mappings`: Remove attribute mappings
35 - `add_mappings`: Add attribute mappings
36
37 Operators:
38
39 self |= b::Profile
40 union of this profile and the other
41 """
42
44 self.__readables = {}
45 self.__writables = {}
46 self.__deletables = {}
47
49 """
50 Check for read-access mapping of attribute
51
52 Returns True if the mapping exists
53 """
54 return attribute in self.__readables
55
57 """
58 Check for write-access mapping of attribute
59
60 Returns True if the mapping exists
61 """
62 return attribute in self.__writables
63
65 """
66 Check for delete-access mapping of attribute
67
68 Returns True if the mapping exists
69 """
70 return attribute in self.__deletables
71
73 """
74 Get the mapped value of a readable attribute
75
76 Returns ::string
77 """
78 return self.__readables[attribute]
79
81 """
82 Get the mapped value of a writable attribute
83
84 Returns ::string
85 """
86 return self.__writables[attribute]
87
89 """
90 Get the mapped value of a deletable attribute
91
92 Returns ::string
93 """
94 return self.__deletables[attribute]
95
96
98 """
99 union of this profile and the other
100
101 Parameters:
102
103 - `other` :: Profile
104 the other profile
105 """
106
107
108
109
110
111
112
113 assert reduce(lambda x, y: x and y,
114 (reduce(lambda x,y: x and y,
115 (a[key] == b[key]
116 for key
117 in set(a.iterkeys()) & set(b.iterkeys())),
118 True)
119 for a, b
120 in zip((self.__readables, self.__writables,
121 self.__deletables),
122 (other.__readables, other.__writables,
123 other.__deletables))),
124 True), \
125 "Profiles have conflicting mappings, can't take union"
126
127
128 self.__readables.update(other.__readables)
129 self.__writables.update(other.__writables)
130 self.__deletables.update(other.__deletables)
131 return self
132
134 """
135 Remove attribute mappings
136
137 Parameters:
138
139 `modifiers` :: string
140 the types of delegation access to the target to remove. Valid
141 modifiers are combinations of:
142
143 - `r`: read
144 - `w`: write
145 - `d`: delete
146
147 `names` :: (source_name::string...)
148 names of source_name parts of, the mappings to remove
149 """
150
151 for name in names:
152 assert isinstance(name, basestring), \
153 "Invalid arguments: names should be list of strings: %s" \
154 % (names)
155
156 for name in names:
157 if "r" in modifiers:
158 self.__readables.pop(name)
159
160 if "w" in modifiers:
161 self.__writables.pop(name)
162
163 if "d" in modifiers:
164 self.__deletables.pop(name)
165
166
168 """
169 Map source to target attribute names, for delegation.
170
171 Parameters:
172
173 `modifiers` :: string
174 the types of delegation access to the target. Valid modifiers
175 are combinations of:
176
177 - `r`: read
178 - `w`: write
179 - `d`: delete
180
181 `names` :: (source_name::string...)
182 names of the attribute names to delegate. The target attribute
183 name is the same as `source_name`. This is the same as adding a
184 {source_name : source_name} mapping, using the mapped_names
185 argument.
186
187 `mapped_names` :: {source_name::string : target_name::string ...}
188 names of source and target attributes to map and delegate.
189
190 `target_name`s will be mangled if they have a __ prefix.
191 Mangling will occur every time it is delegated to, so you can
192 change the target object any time.
193 """
194 for name in names:
195 assert isinstance(name, basestring), (
196 "Invalid arguments: names should be list of strings. Got: %s" %
197 names)
198
199 for key, name in mapped_names.iteritems():
200 assert isinstance(key, basestring) and \
201 isinstance(name, basestring), \
202 "Invalid arguments: mapped_names should be dict of " + \
203 "string:string. Got: %s" % mapped_names
204
205 if "r" in modifiers:
206 self.__readables.update(zip(names, names), **mapped_names)
207
208 if "w" in modifiers:
209 self.__writables.update(zip(names, names), **mapped_names)
210
211 if "d" in modifiers:
212 self.__deletables.update(zip(names, names), **mapped_names)
213