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@0 | 59 | def __init__(self, importer=None): |
paul@0 | 60 | ASTVisitor.__init__(self) |
paul@0 | 61 | self.visitor = self |
paul@0 | 62 | |
paul@0 | 63 | self.importer = importer |
paul@0 | 64 | |
paul@0 | 65 | self.namespace = {} |
paul@0 | 66 | self.namespace.update(builtins_namespace) |
paul@0 | 67 | |
paul@0 | 68 | self.in_global = 1 |
paul@0 | 69 | self.in_init = 0 |
paul@0 | 70 | self.expr = None |
paul@0 | 71 | |
paul@0 | 72 | self.classes = [] |
paul@0 | 73 | self.module = None |
paul@0 | 74 | |
paul@0 | 75 | def parse(self, filename): |
paul@0 | 76 | module = compiler.parseFile(filename) |
paul@0 | 77 | self.process(module) |
paul@0 | 78 | |
paul@0 | 79 | def process(self, module): |
paul@0 | 80 | self.module = module |
paul@0 | 81 | return self.dispatch(module) |
paul@0 | 82 | |
paul@0 | 83 | def default(self, node, *args): |
paul@0 | 84 | raise InspectError, (None, node) |
paul@0 | 85 | |
paul@0 | 86 | def dispatch(self, node, *args): |
paul@0 | 87 | return ASTVisitor.dispatch(self, node, *args) |
paul@0 | 88 | |
paul@0 | 89 | def store(self, name, obj): |
paul@0 | 90 | if self.in_global: |
paul@0 | 91 | if not self.classes: |
paul@0 | 92 | self.namespace[name] = obj |
paul@0 | 93 | else: |
paul@0 | 94 | self.classes[-1].namespace[name] = obj |
paul@0 | 95 | |
paul@0 | 96 | def store_attr(self, name): |
paul@0 | 97 | if self.in_init: |
paul@0 | 98 | self.classes[-1].instattr.add(name) |
paul@0 | 99 | |
paul@0 | 100 | def NOP(self, node): |
paul@0 | 101 | return node |
paul@0 | 102 | |
paul@0 | 103 | visitAnd = NOP |
paul@0 | 104 | |
paul@0 | 105 | def visitAssign(self, node): |
paul@0 | 106 | self.expr = self.dispatch(node.expr) |
paul@0 | 107 | for n in node.nodes: |
paul@0 | 108 | self.dispatch(n) |
paul@0 | 109 | return None |
paul@0 | 110 | |
paul@0 | 111 | def visitAssAttr(self, node): |
paul@0 | 112 | expr = self.dispatch(node.expr) |
paul@0 | 113 | if expr is not None and isinstance(expr, Self): |
paul@0 | 114 | self.store_attr(node.attrname) |
paul@0 | 115 | return None |
paul@0 | 116 | |
paul@0 | 117 | def visitAssList(self, node): |
paul@0 | 118 | for n in node.nodes: |
paul@0 | 119 | self.dispatch(n) |
paul@0 | 120 | return None |
paul@0 | 121 | |
paul@0 | 122 | def visitAssName(self, node): |
paul@0 | 123 | self.store(node.name, self.expr) |
paul@0 | 124 | return None |
paul@0 | 125 | |
paul@0 | 126 | visitAssTuple = visitAssList |
paul@0 | 127 | |
paul@0 | 128 | visitCallFunc = NOP |
paul@0 | 129 | |
paul@0 | 130 | def visitClass(self, node): |
paul@0 | 131 | if not self.in_global: |
paul@0 | 132 | raise InspectError, "Class is %s not global: cannot handle this reasonably." % node.name |
paul@0 | 133 | else: |
paul@0 | 134 | cls = Class(node) |
paul@0 | 135 | for base in node.bases: |
paul@0 | 136 | base_ref = self.dispatch(base) |
paul@0 | 137 | if base_ref is None: |
paul@0 | 138 | raise InspectError, "Base class %s for class %s is not found: it may be hidden in some way." % (base, node.name) |
paul@0 | 139 | cls.add_base(base_ref) |
paul@0 | 140 | |
paul@0 | 141 | # Make an entry for the class. |
paul@0 | 142 | |
paul@0 | 143 | self.store(node.name, cls) |
paul@0 | 144 | |
paul@0 | 145 | self.classes.append(cls) |
paul@0 | 146 | self.dispatch(node.code) |
paul@0 | 147 | self.classes.pop() |
paul@0 | 148 | |
paul@0 | 149 | return node |
paul@0 | 150 | |
paul@0 | 151 | visitConst = NOP |
paul@0 | 152 | |
paul@0 | 153 | visitDict = NOP |
paul@0 | 154 | |
paul@0 | 155 | visitDiscard = NOP |
paul@0 | 156 | |
paul@0 | 157 | visitFor = NOP |
paul@0 | 158 | |
paul@0 | 159 | def visitFrom(self, node): |
paul@0 | 160 | if self.importer is None: |
paul@0 | 161 | raise InspectError, "Please use the micropython.Importer class for code which uses the 'from' statement." |
paul@0 | 162 | |
paul@0 | 163 | module = self.importer.load(node.modname, 1) |
paul@0 | 164 | |
paul@0 | 165 | if module is None: |
paul@0 | 166 | print "Warning:", node.modname, "not imported." |
paul@0 | 167 | return None |
paul@0 | 168 | |
paul@0 | 169 | for name, alias in node.names: |
paul@0 | 170 | if name != "*": |
paul@0 | 171 | self.namespace[alias] = module.namespace[name] |
paul@0 | 172 | else: |
paul@0 | 173 | for n in module.namespace.keys(): |
paul@0 | 174 | self.namespace[n] = module.namespace[n] |
paul@0 | 175 | |
paul@0 | 176 | return None |
paul@0 | 177 | |
paul@0 | 178 | def visitFunction(self, node): |
paul@0 | 179 | self.store(node.name, Function(node.name)) |
paul@0 | 180 | |
paul@0 | 181 | in_global = self.in_global |
paul@0 | 182 | self.in_global = 0 |
paul@0 | 183 | if node.name == "__init__" and self.classes: |
paul@0 | 184 | self.in_init = 1 |
paul@0 | 185 | self.dispatch(node.code) |
paul@0 | 186 | self.in_init = 0 |
paul@0 | 187 | self.in_global = in_global |
paul@0 | 188 | return None |
paul@0 | 189 | |
paul@0 | 190 | def visitGetattr(self, node): |
paul@0 | 191 | expr = self.dispatch(node.expr) |
paul@0 | 192 | if expr is not None and isinstance(expr, Module): |
paul@0 | 193 | return expr.namespace.get(node.attrname) |
paul@0 | 194 | else: |
paul@0 | 195 | return None |
paul@0 | 196 | |
paul@0 | 197 | def visitIf(self, node): |
paul@0 | 198 | for test, body in node.tests: |
paul@0 | 199 | self.dispatch(body) |
paul@0 | 200 | if node.else_ is not None: |
paul@0 | 201 | self.dispatch(node.else_) |
paul@0 | 202 | return None |
paul@0 | 203 | |
paul@0 | 204 | def visitImport(self, node): |
paul@0 | 205 | if self.importer is None: |
paul@0 | 206 | raise InspectError, "Please use the micropython.Importer class for code which uses the 'import' statement." |
paul@0 | 207 | |
paul@0 | 208 | for name, alias in node.names: |
paul@0 | 209 | self.namespace[alias] = self.importer.load(name, 1) |
paul@0 | 210 | |
paul@0 | 211 | return None |
paul@0 | 212 | |
paul@0 | 213 | visitList = NOP |
paul@0 | 214 | |
paul@0 | 215 | def visitModule(self, node): |
paul@0 | 216 | return self.dispatch(node.node) |
paul@0 | 217 | |
paul@0 | 218 | def visitName(self, node): |
paul@0 | 219 | name = node.name |
paul@0 | 220 | if name == "self": |
paul@0 | 221 | return Self() |
paul@0 | 222 | elif self.namespace.has_key(name): |
paul@0 | 223 | return self.namespace[name] |
paul@0 | 224 | else: |
paul@0 | 225 | return None |
paul@0 | 226 | |
paul@0 | 227 | visitNot = NOP |
paul@0 | 228 | |
paul@0 | 229 | visitOr = NOP |
paul@0 | 230 | |
paul@0 | 231 | visitPass = NOP |
paul@0 | 232 | |
paul@0 | 233 | visitRaise = NOP |
paul@0 | 234 | |
paul@0 | 235 | def visitStmt(self, node): |
paul@0 | 236 | for n in node.nodes: |
paul@0 | 237 | self.dispatch(n) |
paul@0 | 238 | return None |
paul@0 | 239 | |
paul@0 | 240 | visitSubscript = NOP |
paul@0 | 241 | |
paul@0 | 242 | def visitTryExcept(self, node): |
paul@0 | 243 | self.dispatch(node.body) |
paul@0 | 244 | for name, var, n in node.handlers: |
paul@0 | 245 | self.dispatch(n) |
paul@0 | 246 | if node.else_ is not None: |
paul@0 | 247 | self.dispatch(node.else_) |
paul@0 | 248 | return None |
paul@0 | 249 | |
paul@0 | 250 | class Self: |
paul@0 | 251 | |
paul@0 | 252 | "A reference to an object within a method." |
paul@0 | 253 | |
paul@0 | 254 | pass |
paul@0 | 255 | |
paul@0 | 256 | builtins_namespace = {} |
paul@0 | 257 | for key in ['ArithmeticError', 'AssertionError', 'AttributeError', |
paul@0 | 258 | 'BaseException', 'DeprecationWarning', 'EOFError', 'Ellipsis', |
paul@0 | 259 | 'EnvironmentError', 'Exception', 'False', 'FloatingPointError', |
paul@0 | 260 | 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', |
paul@0 | 261 | 'ImportWarning', 'IndentationError', 'IndexError', 'KeyError', |
paul@0 | 262 | 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'NameError', |
paul@0 | 263 | 'None', 'NotImplemented', 'NotImplementedError', 'OSError', |
paul@0 | 264 | 'OverflowError', 'PendingDeprecationWarning', 'ReferenceError', |
paul@0 | 265 | 'RuntimeError', 'RuntimeWarning', 'StandardError', 'StopIteration', |
paul@0 | 266 | 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', |
paul@0 | 267 | 'TabError', 'True', 'TypeError', 'UnboundLocalError', |
paul@0 | 268 | 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', |
paul@0 | 269 | 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', |
paul@0 | 270 | 'ValueError', 'Warning', 'ZeroDivisionError']: |
paul@0 | 271 | builtins_namespace[key] = Class(key) |
paul@0 | 272 | |
paul@0 | 273 | # vim: tabstop=4 expandtab shiftwidth=4 |