Example #1
0
    def browse_function(self, function, identifier):
        """Returns an ObjectLink with the function's signature.

        Returns a dictionary in an ObjectLink with type FunctionType.

        The dictionary contains the elements:
            name -- the name the function was defined as
            signature -- the function's calling signature
            doc -- the function's docstring
            file -- the file the function is defined in
            line -- the line in the file the function begins on

        The signature element is a list of dictionaries, each of which
        includes a 'name' element.  If that argument has a default, it
        is provided in the 'default' element.  If the argument is a
        variable argument list, its dictionary will have a 'list' key
        set.  If the argument accepts arbitrary keyword arguments, its
        dictionary will have a 'keywords' element set.
        """
        code = function.func_code
        argcount = code.co_argcount
        takesList = (code.co_flags & 0x04) and 1
        takesKeywords = (code.co_flags & 0x08) and 1

        args = [None] * (argcount + takesList + takesKeywords)
        for i in xrange(len(args)):
            args[i] = {'name': code.co_varnames[i]}

        if function.func_defaults:
            i_d = 0
            for i in xrange(argcount - len(function.func_defaults), argcount):
                args[i]['default'] = self.browse_other(
                    function.func_defaults[i_d],
                    '%s.func_defaults[%d]' % (identifier, i_d))

                i_d = i_d + 1

        if takesKeywords:
            args[-1]['keywords'] = 1

        if takesList:
            args[-(1 + takesKeywords)]['list'] = 1

        # maybe also: function.func_globals

        rval = {
            'name': function.__name__,
            'signature': args,
            'doc': text.docstringLStrip(function.__doc__),
            'file': code.co_filename,
            'line': code.co_firstlineno,
        }

        return ObjectLink(rval, identifier, types.FunctionType, id(function))
Example #2
0
    def browse_function(self, function, identifier):
        """Returns an ObjectLink with the function's signature.

        Returns a dictionary in an ObjectLink with type FunctionType.

        The dictionary contains the elements:
            name -- the name the function was defined as
            signature -- the function's calling signature
            doc -- the function's docstring
            file -- the file the function is defined in
            line -- the line in the file the function begins on

        The signature element is a list of dictionaries, each of which
        includes a 'name' element.  If that argument has a default, it
        is provided in the 'default' element.  If the argument is a
        variable argument list, its dictionary will have a 'list' key
        set.  If the argument accepts arbitrary keyword arguments, its
        dictionary will have a 'keywords' element set.
        """
        code = function.func_code
        argcount = code.co_argcount
        takesList = (code.co_flags & 0x04) and 1
        takesKeywords = (code.co_flags & 0x08) and 1

        args = [None] * (argcount + takesList + takesKeywords)
        for i in xrange(len(args)):
            args[i] = {"name": code.co_varnames[i]}

        if function.func_defaults:
            i_d = 0
            for i in xrange(argcount - len(function.func_defaults), argcount):
                args[i]["default"] = self.browse_other(
                    function.func_defaults[i_d], "%s.func_defaults[%d]" % (identifier, i_d)
                )

                i_d = i_d + 1

        if takesKeywords:
            args[-1]["keywords"] = 1

        if takesList:
            args[-(1 + takesKeywords)]["list"] = 1

        # maybe also: function.func_globals

        rval = {
            "name": function.__name__,
            "signature": args,
            "doc": text.docstringLStrip(function.__doc__),
            "file": code.co_filename,
            "line": code.co_firstlineno,
        }

        return ObjectLink(rval, identifier, types.FunctionType, id(function))
Example #3
0
    def browse_class(self, theClass, identifier):
        """Returns an ObjectLink with the class's attributes.

        Returns a dictionary in an ObjectLink with type ClassType.

        The dictionary contains the members:
            name -- the name the class was defined with
            doc -- the class's docstring
            methods -- class methods
            members -- other members of the class
            module -- the module the class is defined in
        """
        if not identifier:
            identifier = theClass.__name__
        members = {}
        methods = {}
        for i in dir(theClass):
            if (i[0] == '_') and (i != '__init__'):
                continue

            mIdentifier = string.join([identifier, i], ".")
            member = getattr(theClass, i)
            mType = type(member)

            if mType is types.MethodType:
                methods[i] = self.browse_method(member, mIdentifier)
            else:
                members[i] = self.browse_other(member, mIdentifier)

        rval = {
            "name":
            theClass.__name__,
            "doc":
            text.docstringLStrip(theClass.__doc__),
            "members":
            members,
            "methods":
            methods,
            "bases":
            self.browse_other(theClass.__bases__, identifier + ".__bases__"),
            "module":
            getattr(theClass, '__module__', None),
        }

        return ObjectLink(rval, identifier, types.ClassType)
Example #4
0
    def browse_module(self, module, identifier):
        """Returns an ObjectString with the module's properties and members.

        Returns a dictionary in an ObjectLink with type ModuleType.

        The dictionary contains the elements:
            name -- the name the module was defined as
            doc -- documentation string for the module
            file -- the file the module is defined in
            classes -- the public classes provided by the module
            functions -- the public functions provided by the module
            data -- the public data members provided by the module

        (\"Public\" is taken to be \"anything that doesn't start with _\")
        """
        functions = {}
        classes = {}
        data = {}
        for key, value in module.__dict__.items():
            if key[0] == '_':
                continue

            mIdentifier = "%s.%s" % (identifier, key)

            if type(value) is types.ClassType:
                classes[key] = self.browse_class(value, mIdentifier)
            elif type(value) is types.FunctionType:
                functions[key] = self.browse_function(value, mIdentifier)
            elif type(value) is types.ModuleType:
                pass  # pass on imported modules
            else:
                data[key] = self.browse_other(value, mIdentifier)

        rval = {
            'name': module.__name__,
            'doc': text.docstringLStrip(module.__doc__),
            'file': getattr(module, '__file__', None),
            'classes': classes,
            'functions': functions,
            'data': data,
        }

        return ObjectLink(rval, identifier, types.ModuleType, id(module))
Example #5
0
    def browse_module(self, module, identifier):
        """Returns an ObjectString with the module's properties and members.

        Returns a dictionary in an ObjectLink with type ModuleType.

        The dictionary contains the elements:
            name -- the name the module was defined as
            doc -- documentation string for the module
            file -- the file the module is defined in
            classes -- the public classes provided by the module
            functions -- the public functions provided by the module
            data -- the public data members provided by the module

        (\"Public\" is taken to be \"anything that doesn't start with _\")
        """
        functions = {}
        classes = {}
        data = {}
        for key, value in module.__dict__.items():
            if key[0] == "_":
                continue

            mIdentifier = "%s.%s" % (identifier, key)

            if type(value) is types.ClassType:
                classes[key] = self.browse_class(value, mIdentifier)
            elif type(value) is types.FunctionType:
                functions[key] = self.browse_function(value, mIdentifier)
            elif type(value) is types.ModuleType:
                pass  # pass on imported modules
            else:
                data[key] = self.browse_other(value, mIdentifier)

        rval = {
            "name": module.__name__,
            "doc": text.docstringLStrip(module.__doc__),
            "file": getattr(module, "__file__", None),
            "classes": classes,
            "functions": functions,
            "data": data,
        }

        return ObjectLink(rval, identifier, types.ModuleType, id(module))
Example #6
0
    def browse_class(self, theClass, identifier):
        """Returns an ObjectLink with the class's attributes.

        Returns a dictionary in an ObjectLink with type ClassType.

        The dictionary contains the members:
            name -- the name the class was defined with
            doc -- the class's docstring
            methods -- class methods
            members -- other members of the class
            module -- the module the class is defined in
        """
        if not identifier:
            identifier = theClass.__name__
        members = {}
        methods = {}
        for i in dir(theClass):
            if (i[0] == "_") and (i != "__init__"):
                continue

            mIdentifier = string.join([identifier, i], ".")
            member = getattr(theClass, i)
            mType = type(member)

            if mType is types.MethodType:
                methods[i] = self.browse_method(member, mIdentifier)
            else:
                members[i] = self.browse_other(member, mIdentifier)

        rval = {
            "name": theClass.__name__,
            "doc": text.docstringLStrip(theClass.__doc__),
            "members": members,
            "methods": methods,
            "bases": self.browse_other(theClass.__bases__, identifier + ".__bases__"),
            "module": getattr(theClass, "__module__", None),
        }

        return ObjectLink(rval, identifier, types.ClassType)