Sjoerd Mullender | 8cb4b1f | 1995-07-28 09:30:01 +0000 | [diff] [blame] | 1 | '''Parse a Python file and retrieve classes and methods. |
| 2 | |
| 3 | Parse enough of a Python file to recognize class and method |
| 4 | definitions and to find out the superclasses of a class. |
| 5 | |
| 6 | The interface consists of a single function: |
| 7 | readmodule(module, path) |
| 8 | module is the name of a Python module, path is an optional list of |
| 9 | directories where the module is to be searched. If present, path is |
| 10 | prepended to the system search path sys.path. |
| 11 | The return value is a dictionary. The keys of the dictionary are |
| 12 | the names of the classes defined in the module (including classes |
| 13 | that are defined via the from XXX import YYY construct). The values |
| 14 | are class instances of the class Class defined here. |
| 15 | |
| 16 | A class is described by the class Class in this module. Instances |
| 17 | of this class have the following instance variables: |
| 18 | name -- the name of the class |
| 19 | super -- a list of super classes (Class instances) |
| 20 | methods -- a dictionary of methods |
| 21 | file -- the file in which the class was defined |
| 22 | lineno -- the line in the file on which the class statement occurred |
| 23 | The dictionary of methods uses the method names as keys and the line |
| 24 | numbers on which the method was defined as values. |
| 25 | If the name of a super class is not recognized, the corresponding |
| 26 | entry in the list of super classes is not a class instance but a |
| 27 | string giving the name of the super class. Since import statements |
| 28 | are recognized and imported modules are scanned as well, this |
| 29 | shouldn't happen often. |
| 30 | |
| 31 | BUGS |
| 32 | Continuation lines are not dealt with at all and strings may confuse |
| 33 | the hell out of the parser, but it usually works.''' |
| 34 | |
| 35 | import os |
| 36 | import sys |
| 37 | import imp |
| 38 | import regex |
| 39 | import string |
| 40 | |
| 41 | id = '\\(<id>[A-Za-z_][A-Za-z0-9_]*\\)' # match identifier |
| 42 | blank_line = regex.compile('^[ \t]*\\($\\|#\\)') |
| 43 | is_class = regex.symcomp('^class[ \t]+'+id+'[ \t]*\\(<sup>([^)]*)\\)?[ \t]*:') |
| 44 | is_method = regex.symcomp('^[ \t]+def[ \t]+'+id+'[ \t]*(') |
| 45 | is_import = regex.symcomp('^import[ \t]*\\(<imp>[^#]+\\)') |
| 46 | is_from = regex.symcomp('^from[ \t]+'+id+'[ \t]+import[ \t]+\\(<imp>[^#]+\\)') |
| 47 | dedent = regex.compile('^[^ \t]') |
| 48 | indent = regex.compile('^[^ \t]*') |
| 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.''' |
Sjoerd Mullender | 825bae7 | 1995-11-02 17:21:33 +0000 | [diff] [blame] | 55 | def __init__(self, module, name, super, file, lineno): |
| 56 | self.module = module |
Sjoerd Mullender | 8cb4b1f | 1995-07-28 09:30:01 +0000 | [diff] [blame] | 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 | def readmodule(module, path = []): |
| 69 | '''Read a module file and return a dictionary of classes. |
| 70 | |
| 71 | Search for MODULE in PATH and sys.path, read and parse the |
| 72 | module and return a dictionary with one entry for each class |
| 73 | found in the module.''' |
| 74 | |
| 75 | if _modules.has_key(module): |
| 76 | # we've seen this module before... |
| 77 | return _modules[module] |
| 78 | if module in sys.builtin_module_names: |
| 79 | # this is a built-in module |
| 80 | dict = {} |
| 81 | _modules[module] = dict |
| 82 | return dict |
| 83 | |
| 84 | # search the path for the module |
| 85 | f = None |
| 86 | suffixes = imp.get_suffixes() |
| 87 | for dir in path + sys.path: |
| 88 | for suff, mode, type in suffixes: |
| 89 | file = os.path.join(dir, module + suff) |
| 90 | try: |
| 91 | f = open(file, mode) |
| 92 | except IOError: |
| 93 | pass |
| 94 | else: |
| 95 | # found the module |
| 96 | break |
| 97 | if f: |
| 98 | break |
| 99 | if not f: |
| 100 | raise IOError, 'module ' + module + ' not found' |
| 101 | if type != imp.PY_SOURCE: |
| 102 | # not Python source, can't do anything with this module |
| 103 | f.close() |
| 104 | dict = {} |
| 105 | _modules[module] = dict |
| 106 | return dict |
| 107 | |
| 108 | cur_class = None |
| 109 | dict = {} |
| 110 | _modules[module] = dict |
| 111 | imports = [] |
| 112 | lineno = 0 |
| 113 | while 1: |
| 114 | line = f.readline() |
| 115 | if not line: |
| 116 | break |
| 117 | lineno = lineno + 1 # count lines |
| 118 | line = line[:-1] # remove line feed |
| 119 | if blank_line.match(line) >= 0: |
| 120 | # ignore blank (and comment only) lines |
| 121 | continue |
| 122 | ## if indent.match(line) >= 0: |
| 123 | ## indentation = len(string.expandtabs(indent.group(0), 8)) |
| 124 | if is_import.match(line) >= 0: |
| 125 | # import module |
| 126 | for n in string.splitfields(is_import.group('imp'), ','): |
| 127 | n = string.strip(n) |
| 128 | try: |
| 129 | # recursively read the |
| 130 | # imported module |
| 131 | d = readmodule(n, path) |
| 132 | except: |
| 133 | print 'module',n,'not found' |
| 134 | pass |
| 135 | continue |
| 136 | if is_from.match(line) >= 0: |
| 137 | # from module import stuff |
| 138 | mod = is_from.group('id') |
| 139 | names = string.splitfields(is_from.group('imp'), ',') |
| 140 | try: |
| 141 | # recursively read the imported module |
| 142 | d = readmodule(mod, path) |
| 143 | except: |
| 144 | print 'module',mod,'not found' |
| 145 | continue |
| 146 | # add any classes that were defined in the |
| 147 | # imported module to our name space if they |
| 148 | # were mentioned in the list |
| 149 | for n in names: |
| 150 | n = string.strip(n) |
| 151 | if d.has_key(n): |
| 152 | dict[n] = d[n] |
| 153 | elif n == '*': |
| 154 | # only add a name if not |
| 155 | # already there (to mimic what |
| 156 | # Python does internally) |
Guido van Rossum | b5fa1cb | 1996-10-10 16:00:28 +0000 | [diff] [blame] | 157 | # also don't add names that |
| 158 | # start with _ |
Sjoerd Mullender | 8cb4b1f | 1995-07-28 09:30:01 +0000 | [diff] [blame] | 159 | for n in d.keys(): |
Guido van Rossum | b5fa1cb | 1996-10-10 16:00:28 +0000 | [diff] [blame] | 160 | if n[0] != '_' and \ |
| 161 | not dict.has_key(n): |
Sjoerd Mullender | 8cb4b1f | 1995-07-28 09:30:01 +0000 | [diff] [blame] | 162 | dict[n] = d[n] |
| 163 | continue |
| 164 | if is_class.match(line) >= 0: |
| 165 | # we found a class definition |
| 166 | class_name = is_class.group('id') |
| 167 | inherit = is_class.group('sup') |
| 168 | if inherit: |
| 169 | # the class inherits from other classes |
| 170 | inherit = string.strip(inherit[1:-1]) |
| 171 | names = [] |
| 172 | for n in string.splitfields(inherit, ','): |
| 173 | n = string.strip(n) |
| 174 | if dict.has_key(n): |
| 175 | # we know this super class |
| 176 | n = dict[n] |
| 177 | else: |
| 178 | c = string.splitfields(n, '.') |
| 179 | if len(c) > 1: |
| 180 | # super class |
| 181 | # is of the |
| 182 | # form module.class: |
| 183 | # look in |
| 184 | # module for class |
| 185 | m = c[-2] |
| 186 | c = c[-1] |
| 187 | if _modules.has_key(m): |
| 188 | d = _modules[m] |
| 189 | if d.has_key(c): |
| 190 | n = d[c] |
| 191 | names.append(n) |
| 192 | inherit = names |
| 193 | # remember this class |
Sjoerd Mullender | 825bae7 | 1995-11-02 17:21:33 +0000 | [diff] [blame] | 194 | cur_class = Class(module, class_name, inherit, file, lineno) |
Sjoerd Mullender | 8cb4b1f | 1995-07-28 09:30:01 +0000 | [diff] [blame] | 195 | dict[class_name] = cur_class |
| 196 | continue |
| 197 | if is_method.match(line) >= 0: |
| 198 | # found a method definition |
| 199 | if cur_class: |
| 200 | # and we know the class it belongs to |
| 201 | meth_name = is_method.group('id') |
| 202 | cur_class._addmethod(meth_name, lineno) |
| 203 | continue |
| 204 | if dedent.match(line) >= 0: |
| 205 | # end of class definition |
| 206 | cur_class = None |
| 207 | f.close() |
| 208 | return dict |