-
Notifications
You must be signed in to change notification settings - Fork 21
/
ipython_utils.py
239 lines (183 loc) · 7.18 KB
/
ipython_utils.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# -*- coding: utf-8 -*-
import ast
import logging
import sys
from contextlib import contextmanager
from io import StringIO
from typing import Any, Callable, Generator, List, Optional, TextIO
from IPython.core.displayhook import DisplayHook
from IPython.core.displaypub import CapturingDisplayPublisher, DisplayPublisher
from IPython.core.interactiveshell import ExecutionResult, InteractiveShell
from IPython.utils.capture import CapturedIO
from traitlets import MetaHasTraits
from ipyflow.singletons import shell
logger = logging.getLogger(__name__)
logger.setLevel(logging.WARNING)
class _IpythonState:
def __init__(self) -> None:
self.cell_counter: Optional[int] = None
@contextmanager
def save_number_of_currently_executing_cell(self) -> Generator[None, None, None]:
self.cell_counter = shell().execution_count
try:
yield
finally:
self.cell_counter = None
@contextmanager
def ast_transformer_context(
self, transformers: List[ast.NodeTransformer]
) -> Generator[None, None, None]:
old = shell().ast_transformers
shell().ast_transformers = old + transformers
try:
yield
finally:
shell().ast_transformers = old
@contextmanager
def input_transformer_context(
self, transformers: List[Callable[[List[str]], List[str]]]
) -> Generator[None, None, None]:
old = shell().input_transformers_post
shell().input_transformers_post = old + transformers
try:
yield
finally:
shell().input_transformers_post = old
_IPY = _IpythonState()
@contextmanager
def save_number_of_currently_executing_cell() -> Generator[None, None, None]:
with _IPY.save_number_of_currently_executing_cell():
yield
@contextmanager
def ast_transformer_context(transformers) -> Generator[None, None, None]:
with _IPY.ast_transformer_context(transformers):
yield
@contextmanager
def input_transformer_context(transformers) -> Generator[None, None, None]:
with _IPY.input_transformer_context(transformers):
yield
def cell_counter() -> int:
if _IPY.cell_counter is None:
raise ValueError("should be inside context manager here")
return _IPY.cell_counter
def run_cell(cell, **kwargs) -> ExecutionResult:
return shell().run_cell(
cell,
store_history=kwargs.pop("store_history", True),
silent=kwargs.pop("silent", False),
)
class Tee:
def __init__(self, out1, out2):
self.out1 = out1
self.out2 = out2
def __getattr__(self, item):
if item in ("out1", "out2"):
raise AttributeError()
# delegate to the first output stream
return getattr(self.out1, item)
def write(self, data):
self.out1.write(data)
self.out2.write(data)
def flush(self):
self.out1.flush()
self.out2.flush()
class TeeDisplayHook:
def __init__(self, disp1: DisplayHook, disp2: DisplayHook) -> None:
self.disp1 = disp1
self.disp2 = disp2
def __getattr__(self, item: str) -> Any:
if item in ("disp1", "disp2"):
raise AttributeError()
# delegate to the first display hook
return getattr(self.disp1, item)
def __call__(self, result=None) -> None:
self.disp1(result=result)
self.disp2(result=result)
class TeeDisplayPublisher:
def __init__(self, pub1: DisplayPublisher, pub2: DisplayPublisher) -> None:
self.pub1 = pub1
self.pub2 = pub2
def __getattr__(self, item: str) -> Any:
if item in ("pub1", "pub2"):
raise AttributeError()
# delegate to the first publisher
return getattr(self.pub1, item)
def publish(self, *args, **kwargs) -> None:
self.pub1.publish(*args, **kwargs)
self.pub2.publish(*args, **kwargs)
def clear_output(self, *args, **kwargs) -> None:
self.pub1.clear_output(*args, **kwargs)
self.pub2.clear_output(*args, **kwargs)
def set_parent(self, *args, **kwargs) -> None:
if hasattr(self.pub1, "set_parent"):
self.pub1.set_parent(*args, **kwargs)
if hasattr(self.pub2, "set_parent"):
self.pub2.set_parent(*args, **kwargs)
class TeeCompatibleCapturingDisplayPublisher(CapturingDisplayPublisher):
def clear_output(self, wait=False):
self.outputs.clear()
class CaptureOutputTee:
"""
Context manager for capturing and replicating stdout/err and rich display publishers.
NB: This is a modified version of IPython.utils.capture.capture_output that doesn't capture
the displayhook as well (the thing that renders the final expression in a cell). Trying
to capture both it and the display publisher seems like it can confuse ipywidgets, and
for this we can always rerender it anyway if necessary using the Out dictionary.
"""
def __init__(self, stdout=True, stderr=True, display=True) -> None:
self.stdout = stdout
self.stderr = stderr
self.display = display
self.shell: Optional[InteractiveShell] = None
self.sys_stdout: Optional[TextIO] = None
self.sys_stderr: Optional[TextIO] = None
def __enter__(self) -> CapturedIO:
self.sys_stdout = sys.stdout
self.sys_stderr = sys.stderr
if self.display:
self.shell = shell()
if self.shell is None:
self.save_display_pub = None
self.display = False
stdout = stderr = outputs = None
if self.stdout:
stdout = StringIO()
sys.stdout = Tee(sys.stdout, stdout) # type: ignore
if self.stderr:
stderr = StringIO()
sys.stderr = Tee(sys.stderr, stderr) # type: ignore
if self.display and self.shell is not None:
self.save_display_pub = self.shell.display_pub
capture_display_pub = TeeCompatibleCapturingDisplayPublisher()
outputs = capture_display_pub.outputs
self.shell.display_pub = TeeDisplayPublisher(
self.save_display_pub, capture_display_pub
)
return CapturedIO(stdout, stderr, outputs)
def __exit__(self, exc_type, exc_value, traceback) -> None:
sys.stdout = self.sys_stdout
sys.stderr = self.sys_stderr
if self.display and self.shell:
self.shell.display_pub = self.save_display_pub
_PURPLE = "\033[95m"
_RED = "\033[91m"
_RESET = "\033[0m"
# allow exceptions for the test_no_prints test
print_ = print
def print_purple(text: str, **kwargs) -> None:
# The ANSI escape code for purple text is \033[95m
# The \033 is the escape code, and [95m specifies the color (purple)
# Reset code is \033[0m that resets the style to default
print_(f"{_PURPLE}{text}{_RESET}", **kwargs)
def print_red(text: str, **kwargs) -> None:
print_(f"{_RED}{text}{_RESET}", **kwargs)
def make_mro_inserter_metaclass(old_class, new_class):
class MetaMroInserter(MetaHasTraits):
def mro(cls):
ret = []
for clazz in super().mro():
if clazz is old_class:
ret.append(new_class)
ret.append(clazz)
return ret
return MetaMroInserter