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