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