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