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