paul@0 | 1 | #!/usr/bin/env python |
paul@0 | 2 | |
paul@0 | 3 | """ |
paul@0 | 4 | Inspect source files, obtaining details of classes and attributes. |
paul@0 | 5 | |
paul@0 | 6 | Copyright (C) 2007 Paul Boddie <paul@boddie.org.uk> |
paul@0 | 7 | |
paul@0 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@0 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@0 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@0 | 11 | version. |
paul@0 | 12 | |
paul@0 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@0 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@0 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@0 | 16 | details. |
paul@0 | 17 | |
paul@0 | 18 | You should have received a copy of the GNU General Public License along with |
paul@0 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@0 | 20 | """ |
paul@0 | 21 | |
paul@0 | 22 | import compiler.ast |
paul@0 | 23 | from compiler.visitor import ASTVisitor |
paul@0 | 24 | try: |
paul@0 | 25 | set |
paul@0 | 26 | except NameError: |
paul@0 | 27 | from sets import Set as set |
paul@0 | 28 | |
paul@0 | 29 | class InspectError(Exception): |
paul@0 | 30 | |
paul@0 | 31 | "An inspection error." |
paul@0 | 32 | |
paul@0 | 33 | pass |
paul@0 | 34 | |
paul@0 | 35 | class Class: |
paul@0 | 36 | |
paul@0 | 37 | "An inspected class." |
paul@0 | 38 | |
paul@0 | 39 | def __init__(self, name): |
paul@0 | 40 | self.name = name |
paul@0 | 41 | self.bases = [] |
paul@0 | 42 | self.namespace = {} |
paul@0 | 43 | self.instattr = set() |
paul@0 | 44 | |
paul@0 | 45 | def add_base(self, base): |
paul@0 | 46 | self.bases.append(base) |
paul@0 | 47 | |
paul@0 | 48 | class Function: |
paul@0 | 49 | |
paul@0 | 50 | "An inspected function." |
paul@0 | 51 | |
paul@0 | 52 | def __init__(self, name): |
paul@0 | 53 | self.name = name |
paul@0 | 54 | |
paul@0 | 55 | class Module(ASTVisitor): |
paul@0 | 56 | |
paul@0 | 57 | "An inspected module." |
paul@0 | 58 | |
paul@1 | 59 | def __init__(self, name, importer=None): |
paul@0 | 60 | ASTVisitor.__init__(self) |
paul@0 | 61 | self.visitor = self |
paul@0 | 62 | |
paul@1 | 63 | self.name = name |
paul@0 | 64 | self.importer = importer |
paul@2 | 65 | self.loaded = 0 |
paul@0 | 66 | |
paul@1 | 67 | # Module namespace. |
paul@1 | 68 | |
paul@0 | 69 | self.namespace = {} |
paul@0 | 70 | self.namespace.update(builtins_namespace) |
paul@0 | 71 | |
paul@1 | 72 | # Current expression state. |
paul@1 | 73 | |
paul@1 | 74 | self.expr = None |
paul@1 | 75 | |
paul@1 | 76 | # Namespace state. |
paul@1 | 77 | |
paul@0 | 78 | self.in_global = 1 |
paul@0 | 79 | self.in_init = 0 |
paul@0 | 80 | self.classes = [] |
paul@0 | 81 | self.module = None |
paul@0 | 82 | |
paul@0 | 83 | def parse(self, filename): |
paul@0 | 84 | module = compiler.parseFile(filename) |
paul@0 | 85 | self.process(module) |
paul@0 | 86 | |
paul@0 | 87 | def process(self, module): |
paul@0 | 88 | self.module = module |
paul@1 | 89 | processed = self.dispatch(module) |
paul@1 | 90 | if self.namespace.has_key("__all__"): |
paul@1 | 91 | all = self.namespace["__all__"] |
paul@1 | 92 | if isinstance(all, compiler.ast.List): |
paul@1 | 93 | for n in all.nodes: |
paul@1 | 94 | self.namespace[n.value] = self.importer.add_module(self.name + "." + n.value) |
paul@1 | 95 | return processed |
paul@0 | 96 | |
paul@0 | 97 | def default(self, node, *args): |
paul@1 | 98 | raise InspectError, node.__class__ |
paul@0 | 99 | |
paul@0 | 100 | def dispatch(self, node, *args): |
paul@0 | 101 | return ASTVisitor.dispatch(self, node, *args) |
paul@0 | 102 | |
paul@0 | 103 | def store(self, name, obj): |
paul@0 | 104 | if self.in_global: |
paul@0 | 105 | if not self.classes: |
paul@0 | 106 | self.namespace[name] = obj |
paul@0 | 107 | else: |
paul@0 | 108 | self.classes[-1].namespace[name] = obj |
paul@0 | 109 | |
paul@0 | 110 | def store_attr(self, name): |
paul@0 | 111 | if self.in_init: |
paul@0 | 112 | self.classes[-1].instattr.add(name) |
paul@0 | 113 | |
paul@0 | 114 | def NOP(self, node): |
paul@0 | 115 | return node |
paul@0 | 116 | |
paul@1 | 117 | visitAdd = NOP |
paul@1 | 118 | |
paul@0 | 119 | visitAnd = NOP |
paul@0 | 120 | |
paul@0 | 121 | def visitAssign(self, node): |
paul@0 | 122 | self.expr = self.dispatch(node.expr) |
paul@0 | 123 | for n in node.nodes: |
paul@0 | 124 | self.dispatch(n) |
paul@0 | 125 | return None |
paul@0 | 126 | |
paul@0 | 127 | def visitAssAttr(self, node): |
paul@0 | 128 | expr = self.dispatch(node.expr) |
paul@0 | 129 | if expr is not None and isinstance(expr, Self): |
paul@0 | 130 | self.store_attr(node.attrname) |
paul@0 | 131 | return None |
paul@0 | 132 | |
paul@0 | 133 | def visitAssList(self, node): |
paul@0 | 134 | for n in node.nodes: |
paul@0 | 135 | self.dispatch(n) |
paul@0 | 136 | return None |
paul@0 | 137 | |
paul@0 | 138 | def visitAssName(self, node): |
paul@0 | 139 | self.store(node.name, self.expr) |
paul@0 | 140 | return None |
paul@0 | 141 | |
paul@0 | 142 | visitAssTuple = visitAssList |
paul@0 | 143 | |
paul@2 | 144 | visitAugAssign = NOP |
paul@2 | 145 | |
paul@2 | 146 | visitBitand = NOP |
paul@2 | 147 | |
paul@2 | 148 | visitBitor = NOP |
paul@2 | 149 | |
paul@0 | 150 | visitCallFunc = NOP |
paul@0 | 151 | |
paul@0 | 152 | def visitClass(self, node): |
paul@0 | 153 | if not self.in_global: |
paul@0 | 154 | raise InspectError, "Class is %s not global: cannot handle this reasonably." % node.name |
paul@0 | 155 | else: |
paul@1 | 156 | cls = Class(node.name) |
paul@0 | 157 | for base in node.bases: |
paul@0 | 158 | base_ref = self.dispatch(base) |
paul@0 | 159 | if base_ref is None: |
paul@0 | 160 | raise InspectError, "Base class %s for class %s is not found: it may be hidden in some way." % (base, node.name) |
paul@0 | 161 | cls.add_base(base_ref) |
paul@0 | 162 | |
paul@0 | 163 | # Make an entry for the class. |
paul@0 | 164 | |
paul@0 | 165 | self.store(node.name, cls) |
paul@0 | 166 | |
paul@0 | 167 | self.classes.append(cls) |
paul@0 | 168 | self.dispatch(node.code) |
paul@0 | 169 | self.classes.pop() |
paul@0 | 170 | |
paul@0 | 171 | return node |
paul@0 | 172 | |
paul@0 | 173 | visitConst = NOP |
paul@0 | 174 | |
paul@0 | 175 | visitDict = NOP |
paul@0 | 176 | |
paul@0 | 177 | visitDiscard = NOP |
paul@0 | 178 | |
paul@0 | 179 | visitFor = NOP |
paul@0 | 180 | |
paul@0 | 181 | def visitFrom(self, node): |
paul@0 | 182 | if self.importer is None: |
paul@0 | 183 | raise InspectError, "Please use the micropython.Importer class for code which uses the 'from' statement." |
paul@0 | 184 | |
paul@0 | 185 | module = self.importer.load(node.modname, 1) |
paul@0 | 186 | |
paul@0 | 187 | if module is None: |
paul@0 | 188 | print "Warning:", node.modname, "not imported." |
paul@0 | 189 | return None |
paul@0 | 190 | |
paul@0 | 191 | for name, alias in node.names: |
paul@0 | 192 | if name != "*": |
paul@2 | 193 | self.namespace[alias or name] = attr = module.namespace[name] |
paul@2 | 194 | if isinstance(attr, Module) and not attr.loaded: |
paul@2 | 195 | self.importer.load(attr.name) |
paul@0 | 196 | else: |
paul@0 | 197 | for n in module.namespace.keys(): |
paul@2 | 198 | self.namespace[n] = attr = module.namespace[n] |
paul@2 | 199 | if isinstance(attr, Module) and not attr.loaded: |
paul@2 | 200 | self.importer.load(attr.name) |
paul@0 | 201 | |
paul@0 | 202 | return None |
paul@0 | 203 | |
paul@0 | 204 | def visitFunction(self, node): |
paul@0 | 205 | self.store(node.name, Function(node.name)) |
paul@0 | 206 | |
paul@0 | 207 | in_global = self.in_global |
paul@0 | 208 | self.in_global = 0 |
paul@0 | 209 | if node.name == "__init__" and self.classes: |
paul@0 | 210 | self.in_init = 1 |
paul@0 | 211 | self.dispatch(node.code) |
paul@0 | 212 | self.in_init = 0 |
paul@0 | 213 | self.in_global = in_global |
paul@0 | 214 | return None |
paul@0 | 215 | |
paul@0 | 216 | def visitGetattr(self, node): |
paul@0 | 217 | expr = self.dispatch(node.expr) |
paul@0 | 218 | if expr is not None and isinstance(expr, Module): |
paul@0 | 219 | return expr.namespace.get(node.attrname) |
paul@0 | 220 | else: |
paul@0 | 221 | return None |
paul@0 | 222 | |
paul@0 | 223 | def visitIf(self, node): |
paul@0 | 224 | for test, body in node.tests: |
paul@0 | 225 | self.dispatch(body) |
paul@0 | 226 | if node.else_ is not None: |
paul@0 | 227 | self.dispatch(node.else_) |
paul@0 | 228 | return None |
paul@0 | 229 | |
paul@0 | 230 | def visitImport(self, node): |
paul@0 | 231 | if self.importer is None: |
paul@0 | 232 | raise InspectError, "Please use the micropython.Importer class for code which uses the 'import' statement." |
paul@0 | 233 | |
paul@0 | 234 | for name, alias in node.names: |
paul@2 | 235 | if alias is not None: |
paul@2 | 236 | self.namespace[alias] = self.importer.load(name, 1) |
paul@2 | 237 | else: |
paul@2 | 238 | self.namespace[name.split(".")[0]] = self.importer.load(name) |
paul@0 | 239 | |
paul@0 | 240 | return None |
paul@0 | 241 | |
paul@1 | 242 | visitLeftShift = NOP |
paul@1 | 243 | |
paul@0 | 244 | visitList = NOP |
paul@0 | 245 | |
paul@0 | 246 | def visitModule(self, node): |
paul@0 | 247 | return self.dispatch(node.node) |
paul@0 | 248 | |
paul@2 | 249 | visitMul = NOP |
paul@2 | 250 | |
paul@0 | 251 | def visitName(self, node): |
paul@0 | 252 | name = node.name |
paul@0 | 253 | if name == "self": |
paul@0 | 254 | return Self() |
paul@0 | 255 | elif self.namespace.has_key(name): |
paul@0 | 256 | return self.namespace[name] |
paul@0 | 257 | else: |
paul@0 | 258 | return None |
paul@0 | 259 | |
paul@0 | 260 | visitNot = NOP |
paul@0 | 261 | |
paul@0 | 262 | visitOr = NOP |
paul@0 | 263 | |
paul@0 | 264 | visitPass = NOP |
paul@0 | 265 | |
paul@0 | 266 | visitRaise = NOP |
paul@0 | 267 | |
paul@1 | 268 | visitRightShift = NOP |
paul@1 | 269 | |
paul@0 | 270 | def visitStmt(self, node): |
paul@0 | 271 | for n in node.nodes: |
paul@0 | 272 | self.dispatch(n) |
paul@0 | 273 | return None |
paul@0 | 274 | |
paul@0 | 275 | visitSubscript = NOP |
paul@0 | 276 | |
paul@0 | 277 | def visitTryExcept(self, node): |
paul@0 | 278 | self.dispatch(node.body) |
paul@0 | 279 | for name, var, n in node.handlers: |
paul@0 | 280 | self.dispatch(n) |
paul@0 | 281 | if node.else_ is not None: |
paul@0 | 282 | self.dispatch(node.else_) |
paul@0 | 283 | return None |
paul@0 | 284 | |
paul@1 | 285 | visitTuple = NOP |
paul@1 | 286 | |
paul@0 | 287 | class Self: |
paul@0 | 288 | |
paul@0 | 289 | "A reference to an object within a method." |
paul@0 | 290 | |
paul@0 | 291 | pass |
paul@0 | 292 | |
paul@0 | 293 | builtins_namespace = {} |
paul@0 | 294 | for key in ['ArithmeticError', 'AssertionError', 'AttributeError', |
paul@0 | 295 | 'BaseException', 'DeprecationWarning', 'EOFError', 'Ellipsis', |
paul@0 | 296 | 'EnvironmentError', 'Exception', 'False', 'FloatingPointError', |
paul@0 | 297 | 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', |
paul@0 | 298 | 'ImportWarning', 'IndentationError', 'IndexError', 'KeyError', |
paul@0 | 299 | 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'NameError', |
paul@0 | 300 | 'None', 'NotImplemented', 'NotImplementedError', 'OSError', |
paul@0 | 301 | 'OverflowError', 'PendingDeprecationWarning', 'ReferenceError', |
paul@0 | 302 | 'RuntimeError', 'RuntimeWarning', 'StandardError', 'StopIteration', |
paul@0 | 303 | 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', |
paul@0 | 304 | 'TabError', 'True', 'TypeError', 'UnboundLocalError', |
paul@0 | 305 | 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', |
paul@0 | 306 | 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', |
paul@1 | 307 | 'ValueError', 'Warning', 'ZeroDivisionError', 'object']: |
paul@0 | 308 | builtins_namespace[key] = Class(key) |
paul@0 | 309 | |
paul@0 | 310 | # vim: tabstop=4 expandtab shiftwidth=4 |