aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/ipython/py2/IPython/utils/capture.py
blob: bb241b0fadefd819d292adcf7dfb1e618f6797bc (plain) (blame)
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
# encoding: utf-8 
"""IO capturing utilities.""" 
 
# Copyright (c) IPython Development Team. 
# Distributed under the terms of the Modified BSD License. 
 
from __future__ import print_function, absolute_import 
 
import sys 
 
from IPython.utils.py3compat import PY3 
 
if PY3: 
    from io import StringIO 
else: 
    from StringIO import StringIO 
 
#----------------------------------------------------------------------------- 
# Classes and functions 
#----------------------------------------------------------------------------- 
 
 
class RichOutput(object): 
    def __init__(self, data=None, metadata=None, transient=None, update=False):
        self.data = data or {} 
        self.metadata = metadata or {} 
        self.transient = transient or {}
        self.update = update

    def display(self): 
        from IPython.display import publish_display_data 
        publish_display_data(data=self.data, metadata=self.metadata,
                             transient=self.transient, update=self.update)

    def _repr_mime_(self, mime): 
        if mime not in self.data: 
            return 
        data = self.data[mime] 
        if mime in self.metadata: 
            return data, self.metadata[mime] 
        else: 
            return data 
             
    def _repr_html_(self): 
        return self._repr_mime_("text/html") 

    def _repr_latex_(self): 
        return self._repr_mime_("text/latex") 

    def _repr_json_(self): 
        return self._repr_mime_("application/json") 

    def _repr_javascript_(self): 
        return self._repr_mime_("application/javascript") 

    def _repr_png_(self): 
        return self._repr_mime_("image/png") 

    def _repr_jpeg_(self): 
        return self._repr_mime_("image/jpeg") 

    def _repr_svg_(self): 
        return self._repr_mime_("image/svg+xml") 
 
 
class CapturedIO(object): 
    """Simple object for containing captured stdout/err and rich display StringIO objects 
 
    Each instance `c` has three attributes: 
 
    - ``c.stdout`` : standard output as a string 
    - ``c.stderr`` : standard error as a string 
    - ``c.outputs``: a list of rich display outputs 
 
    Additionally, there's a ``c.show()`` method which will print all of the 
    above in the same order, and can be invoked simply via ``c()``. 
    """ 

    def __init__(self, stdout, stderr, outputs=None): 
        self._stdout = stdout 
        self._stderr = stderr 
        if outputs is None: 
            outputs = [] 
        self._outputs = outputs 

    def __str__(self): 
        return self.stdout 

    @property 
    def stdout(self): 
        "Captured standard output" 
        if not self._stdout: 
            return '' 
        return self._stdout.getvalue() 

    @property 
    def stderr(self): 
        "Captured standard error" 
        if not self._stderr: 
            return '' 
        return self._stderr.getvalue() 

    @property 
    def outputs(self): 
        """A list of the captured rich display outputs, if any. 

        If you have a CapturedIO object ``c``, these can be displayed in IPython 
        using:: 
 
            from IPython.display import display 
            for o in c.outputs: 
                display(o) 
        """ 
        return [ RichOutput(**kargs) for kargs in self._outputs ]

    def show(self): 
        """write my output to sys.stdout/err as appropriate""" 
        sys.stdout.write(self.stdout) 
        sys.stderr.write(self.stderr) 
        sys.stdout.flush() 
        sys.stderr.flush() 
        for kargs in self._outputs:
            RichOutput(**kargs).display()

    __call__ = show 
 
 
class capture_output(object): 
    """context manager for capturing stdout/err""" 
    stdout = True 
    stderr = True 
    display = True 

    def __init__(self, stdout=True, stderr=True, display=True): 
        self.stdout = stdout 
        self.stderr = stderr 
        self.display = display 
        self.shell = None 

    def __enter__(self): 
        from IPython.core.getipython import get_ipython 
        from IPython.core.displaypub import CapturingDisplayPublisher 
        from IPython.core.displayhook import CapturingDisplayHook

        self.sys_stdout = sys.stdout 
        self.sys_stderr = sys.stderr 

        if self.display: 
            self.shell = get_ipython() 
            if self.shell is None: 
                self.save_display_pub = None 
                self.display = False 

        stdout = stderr = outputs = None 
        if self.stdout: 
            stdout = sys.stdout = StringIO() 
        if self.stderr: 
            stderr = sys.stderr = StringIO() 
        if self.display: 
            self.save_display_pub = self.shell.display_pub 
            self.shell.display_pub = CapturingDisplayPublisher() 
            outputs = self.shell.display_pub.outputs 
            self.save_display_hook = sys.displayhook
            sys.displayhook = CapturingDisplayHook(shell=self.shell,
                                                   outputs=outputs)

        return CapturedIO(stdout, stderr, outputs) 

    def __exit__(self, exc_type, exc_value, traceback): 
        sys.stdout = self.sys_stdout 
        sys.stderr = self.sys_stderr 
        if self.display and self.shell: 
            self.shell.display_pub = self.save_display_pub 
            sys.displayhook = self.save_display_hook