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

Method format2

IPython/utils/PyColorize.py:207–281  ·  view source on GitHub ↗

Parse and send the colored source. If out and scheme are not specified, the defaults (given to constructor) are used. out should be a file-type object. Optionally, out can be given as the string 'str' and the parser will automatically return the output in a

(self, raw, out = None)

Source from the content-addressed store, hash-verified

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':
234 error = False
235 self.out.write(raw)
236 if string_output:
237 return raw, error
238 return None, error
239
240 # local shorthands
241 colors = self.color_table[self.style].colors
242 self.colors = colors # put in object so __call__ sees it
243
244 # Remove trailing whitespace and normalize tabs
245 self.raw = raw.expandtabs().rstrip()
246
247 # store line offsets in self.lines
248 self.lines = [0, 0]
249 pos = 0
250 raw_find = self.raw.find
251 lines_append = self.lines.append
252 while True:
253 pos = raw_find('\n', pos) + 1
254 if not pos:
255 break
256 lines_append(pos)
257 lines_append(len(self.raw))
258
259 # parse the source and write it
260 self.pos = 0
261 text = StringIO(self.raw)
262
263 error = False
264 try:

Callers 2

formatMethod · 0.95
__format_lineMethod · 0.80

Calls 2

generate_tokensFunction · 0.85
writeMethod · 0.45

Tested by

no test coverage detected