__init__.py
1.28 KB
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
from threading import local
from floraconcierge.client import ApiClient
_active = local()
def get_apiclient():
"""
:rtype: ApiClient
"""
if not hasattr(_active, "value"):
raise ValueError('Not activated any apiclient. Please ``activate`` apiclient first')
client = _active.value
assert isinstance(_active.value, ApiClient)
return client
def activate(client):
"""
Sets the api client for the current thread.
The ``timezone`` argument must be an instance of a ApiClient subclass
"""
assert isinstance(client, ApiClient), 'ApiClient object required'
_active.value = client
def deactivate():
"""
Unsets the api client for the current thread.
"""
if hasattr(_active, "value"):
del _active.value
class override(object):
"""
Temporarily set the api client for the current thread.
"""
def __init__(self, client):
self.client = client
self.old_client = getattr(_active, 'value', None)
def __enter__(self):
if self.client is None:
deactivate()
else:
activate(self.client)
def __exit__(self, exc_type, exc_value, traceback):
if self.old_client is None:
deactivate()
else:
_active.value = self.old_client