/
context.py
100 lines (71 loc) · 2.45 KB
/
context.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
'''
Context
=======
.. versionadded:: 1.8.0
.. warning::
This is experimental and subject to change as long as this warning notice
is present.
Kivy have few "global" instances that is used directly by many piece of the
framework: `Cache`, `Builder`, `Clock`.
TODO: document this module.
'''
__all__ = ('Context', 'ProxyContext', 'register_context', 'get_current_context')
_contexts = {}
_default_context = None
_context_stack = []
class ProxyContext(object):
__slots__ = ['_obj']
def __init__(self, obj):
object.__init__(self)
object.__setattr__(self, '_obj', obj)
def __getattribute__(self, name):
return getattr(object.__getattribute__(self, '_obj'), name)
def __delattr__(self, name):
delattr(object.__getattribute__(self, '_obj'), name)
def __setattr__(self, name, value):
setattr(object.__getattribute__(self, '_obj'), name, value)
def __bool__(self):
return bool(object.__getattribute__(self, '_obj'))
def __str__(self):
return str(object.__getattribute__(self, '_obj'))
def __repr__(self):
return repr(object.__getattribute__(self, '_obj'))
class Context(dict):
def __init__(self, init=False):
dict.__init__(self)
self.sandbox = None
if not init:
return
for name in _contexts:
context = _contexts[name]
instance = context['cls'](*context['args'], **context['kwargs'])
self[name] = instance
def push(self):
_context_stack.append(self)
for name, instance in self.items():
object.__setattr__(_contexts[name]['proxy'], '_obj', instance)
def pop(self):
# After poping context from stack. Update proxy's _obj with
# instances in current context
_context_stack.pop(-1)
for name, instance in get_current_context().items():
object.__setattr__(_contexts[name]['proxy'], '_obj', instance)
def register_context(name, cls, *args, **kwargs):
'''Register a new context
'''
instance = cls(*args, **kwargs)
proxy = ProxyContext(instance)
_contexts[name] = {
'cls': cls,
'args': args,
'kwargs': kwargs,
'proxy': proxy}
_default_context[name] = instance
return proxy
def get_current_context():
'''Return the current context
'''
if not _context_stack:
return _default_context
return _context_stack[-1]
_default_context = Context(init=False)