| 1 | """Parse a Python module and describe its classes and methods.
|
|---|
| 2 |
|
|---|
| 3 | Parse enough of a Python file to recognize imports and class and
|
|---|
| 4 | method definitions, and to find out the superclasses of a class.
|
|---|
| 5 |
|
|---|
| 6 | The interface consists of a single function:
|
|---|
| 7 | readmodule_ex(module [, path])
|
|---|
| 8 | where module is the name of a Python module, and path is an optional
|
|---|
| 9 | list of directories where the module is to be searched. If present,
|
|---|
| 10 | path is prepended to the system search path sys.path. The return
|
|---|
| 11 | value is a dictionary. The keys of the dictionary are the names of
|
|---|
| 12 | the classes defined in the module (including classes that are defined
|
|---|
| 13 | via the from XXX import YYY construct). The values are class
|
|---|
| 14 | instances of the class Class defined here. One special key/value pair
|
|---|
| 15 | is present for packages: the key '__path__' has a list as its value
|
|---|
| 16 | which contains the package search path.
|
|---|
| 17 |
|
|---|
| 18 | A class is described by the class Class in this module. Instances
|
|---|
| 19 | of this class have the following instance variables:
|
|---|
| 20 | module -- the module name
|
|---|
| 21 | name -- the name of the class
|
|---|
| 22 | super -- a list of super classes (Class instances)
|
|---|
| 23 | methods -- a dictionary of methods
|
|---|
| 24 | file -- the file in which the class was defined
|
|---|
| 25 | lineno -- the line in the file on which the class statement occurred
|
|---|
| 26 | The dictionary of methods uses the method names as keys and the line
|
|---|
| 27 | numbers on which the method was defined as values.
|
|---|
| 28 | If the name of a super class is not recognized, the corresponding
|
|---|
| 29 | entry in the list of super classes is not a class instance but a
|
|---|
| 30 | string giving the name of the super class. Since import statements
|
|---|
| 31 | are recognized and imported modules are scanned as well, this
|
|---|
| 32 | shouldn't happen often.
|
|---|
| 33 |
|
|---|
| 34 | A function is described by the class Function in this module.
|
|---|
| 35 | Instances of this class have the following instance variables:
|
|---|
| 36 | module -- the module name
|
|---|
| 37 | name -- the name of the class
|
|---|
| 38 | file -- the file in which the class was defined
|
|---|
| 39 | lineno -- the line in the file on which the class statement occurred
|
|---|
| 40 | """
|
|---|
| 41 |
|
|---|
| 42 | import sys
|
|---|
| 43 | import imp
|
|---|
| 44 | import tokenize # Python tokenizer
|
|---|
| 45 | from token import NAME, DEDENT, NEWLINE, OP
|
|---|
| 46 | from operator import itemgetter
|
|---|
| 47 |
|
|---|
| 48 | __all__ = ["readmodule", "readmodule_ex", "Class", "Function"]
|
|---|
| 49 |
|
|---|
| 50 | _modules = {} # cache of modules we've seen
|
|---|
| 51 |
|
|---|
| 52 | # each Python class is represented by an instance of this class
|
|---|
| 53 | class Class:
|
|---|
| 54 | '''Class to represent a Python class.'''
|
|---|
| 55 | def __init__(self, module, name, super, file, lineno):
|
|---|
| 56 | self.module = module
|
|---|
| 57 | self.name = name
|
|---|
| 58 | if super is None:
|
|---|
| 59 | super = []
|
|---|
| 60 | self.super = super
|
|---|
| 61 | self.methods = {}
|
|---|
| 62 | self.file = file
|
|---|
| 63 | self.lineno = lineno
|
|---|
| 64 |
|
|---|
| 65 | def _addmethod(self, name, lineno):
|
|---|
| 66 | self.methods[name] = lineno
|
|---|
| 67 |
|
|---|
| 68 | class Function:
|
|---|
| 69 | '''Class to represent a top-level Python function'''
|
|---|
| 70 | def __init__(self, module, name, file, lineno):
|
|---|
| 71 | self.module = module
|
|---|
| 72 | self.name = name
|
|---|
| 73 | self.file = file
|
|---|
| 74 | self.lineno = lineno
|
|---|
| 75 |
|
|---|
| 76 | def readmodule(module, path=[]):
|
|---|
| 77 | '''Backwards compatible interface.
|
|---|
| 78 |
|
|---|
| 79 | Call readmodule_ex() and then only keep Class objects from the
|
|---|
|
|---|