1
|
|
|
"""Class Namespaces (internal module). |
2
|
|
|
|
3
|
|
|
All of the guts of the class namespace implementation. |
4
|
|
|
|
5
|
|
|
""" |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
# See https://blog.ionelmc.ro/2015/02/09/understanding-python-metaclasses/ |
9
|
|
|
|
10
|
6 |
|
import collections.abc |
11
|
6 |
|
import functools |
12
|
6 |
|
import itertools |
13
|
6 |
|
import weakref |
14
|
|
|
|
15
|
6 |
|
from . import ops |
16
|
6 |
|
from .descriptor_inspector import _DescriptorInspector |
17
|
6 |
|
from .flags import ENABLE_SET_NAME |
18
|
6 |
|
from .proxy import _Proxy |
19
|
6 |
|
from .scope_proxy import _ScopeProxy |
20
|
|
|
|
21
|
|
|
|
22
|
6 |
|
_PROXY_INFOS = weakref.WeakKeyDictionary() |
23
|
|
|
|
24
|
|
|
|
25
|
6 |
|
def _mro_to_chained(mro, dct): |
26
|
|
|
"""Return a chained map of lookups for the given namespace and mro.""" |
27
|
6 |
|
return collections.ChainMap(*[ |
28
|
|
|
Namespace.get_namespace(cls, dct.path) for cls in |
29
|
|
|
itertools.takewhile( |
30
|
|
|
functools.partial(Namespace.no_blocker, dct), |
31
|
|
|
(cls for cls in mro if isinstance(cls, _Namespaceable))) if |
32
|
|
|
Namespace.namespace_exists(dct.path, cls)]) |
33
|
|
|
|
34
|
|
|
|
35
|
6 |
|
def _instance_map(ns_proxy): |
36
|
|
|
"""Return a map, possibly chained, of lookups for the given instance.""" |
37
|
6 |
|
dct, instance, _ = _PROXY_INFOS[ns_proxy] |
38
|
6 |
|
if instance is not None: |
39
|
6 |
|
if isinstance(instance, _Namespaceable): |
40
|
6 |
|
return _mro_to_chained(instance.__mro__, dct) |
41
|
|
|
else: |
42
|
6 |
|
return Namespace.get_namespace(instance, dct.path) |
43
|
|
|
else: |
44
|
6 |
|
return {} |
45
|
|
|
|
46
|
|
|
|
47
|
6 |
|
def _mro_map(ns_proxy): |
48
|
|
|
"""Return a chained map of lookups for the given owner class.""" |
49
|
6 |
|
dct, _, owner = _PROXY_INFOS[ns_proxy] |
50
|
6 |
|
mro = owner.__mro__ |
51
|
6 |
|
mro = mro[mro.index(dct.parent_object):] |
52
|
6 |
|
return _mro_to_chained(mro, dct) |
53
|
|
|
|
54
|
|
|
|
55
|
6 |
|
def _retarget(ns_proxy): |
56
|
|
|
"""Convert a class lookup to an instance lookup, if needed.""" |
57
|
6 |
|
dct, instance, owner = _PROXY_INFOS[ns_proxy] |
58
|
6 |
|
if instance is None and isinstance(type(owner), _Namespaceable): |
59
|
6 |
|
instance, owner = owner, type(owner) |
60
|
6 |
|
dct = Namespace.get_namespace(owner, dct.path) |
61
|
6 |
|
ns_proxy = _NamespaceProxy(dct, instance, owner) |
62
|
6 |
|
return ns_proxy |
63
|
|
|
|
64
|
|
|
|
65
|
6 |
|
class _NamespaceProxy(_Proxy): |
66
|
|
|
|
67
|
|
|
"""Proxy object for manipulating and querying namespaces.""" |
68
|
|
|
|
69
|
6 |
|
__slots__ = '__weakref__', |
70
|
|
|
|
71
|
6 |
|
def __init__(self, dct, instance, owner): |
72
|
6 |
|
_PROXY_INFOS[self] = dct, instance, owner |
73
|
|
|
|
74
|
6 |
|
def __dir__(self): |
75
|
6 |
|
return collections.ChainMap(_instance_map(self), _mro_map(self)) |
76
|
|
|
|
77
|
6 |
|
def __getattribute__(self, name): |
78
|
6 |
|
self = _retarget(self) |
79
|
6 |
|
_, instance, owner = _PROXY_INFOS[self] |
80
|
6 |
|
instance_map = _instance_map(self) |
81
|
6 |
|
mro_map = _mro_map(self) |
82
|
6 |
|
instance_value = ops.get(instance_map, name) |
83
|
6 |
|
mro_value = ops.get(mro_map, name) |
84
|
6 |
|
if ops.is_data(mro_value): |
85
|
6 |
|
return mro_value.get(instance, owner) |
86
|
6 |
|
elif issubclass(owner, type) and ops.has_get(instance_value): |
87
|
6 |
|
return instance_value.get(None, instance) |
88
|
6 |
|
elif instance_value is not None: |
89
|
6 |
|
return instance_value.object |
90
|
6 |
|
elif ops.has_get(mro_value): |
91
|
6 |
|
return mro_value.get(instance, owner) |
92
|
6 |
|
elif mro_value is not None: |
93
|
6 |
|
return mro_value.object |
94
|
|
|
else: |
95
|
6 |
|
raise AttributeError(name) |
96
|
|
|
|
97
|
6 |
|
def __setattr__(self, name, value): |
98
|
6 |
|
self = _retarget(self) |
99
|
6 |
|
dct, instance, owner = _PROXY_INFOS[self] |
100
|
6 |
|
if instance is None: |
101
|
6 |
|
real_map = Namespace.get_namespace(owner, dct.path) |
102
|
6 |
|
real_map[name] = value |
103
|
6 |
|
return |
104
|
6 |
|
mro_map = _mro_map(self) |
105
|
6 |
|
target_value = ops.get_data(mro_map, name) |
106
|
6 |
|
if target_value is not None: |
107
|
|
|
target_value.set(instance, value) |
108
|
|
|
return |
109
|
6 |
|
instance_map = Namespace.get_namespace(instance, dct.path) |
110
|
6 |
|
instance_map[name] = value |
111
|
|
|
|
112
|
6 |
|
def __delattr__(self, name): |
113
|
6 |
|
self = _retarget(self) |
114
|
6 |
|
dct, instance, owner = _PROXY_INFOS[self] |
115
|
6 |
|
real_map = Namespace.get_namespace(owner, dct.path) |
116
|
6 |
|
if instance is None: |
117
|
6 |
|
ops.delete(real_map, name) |
118
|
6 |
|
return |
119
|
6 |
|
value = ops.get_data(real_map, name) |
120
|
6 |
|
if value is not None: |
121
|
|
|
value.delete(instance) |
122
|
|
|
return |
123
|
6 |
|
instance_map = Namespace.get_namespace(instance, dct.path) |
124
|
6 |
|
ops.delete(instance_map, name) |
125
|
|
|
|
126
|
|
|
|
127
|
6 |
|
class Namespace(dict): |
128
|
|
|
|
129
|
|
|
"""Namespace.""" |
130
|
|
|
|
131
|
6 |
|
__slots__ = 'name', 'scope', 'parent', 'active', 'parent_object' |
132
|
|
|
|
133
|
6 |
|
__namespaces = {} |
134
|
|
|
|
135
|
6 |
|
def __init__(self, *args, **kwargs): |
136
|
6 |
|
super().__init__(*args, **kwargs) |
137
|
6 |
|
bad_values = tuple( |
138
|
|
|
value for value in self.values() if |
139
|
|
|
isinstance(value, (Namespace, _Proxy))) |
140
|
6 |
|
if bad_values: |
141
|
6 |
|
raise ValueError('Bad values: {}'.format(bad_values)) |
142
|
6 |
|
self.name = None |
143
|
6 |
|
self.scope = None |
144
|
6 |
|
self.parent = None |
145
|
6 |
|
self.active = False |
146
|
6 |
|
self.parent_object = None |
147
|
|
|
|
148
|
6 |
|
@classmethod |
149
|
|
|
def premake(cls, name, parent): |
150
|
|
|
"""Return an empty namespace with the given name and parent.""" |
151
|
6 |
|
self = cls() |
152
|
6 |
|
self.name = name |
153
|
6 |
|
self.parent = parent |
154
|
6 |
|
return self |
155
|
|
|
|
156
|
6 |
|
def __setitem__(self, key, value): |
157
|
6 |
|
if ( |
158
|
|
|
self.parent_object is not None and |
159
|
|
|
isinstance(value, Namespace) and value.name != key): |
160
|
6 |
|
value.push(key, self.scope) |
161
|
6 |
|
value.add(self.parent_object) |
162
|
6 |
|
super().__setitem__(key, value) |
163
|
|
|
|
164
|
6 |
|
def __enter__(self): |
165
|
6 |
|
self.activate() |
166
|
6 |
|
return self |
167
|
|
|
|
168
|
6 |
|
def __exit__(self, exc_type, exc_value, traceback): |
169
|
6 |
|
if self.name is None: |
170
|
6 |
|
raise RuntimeError('Namespace must be named.') |
171
|
6 |
|
self.deactivate() |
172
|
|
|
|
173
|
6 |
|
@property |
174
|
|
|
def path(self): |
175
|
|
|
"""Return the full path of the namespace.""" |
176
|
6 |
|
if self.name is None or self.parent is None: |
177
|
|
|
raise ValueError |
178
|
6 |
|
if isinstance(self.parent, Namespace): |
179
|
6 |
|
parent_path = self.parent.path |
180
|
|
|
else: |
181
|
6 |
|
parent_path = () |
182
|
6 |
|
return parent_path + (self.name,) |
183
|
|
|
|
184
|
6 |
|
@classmethod |
185
|
|
|
def __get_helper(cls, target, path): |
186
|
|
|
"""Return the namespace for `target` at `path`, create if needed.""" |
187
|
6 |
|
if isinstance(target, _Namespaceable): |
188
|
6 |
|
path_ = target, path |
189
|
6 |
|
namespaces = cls.__namespaces |
190
|
|
|
else: |
191
|
6 |
|
path_ = path |
192
|
6 |
|
try: |
193
|
6 |
|
namespaces = target.__namespaces |
194
|
6 |
|
except AttributeError: |
195
|
6 |
|
namespaces = {} |
196
|
6 |
|
target.__namespaces = namespaces |
197
|
6 |
|
return path_, namespaces |
198
|
|
|
|
199
|
6 |
|
@classmethod |
200
|
|
|
def namespace_exists(cls, path, target): |
201
|
|
|
"""Return whether the given namespace exists.""" |
202
|
6 |
|
path_, namespaces = cls.__get_helper(target, path) |
203
|
6 |
|
return path_ in namespaces |
204
|
|
|
|
205
|
6 |
|
@classmethod |
206
|
|
|
def get_namespace(cls, target, path): |
207
|
|
|
"""Return a namespace with given target and path, create if needed.""" |
208
|
6 |
|
path_, namespaces = cls.__get_helper(target, path) |
209
|
6 |
|
try: |
210
|
6 |
|
return namespaces[path_] |
211
|
6 |
|
except KeyError: |
212
|
6 |
|
if len(path) == 1: |
213
|
6 |
|
parent = {} |
214
|
|
|
else: |
215
|
6 |
|
parent = cls.get_namespace(target, path[:-1]) |
216
|
6 |
|
return namespaces.setdefault(path_, cls.premake(path[-1], parent)) |
217
|
|
|
|
218
|
6 |
|
@classmethod |
219
|
|
|
def no_blocker(cls, dct, cls_): |
220
|
|
|
"""Return False if there's a non-Namespace object in the path.""" |
221
|
6 |
|
try: |
222
|
6 |
|
namespace = vars(cls_) |
223
|
6 |
|
for name in dct.path: |
224
|
6 |
|
namespace = namespace[name] |
225
|
6 |
|
if not isinstance(namespace, cls): |
226
|
6 |
|
return False |
227
|
6 |
|
except KeyError: |
|
|
|
|
228
|
6 |
|
pass |
229
|
6 |
|
return True |
230
|
|
|
|
231
|
6 |
|
def add(self, target): |
232
|
|
|
"""Add self as a namespace under target.""" |
233
|
6 |
|
path, namespaces = self.__get_helper(target, self.path) |
234
|
6 |
|
res = namespaces.setdefault(path, self) |
235
|
6 |
|
if res is self: |
236
|
6 |
|
self.parent_object = target |
237
|
|
|
|
238
|
6 |
|
def set_if_none(self, name, value): |
239
|
|
|
"""Set the attribute `name` to `value`, if it's initially None.""" |
240
|
6 |
|
if getattr(self, name) is None: |
241
|
6 |
|
setattr(self, name, value) |
242
|
|
|
|
243
|
6 |
|
def push(self, name, scope): |
244
|
|
|
"""Bind self to the given name and scope, and activate.""" |
245
|
6 |
|
self.set_if_none('name', name) |
246
|
6 |
|
self.set_if_none('scope', scope) |
247
|
6 |
|
self.set_if_none('parent', scope.dicts[0]) |
248
|
6 |
|
if name != self.name: |
249
|
6 |
|
raise ValueError('Cannot rename namespace') |
250
|
6 |
|
if scope is not self.scope: |
251
|
|
|
raise ValueError('Cannot reuse namespace') |
252
|
6 |
|
if scope.dicts[0] is not self.parent: |
253
|
|
|
raise ValueError('Cannot reparent namespace') |
254
|
6 |
|
self.scope.namespaces.append(self) |
255
|
6 |
|
self.activate() |
256
|
|
|
|
257
|
6 |
|
def activate(self): |
258
|
|
|
"""Take over as the scope for the target.""" |
259
|
6 |
|
if self.scope is not None and not self.active: |
260
|
6 |
|
if self.scope.dicts[0] is not self.parent: |
261
|
|
|
raise ValueError('Cannot reparent namespace') |
262
|
6 |
|
self.active = True |
263
|
6 |
|
self.scope.dicts.insert(0, self) |
264
|
|
|
|
265
|
6 |
|
def deactivate(self): |
266
|
|
|
"""Stop being the scope for the target.""" |
267
|
6 |
|
if self.scope is not None and self.active: |
268
|
6 |
|
self.active = False |
269
|
6 |
|
self.scope.dicts.pop(0) |
270
|
|
|
|
271
|
6 |
|
def __get__(self, instance, owner): |
272
|
6 |
|
return _NamespaceProxy(self, instance, owner) |
273
|
|
|
|
274
|
|
|
|
275
|
6 |
|
class _NamespaceScope(collections.abc.MutableMapping): |
276
|
|
|
|
277
|
|
|
"""The class creation namespace for _Namespaceables.""" |
278
|
|
|
|
279
|
6 |
|
__slots__ = 'dicts', 'namespaces', 'proxies', 'scope_proxy' |
280
|
|
|
|
281
|
6 |
|
def __init__(self, dct): |
282
|
6 |
|
self.dicts = [dct] |
283
|
6 |
|
self.namespaces = [] |
284
|
6 |
|
self.proxies = proxies = weakref.WeakKeyDictionary() |
285
|
|
|
|
286
|
6 |
|
class ScopeProxy(_ScopeProxy): |
287
|
|
|
|
288
|
|
|
"""Local version of ScopeProxy for this scope.""" |
289
|
|
|
|
290
|
6 |
|
__slots__ = () |
291
|
|
|
|
292
|
6 |
|
def __init__(self, dct): |
293
|
6 |
|
super().__init__(dct, proxies) |
294
|
|
|
|
295
|
6 |
|
self.scope_proxy = ScopeProxy |
296
|
|
|
|
297
|
6 |
|
def __getitem__(self, key): |
298
|
6 |
|
value = collections.ChainMap(*self.dicts)[key] |
299
|
6 |
|
if isinstance(value, Namespace): |
300
|
6 |
|
value = self.scope_proxy(value) |
301
|
6 |
|
return value |
302
|
|
|
|
303
|
6 |
|
def __setitem__(self, key, value): |
304
|
6 |
|
dct = self.dicts[0] |
305
|
6 |
|
if isinstance(value, self.scope_proxy): |
306
|
6 |
|
value = self.proxies[value] |
307
|
6 |
|
if isinstance(value, Namespace) and value.name != key: |
308
|
6 |
|
value.push(key, self) |
309
|
6 |
|
dct[key] = value |
310
|
|
|
|
311
|
6 |
|
def __delitem__(self, key): |
312
|
6 |
|
del self.dicts[0][key] |
313
|
|
|
|
314
|
6 |
|
def __iter__(self): |
315
|
|
|
return iter(collections.ChainMap(*self.dicts)) |
316
|
|
|
|
317
|
6 |
|
def __len__(self): |
318
|
|
|
return len(collections.ChainMap(*self.dicts)) |
319
|
|
|
|
320
|
|
|
|
321
|
6 |
|
_NAMESPACE_SCOPES = weakref.WeakKeyDictionary() |
322
|
|
|
|
323
|
|
|
|
324
|
6 |
|
class _Namespaceable(type): |
325
|
|
|
|
326
|
|
|
"""Metaclass for classes that can contain namespaces. |
327
|
|
|
|
328
|
|
|
Using the metaclass directly is a bad idea. Use a base class instead. |
329
|
|
|
""" |
330
|
|
|
|
331
|
6 |
|
@classmethod |
332
|
|
|
def __prepare__(mcs, name, bases, **kwargs): |
333
|
6 |
|
return _NamespaceScope(super().__prepare__(name, bases, **kwargs)) |
334
|
|
|
|
335
|
6 |
|
def __new__(mcs, name, bases, dct, **kwargs): |
336
|
6 |
|
cls = super().__new__(mcs, name, bases, dct.dicts[0], **kwargs) |
337
|
6 |
|
_NAMESPACE_SCOPES[cls] = dct |
338
|
6 |
|
for namespace in dct.namespaces: |
339
|
6 |
|
namespace.add(cls) |
340
|
6 |
|
if ENABLE_SET_NAME: |
341
|
2 |
|
for name, value in namespace.items(): |
342
|
2 |
|
wrapped = _DescriptorInspector(value) |
343
|
2 |
|
if wrapped.has_set_name: |
344
|
2 |
|
wrapped.set_name(cls, name) |
345
|
6 |
|
return cls |
346
|
|
|
|
347
|
6 |
|
def __setattr__(cls, name, value): |
348
|
6 |
|
if isinstance(value, Namespace) and value.name != name: |
349
|
6 |
|
value.push(name, _NAMESPACE_SCOPES[cls]) |
350
|
6 |
|
value.add(cls) |
351
|
6 |
|
if issubclass(cls, _Namespaceable): |
352
|
6 |
|
super().__setattr__(cls, name, value) |
353
|
|
|
else: |
354
|
6 |
|
super().__setattr__(name, value) |
355
|
|
|
|
356
|
|
|
|
357
|
6 |
|
class Namespaceable(metaclass=_Namespaceable): |
358
|
|
|
|
359
|
|
|
"""Base class for classes that can contain namespaces.""" |
360
|
|
|
|
Except handlers which only contain
pass
and do not have anelse
clause can usually simply be removed: