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
195
196
197
198
199
200
201
|
from abc import abstractmethod, ABC
from collections import OrderedDict
from contextvars import ContextVar, Token
from copy import deepcopy
from typing import List, Optional, ContextManager, Any, Tuple
from public import public
@public
class Action(object):
"""An Action."""
inside: bool
def __init__(self):
self.inside = False
def __enter__(self):
getcontext().enter_action(self)
self.inside = True
return self
def __exit__(self, exc_type, exc_val, exc_tb):
getcontext().exit_action(self)
self.inside = False
@public
class Tree(OrderedDict):
def get_by_key(self, path: List) -> Any:
"""
Get the value in the tree at a position given by the path.
:param path: The path to get.
:return: The value in the tree.
"""
if len(path) == 0:
return self
value = self[path[0]]
if len(path) == 1:
return value
elif isinstance(value, Tree):
return value.get_by_key(path[1:])
else:
raise ValueError
def get_by_index(self, path: List[int]) -> Tuple[Any, Any]:
"""
Get the key and value in the tree at a position given by the path of indices
(the nodes inside a level of a tree are ordered by insertion order).
:param path: The path to get.
:return: The key and value.
"""
if len(path) == 0:
raise ValueError
key = list(self.keys())[path[0]]
value = self[key]
if len(path) == 1:
return key, value
elif isinstance(value, Tree):
return value.get_by_index(path[1:])
else:
raise ValueError
def repr(self, depth: int = 0) -> str:
"""
Construct a textual representation of the tree. Useful for visualization and debugging.
:param depth:
:return: The resulting textual representation.
"""
result = ""
for key, value in self.items():
if isinstance(value, Tree):
result += "\t" * depth + str(key) + "\n"
result += value.repr(depth + 1)
else:
result += "\t" * depth + str(key) + ":" + str(value) + "\n"
return result
def __repr__(self):
return self.repr()
@public
class Context(ABC):
"""A context is an object that traces actions which happen. There is always one
context active, see functions :py:func:`getcontext`, :py:func:`setcontext` and :py:func:`resetcontext`.
"""
@abstractmethod
def enter_action(self, action: Action) -> None:
"""
Enter into an action (i.e. start executing it).
:param action: The action.
"""
...
@abstractmethod
def exit_action(self, action: Action) -> None:
"""
Exit from an action (i.e. stop executing it).
:param action: The action.
"""
...
def __str__(self):
return self.__class__.__name__
@public
class NullContext(Context):
"""A context that does not trace any actions."""
def enter_action(self, action: Action) -> None:
pass
def exit_action(self, action: Action) -> None:
pass
@public
class DefaultContext(Context):
"""A context that traces executions of actions in a tree."""
actions: Tree
current: List[Action]
def enter_action(self, action: Action) -> None:
self.actions.get_by_key(self.current)[action] = Tree()
self.current.append(action)
def exit_action(self, action: Action) -> None:
if len(self.current) < 1 or self.current[-1] != action:
raise ValueError
self.current.pop()
def __init__(self):
self.actions = Tree()
self.current = []
def __repr__(self):
return f"{self.__class__.__name__}({self.actions!r}, current={self.current!r})"
_actual_context: ContextVar[Context] = ContextVar("operational_context", default=NullContext())
class _ContextManager(object):
def __init__(self, new_context):
self.new_context = deepcopy(new_context)
def __enter__(self) -> Context:
self.token = setcontext(self.new_context)
return self.new_context
def __exit__(self, t, v, tb):
resetcontext(self.token)
@public
def getcontext() -> Context:
"""Get the current thread/task context."""
return _actual_context.get()
@public
def setcontext(ctx: Context) -> Token:
"""
Set the current thread/task context.
:param ctx: A context to set.
:return: A token to restore previous context.
"""
return _actual_context.set(ctx)
@public
def resetcontext(token: Token):
"""
Reset the context to a previous value.
:param token: A token to restore.
"""
_actual_context.reset(token)
@public
def local(ctx: Optional[Context] = None) -> ContextManager:
"""
Use a local context.
:param ctx: If none, current context is copied.
:return: A context manager.
"""
if ctx is None:
ctx = getcontext()
return _ContextManager(ctx)
|