-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathcontext.py
More file actions
33 lines (27 loc) · 1.03 KB
/
Copy pathcontext.py
File metadata and controls
33 lines (27 loc) · 1.03 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
"""Class to store the python code context"""
from .symbolsstack import SymbolsStack
from .tokenendmode import TokenEndMode
class Context:
"""Class to store the python code context"""
def __init__(self, values=None):
values = values if values is not None else {
"token_end_mode": TokenEndMode.LINE_FEED,
"class_name": "",
"locals": SymbolsStack(),
"globals": SymbolsStack(), # Not working yet
"loop_label_name": "",
"docstring": False,
}
self.ctx_stack = [values]
def last(self):
"""Return actual context state"""
return self.ctx_stack[-1]
def push(self, values):
"""Push new context state with new values"""
value = self.ctx_stack[-1].copy()
value.update(values)
self.ctx_stack.append(value)
def pop(self):
"""Pop last context state"""
assert len(self.ctx_stack) > 1, "Pop context failed. This is a last context in the stack."
return self.ctx_stack.pop()