paul@0 | 1 | #!/usr/bin/env python |
paul@0 | 2 | |
paul@0 | 3 | """ |
paul@0 | 4 | The micropython package for processing Python source code. The code originates |
paul@0 | 5 | from the simplify package but has had various details related to that package |
paul@0 | 6 | removed. |
paul@0 | 7 | |
paul@39 | 8 | Copyright (C) 2006, 2007, 2008 Paul Boddie <paul@boddie.org.uk> |
paul@0 | 9 | |
paul@0 | 10 | This program is free software; you can redistribute it and/or modify it under |
paul@0 | 11 | the terms of the GNU General Public License as published by the Free Software |
paul@0 | 12 | Foundation; either version 3 of the License, or (at your option) any later |
paul@0 | 13 | version. |
paul@0 | 14 | |
paul@0 | 15 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@0 | 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@0 | 17 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@0 | 18 | details. |
paul@0 | 19 | |
paul@0 | 20 | You should have received a copy of the GNU General Public License along with |
paul@0 | 21 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@0 | 22 | |
paul@0 | 23 | -------- |
paul@0 | 24 | |
paul@0 | 25 | To use this module, an importer should be constructed and the load_from_file |
paul@0 | 26 | method used. Here, the standard path for module searching is employed: |
paul@0 | 27 | |
paul@0 | 28 | importer = Importer(sys.path) |
paul@0 | 29 | importer.load_from_file(filename) |
paul@4 | 30 | importer.vacuum() |
paul@0 | 31 | |
paul@0 | 32 | Such importer objects are the most convenient mechanism through which the |
paul@0 | 33 | functionality of the micropython package may be accessed. |
paul@0 | 34 | """ |
paul@0 | 35 | |
paul@39 | 36 | from micropython.common import * |
paul@17 | 37 | import micropython.ast |
paul@0 | 38 | import micropython.inspect |
paul@7 | 39 | import micropython.table |
paul@0 | 40 | import os |
paul@0 | 41 | try: |
paul@0 | 42 | set |
paul@0 | 43 | except NameError: |
paul@0 | 44 | from sets import Set as set |
paul@0 | 45 | |
paul@0 | 46 | class Importer: |
paul@0 | 47 | |
paul@39 | 48 | """ |
paul@39 | 49 | An import machine, searching for and loading modules. In addition, this |
paul@39 | 50 | class supports the generation of a program image. |
paul@39 | 51 | """ |
paul@0 | 52 | |
paul@35 | 53 | def __init__(self, path=None, verbose=0): |
paul@0 | 54 | |
paul@0 | 55 | """ |
paul@0 | 56 | Initialise the importer with the given search 'path' - a list of |
paul@0 | 57 | directories to search for Python modules. |
paul@35 | 58 | |
paul@35 | 59 | The optional 'verbose' parameter causes output concerning the activities |
paul@35 | 60 | of the object to be produced if set to a true value (not the default). |
paul@0 | 61 | """ |
paul@0 | 62 | |
paul@0 | 63 | self.path = path or [os.getcwd()] |
paul@35 | 64 | self.verbose = verbose |
paul@61 | 65 | self.modules = {} |
paul@2 | 66 | self.loading = set() |
paul@0 | 67 | |
paul@60 | 68 | # Remember the tables once generated. |
paul@60 | 69 | |
paul@60 | 70 | self.objtable = None |
paul@60 | 71 | self.paramtable = None |
paul@60 | 72 | |
paul@3 | 73 | def vacuum(self): |
paul@3 | 74 | |
paul@3 | 75 | "Tidy up the modules." |
paul@3 | 76 | |
paul@3 | 77 | for name, module in self.modules.items(): |
paul@3 | 78 | if module.loaded: |
paul@3 | 79 | module.vacuum() |
paul@3 | 80 | else: |
paul@3 | 81 | del self.modules[name] |
paul@3 | 82 | |
paul@0 | 83 | def get_modules(self): |
paul@0 | 84 | |
paul@0 | 85 | "Return all modules known to the importer." |
paul@0 | 86 | |
paul@0 | 87 | return self.modules.values() |
paul@0 | 88 | |
paul@62 | 89 | def get_image(self, with_builtins=0, optimisations=None): |
paul@9 | 90 | |
paul@9 | 91 | "Return a dictionary mapping modules to structures." |
paul@9 | 92 | |
paul@60 | 93 | objtable = self.get_object_table() |
paul@60 | 94 | paramtable = self.get_parameter_table() |
paul@21 | 95 | |
paul@9 | 96 | image = [] |
paul@18 | 97 | |
paul@18 | 98 | for module_name, module in self.modules.items(): |
paul@44 | 99 | if not with_builtins and module_name == "__builtins__": |
paul@44 | 100 | continue |
paul@44 | 101 | |
paul@54 | 102 | # Fix the attributes. |
paul@54 | 103 | |
paul@54 | 104 | module.finalise_attributes() |
paul@54 | 105 | |
paul@9 | 106 | pos = len(image) |
paul@15 | 107 | |
paul@21 | 108 | # Append constants to the image. |
paul@21 | 109 | |
paul@22 | 110 | for const in module.constants(): |
paul@21 | 111 | const.location = pos |
paul@21 | 112 | image.append(const) |
paul@21 | 113 | pos += 1 |
paul@15 | 114 | |
paul@55 | 115 | # Position the module in the image and make a translation. |
paul@55 | 116 | |
paul@55 | 117 | module.location = pos |
paul@62 | 118 | trans = micropython.ast.Translation(module, objtable, paramtable, self.modules.get("__builtins__"), optimisations) |
paul@55 | 119 | |
paul@37 | 120 | # Add header details. |
paul@37 | 121 | |
paul@37 | 122 | image.append(module) |
paul@37 | 123 | pos += 1 |
paul@37 | 124 | |
paul@15 | 125 | # Append module attributes to the image. |
paul@15 | 126 | |
paul@9 | 127 | attributes = module.module_attributes() |
paul@49 | 128 | image += module.attributes_as_list() |
paul@37 | 129 | pos += len(attributes.keys()) |
paul@9 | 130 | |
paul@15 | 131 | # Append classes and functions to the image. |
paul@15 | 132 | |
paul@9 | 133 | for obj in module.all_objects: |
paul@55 | 134 | if isinstance(obj, micropython.inspect.Class): |
paul@54 | 135 | |
paul@55 | 136 | # Fix the attributes. |
paul@54 | 137 | |
paul@55 | 138 | obj.finalise_attributes() |
paul@15 | 139 | |
paul@15 | 140 | # Position the class in the image. |
paul@15 | 141 | |
paul@22 | 142 | obj.location = pos |
paul@22 | 143 | |
paul@22 | 144 | # Add header details. |
paul@22 | 145 | |
paul@22 | 146 | image.append(obj) |
paul@22 | 147 | pos += 1 |
paul@15 | 148 | |
paul@15 | 149 | # Append class attributes to the image. |
paul@15 | 150 | |
paul@9 | 151 | attributes = obj.class_attributes() |
paul@49 | 152 | image += obj.attributes_as_list() |
paul@37 | 153 | pos += len(attributes.keys()) |
paul@9 | 154 | |
paul@38 | 155 | # Class-level code is generated separately. |
paul@41 | 156 | # The code location is set within the code generation |
paul@41 | 157 | # process for the module. |
paul@15 | 158 | |
paul@9 | 159 | # NOTE: Generate module and function code here. |
paul@9 | 160 | |
paul@9 | 161 | elif isinstance(obj, micropython.inspect.Function): |
paul@15 | 162 | |
paul@55 | 163 | # Fix the attributes. |
paul@55 | 164 | |
paul@55 | 165 | obj.finalise_attributes() |
paul@55 | 166 | |
paul@15 | 167 | # Position the function in the image. |
paul@15 | 168 | |
paul@22 | 169 | obj.location = pos |
paul@22 | 170 | |
paul@22 | 171 | # Add header details. |
paul@22 | 172 | |
paul@22 | 173 | image.append(obj) |
paul@22 | 174 | pos += 1 |
paul@9 | 175 | |
paul@15 | 176 | # Append the function code to the image. |
paul@15 | 177 | |
paul@44 | 178 | obj.code_location = pos |
paul@17 | 179 | code = trans.get_code(obj) |
paul@17 | 180 | image += code |
paul@17 | 181 | pos += len(code) |
paul@15 | 182 | |
paul@18 | 183 | # Remember the position of the module code. |
paul@18 | 184 | |
paul@19 | 185 | module.code_location = pos |
paul@18 | 186 | |
paul@15 | 187 | # Append the module top-level code to the image. |
paul@15 | 188 | |
paul@17 | 189 | code = trans.get_module_code() |
paul@17 | 190 | image += code |
paul@17 | 191 | pos += len(code) |
paul@15 | 192 | |
paul@19 | 193 | return image |
paul@9 | 194 | |
paul@7 | 195 | def get_object_table(self): |
paul@7 | 196 | |
paul@7 | 197 | "Return a table with details of attributes for classes and modules." |
paul@7 | 198 | |
paul@60 | 199 | if self.objtable is None: |
paul@60 | 200 | t = self.objtable = micropython.table.Table() |
paul@60 | 201 | for module in self.get_modules(): |
paul@60 | 202 | t.add(module.full_name(), module.module_attributes()) |
paul@60 | 203 | for obj in module.all_objects: |
paul@60 | 204 | if isinstance(obj, micropython.inspect.Class): |
paul@60 | 205 | t.add(obj.full_name(), obj.all_attributes()) |
paul@60 | 206 | |
paul@60 | 207 | return self.objtable |
paul@7 | 208 | |
paul@7 | 209 | def get_parameter_table(self): |
paul@7 | 210 | |
paul@7 | 211 | "Return a table with details of parameters for functions and methods." |
paul@7 | 212 | |
paul@60 | 213 | # Need the object table to get at class details. |
paul@60 | 214 | |
paul@60 | 215 | objtable = self.get_object_table() |
paul@60 | 216 | |
paul@60 | 217 | if self.paramtable is None: |
paul@60 | 218 | t = self.paramtable = micropython.table.Table() |
paul@60 | 219 | for module in self.get_modules(): |
paul@60 | 220 | for obj in module.all_objects: |
paul@60 | 221 | if isinstance(obj, micropython.inspect.Function): |
paul@60 | 222 | t.add(obj.full_name(), obj.parameters()) |
paul@60 | 223 | |
paul@60 | 224 | # Classes are callable, too. |
paul@61 | 225 | # Take details of the appropriate __init__ method to make an |
paul@61 | 226 | # entry for an instantiation function for the class. |
paul@60 | 227 | |
paul@60 | 228 | elif isinstance(obj, micropython.inspect.Class): |
paul@61 | 229 | t.add(obj.full_name(), obj.get_instantiator().parameters()) |
paul@60 | 230 | |
paul@60 | 231 | return self.paramtable |
paul@7 | 232 | |
paul@9 | 233 | # Import methods. |
paul@9 | 234 | |
paul@0 | 235 | def find_in_path(self, name): |
paul@0 | 236 | |
paul@0 | 237 | """ |
paul@0 | 238 | Find the given module 'name' in the search path, returning None where no |
paul@0 | 239 | such module could be found, or a 2-tuple from the 'find' method |
paul@0 | 240 | otherwise. |
paul@0 | 241 | """ |
paul@0 | 242 | |
paul@0 | 243 | for d in self.path: |
paul@0 | 244 | m = self.find(d, name) |
paul@0 | 245 | if m: return m |
paul@0 | 246 | return None |
paul@0 | 247 | |
paul@0 | 248 | def find(self, d, name): |
paul@0 | 249 | |
paul@0 | 250 | """ |
paul@0 | 251 | In the directory 'd', find the given module 'name', where 'name' can |
paul@0 | 252 | either refer to a single file module or to a package. Return None if the |
paul@0 | 253 | 'name' cannot be associated with either a file or a package directory, |
paul@0 | 254 | or a 2-tuple from '_find_package' or '_find_module' otherwise. |
paul@0 | 255 | """ |
paul@0 | 256 | |
paul@0 | 257 | m = self._find_package(d, name) |
paul@0 | 258 | if m: return m |
paul@0 | 259 | m = self._find_module(d, name) |
paul@0 | 260 | if m: return m |
paul@0 | 261 | return None |
paul@0 | 262 | |
paul@0 | 263 | def _find_module(self, d, name): |
paul@0 | 264 | |
paul@0 | 265 | """ |
paul@0 | 266 | In the directory 'd', find the given module 'name', returning None where |
paul@0 | 267 | no suitable file exists in the directory, or a 2-tuple consisting of |
paul@0 | 268 | None (indicating that no package directory is involved) and a filename |
paul@0 | 269 | indicating the location of the module. |
paul@0 | 270 | """ |
paul@0 | 271 | |
paul@0 | 272 | name_py = name + os.extsep + "py" |
paul@0 | 273 | filename = self._find_file(d, name_py) |
paul@0 | 274 | if filename: |
paul@0 | 275 | return None, filename |
paul@0 | 276 | return None |
paul@0 | 277 | |
paul@0 | 278 | def _find_package(self, d, name): |
paul@0 | 279 | |
paul@0 | 280 | """ |
paul@0 | 281 | In the directory 'd', find the given package 'name', returning None |
paul@0 | 282 | where no suitable package directory exists, or a 2-tuple consisting of |
paul@0 | 283 | a directory (indicating the location of the package directory itself) |
paul@0 | 284 | and a filename indicating the location of the __init__.py module which |
paul@0 | 285 | declares the package's top-level contents. |
paul@0 | 286 | """ |
paul@0 | 287 | |
paul@0 | 288 | filename = self._find_file(d, name) |
paul@0 | 289 | if filename: |
paul@0 | 290 | init_py = "__init__" + os.path.extsep + "py" |
paul@0 | 291 | init_py_filename = self._find_file(filename, init_py) |
paul@0 | 292 | if init_py_filename: |
paul@0 | 293 | return filename, init_py_filename |
paul@0 | 294 | return None |
paul@0 | 295 | |
paul@0 | 296 | def _find_file(self, d, filename): |
paul@0 | 297 | |
paul@0 | 298 | """ |
paul@0 | 299 | Return the filename obtained when searching the directory 'd' for the |
paul@0 | 300 | given 'filename', or None if no actual file exists for the filename. |
paul@0 | 301 | """ |
paul@0 | 302 | |
paul@0 | 303 | filename = os.path.join(d, filename) |
paul@0 | 304 | if os.path.exists(filename): |
paul@0 | 305 | return filename |
paul@0 | 306 | else: |
paul@0 | 307 | return None |
paul@0 | 308 | |
paul@0 | 309 | def load(self, name, return_leaf=0): |
paul@0 | 310 | |
paul@0 | 311 | """ |
paul@0 | 312 | Load the module or package with the given 'name'. Return an object |
paul@0 | 313 | referencing the loaded module or package, or None if no such module or |
paul@0 | 314 | package exists. |
paul@0 | 315 | """ |
paul@0 | 316 | |
paul@4 | 317 | if self.modules.has_key(name) and self.modules[name].loaded: |
paul@4 | 318 | #print "Cached (%s)" % name |
paul@4 | 319 | return self.modules[name] |
paul@35 | 320 | if self.verbose: |
paul@35 | 321 | print "Loading", name |
paul@0 | 322 | |
paul@0 | 323 | # Split the name into path components, and try to find the uppermost in |
paul@0 | 324 | # the search path. |
paul@0 | 325 | |
paul@0 | 326 | path = name.split(".") |
paul@0 | 327 | m = self.find_in_path(path[0]) |
paul@0 | 328 | if not m: |
paul@35 | 329 | if self.verbose: |
paul@35 | 330 | print "Not found (%s)" % path[0] |
paul@0 | 331 | return None # NOTE: Import error. |
paul@0 | 332 | d, filename = m |
paul@0 | 333 | |
paul@0 | 334 | # Either acquire a reference to an already-imported module, or load the |
paul@0 | 335 | # module from a file. |
paul@0 | 336 | |
paul@2 | 337 | top = module = self.load_from_file(filename, path[0]) |
paul@0 | 338 | |
paul@0 | 339 | # For hierarchical names, traverse each path component... |
paul@0 | 340 | |
paul@0 | 341 | if len(path) > 1: |
paul@0 | 342 | if not d: |
paul@35 | 343 | if self.verbose: |
paul@35 | 344 | print "No package (%s)" % filename |
paul@0 | 345 | return None # NOTE: Import error (package not found). |
paul@1 | 346 | else: |
paul@1 | 347 | self.add_submodules(d, module) |
paul@0 | 348 | |
paul@0 | 349 | path_so_far = path[:1] |
paul@0 | 350 | for p in path[1:]: |
paul@0 | 351 | path_so_far.append(p) |
paul@0 | 352 | |
paul@0 | 353 | # Find the package or module concerned. |
paul@0 | 354 | |
paul@0 | 355 | m = self.find(d, p) |
paul@0 | 356 | if not m: |
paul@35 | 357 | if self.verbose: |
paul@35 | 358 | print "Not found (%s)" % p |
paul@0 | 359 | return None # NOTE: Import error. |
paul@0 | 360 | d, filename = m |
paul@0 | 361 | module_name = ".".join(path_so_far) |
paul@0 | 362 | |
paul@0 | 363 | # Either reference an imported module or load one from a file. |
paul@0 | 364 | |
paul@2 | 365 | submodule = self.load_from_file(filename, module_name) |
paul@0 | 366 | |
paul@1 | 367 | if d: |
paul@1 | 368 | self.add_submodules(d, module) |
paul@1 | 369 | |
paul@0 | 370 | # Store the submodule within its parent module. |
paul@0 | 371 | |
paul@32 | 372 | module.set_module(p, submodule) |
paul@0 | 373 | module = submodule |
paul@0 | 374 | |
paul@0 | 375 | # Return either the deepest or the uppermost module. |
paul@0 | 376 | |
paul@0 | 377 | if return_leaf: |
paul@0 | 378 | return module |
paul@0 | 379 | else: |
paul@0 | 380 | return top |
paul@0 | 381 | |
paul@0 | 382 | def load_from_file(self, name, module_name=None): |
paul@0 | 383 | |
paul@0 | 384 | """ |
paul@0 | 385 | Load the module with the given 'name' (which may be a full module path). |
paul@0 | 386 | """ |
paul@0 | 387 | |
paul@0 | 388 | if module_name is None: |
paul@0 | 389 | module_name = "__main__" |
paul@0 | 390 | |
paul@1 | 391 | module = self.add_module(module_name) |
paul@2 | 392 | if not module.loaded and module not in self.loading: |
paul@2 | 393 | self.loading.add(module) |
paul@4 | 394 | #print "Parsing", name |
paul@2 | 395 | module.parse(name) |
paul@4 | 396 | #print "Done", name |
paul@2 | 397 | self.loading.remove(module) |
paul@2 | 398 | module.loaded = 1 |
paul@0 | 399 | |
paul@0 | 400 | # Record the module. |
paul@0 | 401 | |
paul@4 | 402 | #print "Loaded", module_name, "with namespace", module.namespace.keys() |
paul@0 | 403 | return module |
paul@0 | 404 | |
paul@1 | 405 | def add_module(self, module_name): |
paul@1 | 406 | |
paul@1 | 407 | """ |
paul@1 | 408 | Return the module with the given 'module_name', adding a new module |
paul@1 | 409 | object if one does not already exist. |
paul@1 | 410 | """ |
paul@1 | 411 | |
paul@1 | 412 | if not self.modules.has_key(module_name): |
paul@15 | 413 | self.modules[module_name] = module = micropython.inspect.InspectedModule(module_name, self) |
paul@1 | 414 | else: |
paul@1 | 415 | module = self.modules[module_name] |
paul@1 | 416 | return module |
paul@1 | 417 | |
paul@1 | 418 | def add_submodules(self, pathname, module): |
paul@1 | 419 | |
paul@1 | 420 | """ |
paul@1 | 421 | Work around insufficient __all__ declarations and examine the directory |
paul@1 | 422 | with the given 'pathname', adding submodules to the given 'module'. |
paul@1 | 423 | """ |
paul@1 | 424 | |
paul@1 | 425 | for filename in os.listdir(pathname): |
paul@50 | 426 | submodule, ext = os.path.splitext(filename) |
paul@50 | 427 | if ext not in ("", ".py"): |
paul@50 | 428 | continue |
paul@32 | 429 | module.set_module(submodule, self.add_module(module.name + "." + submodule)) |
paul@1 | 430 | |
paul@0 | 431 | # vim: tabstop=4 expandtab shiftwidth=4 |