-
Notifications
You must be signed in to change notification settings - Fork 973
/
context_base.py
194 lines (145 loc) · 5.02 KB
/
context_base.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
"""
Copyright 2019 Goldman Sachs.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
"""
import threading
from gs_quant.errors import MqUninitialisedError, MqValueError
thread_local = threading.local()
class ContextMeta(type):
@property
def __path_key(cls) -> str:
return '{}_path'.format(cls.__name__)
@property
def __default_key(cls) -> str:
return '{}_default'.format(cls.__name__)
@classmethod
def default_value(mcs) -> object:
return None
@property
def path(cls) -> tuple:
return getattr(thread_local, cls.__path_key, ())
@property
def current(cls):
"""
The current instance of this context
"""
path = cls.path
current = cls.__default if not path else next(iter(path))
if current is None:
raise MqUninitialisedError('{} is not initialised'.format(cls.__name__))
return current
@current.setter
def current(cls, current):
path = cls.path
if cls.has_prior:
raise MqValueError('Cannot set current while in a nested context {}'.format(cls.__name__))
if len(path) == 1:
cur = cls.current
try:
if cur.is_entered:
raise MqValueError('Cannot set current while in a nested context {}'.format(cls.__name__))
except AttributeError:
pass
setattr(thread_local, cls.__path_key, (current,))
@property
def current_is_set(cls) -> bool:
return bool(cls.path) or cls.__default is not None
@property
def __default(cls):
default = getattr(thread_local, cls.__default_key, None)
if default is None:
default = cls.default_value()
if default is not None:
setattr(thread_local, cls.__default_key, default)
return default
@property
def prior(cls):
path = cls.path
if len(path) < 2:
raise MqValueError('Current {} has no prior'.format(cls.__name__))
return path[1]
@property
def has_prior(cls):
path = cls.path
return len(path) >= 2
def push(cls, context):
setattr(thread_local, cls.__path_key, (context,) + cls.path)
def pop(cls):
path = cls.path
setattr(thread_local, cls.__path_key, path[1:])
return path[0]
class ContextBase(metaclass=ContextMeta):
def __enter__(self):
self._cls.push(self)
setattr(thread_local, self.__entered_key, True)
self._on_enter()
return self
def __exit__(self, exc_type, exc_val, exc_tb):
try:
self._on_exit(exc_type, exc_val, exc_tb)
finally:
self._cls.pop()
setattr(thread_local, self.__entered_key, False)
async def __aenter__(self):
self._cls.push(self)
setattr(thread_local, self.__entered_key, True)
await self._on_aenter()
return self
async def __aexit__(self, exc_type, exc_val, exc_tb):
try:
await self._on_aexit(exc_type, exc_val, exc_tb)
finally:
self._cls.pop()
setattr(thread_local, self.__entered_key, False)
@property
def __entered_key(self) -> str:
return '{}_entered'.format(id(self))
@property
def _cls(self) -> ContextMeta:
seen = set()
stack = [self.__class__]
cls = None
while stack:
base = stack.pop()
if ContextBase in base.__bases__ or ContextBaseWithDefault in base.__bases__:
cls = base
break
if base not in seen:
seen.add(base)
stack.extend(b for b in base.__bases__ if issubclass(b, ContextBase))
return cls or self.__class__
@property
def is_entered(self) -> bool:
return getattr(thread_local, self.__entered_key, False)
def _on_enter(self):
pass
def _on_exit(self, exc_type, exc_val, exc_tb):
pass
async def _on_aenter(self):
pass
async def _on_aexit(self, exc_type, exc_val, exc_tb):
pass
class ContextBaseWithDefault(ContextBase):
@classmethod
def default_value(cls) -> object:
return cls()
try:
from contextlib import nullcontext
except ImportError:
from contextlib import AbstractContextManager
class nullcontext(AbstractContextManager):
def __init__(self, enter_result=None):
self.enter_result = enter_result
def __enter__(self):
return self.enter_result
def __exit__(self, exc_type, exc_val, exc_tb):
pass