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@63 | 53 | supported_optimisations = micropython.ast.Translation.supported_optimisations |
paul@63 | 54 | |
paul@35 | 55 | def __init__(self, path=None, verbose=0): |
paul@0 | 56 | |
paul@0 | 57 | """ |
paul@0 | 58 | Initialise the importer with the given search 'path' - a list of |
paul@0 | 59 | directories to search for Python modules. |
paul@35 | 60 | |
paul@35 | 61 | The optional 'verbose' parameter causes output concerning the activities |
paul@35 | 62 | of the object to be produced if set to a true value (not the default). |
paul@0 | 63 | """ |
paul@0 | 64 | |
paul@0 | 65 | self.path = path or [os.getcwd()] |
paul@35 | 66 | self.verbose = verbose |
paul@61 | 67 | self.modules = {} |
paul@2 | 68 | self.loading = set() |
paul@0 | 69 | |
paul@60 | 70 | # Remember the tables once generated. |
paul@60 | 71 | |
paul@60 | 72 | self.objtable = None |
paul@60 | 73 | self.paramtable = None |
paul@60 | 74 | |
paul@3 | 75 | def vacuum(self): |
paul@3 | 76 | |
paul@3 | 77 | "Tidy up the modules." |
paul@3 | 78 | |
paul@3 | 79 | for name, module in self.modules.items(): |
paul@3 | 80 | if module.loaded: |
paul@3 | 81 | module.vacuum() |
paul@3 | 82 | else: |
paul@3 | 83 | del self.modules[name] |
paul@3 | 84 | |
paul@0 | 85 | def get_modules(self): |
paul@0 | 86 | |
paul@0 | 87 | "Return all modules known to the importer." |
paul@0 | 88 | |
paul@0 | 89 | return self.modules.values() |
paul@0 | 90 | |
paul@62 | 91 | def get_image(self, with_builtins=0, optimisations=None): |
paul@9 | 92 | |
paul@9 | 93 | "Return a dictionary mapping modules to structures." |
paul@9 | 94 | |
paul@60 | 95 | objtable = self.get_object_table() |
paul@60 | 96 | paramtable = self.get_parameter_table() |
paul@21 | 97 | |
paul@9 | 98 | image = [] |
paul@18 | 99 | |
paul@18 | 100 | for module_name, module in self.modules.items(): |
paul@44 | 101 | if not with_builtins and module_name == "__builtins__": |
paul@44 | 102 | continue |
paul@44 | 103 | |
paul@54 | 104 | # Fix the attributes. |
paul@54 | 105 | |
paul@54 | 106 | module.finalise_attributes() |
paul@54 | 107 | |
paul@9 | 108 | pos = len(image) |
paul@15 | 109 | |
paul@21 | 110 | # Append constants to the image. |
paul@21 | 111 | |
paul@22 | 112 | for const in module.constants(): |
paul@21 | 113 | const.location = pos |
paul@21 | 114 | image.append(const) |
paul@21 | 115 | pos += 1 |
paul@15 | 116 | |
paul@55 | 117 | # Position the module in the image and make a translation. |
paul@55 | 118 | |
paul@55 | 119 | module.location = pos |
paul@72 | 120 | trans = micropython.ast.Translation(module, self, optimisations) |
paul@55 | 121 | |
paul@37 | 122 | # Add header details. |
paul@37 | 123 | |
paul@37 | 124 | image.append(module) |
paul@37 | 125 | pos += 1 |
paul@37 | 126 | |
paul@15 | 127 | # Append module attributes to the image. |
paul@15 | 128 | |
paul@9 | 129 | attributes = module.module_attributes() |
paul@49 | 130 | image += module.attributes_as_list() |
paul@37 | 131 | pos += len(attributes.keys()) |
paul@9 | 132 | |
paul@15 | 133 | # Append classes and functions to the image. |
paul@15 | 134 | |
paul@9 | 135 | for obj in module.all_objects: |
paul@55 | 136 | if isinstance(obj, micropython.inspect.Class): |
paul@54 | 137 | |
paul@55 | 138 | # Fix the attributes. |
paul@54 | 139 | |
paul@55 | 140 | obj.finalise_attributes() |
paul@15 | 141 | |
paul@15 | 142 | # Position the class in the image. |
paul@15 | 143 | |
paul@22 | 144 | obj.location = pos |
paul@22 | 145 | |
paul@22 | 146 | # Add header details. |
paul@22 | 147 | |
paul@22 | 148 | image.append(obj) |
paul@22 | 149 | pos += 1 |
paul@15 | 150 | |
paul@15 | 151 | # Append class attributes to the image. |
paul@15 | 152 | |
paul@9 | 153 | attributes = obj.class_attributes() |
paul@49 | 154 | image += obj.attributes_as_list() |
paul@37 | 155 | pos += len(attributes.keys()) |
paul@9 | 156 | |
paul@38 | 157 | # Class-level code is generated separately. |
paul@41 | 158 | # The code location is set within the code generation |
paul@41 | 159 | # process for the module. |
paul@15 | 160 | |
paul@9 | 161 | # NOTE: Generate module and function code here. |
paul@9 | 162 | |
paul@9 | 163 | elif isinstance(obj, micropython.inspect.Function): |
paul@15 | 164 | |
paul@55 | 165 | # Fix the attributes. |
paul@55 | 166 | |
paul@55 | 167 | obj.finalise_attributes() |
paul@55 | 168 | |
paul@15 | 169 | # Position the function in the image. |
paul@15 | 170 | |
paul@22 | 171 | obj.location = pos |
paul@22 | 172 | |
paul@22 | 173 | # Add header details. |
paul@22 | 174 | |
paul@22 | 175 | image.append(obj) |
paul@22 | 176 | pos += 1 |
paul@9 | 177 | |
paul@66 | 178 | # Append any default values to the image. |
paul@66 | 179 | |
paul@66 | 180 | image += obj.default_attrs |
paul@66 | 181 | pos += len(obj.default_attrs) |
paul@66 | 182 | |
paul@15 | 183 | # Append the function code to the image. |
paul@15 | 184 | |
paul@44 | 185 | obj.code_location = pos |
paul@17 | 186 | code = trans.get_code(obj) |
paul@17 | 187 | image += code |
paul@17 | 188 | pos += len(code) |
paul@15 | 189 | |
paul@18 | 190 | # Remember the position of the module code. |
paul@18 | 191 | |
paul@19 | 192 | module.code_location = pos |
paul@18 | 193 | |
paul@15 | 194 | # Append the module top-level code to the image. |
paul@15 | 195 | |
paul@17 | 196 | code = trans.get_module_code() |
paul@17 | 197 | image += code |
paul@17 | 198 | pos += len(code) |
paul@15 | 199 | |
paul@19 | 200 | return image |
paul@9 | 201 | |
paul@7 | 202 | def get_object_table(self): |
paul@7 | 203 | |
paul@7 | 204 | "Return a table with details of attributes for classes and modules." |
paul@7 | 205 | |
paul@60 | 206 | if self.objtable is None: |
paul@60 | 207 | t = self.objtable = micropython.table.Table() |
paul@60 | 208 | for module in self.get_modules(): |
paul@60 | 209 | t.add(module.full_name(), module.module_attributes()) |
paul@60 | 210 | for obj in module.all_objects: |
paul@60 | 211 | if isinstance(obj, micropython.inspect.Class): |
paul@60 | 212 | t.add(obj.full_name(), obj.all_attributes()) |
paul@60 | 213 | |
paul@60 | 214 | return self.objtable |
paul@7 | 215 | |
paul@7 | 216 | def get_parameter_table(self): |
paul@7 | 217 | |
paul@7 | 218 | "Return a table with details of parameters for functions and methods." |
paul@7 | 219 | |
paul@60 | 220 | # Need the object table to get at class details. |
paul@60 | 221 | |
paul@60 | 222 | objtable = self.get_object_table() |
paul@60 | 223 | |
paul@60 | 224 | if self.paramtable is None: |
paul@60 | 225 | t = self.paramtable = micropython.table.Table() |
paul@71 | 226 | |
paul@71 | 227 | # Visit each module, getting function and method details. |
paul@71 | 228 | |
paul@60 | 229 | for module in self.get_modules(): |
paul@60 | 230 | for obj in module.all_objects: |
paul@60 | 231 | if isinstance(obj, micropython.inspect.Function): |
paul@60 | 232 | t.add(obj.full_name(), obj.parameters()) |
paul@60 | 233 | |
paul@60 | 234 | # Classes are callable, too. |
paul@61 | 235 | # Take details of the appropriate __init__ method to make an |
paul@61 | 236 | # entry for an instantiation function for the class. |
paul@60 | 237 | |
paul@60 | 238 | elif isinstance(obj, micropython.inspect.Class): |
paul@61 | 239 | t.add(obj.full_name(), obj.get_instantiator().parameters()) |
paul@60 | 240 | |
paul@71 | 241 | # Filter out all parameter table entries not referenced by keyword |
paul@71 | 242 | # arguments. |
paul@71 | 243 | |
paul@71 | 244 | keyword_names = set() |
paul@71 | 245 | |
paul@71 | 246 | for module in self.get_modules(): |
paul@71 | 247 | keyword_names.update(module.keyword_names) |
paul@71 | 248 | |
paul@71 | 249 | for function_name, parameters in t.table.items(): |
paul@71 | 250 | for name in parameters.keys(): |
paul@71 | 251 | if name in keyword_names: |
paul@71 | 252 | break |
paul@71 | 253 | else: |
paul@71 | 254 | del t.table[function_name] |
paul@71 | 255 | |
paul@60 | 256 | return self.paramtable |
paul@7 | 257 | |
paul@9 | 258 | # Import methods. |
paul@9 | 259 | |
paul@0 | 260 | def find_in_path(self, name): |
paul@0 | 261 | |
paul@0 | 262 | """ |
paul@0 | 263 | Find the given module 'name' in the search path, returning None where no |
paul@0 | 264 | such module could be found, or a 2-tuple from the 'find' method |
paul@0 | 265 | otherwise. |
paul@0 | 266 | """ |
paul@0 | 267 | |
paul@0 | 268 | for d in self.path: |
paul@0 | 269 | m = self.find(d, name) |
paul@0 | 270 | if m: return m |
paul@0 | 271 | return None |
paul@0 | 272 | |
paul@0 | 273 | def find(self, d, name): |
paul@0 | 274 | |
paul@0 | 275 | """ |
paul@0 | 276 | In the directory 'd', find the given module 'name', where 'name' can |
paul@0 | 277 | either refer to a single file module or to a package. Return None if the |
paul@0 | 278 | 'name' cannot be associated with either a file or a package directory, |
paul@0 | 279 | or a 2-tuple from '_find_package' or '_find_module' otherwise. |
paul@0 | 280 | """ |
paul@0 | 281 | |
paul@0 | 282 | m = self._find_package(d, name) |
paul@0 | 283 | if m: return m |
paul@0 | 284 | m = self._find_module(d, name) |
paul@0 | 285 | if m: return m |
paul@0 | 286 | return None |
paul@0 | 287 | |
paul@0 | 288 | def _find_module(self, d, name): |
paul@0 | 289 | |
paul@0 | 290 | """ |
paul@0 | 291 | In the directory 'd', find the given module 'name', returning None where |
paul@0 | 292 | no suitable file exists in the directory, or a 2-tuple consisting of |
paul@0 | 293 | None (indicating that no package directory is involved) and a filename |
paul@0 | 294 | indicating the location of the module. |
paul@0 | 295 | """ |
paul@0 | 296 | |
paul@0 | 297 | name_py = name + os.extsep + "py" |
paul@0 | 298 | filename = self._find_file(d, name_py) |
paul@0 | 299 | if filename: |
paul@0 | 300 | return None, filename |
paul@0 | 301 | return None |
paul@0 | 302 | |
paul@0 | 303 | def _find_package(self, d, name): |
paul@0 | 304 | |
paul@0 | 305 | """ |
paul@0 | 306 | In the directory 'd', find the given package 'name', returning None |
paul@0 | 307 | where no suitable package directory exists, or a 2-tuple consisting of |
paul@0 | 308 | a directory (indicating the location of the package directory itself) |
paul@0 | 309 | and a filename indicating the location of the __init__.py module which |
paul@0 | 310 | declares the package's top-level contents. |
paul@0 | 311 | """ |
paul@0 | 312 | |
paul@0 | 313 | filename = self._find_file(d, name) |
paul@0 | 314 | if filename: |
paul@0 | 315 | init_py = "__init__" + os.path.extsep + "py" |
paul@0 | 316 | init_py_filename = self._find_file(filename, init_py) |
paul@0 | 317 | if init_py_filename: |
paul@0 | 318 | return filename, init_py_filename |
paul@0 | 319 | return None |
paul@0 | 320 | |
paul@0 | 321 | def _find_file(self, d, filename): |
paul@0 | 322 | |
paul@0 | 323 | """ |
paul@0 | 324 | Return the filename obtained when searching the directory 'd' for the |
paul@0 | 325 | given 'filename', or None if no actual file exists for the filename. |
paul@0 | 326 | """ |
paul@0 | 327 | |
paul@0 | 328 | filename = os.path.join(d, filename) |
paul@0 | 329 | if os.path.exists(filename): |
paul@0 | 330 | return filename |
paul@0 | 331 | else: |
paul@0 | 332 | return None |
paul@0 | 333 | |
paul@0 | 334 | def load(self, name, return_leaf=0): |
paul@0 | 335 | |
paul@0 | 336 | """ |
paul@0 | 337 | Load the module or package with the given 'name'. Return an object |
paul@0 | 338 | referencing the loaded module or package, or None if no such module or |
paul@0 | 339 | package exists. |
paul@0 | 340 | """ |
paul@0 | 341 | |
paul@4 | 342 | if self.modules.has_key(name) and self.modules[name].loaded: |
paul@4 | 343 | #print "Cached (%s)" % name |
paul@4 | 344 | return self.modules[name] |
paul@35 | 345 | if self.verbose: |
paul@35 | 346 | print "Loading", name |
paul@0 | 347 | |
paul@0 | 348 | # Split the name into path components, and try to find the uppermost in |
paul@0 | 349 | # the search path. |
paul@0 | 350 | |
paul@0 | 351 | path = name.split(".") |
paul@0 | 352 | m = self.find_in_path(path[0]) |
paul@0 | 353 | if not m: |
paul@35 | 354 | if self.verbose: |
paul@35 | 355 | print "Not found (%s)" % path[0] |
paul@0 | 356 | return None # NOTE: Import error. |
paul@0 | 357 | d, filename = m |
paul@0 | 358 | |
paul@0 | 359 | # Either acquire a reference to an already-imported module, or load the |
paul@0 | 360 | # module from a file. |
paul@0 | 361 | |
paul@2 | 362 | top = module = self.load_from_file(filename, path[0]) |
paul@0 | 363 | |
paul@0 | 364 | # For hierarchical names, traverse each path component... |
paul@0 | 365 | |
paul@0 | 366 | if len(path) > 1: |
paul@0 | 367 | if not d: |
paul@35 | 368 | if self.verbose: |
paul@35 | 369 | print "No package (%s)" % filename |
paul@0 | 370 | return None # NOTE: Import error (package not found). |
paul@1 | 371 | else: |
paul@1 | 372 | self.add_submodules(d, module) |
paul@0 | 373 | |
paul@0 | 374 | path_so_far = path[:1] |
paul@0 | 375 | for p in path[1:]: |
paul@0 | 376 | path_so_far.append(p) |
paul@0 | 377 | |
paul@0 | 378 | # Find the package or module concerned. |
paul@0 | 379 | |
paul@0 | 380 | m = self.find(d, p) |
paul@0 | 381 | if not m: |
paul@35 | 382 | if self.verbose: |
paul@35 | 383 | print "Not found (%s)" % p |
paul@0 | 384 | return None # NOTE: Import error. |
paul@0 | 385 | d, filename = m |
paul@0 | 386 | module_name = ".".join(path_so_far) |
paul@0 | 387 | |
paul@0 | 388 | # Either reference an imported module or load one from a file. |
paul@0 | 389 | |
paul@2 | 390 | submodule = self.load_from_file(filename, module_name) |
paul@0 | 391 | |
paul@1 | 392 | if d: |
paul@1 | 393 | self.add_submodules(d, module) |
paul@1 | 394 | |
paul@0 | 395 | # Store the submodule within its parent module. |
paul@0 | 396 | |
paul@32 | 397 | module.set_module(p, submodule) |
paul@0 | 398 | module = submodule |
paul@0 | 399 | |
paul@0 | 400 | # Return either the deepest or the uppermost module. |
paul@0 | 401 | |
paul@0 | 402 | if return_leaf: |
paul@0 | 403 | return module |
paul@0 | 404 | else: |
paul@0 | 405 | return top |
paul@0 | 406 | |
paul@0 | 407 | def load_from_file(self, name, module_name=None): |
paul@0 | 408 | |
paul@0 | 409 | """ |
paul@0 | 410 | Load the module with the given 'name' (which may be a full module path). |
paul@0 | 411 | """ |
paul@0 | 412 | |
paul@0 | 413 | if module_name is None: |
paul@0 | 414 | module_name = "__main__" |
paul@0 | 415 | |
paul@1 | 416 | module = self.add_module(module_name) |
paul@2 | 417 | if not module.loaded and module not in self.loading: |
paul@2 | 418 | self.loading.add(module) |
paul@4 | 419 | #print "Parsing", name |
paul@2 | 420 | module.parse(name) |
paul@4 | 421 | #print "Done", name |
paul@2 | 422 | self.loading.remove(module) |
paul@2 | 423 | module.loaded = 1 |
paul@0 | 424 | |
paul@0 | 425 | # Record the module. |
paul@0 | 426 | |
paul@4 | 427 | #print "Loaded", module_name, "with namespace", module.namespace.keys() |
paul@0 | 428 | return module |
paul@0 | 429 | |
paul@1 | 430 | def add_module(self, module_name): |
paul@1 | 431 | |
paul@1 | 432 | """ |
paul@1 | 433 | Return the module with the given 'module_name', adding a new module |
paul@1 | 434 | object if one does not already exist. |
paul@1 | 435 | """ |
paul@1 | 436 | |
paul@1 | 437 | if not self.modules.has_key(module_name): |
paul@15 | 438 | self.modules[module_name] = module = micropython.inspect.InspectedModule(module_name, self) |
paul@1 | 439 | else: |
paul@1 | 440 | module = self.modules[module_name] |
paul@1 | 441 | return module |
paul@1 | 442 | |
paul@1 | 443 | def add_submodules(self, pathname, module): |
paul@1 | 444 | |
paul@1 | 445 | """ |
paul@1 | 446 | Work around insufficient __all__ declarations and examine the directory |
paul@1 | 447 | with the given 'pathname', adding submodules to the given 'module'. |
paul@1 | 448 | """ |
paul@1 | 449 | |
paul@1 | 450 | for filename in os.listdir(pathname): |
paul@50 | 451 | submodule, ext = os.path.splitext(filename) |
paul@50 | 452 | if ext not in ("", ".py"): |
paul@50 | 453 | continue |
paul@32 | 454 | module.set_module(submodule, self.add_module(module.name + "." + submodule)) |
paul@1 | 455 | |
paul@0 | 456 | # vim: tabstop=4 expandtab shiftwidth=4 |