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@3 | 35 | class NamespaceDict: |
paul@3 | 36 | |
paul@3 | 37 | "A mix-in providing dictionary methods." |
paul@3 | 38 | |
paul@3 | 39 | def __getitem__(self, name): |
paul@3 | 40 | return self.namespace[name] |
paul@3 | 41 | |
paul@3 | 42 | def keys(self): |
paul@3 | 43 | return self.namespace.keys() |
paul@3 | 44 | |
paul@4 | 45 | class Naming: |
paul@4 | 46 | |
paul@4 | 47 | "A mix-in providing naming conveniences." |
paul@4 | 48 | |
paul@4 | 49 | def full_name(self): |
paul@4 | 50 | if self.name is not None: |
paul@4 | 51 | return self.module_name + "." + self.name |
paul@4 | 52 | else: |
paul@4 | 53 | return self.module_name |
paul@4 | 54 | |
paul@4 | 55 | class Class(NamespaceDict, Naming): |
paul@0 | 56 | |
paul@0 | 57 | "An inspected class." |
paul@0 | 58 | |
paul@4 | 59 | def __init__(self, name, module_name): |
paul@0 | 60 | self.name = name |
paul@4 | 61 | self.module_name = module_name |
paul@0 | 62 | self.bases = [] |
paul@0 | 63 | self.namespace = {} |
paul@0 | 64 | self.instattr = set() |
paul@0 | 65 | |
paul@0 | 66 | def add_base(self, base): |
paul@0 | 67 | self.bases.append(base) |
paul@0 | 68 | |
paul@4 | 69 | def __repr__(self): |
paul@4 | 70 | return "Class(%r, %r)" % (self.name, self.module_name) |
paul@4 | 71 | |
paul@4 | 72 | def class_attributes(self): |
paul@4 | 73 | |
paul@4 | 74 | "Return all class attributes, indicating the class which provides them." |
paul@4 | 75 | |
paul@4 | 76 | attr = {} |
paul@4 | 77 | reversed_bases = self.bases[:] |
paul@4 | 78 | reversed_bases.reverse() |
paul@4 | 79 | for cls in reversed_bases: |
paul@4 | 80 | attr.update(cls.class_attributes()) |
paul@4 | 81 | for name, value in self.namespace.items(): |
paul@4 | 82 | attr[name] = self.full_name() |
paul@4 | 83 | return attr |
paul@4 | 84 | |
paul@4 | 85 | def instance_attributes(self): |
paul@4 | 86 | |
paul@4 | 87 | """ |
paul@4 | 88 | Return all attributes for an instance, indicating either the class which |
paul@4 | 89 | provides them or that the instance itself provides them. |
paul@4 | 90 | """ |
paul@4 | 91 | |
paul@4 | 92 | attr = {} |
paul@4 | 93 | attr.update(self.class_attributes()) |
paul@5 | 94 | for i, name in enumerate(self.instattr): |
paul@4 | 95 | if attr.has_key(name): |
paul@4 | 96 | print "Instance attribute", name, "overrides class attribute." |
paul@5 | 97 | attr[name] = i |
paul@4 | 98 | return attr |
paul@4 | 99 | |
paul@4 | 100 | class Function(NamespaceDict, Naming): |
paul@0 | 101 | |
paul@0 | 102 | "An inspected function." |
paul@0 | 103 | |
paul@4 | 104 | def __init__(self, name, module_name, argnames, has_star, has_dstar): |
paul@3 | 105 | self.name = name |
paul@4 | 106 | self.module_name = module_name |
paul@3 | 107 | self.argnames = argnames |
paul@3 | 108 | self.has_star = has_star |
paul@3 | 109 | self.has_dstar = has_dstar |
paul@3 | 110 | self.namespace = {} |
paul@3 | 111 | |
paul@4 | 112 | def __repr__(self): |
paul@4 | 113 | return "Function(%r, %r, %r, %r, %r)" % ( |
paul@4 | 114 | self.name, self.module_name, self.argnames, self.has_star, self.has_dstar |
paul@4 | 115 | ) |
paul@4 | 116 | |
paul@4 | 117 | class UnresolvedName(NamespaceDict, Naming): |
paul@3 | 118 | |
paul@3 | 119 | "A module, class or function which was mentioned but could not be imported." |
paul@3 | 120 | |
paul@4 | 121 | def __init__(self, name, module_name): |
paul@0 | 122 | self.name = name |
paul@4 | 123 | self.module_name = module_name |
paul@3 | 124 | self.namespace = {} |
paul@0 | 125 | |
paul@4 | 126 | def __repr__(self): |
paul@4 | 127 | return "UnresolvedName(%r, %r)" % (self.name, self.module_name) |
paul@4 | 128 | |
paul@3 | 129 | class Module(ASTVisitor, NamespaceDict): |
paul@0 | 130 | |
paul@0 | 131 | "An inspected module." |
paul@0 | 132 | |
paul@1 | 133 | def __init__(self, name, importer=None): |
paul@0 | 134 | ASTVisitor.__init__(self) |
paul@0 | 135 | self.visitor = self |
paul@0 | 136 | |
paul@1 | 137 | self.name = name |
paul@0 | 138 | self.importer = importer |
paul@2 | 139 | self.loaded = 0 |
paul@0 | 140 | |
paul@1 | 141 | # Module namespace. |
paul@1 | 142 | |
paul@0 | 143 | self.namespace = {} |
paul@0 | 144 | |
paul@1 | 145 | # Current expression state. |
paul@1 | 146 | |
paul@1 | 147 | self.expr = None |
paul@1 | 148 | |
paul@1 | 149 | # Namespace state. |
paul@1 | 150 | |
paul@0 | 151 | self.in_global = 1 |
paul@0 | 152 | self.in_init = 0 |
paul@0 | 153 | self.classes = [] |
paul@0 | 154 | self.module = None |
paul@0 | 155 | |
paul@0 | 156 | def parse(self, filename): |
paul@0 | 157 | module = compiler.parseFile(filename) |
paul@0 | 158 | self.process(module) |
paul@0 | 159 | |
paul@0 | 160 | def process(self, module): |
paul@0 | 161 | self.module = module |
paul@1 | 162 | processed = self.dispatch(module) |
paul@1 | 163 | if self.namespace.has_key("__all__"): |
paul@1 | 164 | all = self.namespace["__all__"] |
paul@1 | 165 | if isinstance(all, compiler.ast.List): |
paul@1 | 166 | for n in all.nodes: |
paul@1 | 167 | self.namespace[n.value] = self.importer.add_module(self.name + "." + n.value) |
paul@1 | 168 | return processed |
paul@0 | 169 | |
paul@3 | 170 | def vacuum(self): |
paul@3 | 171 | for name, value in self.namespace.items(): |
paul@3 | 172 | if isinstance(value, Module) and not value.loaded: |
paul@3 | 173 | del self.namespace[name] |
paul@0 | 174 | |
paul@4 | 175 | def full_name(self): |
paul@4 | 176 | return self.name |
paul@4 | 177 | |
paul@4 | 178 | def __repr__(self): |
paul@4 | 179 | return "Module(%r)" % self.name |
paul@4 | 180 | |
paul@3 | 181 | # Namespace methods. |
paul@0 | 182 | |
paul@0 | 183 | def store(self, name, obj): |
paul@0 | 184 | if self.in_global: |
paul@0 | 185 | if not self.classes: |
paul@0 | 186 | self.namespace[name] = obj |
paul@0 | 187 | else: |
paul@0 | 188 | self.classes[-1].namespace[name] = obj |
paul@0 | 189 | |
paul@0 | 190 | def store_attr(self, name): |
paul@0 | 191 | if self.in_init: |
paul@0 | 192 | self.classes[-1].instattr.add(name) |
paul@0 | 193 | |
paul@3 | 194 | # Visitor methods. |
paul@3 | 195 | |
paul@3 | 196 | def default(self, node, *args): |
paul@3 | 197 | raise InspectError, node.__class__ |
paul@3 | 198 | |
paul@3 | 199 | def dispatch(self, node, *args): |
paul@3 | 200 | return ASTVisitor.dispatch(self, node, *args) |
paul@3 | 201 | |
paul@0 | 202 | def NOP(self, node): |
paul@0 | 203 | return node |
paul@0 | 204 | |
paul@1 | 205 | visitAdd = NOP |
paul@1 | 206 | |
paul@0 | 207 | visitAnd = NOP |
paul@0 | 208 | |
paul@4 | 209 | visitAssert = NOP |
paul@4 | 210 | |
paul@0 | 211 | def visitAssign(self, node): |
paul@0 | 212 | self.expr = self.dispatch(node.expr) |
paul@0 | 213 | for n in node.nodes: |
paul@0 | 214 | self.dispatch(n) |
paul@0 | 215 | return None |
paul@0 | 216 | |
paul@0 | 217 | def visitAssAttr(self, node): |
paul@0 | 218 | expr = self.dispatch(node.expr) |
paul@0 | 219 | if expr is not None and isinstance(expr, Self): |
paul@0 | 220 | self.store_attr(node.attrname) |
paul@0 | 221 | return None |
paul@0 | 222 | |
paul@0 | 223 | def visitAssList(self, node): |
paul@0 | 224 | for n in node.nodes: |
paul@0 | 225 | self.dispatch(n) |
paul@0 | 226 | return None |
paul@0 | 227 | |
paul@0 | 228 | def visitAssName(self, node): |
paul@0 | 229 | self.store(node.name, self.expr) |
paul@0 | 230 | return None |
paul@0 | 231 | |
paul@0 | 232 | visitAssTuple = visitAssList |
paul@0 | 233 | |
paul@2 | 234 | visitAugAssign = NOP |
paul@2 | 235 | |
paul@4 | 236 | visitBackquote = NOP |
paul@4 | 237 | |
paul@2 | 238 | visitBitand = NOP |
paul@2 | 239 | |
paul@2 | 240 | visitBitor = NOP |
paul@2 | 241 | |
paul@4 | 242 | visitBitxor = NOP |
paul@4 | 243 | |
paul@4 | 244 | visitBreak = NOP |
paul@4 | 245 | |
paul@0 | 246 | visitCallFunc = NOP |
paul@0 | 247 | |
paul@0 | 248 | def visitClass(self, node): |
paul@0 | 249 | if not self.in_global: |
paul@4 | 250 | print "Class %r in %r is not global: ignored." % (node.name, self) |
paul@0 | 251 | else: |
paul@4 | 252 | cls = Class(node.name, self.name) |
paul@0 | 253 | for base in node.bases: |
paul@0 | 254 | base_ref = self.dispatch(base) |
paul@0 | 255 | if base_ref is None: |
paul@4 | 256 | raise InspectError, "Base class %r for class %r in %r is not found: it may be hidden in some way." % (base, self, cls) |
paul@0 | 257 | cls.add_base(base_ref) |
paul@0 | 258 | |
paul@0 | 259 | # Make an entry for the class. |
paul@0 | 260 | |
paul@0 | 261 | self.store(node.name, cls) |
paul@0 | 262 | |
paul@0 | 263 | self.classes.append(cls) |
paul@0 | 264 | self.dispatch(node.code) |
paul@0 | 265 | self.classes.pop() |
paul@0 | 266 | |
paul@0 | 267 | return node |
paul@0 | 268 | |
paul@4 | 269 | visitCompare = NOP |
paul@4 | 270 | |
paul@0 | 271 | visitConst = NOP |
paul@0 | 272 | |
paul@4 | 273 | visitContinue = NOP |
paul@4 | 274 | |
paul@4 | 275 | visitDecorators = NOP |
paul@4 | 276 | |
paul@0 | 277 | visitDict = NOP |
paul@0 | 278 | |
paul@0 | 279 | visitDiscard = NOP |
paul@0 | 280 | |
paul@4 | 281 | visitDiv = NOP |
paul@4 | 282 | |
paul@4 | 283 | visitEllipsis = NOP |
paul@4 | 284 | |
paul@4 | 285 | visitExec = NOP |
paul@4 | 286 | |
paul@4 | 287 | visitExpression = NOP |
paul@4 | 288 | |
paul@4 | 289 | visitFloorDiv = NOP |
paul@4 | 290 | |
paul@0 | 291 | visitFor = NOP |
paul@0 | 292 | |
paul@0 | 293 | def visitFrom(self, node): |
paul@0 | 294 | if self.importer is None: |
paul@0 | 295 | raise InspectError, "Please use the micropython.Importer class for code which uses the 'from' statement." |
paul@0 | 296 | |
paul@0 | 297 | module = self.importer.load(node.modname, 1) |
paul@0 | 298 | |
paul@0 | 299 | if module is None: |
paul@0 | 300 | print "Warning:", node.modname, "not imported." |
paul@0 | 301 | |
paul@0 | 302 | for name, alias in node.names: |
paul@0 | 303 | if name != "*": |
paul@4 | 304 | if module is not None and module.namespace.has_key(name): |
paul@3 | 305 | self.namespace[alias or name] = attr = module.namespace[name] |
paul@2 | 306 | if isinstance(attr, Module) and not attr.loaded: |
paul@2 | 307 | self.importer.load(attr.name) |
paul@3 | 308 | else: |
paul@4 | 309 | self.namespace[alias or name] = UnresolvedName(name, node.modname) |
paul@3 | 310 | else: |
paul@3 | 311 | if module is not None: |
paul@3 | 312 | for n in module.namespace.keys(): |
paul@3 | 313 | self.namespace[n] = attr = module.namespace[n] |
paul@3 | 314 | if isinstance(attr, Module) and not attr.loaded: |
paul@3 | 315 | self.importer.load(attr.name) |
paul@0 | 316 | |
paul@0 | 317 | return None |
paul@0 | 318 | |
paul@0 | 319 | def visitFunction(self, node): |
paul@3 | 320 | function = Function( |
paul@3 | 321 | node.name, |
paul@4 | 322 | self.name, |
paul@3 | 323 | node.argnames, |
paul@3 | 324 | has_star=(node.flags & 4 != 0), |
paul@3 | 325 | has_dstar=(node.flags & 8 != 0) |
paul@3 | 326 | ) |
paul@0 | 327 | |
paul@0 | 328 | in_global = self.in_global |
paul@0 | 329 | self.in_global = 0 |
paul@0 | 330 | if node.name == "__init__" and self.classes: |
paul@0 | 331 | self.in_init = 1 |
paul@0 | 332 | self.dispatch(node.code) |
paul@0 | 333 | self.in_init = 0 |
paul@0 | 334 | self.in_global = in_global |
paul@3 | 335 | |
paul@3 | 336 | self.store(node.name, function) |
paul@0 | 337 | return None |
paul@0 | 338 | |
paul@4 | 339 | visitGenExpr = NOP |
paul@4 | 340 | |
paul@4 | 341 | visitGenExprFor = NOP |
paul@4 | 342 | |
paul@4 | 343 | visitGenExprIf = NOP |
paul@4 | 344 | |
paul@4 | 345 | visitGenExprInner = NOP |
paul@4 | 346 | |
paul@0 | 347 | def visitGetattr(self, node): |
paul@0 | 348 | expr = self.dispatch(node.expr) |
paul@4 | 349 | if expr is not None: |
paul@4 | 350 | if isinstance(expr, Module): |
paul@4 | 351 | return expr.namespace.get(node.attrname) |
paul@4 | 352 | elif isinstance(expr, UnresolvedName): |
paul@4 | 353 | return UnresolvedName(node.attrname, expr.full_name()) |
paul@4 | 354 | return builtins_namespace.get(node.attrname) |
paul@4 | 355 | |
paul@4 | 356 | visitGlobal = NOP |
paul@0 | 357 | |
paul@0 | 358 | def visitIf(self, node): |
paul@0 | 359 | for test, body in node.tests: |
paul@0 | 360 | self.dispatch(body) |
paul@0 | 361 | if node.else_ is not None: |
paul@0 | 362 | self.dispatch(node.else_) |
paul@0 | 363 | return None |
paul@0 | 364 | |
paul@0 | 365 | def visitImport(self, node): |
paul@0 | 366 | if self.importer is None: |
paul@0 | 367 | raise InspectError, "Please use the micropython.Importer class for code which uses the 'import' statement." |
paul@0 | 368 | |
paul@0 | 369 | for name, alias in node.names: |
paul@2 | 370 | if alias is not None: |
paul@4 | 371 | self.namespace[alias] = self.importer.load(name, 1) or UnresolvedName(None, name) |
paul@2 | 372 | else: |
paul@4 | 373 | self.namespace[name.split(".")[0]] = self.importer.load(name) or UnresolvedName(None, name.split(".")[0]) |
paul@0 | 374 | |
paul@0 | 375 | return None |
paul@0 | 376 | |
paul@4 | 377 | visitInvert = NOP |
paul@4 | 378 | |
paul@4 | 379 | visitKeyword = NOP |
paul@4 | 380 | |
paul@4 | 381 | visitLambda = NOP |
paul@4 | 382 | |
paul@1 | 383 | visitLeftShift = NOP |
paul@1 | 384 | |
paul@0 | 385 | visitList = NOP |
paul@0 | 386 | |
paul@4 | 387 | visitListComp = NOP |
paul@4 | 388 | |
paul@4 | 389 | visitListCompFor = NOP |
paul@4 | 390 | |
paul@4 | 391 | visitListCompIf = NOP |
paul@4 | 392 | |
paul@4 | 393 | visitMod = NOP |
paul@4 | 394 | |
paul@0 | 395 | def visitModule(self, node): |
paul@0 | 396 | return self.dispatch(node.node) |
paul@0 | 397 | |
paul@2 | 398 | visitMul = NOP |
paul@2 | 399 | |
paul@0 | 400 | def visitName(self, node): |
paul@0 | 401 | name = node.name |
paul@0 | 402 | if name == "self": |
paul@0 | 403 | return Self() |
paul@0 | 404 | elif self.namespace.has_key(name): |
paul@0 | 405 | return self.namespace[name] |
paul@3 | 406 | elif builtins_namespace.has_key(name): |
paul@3 | 407 | return builtins_namespace[name] |
paul@0 | 408 | else: |
paul@0 | 409 | return None |
paul@0 | 410 | |
paul@0 | 411 | visitNot = NOP |
paul@0 | 412 | |
paul@0 | 413 | visitOr = NOP |
paul@0 | 414 | |
paul@0 | 415 | visitPass = NOP |
paul@0 | 416 | |
paul@4 | 417 | visitPower = NOP |
paul@4 | 418 | |
paul@4 | 419 | visitPrint = NOP |
paul@4 | 420 | |
paul@4 | 421 | visitPrintnl = NOP |
paul@4 | 422 | |
paul@0 | 423 | visitRaise = NOP |
paul@0 | 424 | |
paul@4 | 425 | visitReturn = NOP |
paul@4 | 426 | |
paul@1 | 427 | visitRightShift = NOP |
paul@1 | 428 | |
paul@4 | 429 | visitSlice = NOP |
paul@4 | 430 | |
paul@0 | 431 | def visitStmt(self, node): |
paul@0 | 432 | for n in node.nodes: |
paul@0 | 433 | self.dispatch(n) |
paul@0 | 434 | return None |
paul@0 | 435 | |
paul@4 | 436 | visitSub = NOP |
paul@4 | 437 | |
paul@0 | 438 | visitSubscript = NOP |
paul@0 | 439 | |
paul@0 | 440 | def visitTryExcept(self, node): |
paul@0 | 441 | self.dispatch(node.body) |
paul@0 | 442 | for name, var, n in node.handlers: |
paul@0 | 443 | self.dispatch(n) |
paul@0 | 444 | if node.else_ is not None: |
paul@0 | 445 | self.dispatch(node.else_) |
paul@0 | 446 | return None |
paul@0 | 447 | |
paul@4 | 448 | visitTryFinally = NOP |
paul@4 | 449 | |
paul@1 | 450 | visitTuple = NOP |
paul@1 | 451 | |
paul@4 | 452 | visitUnaryAdd = NOP |
paul@4 | 453 | |
paul@4 | 454 | visitUnarySub = NOP |
paul@4 | 455 | |
paul@4 | 456 | visitWhile = NOP |
paul@4 | 457 | |
paul@4 | 458 | visitWith = NOP |
paul@4 | 459 | |
paul@4 | 460 | visitYield = NOP |
paul@4 | 461 | |
paul@0 | 462 | class Self: |
paul@0 | 463 | |
paul@0 | 464 | "A reference to an object within a method." |
paul@0 | 465 | |
paul@0 | 466 | pass |
paul@0 | 467 | |
paul@4 | 468 | # Built-in types initialisation. |
paul@4 | 469 | |
paul@0 | 470 | builtins_namespace = {} |
paul@0 | 471 | for key in ['ArithmeticError', 'AssertionError', 'AttributeError', |
paul@0 | 472 | 'BaseException', 'DeprecationWarning', 'EOFError', 'Ellipsis', |
paul@0 | 473 | 'EnvironmentError', 'Exception', 'False', 'FloatingPointError', |
paul@0 | 474 | 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', |
paul@0 | 475 | 'ImportWarning', 'IndentationError', 'IndexError', 'KeyError', |
paul@0 | 476 | 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'NameError', |
paul@0 | 477 | 'None', 'NotImplemented', 'NotImplementedError', 'OSError', |
paul@0 | 478 | 'OverflowError', 'PendingDeprecationWarning', 'ReferenceError', |
paul@0 | 479 | 'RuntimeError', 'RuntimeWarning', 'StandardError', 'StopIteration', |
paul@0 | 480 | 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', |
paul@0 | 481 | 'TabError', 'True', 'TypeError', 'UnboundLocalError', |
paul@0 | 482 | 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', |
paul@0 | 483 | 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', |
paul@4 | 484 | 'ValueError', 'Warning', 'ZeroDivisionError', |
paul@4 | 485 | 'bool', 'buffer', 'complex', 'dict', 'file', 'float', 'int', 'list', |
paul@4 | 486 | 'long', 'object', 'slice', 'str', 'tuple', 'type', 'unicode']: |
paul@4 | 487 | builtins_namespace[key] = Class(key, "__builtins__") |
paul@0 | 488 | |
paul@0 | 489 | # vim: tabstop=4 expandtab shiftwidth=4 |