Completed
Push — master ( 5c1a43...2b7a5b )
by Max
02:14
created

_ns()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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
_NAMESPACES = weakref.WeakKeyDictionary()
9 6
_OWNERS = weakref.WeakKeyDictionary()
10
11
12 6
def namespace(self):
13
    """Return the associated Namespace."""
14 6
    return _owner(self).proxies[self]
15
16
17 6
def _owner(self):
18
    """Return the associated scope."""
19 6
    return _OWNERS[self]
20
21
22 6
class _ScopeProxy(_Proxy):
23
24
    """Proxy object for manipulating namespaces during class creation."""
25
26 6
    __slots__ = ('__weakref__',)
27
28 6
    def __init__(self, dct, owner):
29 6
        _OWNERS[self] = owner
30 6
        owner.proxies[self] = dct
31
32 6
    def __dir__(self):
33 6
        return namespace(self)
34
35 6
    def __getattribute__(self, name):
36 6
        dct = namespace(self)
37 6
        try:
38 6
            value = dct[name]
39 6
        except KeyError:
40 6
            raise AttributeError(name)
41 6
        return _owner(self).wrap(value)
42
43 6
    def __setattr__(self, name, value):
44 6
        namespace(self)[name] = value
45
46 6
    def __delattr__(self, name):
47 6
        ops.delete(namespace(self), name)
48
49 6
    def __enter__(self):
50 6
        return namespace(self).__enter__()
51
52 6
    def __exit__(self, exc_type, exc_value, traceback):
53
        return namespace(self).__exit__(exc_type, exc_value, traceback)
54