Show More
@@ -1,172 +1,166 b'' | |||
|
1 | 1 | # encoding: utf-8 |
|
2 | 2 | """IO capturing utilities.""" |
|
3 | 3 | |
|
4 | 4 | # Copyright (c) IPython Development Team. |
|
5 | 5 | # Distributed under the terms of the Modified BSD License. |
|
6 | 6 | |
|
7 | 7 | |
|
8 | 8 | import sys |
|
9 | ||
|
10 | from IPython.utils.py3compat import PY3 | |
|
11 | ||
|
12 | if PY3: | |
|
13 | from io import StringIO | |
|
14 | else: | |
|
15 | from StringIO import StringIO | |
|
9 | from io import StringIO | |
|
16 | 10 | |
|
17 | 11 | #----------------------------------------------------------------------------- |
|
18 | 12 | # Classes and functions |
|
19 | 13 | #----------------------------------------------------------------------------- |
|
20 | 14 | |
|
21 | 15 | |
|
22 | 16 | class RichOutput(object): |
|
23 | 17 | def __init__(self, data=None, metadata=None): |
|
24 | 18 | self.data = data or {} |
|
25 | 19 | self.metadata = metadata or {} |
|
26 | 20 | |
|
27 | 21 | def display(self): |
|
28 | 22 | from IPython.display import publish_display_data |
|
29 | 23 | publish_display_data(data=self.data, metadata=self.metadata) |
|
30 | 24 | |
|
31 | 25 | def _repr_mime_(self, mime): |
|
32 | 26 | if mime not in self.data: |
|
33 | 27 | return |
|
34 | 28 | data = self.data[mime] |
|
35 | 29 | if mime in self.metadata: |
|
36 | 30 | return data, self.metadata[mime] |
|
37 | 31 | else: |
|
38 | 32 | return data |
|
39 | 33 | |
|
40 | 34 | def _repr_html_(self): |
|
41 | 35 | return self._repr_mime_("text/html") |
|
42 | 36 | |
|
43 | 37 | def _repr_latex_(self): |
|
44 | 38 | return self._repr_mime_("text/latex") |
|
45 | 39 | |
|
46 | 40 | def _repr_json_(self): |
|
47 | 41 | return self._repr_mime_("application/json") |
|
48 | 42 | |
|
49 | 43 | def _repr_javascript_(self): |
|
50 | 44 | return self._repr_mime_("application/javascript") |
|
51 | 45 | |
|
52 | 46 | def _repr_png_(self): |
|
53 | 47 | return self._repr_mime_("image/png") |
|
54 | 48 | |
|
55 | 49 | def _repr_jpeg_(self): |
|
56 | 50 | return self._repr_mime_("image/jpeg") |
|
57 | 51 | |
|
58 | 52 | def _repr_svg_(self): |
|
59 | 53 | return self._repr_mime_("image/svg+xml") |
|
60 | 54 | |
|
61 | 55 | |
|
62 | 56 | class CapturedIO(object): |
|
63 | 57 | """Simple object for containing captured stdout/err and rich display StringIO objects |
|
64 | 58 | |
|
65 | 59 | Each instance `c` has three attributes: |
|
66 | 60 | |
|
67 | 61 | - ``c.stdout`` : standard output as a string |
|
68 | 62 | - ``c.stderr`` : standard error as a string |
|
69 | 63 | - ``c.outputs``: a list of rich display outputs |
|
70 | 64 | |
|
71 | 65 | Additionally, there's a ``c.show()`` method which will print all of the |
|
72 | 66 | above in the same order, and can be invoked simply via ``c()``. |
|
73 | 67 | """ |
|
74 | 68 | |
|
75 | 69 | def __init__(self, stdout, stderr, outputs=None): |
|
76 | 70 | self._stdout = stdout |
|
77 | 71 | self._stderr = stderr |
|
78 | 72 | if outputs is None: |
|
79 | 73 | outputs = [] |
|
80 | 74 | self._outputs = outputs |
|
81 | 75 | |
|
82 | 76 | def __str__(self): |
|
83 | 77 | return self.stdout |
|
84 | 78 | |
|
85 | 79 | @property |
|
86 | 80 | def stdout(self): |
|
87 | 81 | "Captured standard output" |
|
88 | 82 | if not self._stdout: |
|
89 | 83 | return '' |
|
90 | 84 | return self._stdout.getvalue() |
|
91 | 85 | |
|
92 | 86 | @property |
|
93 | 87 | def stderr(self): |
|
94 | 88 | "Captured standard error" |
|
95 | 89 | if not self._stderr: |
|
96 | 90 | return '' |
|
97 | 91 | return self._stderr.getvalue() |
|
98 | 92 | |
|
99 | 93 | @property |
|
100 | 94 | def outputs(self): |
|
101 | 95 | """A list of the captured rich display outputs, if any. |
|
102 | 96 | |
|
103 | 97 | If you have a CapturedIO object ``c``, these can be displayed in IPython |
|
104 | 98 | using:: |
|
105 | 99 | |
|
106 | 100 | from IPython.display import display |
|
107 | 101 | for o in c.outputs: |
|
108 | 102 | display(o) |
|
109 | 103 | """ |
|
110 | 104 | return [ RichOutput(d, md) for d, md in self._outputs ] |
|
111 | 105 | |
|
112 | 106 | def show(self): |
|
113 | 107 | """write my output to sys.stdout/err as appropriate""" |
|
114 | 108 | sys.stdout.write(self.stdout) |
|
115 | 109 | sys.stderr.write(self.stderr) |
|
116 | 110 | sys.stdout.flush() |
|
117 | 111 | sys.stderr.flush() |
|
118 | 112 | for data, metadata in self._outputs: |
|
119 | 113 | RichOutput(data, metadata).display() |
|
120 | 114 | |
|
121 | 115 | __call__ = show |
|
122 | 116 | |
|
123 | 117 | |
|
124 | 118 | class capture_output(object): |
|
125 | 119 | """context manager for capturing stdout/err""" |
|
126 | 120 | stdout = True |
|
127 | 121 | stderr = True |
|
128 | 122 | display = True |
|
129 | 123 | |
|
130 | 124 | def __init__(self, stdout=True, stderr=True, display=True): |
|
131 | 125 | self.stdout = stdout |
|
132 | 126 | self.stderr = stderr |
|
133 | 127 | self.display = display |
|
134 | 128 | self.shell = None |
|
135 | 129 | |
|
136 | 130 | def __enter__(self): |
|
137 | 131 | from IPython.core.getipython import get_ipython |
|
138 | 132 | from IPython.core.displaypub import CapturingDisplayPublisher |
|
139 | 133 | from IPython.core.displayhook import CapturingDisplayHook |
|
140 | 134 | |
|
141 | 135 | self.sys_stdout = sys.stdout |
|
142 | 136 | self.sys_stderr = sys.stderr |
|
143 | 137 | |
|
144 | 138 | if self.display: |
|
145 | 139 | self.shell = get_ipython() |
|
146 | 140 | if self.shell is None: |
|
147 | 141 | self.save_display_pub = None |
|
148 | 142 | self.display = False |
|
149 | 143 | |
|
150 | 144 | stdout = stderr = outputs = None |
|
151 | 145 | if self.stdout: |
|
152 | 146 | stdout = sys.stdout = StringIO() |
|
153 | 147 | if self.stderr: |
|
154 | 148 | stderr = sys.stderr = StringIO() |
|
155 | 149 | if self.display: |
|
156 | 150 | self.save_display_pub = self.shell.display_pub |
|
157 | 151 | self.shell.display_pub = CapturingDisplayPublisher() |
|
158 | 152 | outputs = self.shell.display_pub.outputs |
|
159 | 153 | self.save_display_hook = sys.displayhook |
|
160 | 154 | sys.displayhook = CapturingDisplayHook(shell=self.shell, |
|
161 | 155 | outputs=outputs) |
|
162 | 156 | |
|
163 | 157 | return CapturedIO(stdout, stderr, outputs) |
|
164 | 158 | |
|
165 | 159 | def __exit__(self, exc_type, exc_value, traceback): |
|
166 | 160 | sys.stdout = self.sys_stdout |
|
167 | 161 | sys.stderr = self.sys_stderr |
|
168 | 162 | if self.display and self.shell: |
|
169 | 163 | self.shell.display_pub = self.save_display_pub |
|
170 | 164 | sys.displayhook = self.save_display_hook |
|
171 | 165 | |
|
172 | 166 |
General Comments 0
You need to be logged in to leave comments.
Login now