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