MCPcopy Create free account
hub / github.com/ipython/ipython / Parser

Class Parser

IPython/utils/PyColorize.py:176–331  ·  view source on GitHub ↗

Format colored Python source.

Source from the content-addressed store, hash-verified

174Undefined = object()
175
176class Parser(Colorable):
177 """ Format colored Python source.
178 """
179
180 def __init__(self, color_table=None, out = sys.stdout, parent=None, style=None):
181 """ Create a parser with a specified color table and output channel.
182
183 Call format() to process code.
184 """
185
186 super(Parser, self).__init__(parent=parent)
187
188 self.color_table = color_table if color_table else ANSICodeColors
189 self.out = out
190 self.pos = None
191 self.lines = None
192 self.raw = None
193 if not style:
194 self.style = self.default_style
195 else:
196 self.style = style
197
198
199 def format(self, raw, out=None, scheme=Undefined):
200 import warnings
201 if scheme is not Undefined:
202 warnings.warn('The `scheme` argument of IPython.utils.PyColorize:Parser.format is deprecated since IPython 6.0.'
203 'It will have no effect. Set the parser `style` directly.',
204 stacklevel=2)
205 return self.format2(raw, out)[0]
206
207 def format2(self, raw, out = None):
208 """ Parse and send the colored source.
209
210 If out and scheme are not specified, the defaults (given to
211 constructor) are used.
212
213 out should be a file-type object. Optionally, out can be given as the
214 string 'str' and the parser will automatically return the output in a
215 string."""
216
217 string_output = 0
218 if out == 'str' or self.out == 'str' or \
219 isinstance(self.out, StringIO):
220 # XXX - I don't really like this state handling logic, but at this
221 # point I don't want to make major changes, so adding the
222 # isinstance() check is the simplest I can do to ensure correct
223 # behavior.
224 out_old = self.out
225 self.out = StringIO()
226 string_output = 1
227 elif out is not None:
228 self.out = out
229 else:
230 raise ValueError('`out` or `self.out` should be file-like or the value `"str"`')
231
232 # Fast return of the unmodified input for NoColor scheme
233 if self.style == 'NoColor':

Callers 3

test_unicode_colorizeFunction · 0.90
test_parse_sampleFunction · 0.90
test_parse_errorFunction · 0.90

Calls

no outgoing calls

Tested by 3

test_unicode_colorizeFunction · 0.72
test_parse_sampleFunction · 0.72
test_parse_errorFunction · 0.72