Exemple #1
0
    def pinfo(self, obj, oname='', formatter=None, info=None, detail_level=0):
        """Show detailed information about an object.

        Optional arguments:
        
        - oname: name of the variable pointing to the object.

        - formatter: special formatter for docstrings (see pdoc)

        - info: a structure with some information fields which may have been
        precomputed already.

        - detail_level: if set to 1, more information is given.
        """

        obj_type = type(obj)

        header = self.__head
        if info is None:
            ismagic = 0
            isalias = 0
            ospace = ''
        else:
            ismagic = info.ismagic
            isalias = info.isalias
            ospace = info.namespace
        # Get docstring, special-casing aliases:
        if isalias:
            if not callable(obj):
                try:
                    ds = "Alias to the system command:\n  %s" % obj[1]
                except:
                    ds = "Alias: " + str(obj)
            else:
                ds = "Alias to " + str(obj)
                if obj.__doc__:
                    ds += "\nDocstring:\n" + obj.__doc__
        else:
            ds = getdoc(obj)
            if ds is None:
                ds = '<no docstring>'
        if formatter is not None:
            ds = formatter(ds)

        # store output in a list which gets joined with \n at the end.
        out = myStringIO()

        string_max = 200  # max size of strings to show (snipped if longer)
        shalf = int((string_max - 5) / 2)

        if ismagic:
            obj_type_name = 'Magic function'
        elif isalias:
            obj_type_name = 'System alias'
        else:
            obj_type_name = obj_type.__name__
        out.writeln(header('Type:\t\t') + obj_type_name)

        try:
            bclass = obj.__class__
            out.writeln(header('Base Class:\t') + str(bclass))
        except:
            pass

        # String form, but snip if too long in ? form (full in ??)
        if detail_level >= self.str_detail_level:
            try:
                ostr = str(obj)
                str_head = 'String Form:'
                if not detail_level and len(ostr) > string_max:
                    ostr = ostr[:shalf] + ' <...> ' + ostr[-shalf:]
                    ostr = ("\n" + " " * len(str_head.expandtabs())).\
                           join(map(string.strip,ostr.split("\n")))
                if ostr.find('\n') > -1:
                    # Print multi-line strings starting at the next line.
                    str_sep = '\n'
                else:
                    str_sep = '\t'
                out.writeln("%s%s%s" % (header(str_head), str_sep, ostr))
            except:
                pass

        if ospace:
            out.writeln(header('Namespace:\t') + ospace)

        # Length (for strings and lists)
        try:
            length = str(len(obj))
            out.writeln(header('Length:\t\t') + length)
        except:
            pass

        # Filename where object was defined
        binary_file = False
        try:
            fname = inspect.getabsfile(obj)
            if fname.endswith('<string>'):
                fname = 'Dynamically generated function. No source code available.'
            if (fname.endswith('.so') or fname.endswith('.dll')):
                binary_file = True
            out.writeln(header('File:\t\t') + fname)
        except:
            # if anything goes wrong, we don't want to show source, so it's as
            # if the file was binary
            binary_file = True

        # reconstruct the function definition and print it:
        defln = self.__getdef(obj, oname)
        if defln:
            out.write(header('Definition:\t') + self.format(defln))

        # Docstrings only in detail 0 mode, since source contains them (we
        # avoid repetitions).  If source fails, we add them back, see below.
        if ds and detail_level == 0:
            out.writeln(header('Docstring:\n') + indent(ds))

        # Original source code for any callable
        if detail_level:
            # Flush the source cache because inspect can return out-of-date
            # source
            linecache.checkcache()
            source_success = False
            try:
                source = self.format(getsource(obj, binary_file))
                if source:
                    out.write(header('Source:\n') + source.rstrip())
                    source_success = True
            except Exception, msg:
                pass

            if ds and not source_success:
                out.writeln(
                    header('Docstring [source file open failed]:\n') +
                    indent(ds))
Exemple #2
0
def _pinfo(self,obj,oname='',formatter=None,info=None,detail_level=0):
    """Show detailed information about an object.

    Optional arguments:

    - oname: name of the variable pointing to the object.

    - formatter: special formatter for docstrings (see pdoc)

    - info: a structure with some information fields which may have been
    precomputed already.

    - detail_level: if set to 1, more information is given.

    This is a copy of the method found in IPython 0.8.1 and is duc-taped into
    IPython.OInspect.
    """
    #Some extra imports
    import types
    import linecache
    from IPython.OInspect import myStringIO, getsource
    from IPython.genutils import indent, page
    #We need access to private stuff, blergh
    self.__head = getattr(self, '_%s__head' % self.__class__.__name__)
    self.__getdef = getattr(self, '_%s__getdef' % self.__class__.__name__)


    obj_type = type(obj)

    header = self.__head
    if info is None:
        ismagic = 0
        isalias = 0
        ospace = ''
    else:
        ismagic = info.ismagic
        isalias = info.isalias
        ospace = info.namespace
    # Get docstring, special-casing aliases:
    if isalias:
        ds = "Alias to the system command:\n  %s" % obj[1]
    else:
        ds = _getdoc(obj)
        if ds is None:
            ds = '<no docstring>'
    if formatter is not None:
        ds = formatter(ds)

    ds = _format_docstring(ds)

    # store output in a list which gets joined with \n at the end.
    out = myStringIO()

    string_max = 200 # max size of strings to show (snipped if longer)
    shalf = int((string_max -5)/2)

    # reconstruct the function definition and print it:
    defln = self.__getdef(obj,oname)
    if defln:
        out.write(header('Definition:\t')+self.format(defln))

    # Docstrings only in detail 0 mode, since source contains them (we
    # avoid repetitions).  If source fails, we add them back, see below.
    if ds and detail_level == 0:
        out.writeln(header('Documentation:\n') + indent(ds))

    # Filename where object was defined
    binary_file = False
    try:
        fname = inspect.getabsfile(obj)
        if fname.endswith('<string>'):
            fname = 'Dynamically generated function. No source code available.'
        if (fname.endswith('.so') or fname.endswith('.dll') or
            not os.path.isfile(fname)):
            binary_file = True
    except:
        # if anything goes wrong, we don't want to show source, so it's as
        # if the file was binary
        binary_file = True

    # Original source code for any callable
    if detail_level:
        # Flush the source cache because inspect can return out-of-date source
        linecache.checkcache()
        source_success = False
        try:
            source = self.format(getsource(obj,binary_file))
            if source:
                out.write(header('Source:\n')+source.rstrip())
                source_success = True
        except Exception, msg:
            pass

        if ds and not source_success:
            out.writeln(header('Documentation [source file open failed]:\n')
                        + indent(ds))
Exemple #3
0
class Inspector:
    def __init__(self,
                 color_table,
                 code_color_table,
                 scheme,
                 str_detail_level=0):
        self.color_table = color_table
        self.parser = PyColorize.Parser(code_color_table, out='str')
        self.format = self.parser.format
        self.str_detail_level = str_detail_level
        self.set_active_scheme(scheme)

    def __getargspec(self, obj):
        """Get the names and default values of a function's arguments.

        A tuple of four things is returned: (args, varargs, varkw, defaults).
        'args' is a list of the argument names (it may contain nested lists).
        'varargs' and 'varkw' are the names of the * and ** arguments or None.
        'defaults' is an n-tuple of the default values of the last n arguments.

        Modified version of inspect.getargspec from the Python Standard
        Library."""

        if inspect.isfunction(obj):
            func_obj = obj
        elif inspect.ismethod(obj):
            func_obj = obj.im_func
        else:
            raise TypeError, 'arg is not a Python function'
        args, varargs, varkw = inspect.getargs(func_obj.func_code)
        return args, varargs, varkw, func_obj.func_defaults

    def __getdef(self, obj, oname=''):
        """Return the definition header for any callable object.

        If any exception is generated, None is returned instead and the
        exception is suppressed."""

        try:
            return oname + inspect.formatargspec(*self.__getargspec(obj))
        except:
            return None

    def __head(self, h):
        """Return a header string with proper colors."""
        return '%s%s%s' % (self.color_table.active_colors.header, h,
                           self.color_table.active_colors.normal)

    def set_active_scheme(self, scheme):
        self.color_table.set_active_scheme(scheme)
        self.parser.color_table.set_active_scheme(scheme)

    def noinfo(self, msg, oname):
        """Generic message when no information is found."""
        print 'No %s found' % msg,
        if oname:
            print 'for %s' % oname
        else:
            print

    def pdef(self, obj, oname=''):
        """Print the definition header for any callable object.

        If the object is a class, print the constructor information."""

        if not callable(obj):
            print 'Object is not callable.'
            return

        header = ''

        if inspect.isclass(obj):
            header = self.__head('Class constructor information:\n')
            obj = obj.__init__
        elif type(obj) is types.InstanceType:
            obj = obj.__call__

        output = self.__getdef(obj, oname)
        if output is None:
            self.noinfo('definition header', oname)
        else:
            print >> Term.cout, header, self.format(output),

    def pdoc(self, obj, oname='', formatter=None):
        """Print the docstring for any object.

        Optional:
        -formatter: a function to run the docstring through for specially
        formatted docstrings."""

        head = self.__head  # so that itpl can find it even if private
        ds = getdoc(obj)
        if formatter:
            ds = formatter(ds)
        if inspect.isclass(obj):
            init_ds = getdoc(obj.__init__)
            output = itpl('$head("Class Docstring:")\n'
                          '$indent(ds)\n'
                          '$head("Constructor Docstring"):\n'
                          '$indent(init_ds)')
        elif (type(obj) is types.InstanceType or isinstance(obj,object)) \
                 and hasattr(obj,'__call__'):
            call_ds = getdoc(obj.__call__)
            if call_ds:
                output = itpl('$head("Class Docstring:")\n$indent(ds)\n'
                              '$head("Calling Docstring:")\n$indent(call_ds)')
            else:
                output = ds
        else:
            output = ds
        if output is None:
            self.noinfo('documentation', oname)
            return
        page(output)

    def psource(self, obj, oname=''):
        """Print the source code for an object."""

        # Flush the source cache because inspect can return out-of-date source
        linecache.checkcache()
        try:
            src = getsource(obj)
        except:
            self.noinfo('source', oname)
        else:
            page(self.format(src))

    def pfile(self, obj, oname=''):
        """Show the whole file where an object was defined."""
        try:
            sourcelines, lineno = inspect.getsourcelines(obj)
        except:
            self.noinfo('file', oname)
        else:
            # run contents of file through pager starting at line
            # where the object is defined
            ofile = inspect.getabsfile(obj)

            if (ofile.endswith('.so') or ofile.endswith('.dll')):
                print 'File %r is binary, not printing.' % ofile
            elif not os.path.isfile(ofile):
                print 'File %r does not exist, not printing.' % ofile
            else:
                # Print only text files, not extension binaries.
                page(self.format(open(ofile).read()), lineno)
            #page(self.format(open(inspect.getabsfile(obj)).read()),lineno)

    def pinfo(self, obj, oname='', formatter=None, info=None, detail_level=0):
        """Show detailed information about an object.

        Optional arguments:
        
        - oname: name of the variable pointing to the object.

        - formatter: special formatter for docstrings (see pdoc)

        - info: a structure with some information fields which may have been
        precomputed already.

        - detail_level: if set to 1, more information is given.
        """

        obj_type = type(obj)

        header = self.__head
        if info is None:
            ismagic = 0
            isalias = 0
            ospace = ''
        else:
            ismagic = info.ismagic
            isalias = info.isalias
            ospace = info.namespace
        # Get docstring, special-casing aliases:
        if isalias:
            if not callable(obj):
                try:
                    ds = "Alias to the system command:\n  %s" % obj[1]
                except:
                    ds = "Alias: " + str(obj)
            else:
                ds = "Alias to " + str(obj)
                if obj.__doc__:
                    ds += "\nDocstring:\n" + obj.__doc__
        else:
            ds = getdoc(obj)
            if ds is None:
                ds = '<no docstring>'
        if formatter is not None:
            ds = formatter(ds)

        # store output in a list which gets joined with \n at the end.
        out = myStringIO()

        string_max = 200  # max size of strings to show (snipped if longer)
        shalf = int((string_max - 5) / 2)

        if ismagic:
            obj_type_name = 'Magic function'
        elif isalias:
            obj_type_name = 'System alias'
        else:
            obj_type_name = obj_type.__name__
        out.writeln(header('Type:\t\t') + obj_type_name)

        try:
            bclass = obj.__class__
            out.writeln(header('Base Class:\t') + str(bclass))
        except:
            pass

        # String form, but snip if too long in ? form (full in ??)
        if detail_level >= self.str_detail_level:
            try:
                ostr = str(obj)
                str_head = 'String Form:'
                if not detail_level and len(ostr) > string_max:
                    ostr = ostr[:shalf] + ' <...> ' + ostr[-shalf:]
                    ostr = ("\n" + " " * len(str_head.expandtabs())).\
                           join(map(string.strip,ostr.split("\n")))
                if ostr.find('\n') > -1:
                    # Print multi-line strings starting at the next line.
                    str_sep = '\n'
                else:
                    str_sep = '\t'
                out.writeln("%s%s%s" % (header(str_head), str_sep, ostr))
            except:
                pass

        if ospace:
            out.writeln(header('Namespace:\t') + ospace)

        # Length (for strings and lists)
        try:
            length = str(len(obj))
            out.writeln(header('Length:\t\t') + length)
        except:
            pass

        # Filename where object was defined
        binary_file = False
        try:
            fname = inspect.getabsfile(obj)
            if fname.endswith('<string>'):
                fname = 'Dynamically generated function. No source code available.'
            if (fname.endswith('.so') or fname.endswith('.dll')):
                binary_file = True
            out.writeln(header('File:\t\t') + fname)
        except:
            # if anything goes wrong, we don't want to show source, so it's as
            # if the file was binary
            binary_file = True

        # reconstruct the function definition and print it:
        defln = self.__getdef(obj, oname)
        if defln:
            out.write(header('Definition:\t') + self.format(defln))

        # Docstrings only in detail 0 mode, since source contains them (we
        # avoid repetitions).  If source fails, we add them back, see below.
        if ds and detail_level == 0:
            out.writeln(header('Docstring:\n') + indent(ds))

        # Original source code for any callable
        if detail_level:
            # Flush the source cache because inspect can return out-of-date
            # source
            linecache.checkcache()
            source_success = False
            try:
                source = self.format(getsource(obj, binary_file))
                if source:
                    out.write(header('Source:\n') + source.rstrip())
                    source_success = True
            except Exception, msg:
                pass

            if ds and not source_success:
                out.writeln(
                    header('Docstring [source file open failed]:\n') +
                    indent(ds))

        # Constructor docstring for classes
        if inspect.isclass(obj):
            # reconstruct the function definition and print it:
            try:
                obj_init = obj.__init__
            except AttributeError:
                init_def = init_ds = None
            else:
                init_def = self.__getdef(obj_init, oname)
                init_ds = getdoc(obj_init)
                # Skip Python's auto-generated docstrings
                if init_ds and \
                       init_ds.startswith('x.__init__(...) initializes'):
                    init_ds = None

            if init_def or init_ds:
                out.writeln(header('\nConstructor information:'))
                if init_def:
                    out.write(header('Definition:\t') + self.format(init_def))
                if init_ds:
                    out.writeln(header('Docstring:\n') + indent(init_ds))
        # and class docstring for instances:
        elif obj_type is types.InstanceType or \
                 isinstance(obj,object):

            # First, check whether the instance docstring is identical to the
            # class one, and print it separately if they don't coincide.  In
            # most cases they will, but it's nice to print all the info for
            # objects which use instance-customized docstrings.
            if ds:
                try:
                    cls = getattr(obj, '__class__')
                except:
                    class_ds = None
                else:
                    class_ds = getdoc(cls)
                # Skip Python's auto-generated docstrings
                if class_ds and \
                       (class_ds.startswith('function(code, globals[,') or \
                   class_ds.startswith('instancemethod(function, instance,') or \
                   class_ds.startswith('module(name[,') ):
                    class_ds = None
                if class_ds and ds != class_ds:
                    out.writeln(
                        header('Class Docstring:\n') + indent(class_ds))

            # Next, try to show constructor docstrings
            try:
                init_ds = getdoc(obj.__init__)
                # Skip Python's auto-generated docstrings
                if init_ds and \
                       init_ds.startswith('x.__init__(...) initializes'):
                    init_ds = None
            except AttributeError:
                init_ds = None
            if init_ds:
                out.writeln(
                    header('Constructor Docstring:\n') + indent(init_ds))

            # Call form docstring for callable instances
            if hasattr(obj, '__call__'):
                #out.writeln(header('Callable:\t')+'Yes')
                call_def = self.__getdef(obj.__call__, oname)
                #if call_def is None:
                #    out.writeln(header('Call def:\t')+
                #              'Calling definition not available.')
                if call_def is not None:
                    out.writeln(header('Call def:\t') + self.format(call_def))
                call_ds = getdoc(obj.__call__)
                # Skip Python's auto-generated docstrings
                if call_ds and call_ds.startswith(
                        'x.__call__(...) <==> x(...)'):
                    call_ds = None
                if call_ds:
                    out.writeln(header('Call docstring:\n') + indent(call_ds))

        # Finally send to printer/pager
        output = out.getvalue()
        if output:
            page(output)
Exemple #4
0
    if obj_type is types.ClassType:
        # reconstruct the function definition and print it:
        try:
            obj_init =  obj.__init__
        except AttributeError:
            init_def = init_ds = None
        else:
            init_def = self.__getdef(obj_init,oname)
            init_ds  = _getdoc(obj_init)

        if init_def or init_ds:
            out.writeln(header('\nConstructor information:'))
            if init_def:
                out.write(header('Definition:\t')+ self.format(init_def))
            if init_ds:
                out.writeln(header('Documentation:\n') + indent(init_ds))

    # Finally send to printer/pager
    output = out.getvalue()
    if output:
        page(output)
    # end pinfo


def page(strng,start=0,screen_lines=0,pager_cmd = None):
    '''Non-paging page implementation to provide a non-paging Q-Shell
    '''
    #This comes from IPython.genutils.page
    str_lines = strng.split(os.linesep)[start:]
    str_toprint = os.linesep.join(str_lines)
Exemple #5
0
    def pinfo(self, obj, oname='', formatter=None, info=None, detail_level=0):
        """Show detailed information about an object.

        Optional arguments:
        
        - oname: name of the variable pointing to the object.

        - formatter: special formatter for docstrings (see pdoc)

        - info: a structure with some information fields which may have been
        precomputed already.

        - detail_level: if set to 1, more information is given.
        """

        obj_type = type(obj)

        header = self.__head
        if info is None:
            ismagic = 0
            isalias = 0
            ospace = ''
        else:
            ismagic = info.ismagic
            isalias = info.isalias
            ospace = info.namespace
        # Get docstring, special-casing aliases:
        if isalias:
            ds = "Alias to the system command:\n  %s" % obj[1]
        else:
            ds = getdoc(obj)
        if formatter is not None:
            ds = formatter(ds)

        # store output in a list which gets joined with \n at the end.
        out = myStringIO()

        string_max = 200  # max size of strings to show (snipped if longer)
        shalf = int((string_max - 5) / 2)

        if ismagic:
            obj_type_name = 'Magic function'
        elif isalias:
            obj_type_name = 'System alias'
        else:
            obj_type_name = obj_type.__name__
        out.writeln(header('Type:\t\t') + obj_type_name)

        try:
            bclass = obj.__class__
            out.writeln(header('Base Class:\t') + str(bclass))
        except:
            pass

        # String form, but snip if too long in ? form (full in ??)
        try:
            ostr = str(obj)
            str_head = 'String Form:'
            if not detail_level and len(ostr) > string_max:
                ostr = ostr[:shalf] + ' <...> ' + ostr[-shalf:]
                ostr = ("\n" + " " * len(str_head.expandtabs())).\
                       join(map(string.strip,ostr.split("\n")))
            if ostr.find('\n') > -1:
                # Print multi-line strings starting at the next line.
                str_sep = '\n'
            else:
                str_sep = '\t'
            out.writeln("%s%s%s" % (header(str_head), str_sep, ostr))
        except:
            pass

        if ospace:
            out.writeln(header('Namespace:\t') + ospace)

        # Length (for strings and lists)
        try:
            length = str(len(obj))
            out.writeln(header('Length:\t\t') + length)
        except:
            pass

        # Filename where object was defined
        try:
            file = inspect.getabsfile(obj)
            if file.endswith('<string>'):
                file = 'Dynamically generated function. No source code available.'
            out.writeln(header('File:\t\t') + file)
        except:
            pass

        # reconstruct the function definition and print it:
        defln = self.__getdef(obj, oname)
        if defln:
            out.write(header('Definition:\t') + self.format(defln))

        # Docstrings only in detail 0 mode, since source contains them (we
        # avoid repetitions).  If source fails, we add them back, see below.
        if ds and detail_level == 0:
            out.writeln(header('Docstring:\n') + indent(ds))

        # Original source code for any callable
        if detail_level:
            # Flush the source cache because inspect can return out-of-date source
            linecache.checkcache()
            try:
                source = self.format(inspect.getsource(obj))
                out.write(header('Source:\n') + source.rstrip())
            except:
                if ds:
                    out.writeln(header('Docstring:\n') + indent(ds))

        # Constructor docstring for classes
        if obj_type is types.ClassType:
            # reconstruct the function definition and print it:
            try:
                obj_init = obj.__init__
            except AttributeError:
                init_def = init_ds = None
            else:
                init_def = self.__getdef(obj_init, oname)
                init_ds = getdoc(obj_init)

            if init_def or init_ds:
                out.writeln(header('\nConstructor information:'))
                if init_def:
                    out.write(header('Definition:\t') + self.format(init_def))
                if init_ds:
                    out.writeln(header('Docstring:\n') + indent(init_ds))
        # and class docstring for instances:
        elif obj_type is types.InstanceType:

            # First, check whether the instance docstring is identical to the
            # class one, and print it separately if they don't coincide.  In
            # most cases they will, but it's nice to print all the info for
            # objects which use instance-customized docstrings.
            if ds:
                class_ds = getdoc(obj.__class__)
                if class_ds and ds != class_ds:
                    out.writeln(
                        header('Class Docstring:\n') + indent(class_ds))

            # Next, try to show constructor docstrings
            try:
                init_ds = getdoc(obj.__init__)
            except AttributeError:
                init_ds = None
            if init_ds:
                out.writeln(
                    header('Constructor Docstring:\n') + indent(init_ds))

            # Call form docstring for callable instances
            if hasattr(obj, '__call__'):
                out.writeln(header('Callable:\t') + 'Yes')
                call_def = self.__getdef(obj.__call__, oname)
                if call_def is None:
                    out.write(
                        header('Call def:\t') +
                        'Calling definition not available.')
                else:
                    out.write(header('Call def:\t') + self.format(call_def))
                call_ds = getdoc(obj.__call__)
                if call_ds:
                    out.writeln(header('Call docstring:\n') + indent(call_ds))

        # Finally send to printer/pager
        output = out.getvalue()
        if output:
            page(output)
    def pinfo(self,obj,oname='',formatter=None,info=None,detail_level=0):
        """Show detailed information about an object.

        Optional arguments:
        
        - oname: name of the variable pointing to the object.

        - formatter: special formatter for docstrings (see pdoc)

        - info: a structure with some information fields which may have been
        precomputed already.

        - detail_level: if set to 1, more information is given.
        """

        obj_type = type(obj)

        header = self.__head
        if info is None:
            ismagic = 0
            isalias = 0
            ospace = ''
        else:
            ismagic = info.ismagic
            isalias = info.isalias
            ospace = info.namespace
        # Get docstring, special-casing aliases:
        if isalias:
            if not callable(obj):
                try:
                    ds = "Alias to the system command:\n  %s" % obj[1]
                except:
                    ds = "Alias: " + str(obj)
            else:
                ds = "Alias to " + str(obj)
                if obj.__doc__:
                    ds += "\nDocstring:\n" + obj.__doc__
        else:
            ds = getdoc(obj)
            if ds is None:
                ds = '<no docstring>'
        if formatter is not None:
            ds = formatter(ds)

        # store output in a list which gets joined with \n at the end.
        out = myStringIO()
        
        string_max = 200 # max size of strings to show (snipped if longer)
        shalf = int((string_max -5)/2)

        if ismagic:
            obj_type_name = 'Magic function'
        elif isalias:
            obj_type_name = 'System alias'
        else:
            obj_type_name = obj_type.__name__
        out.writeln(header('Type:\t\t')+obj_type_name)

        try:
            bclass = obj.__class__
            out.writeln(header('Base Class:\t')+str(bclass))
        except: pass

        # String form, but snip if too long in ? form (full in ??)
        if detail_level >= self.str_detail_level:
            try:
                ostr = str(obj)
                str_head = 'String Form:'
                if not detail_level and len(ostr)>string_max:
                    ostr = ostr[:shalf] + ' <...> ' + ostr[-shalf:]
                    ostr = ("\n" + " " * len(str_head.expandtabs())).\
                           join(map(string.strip,ostr.split("\n")))
                if ostr.find('\n') > -1:
                    # Print multi-line strings starting at the next line.
                    str_sep = '\n'
                else:
                    str_sep = '\t'
                out.writeln("%s%s%s" % (header(str_head),str_sep,ostr))
            except:
                pass

        if ospace:
            out.writeln(header('Namespace:\t')+ospace)

        # Length (for strings and lists)
        try:
            length = str(len(obj))
            out.writeln(header('Length:\t\t')+length)
        except: pass

        # Filename where object was defined
        binary_file = False
        try:
            try:
                fname = inspect.getabsfile(obj)
            except TypeError:
                # For an instance, the file that matters is where its class was
                # declared.
                if hasattr(obj,'__class__'):
                    fname = inspect.getabsfile(obj.__class__)
            if fname.endswith('<string>'):
                fname = 'Dynamically generated function. No source code available.'
            if (fname.endswith('.so') or fname.endswith('.dll')):
                binary_file = True
            out.writeln(header('File:\t\t')+fname)
        except:
            # if anything goes wrong, we don't want to show source, so it's as
            # if the file was binary
            binary_file = True

        # reconstruct the function definition and print it:
        defln = self.__getdef(obj,oname)
        if defln:
            out.write(header('Definition:\t')+self.format(defln))
 
        # Docstrings only in detail 0 mode, since source contains them (we
        # avoid repetitions).  If source fails, we add them back, see below.
        if ds and detail_level == 0:
                out.writeln(header('Docstring:\n') + indent(ds))
                
        # Original source code for any callable
        if detail_level:
            # Flush the source cache because inspect can return out-of-date
            # source
            linecache.checkcache()
            source_success = False
            try:
                try:
                    src = getsource(obj,binary_file)
                except TypeError:
                    if hasattr(obj,'__class__'):
                        src = getsource(obj.__class__,binary_file)
                if src is not None:
                    source = self.format(src)
                    out.write(header('Source:\n')+source.rstrip())
                    source_success = True
            except Exception, msg:
                pass
            
            if ds and not source_success:
                out.writeln(header('Docstring [source file open failed]:\n')
                            + indent(ds))
Exemple #7
0
def _pinfo(self, obj, oname='', formatter=None, info=None, detail_level=0):
    """Show detailed information about an object.

    Optional arguments:

    - oname: name of the variable pointing to the object.

    - formatter: special formatter for docstrings (see pdoc)

    - info: a structure with some information fields which may have been
    precomputed already.

    - detail_level: if set to 1, more information is given.

    This is a copy of the method found in IPython 0.8.1 and is duc-taped into
    IPython.OInspect.
    """
    #Some extra imports
    import types
    import linecache
    from IPython.OInspect import myStringIO, getsource
    from IPython.genutils import indent, page
    #We need access to private stuff, blergh
    self.__head = getattr(self, '_%s__head' % self.__class__.__name__)
    self.__getdef = getattr(self, '_%s__getdef' % self.__class__.__name__)

    obj_type = type(obj)

    header = self.__head
    if info is None:
        ismagic = 0
        isalias = 0
        ospace = ''
    else:
        ismagic = info.ismagic
        isalias = info.isalias
        ospace = info.namespace
    # Get docstring, special-casing aliases:
    if isalias:
        ds = "Alias to the system command:\n  %s" % obj[1]
    else:
        ds = _getdoc(obj)
        if ds is None:
            ds = '<no docstring>'
    if formatter is not None:
        ds = formatter(ds)

    ds = _format_docstring(ds)

    # store output in a list which gets joined with \n at the end.
    out = myStringIO()

    string_max = 200  # max size of strings to show (snipped if longer)
    shalf = int((string_max - 5) / 2)

    # reconstruct the function definition and print it:
    defln = self.__getdef(obj, oname)
    if defln:
        out.write(header('Definition:\t') + self.format(defln))

    # Docstrings only in detail 0 mode, since source contains them (we
    # avoid repetitions).  If source fails, we add them back, see below.
    if ds and detail_level == 0:
        out.writeln(header('Documentation:\n') + indent(ds))

    # Filename where object was defined
    binary_file = False
    try:
        fname = inspect.getabsfile(obj)
        if fname.endswith('<string>'):
            fname = 'Dynamically generated function. No source code available.'
        if (fname.endswith('.so') or fname.endswith('.dll')
                or not os.path.isfile(fname)):
            binary_file = True
    except:
        # if anything goes wrong, we don't want to show source, so it's as
        # if the file was binary
        binary_file = True

    # Original source code for any callable
    if detail_level:
        # Flush the source cache because inspect can return out-of-date source
        linecache.checkcache()
        source_success = False
        try:
            source = self.format(getsource(obj, binary_file))
            if source:
                out.write(header('Source:\n') + source.rstrip())
                source_success = True
        except Exception, msg:
            pass

        if ds and not source_success:
            out.writeln(
                header('Documentation [source file open failed]:\n') +
                indent(ds))
Exemple #8
0
    if obj_type is types.ClassType:
        # reconstruct the function definition and print it:
        try:
            obj_init = obj.__init__
        except AttributeError:
            init_def = init_ds = None
        else:
            init_def = self.__getdef(obj_init, oname)
            init_ds = _getdoc(obj_init)

        if init_def or init_ds:
            out.writeln(header('\nConstructor information:'))
            if init_def:
                out.write(header('Definition:\t') + self.format(init_def))
            if init_ds:
                out.writeln(header('Documentation:\n') + indent(init_ds))

    # Finally send to printer/pager
    output = out.getvalue()
    if output:
        page(output)
    # end pinfo


def page(strng, start=0, screen_lines=0, pager_cmd=None):
    '''Non-paging page implementation to provide a non-paging Q-Shell
    '''
    #This comes from IPython.genutils.page
    str_lines = strng.split(os.linesep)[start:]
    str_toprint = os.linesep.join(str_lines)