##// END OF EJS Templates
Add hooking ability for producing help mime bundle. (#14300)...
M Bussonnier -
r28592:6e5ec4db merge
parent child Browse files
Show More
@@ -1,4 +1,3 b''
1 # -*- coding: utf-8 -*-
2 1 """Tools for inspecting Python objects.
3 2
4 3 Uses syntax highlighting for presenting the various information elements.
@@ -22,7 +21,6 b' import inspect'
22 21 import io as stdlib_io
23 22 import linecache
24 23 import os
25 import sys
26 24 import types
27 25 import warnings
28 26
@@ -30,6 +28,8 b' from typing import Any, Optional, Dict, Union, List, Tuple'
30 28
31 29 from typing import TypeAlias
32 30
31 import traitlets
32
33 33 # IPython's own
34 34 from IPython.core import page
35 35 from IPython.lib.pretty import pretty
@@ -41,7 +41,7 b' from IPython.utils.path import compress_user'
41 41 from IPython.utils.text import indent
42 42 from IPython.utils.wildcard import list_namespace
43 43 from IPython.utils.wildcard import typestr2type
44 from IPython.utils.coloransi import TermColors, ColorScheme, ColorSchemeTable
44 from IPython.utils.coloransi import TermColors
45 45 from IPython.utils.py3compat import cast_unicode
46 46 from IPython.utils.colorable import Colorable
47 47 from IPython.utils.decorators import undoc
@@ -145,7 +145,7 b' def get_encoding(obj):'
145 145 # getsourcelines returns lineno with 1-offset and page() uses
146 146 # 0-offset, so we must adjust.
147 147 with stdlib_io.open(ofile, 'rb') as buffer: # Tweaked to use io.open for Python 2
148 encoding, lines = openpy.detect_encoding(buffer.readline)
148 encoding, _lines = openpy.detect_encoding(buffer.readline)
149 149 return encoding
150 150
151 151 def getdoc(obj) -> Union[str,None]:
@@ -328,7 +328,7 b' def _get_wrapped(obj):'
328 328 return orig_obj
329 329 return obj
330 330
331 def find_file(obj) -> str:
331 def find_file(obj) -> Optional[str]:
332 332 """Find the absolute path to the file where an object was defined.
333 333
334 334 This is essentially a robust wrapper around `inspect.getabsfile`.
@@ -346,7 +346,7 b' def find_file(obj) -> str:'
346 346 """
347 347 obj = _get_wrapped(obj)
348 348
349 fname = None
349 fname: Optional[str] = None
350 350 try:
351 351 fname = inspect.getabsfile(obj)
352 352 except TypeError:
@@ -360,7 +360,7 b' def find_file(obj) -> str:'
360 360 except OSError:
361 361 pass
362 362
363 return cast_unicode(fname)
363 return fname
364 364
365 365
366 366 def find_source_lines(obj):
@@ -396,11 +396,20 b' def find_source_lines(obj):'
396 396
397 397 class Inspector(Colorable):
398 398
399 def __init__(self, color_table=InspectColors,
399 mime_hooks = traitlets.Dict(
400 config=True,
401 help="dictionary of mime to callable to add informations into help mimebundle dict",
402 ).tag(config=True)
403
404 def __init__(
405 self,
406 color_table=InspectColors,
400 407 code_color_table=PyColorize.ANSICodeColors,
401 408 scheme=None,
402 409 str_detail_level=0,
403 parent=None, config=None):
410 parent=None,
411 config=None,
412 ):
404 413 super(Inspector, self).__init__(parent=parent, config=config)
405 414 self.color_table = color_table
406 415 self.parser = PyColorize.Parser(out='str', parent=self, style=scheme)
@@ -759,6 +768,10 b' class Inspector(Colorable):'
759 768 detail_level=detail_level,
760 769 omit_sections=omit_sections,
761 770 )
771 for key, hook in self.mime_hooks.items():
772 res = hook(obj, info)
773 if res is not None:
774 bundle[key] = res
762 775 return self.format_mime(bundle)
763 776
764 777 def pinfo(
General Comments 0
You need to be logged in to leave comments. Login now