1
|
|
|
"""Base class for Namespace proxies in class creation.""" |
2
|
|
|
|
3
|
6 |
|
import weakref |
4
|
|
|
|
5
|
6 |
|
from . import ops |
6
|
6 |
|
from .proxy import _Proxy |
7
|
|
|
|
8
|
6 |
|
_PROXY_INFOS = weakref.WeakKeyDictionary() |
9
|
|
|
|
10
|
|
|
|
11
|
6 |
|
def _get(self): |
|
|
|
|
12
|
6 |
|
return _PROXY_INFOS[self][self] |
13
|
|
|
|
14
|
|
|
|
15
|
6 |
|
class _ScopeProxy(_Proxy): |
16
|
|
|
|
17
|
|
|
"""Proxy object for manipulating namespaces during class creation.""" |
18
|
|
|
|
19
|
6 |
|
__slots__ = '__weakref__', |
20
|
|
|
|
21
|
6 |
|
def __init__(self, dct, container): |
22
|
6 |
|
_PROXY_INFOS[self] = container |
23
|
6 |
|
container[self] = dct |
24
|
|
|
|
25
|
6 |
|
def __dir__(self): |
26
|
|
|
# This line will fire if dir(ns) is done during class creation. |
27
|
6 |
|
return _get(self) |
28
|
|
|
|
29
|
6 |
|
def __getattribute__(self, name): |
30
|
|
|
# Have to add some dependencies back... |
31
|
6 |
|
from .namespaces import Namespace |
32
|
6 |
|
dct = _get(self) |
33
|
6 |
|
try: |
34
|
6 |
|
value = dct[name] |
35
|
|
|
# These lines will fire if a non-existent namespace attribute is gotten |
36
|
|
|
# during class creation. |
37
|
6 |
|
except KeyError: |
38
|
6 |
|
raise AttributeError(name) |
39
|
6 |
|
if isinstance(value, Namespace): |
40
|
6 |
|
value = type(self)(value) |
41
|
6 |
|
return value |
42
|
|
|
|
43
|
6 |
|
def __setattr__(self, name, value): |
44
|
6 |
|
_get(self)[name] = value |
45
|
|
|
|
46
|
6 |
|
def __delattr__(self, name): |
47
|
6 |
|
ops.delete(_get(self), name) |
48
|
|
|
|
49
|
6 |
|
def __enter__(self): |
50
|
6 |
|
return _get(self).__enter__() |
51
|
|
|
|
52
|
6 |
|
def __exit__(self, exc_type, exc_value, traceback): |
53
|
|
|
return _get(self).__exit__(exc_type, exc_value, traceback) |
54
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.