##// 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 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 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 = ''
559 heads, bodies = list(zip(*text_plain))
560 _len = max(len(h) for h in heads)
563 for head, body in zip(heads, bodies):
564 body = body.strip('\n')
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):
563 body = body.strip('\n')
564 delim = '\n' if '\n' in body else ' '
565 text += self.__head(head+':') + (_len - len(head))*' ' +delim + body +'\n'
568 bundle['text/plain'] = text
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 573 return bundle
569 574
570 def _get_info(
571 self, obj, oname="", formatter=None, info=None, detail_level=0, omit_sections=()
572 ):
573 """Retrieve an info dict and format it.
574
575 Parameters
576 ----------
577 obj : any
578 Object to inspect and return info from
579 oname : str (default: ''):
580 Name of the variable pointing to `obj`.
581 formatter : callable
582 info
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 = {
575 def _append_info_field(self, bundle, title:str, key:str, info, omit_sections, formatter):
576 """Append an info value to the unformatted mimebundle being constructed by _make_info_unformatted"""
577 if title in omit_sections or key in omit_sections:
578 return
579 field = info[key]
580 if field is not None:
581 formatted_field = self._mime_format(field, formatter)
582 bundle['text/plain'].append((title, formatted_field['text/plain']))
583 bundle['text/html'] += '<h1>' + title + '</h1>\n' + formatted_field['text/html']
584
585 def _make_info_unformatted(self, info, formatter, detail_level, omit_sections):
586 """Assemble the mimebundle as unformatted lists of information"""
587 bundle = {
593 588 'text/plain': [],
594 'text/html': '',
589 'text/html': [],
595 590 }
596 591
592 # A convenience function to simplify calls below
597 593 def append_field(bundle, title:str, key:str, formatter=None):
598 if title in omit_sections or key in omit_sections:
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'
594 self._append_info_field(bundle, title=title, key=key, info=info, omit_sections=omit_sections, formatter=formatter)
605 595
606 596 def code_formatter(text):
607 597 return {
@@ -610,56 +600,79 b' class Inspector(Colorable):'
610 600 }
611 601
612 602 if info['isalias']:
613 append_field(_mime, 'Repr', 'string_form')
603 append_field(bundle, 'Repr', 'string_form')
614 604
615 605 elif info['ismagic']:
616 606 if detail_level > 0:
617 append_field(_mime, 'Source', 'source', code_formatter)
607 append_field(bundle, 'Source', 'source', code_formatter)
618 608 else:
619 append_field(_mime, 'Docstring', 'docstring', formatter)
620 append_field(_mime, 'File', 'file')
609 append_field(bundle, 'Docstring', 'docstring', formatter)
610 append_field(bundle, 'File', 'file')
621 611
622 612 elif info['isclass'] or is_simple_callable(obj):
623 613 # Functions, methods, classes
624 append_field(_mime, 'Signature', 'definition', code_formatter)
625 append_field(_mime, 'Init signature', 'init_definition', code_formatter)
626 append_field(_mime, 'Docstring', 'docstring', formatter)
614 append_field(bundle, 'Signature', 'definition', code_formatter)
615 append_field(bundle, 'Init signature', 'init_definition', code_formatter)
616 append_field(bundle, 'Docstring', 'docstring', formatter)
627 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 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')
633 append_field(_mime, 'Type', 'type_name')
634 append_field(_mime, 'Subclasses', 'subclasses')
622 append_field(bundle, 'File', 'file')
623 append_field(bundle, 'Type', 'type_name')
624 append_field(bundle, 'Subclasses', 'subclasses')
635 625
636 626 else:
637 627 # General Python objects
638 append_field(_mime, 'Signature', 'definition', code_formatter)
639 append_field(_mime, 'Call signature', 'call_def', code_formatter)
640 append_field(_mime, 'Type', 'type_name')
641 append_field(_mime, 'String form', 'string_form')
628 append_field(bundle, 'Signature', 'definition', code_formatter)
629 append_field(bundle, 'Call signature', 'call_def', code_formatter)
630 append_field(bundle, 'Type', 'type_name')
631 append_field(bundle, 'String form', 'string_form')
642 632
643 633 # Namespace
644 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')
648 append_field(_mime, 'File', 'file')
637 append_field(bundle, 'Length', 'length')
638 append_field(bundle, 'File', 'file')
649 639
650 640 # Source or docstring, depending on detail level and whether
651 641 # source found.
652 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 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 677 def pinfo(
665 678 self,
General Comments 0
You need to be logged in to leave comments. Login now