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