##// END OF EJS Templates
Refactor Inspector._get_info to make it easier to subclass and modify behavior....
Jason Grout -
Show More
@@ -552,56 +552,46 b' class Inspector(Colorable):'
552
552
553
553
554 def format_mime(self, bundle):
554 def format_mime(self, bundle):
555
555 """Format a mimebundle being created by _make_info_unformatted into a real mimebundle"""
556 # First, format the field names and values for the text/plain field
556 text_plain = bundle['text/plain']
557 text_plain = bundle['text/plain']
558 if isinstance(text_plain, (list, tuple)):
559 text = ''
560 heads, bodies = list(zip(*text_plain))
561 _len = max(len(h) for h in heads)
557
562
558 text = ''
563 for head, body in zip(heads, bodies):
559 heads, bodies = list(zip(*text_plain))
564 body = body.strip('\n')
560 _len = max(len(h) for h in heads)
565 delim = '\n' if '\n' in body else ' '
566 text += self.__head(head+':') + (_len - len(head))*' ' +delim + body +'\n'
561
567
562 for head, body in zip(heads, bodies):
568 bundle['text/plain'] = text
563 body = body.strip('\n')
564 delim = '\n' if '\n' in body else ' '
565 text += self.__head(head+':') + (_len - len(head))*' ' +delim + body +'\n'
566
569
567 bundle['text/plain'] = text
570 # Next format the text/html value by joining strings if it is a list of strings
571 if isinstance(bundle['text/html'], (list, tuple)):
572 bundle['text/html'] = '\n'.join(bundle['text/html'])
568 return bundle
573 return bundle
569
574
570 def _get_info(
575 def _append_info_field(self, bundle, title:str, key:str, info, omit_sections, formatter):
571 self, obj, oname="", formatter=None, info=None, detail_level=0, omit_sections=()
576 """Append an info value to the unformatted mimebundle being constructed by _make_info_unformatted"""
572 ):
577 if title in omit_sections or key in omit_sections:
573 """Retrieve an info dict and format it.
578 return
574
579 field = info[key]
575 Parameters
580 if field is not None:
576 ----------
581 formatted_field = self._mime_format(field, formatter)
577 obj : any
582 bundle['text/plain'].append((title, formatted_field['text/plain']))
578 Object to inspect and return info from
583 bundle['text/html'] += '<h1>' + title + '</h1>\n' + formatted_field['text/html']
579 oname : str (default: ''):
584
580 Name of the variable pointing to `obj`.
585 def _make_info_unformatted(self, info, formatter, detail_level, omit_sections):
581 formatter : callable
586 """Assemble the mimebundle as unformatted lists of information"""
582 info
587 bundle = {
583 already computed information
584 detail_level : integer
585 Granularity of detail level, if set to 1, give more information.
586 omit_sections : container[str]
587 Titles or keys to omit from output (can be set, tuple, etc., anything supporting `in`)
588 """
589
590 info = self.info(obj, oname=oname, info=info, detail_level=detail_level)
591
592 _mime = {
593 'text/plain': [],
588 'text/plain': [],
594 'text/html': '',
589 'text/html': [],
595 }
590 }
596
591
592 # A convenience function to simplify calls below
597 def append_field(bundle, title:str, key:str, formatter=None):
593 def append_field(bundle, title:str, key:str, formatter=None):
598 if title in omit_sections or key in omit_sections:
594 self._append_info_field(bundle, title=title, key=key, info=info, omit_sections=omit_sections, formatter=formatter)
599 return
600 field = info[key]
601 if field is not None:
602 formatted_field = self._mime_format(field, formatter)
603 bundle['text/plain'].append((title, formatted_field['text/plain']))
604 bundle['text/html'] += '<h1>' + title + '</h1>\n' + formatted_field['text/html'] + '\n'
605
595
606 def code_formatter(text):
596 def code_formatter(text):
607 return {
597 return {
@@ -610,56 +600,79 b' class Inspector(Colorable):'
610 }
600 }
611
601
612 if info['isalias']:
602 if info['isalias']:
613 append_field(_mime, 'Repr', 'string_form')
603 append_field(bundle, 'Repr', 'string_form')
614
604
615 elif info['ismagic']:
605 elif info['ismagic']:
616 if detail_level > 0:
606 if detail_level > 0:
617 append_field(_mime, 'Source', 'source', code_formatter)
607 append_field(bundle, 'Source', 'source', code_formatter)
618 else:
608 else:
619 append_field(_mime, 'Docstring', 'docstring', formatter)
609 append_field(bundle, 'Docstring', 'docstring', formatter)
620 append_field(_mime, 'File', 'file')
610 append_field(bundle, 'File', 'file')
621
611
622 elif info['isclass'] or is_simple_callable(obj):
612 elif info['isclass'] or is_simple_callable(obj):
623 # Functions, methods, classes
613 # Functions, methods, classes
624 append_field(_mime, 'Signature', 'definition', code_formatter)
614 append_field(bundle, 'Signature', 'definition', code_formatter)
625 append_field(_mime, 'Init signature', 'init_definition', code_formatter)
615 append_field(bundle, 'Init signature', 'init_definition', code_formatter)
626 append_field(_mime, 'Docstring', 'docstring', formatter)
616 append_field(bundle, 'Docstring', 'docstring', formatter)
627 if detail_level > 0 and info['source']:
617 if detail_level > 0 and info['source']:
628 append_field(_mime, 'Source', 'source', code_formatter)
618 append_field(bundle, 'Source', 'source', code_formatter)
629 else:
619 else:
630 append_field(_mime, 'Init docstring', 'init_docstring', formatter)
620 append_field(bundle, 'Init docstring', 'init_docstring', formatter)
631
621
632 append_field(_mime, 'File', 'file')
622 append_field(bundle, 'File', 'file')
633 append_field(_mime, 'Type', 'type_name')
623 append_field(bundle, 'Type', 'type_name')
634 append_field(_mime, 'Subclasses', 'subclasses')
624 append_field(bundle, 'Subclasses', 'subclasses')
635
625
636 else:
626 else:
637 # General Python objects
627 # General Python objects
638 append_field(_mime, 'Signature', 'definition', code_formatter)
628 append_field(bundle, 'Signature', 'definition', code_formatter)
639 append_field(_mime, 'Call signature', 'call_def', code_formatter)
629 append_field(bundle, 'Call signature', 'call_def', code_formatter)
640 append_field(_mime, 'Type', 'type_name')
630 append_field(bundle, 'Type', 'type_name')
641 append_field(_mime, 'String form', 'string_form')
631 append_field(bundle, 'String form', 'string_form')
642
632
643 # Namespace
633 # Namespace
644 if info['namespace'] != 'Interactive':
634 if info['namespace'] != 'Interactive':
645 append_field(_mime, 'Namespace', 'namespace')
635 append_field(bundle, 'Namespace', 'namespace')
646
636
647 append_field(_mime, 'Length', 'length')
637 append_field(bundle, 'Length', 'length')
648 append_field(_mime, 'File', 'file')
638 append_field(bundle, 'File', 'file')
649
639
650 # Source or docstring, depending on detail level and whether
640 # Source or docstring, depending on detail level and whether
651 # source found.
641 # source found.
652 if detail_level > 0 and info['source']:
642 if detail_level > 0 and info['source']:
653 append_field(_mime, 'Source', 'source', code_formatter)
643 append_field(bundle, 'Source', 'source', code_formatter)
654 else:
644 else:
655 append_field(_mime, 'Docstring', 'docstring', formatter)
645 append_field(bundle, 'Docstring', 'docstring', formatter)
646
647 append_field(bundle, 'Class docstring', 'class_docstring', formatter)
648 append_field(bundle, 'Init docstring', 'init_docstring', formatter)
649 append_field(bundle, 'Call docstring', 'call_docstring', formatter)
650 return bundle
656
651
657 append_field(_mime, 'Class docstring', 'class_docstring', formatter)
658 append_field(_mime, 'Init docstring', 'init_docstring', formatter)
659 append_field(_mime, 'Call docstring', 'call_docstring', formatter)
660
652
653 def _get_info(
654 self, obj, oname="", formatter=None, info=None, detail_level=0, omit_sections=()
655 ):
656 """Retrieve an info dict and format it.
661
657
662 return self.format_mime(_mime)
658 Parameters
659 ----------
660 obj : any
661 Object to inspect and return info from
662 oname : str (default: ''):
663 Name of the variable pointing to `obj`.
664 formatter : callable
665 info
666 already computed information
667 detail_level : integer
668 Granularity of detail level, if set to 1, give more information.
669 omit_sections : container[str]
670 Titles or keys to omit from output (can be set, tuple, etc., anything supporting `in`)
671 """
672
673 info = self.info(obj, oname=oname, info=info, detail_level=detail_level)
674 bundle = self._make_info_unformatted(info, formatter, detail_level=detail_level, omit_sections=omit_sections)
675 return self.format_mime(bundle)
663
676
664 def pinfo(
677 def pinfo(
665 self,
678 self,
General Comments 0
You need to be logged in to leave comments. Login now