paul@0 | 1 | #!/usr/bin/env python |
paul@0 | 2 | |
paul@0 | 3 | """ |
paulb@222 | 4 | The simplify package for processing Python source code. |
paul@0 | 5 | |
paulb@249 | 6 | Copyright (C) 2006, 2007 Paul Boddie <paul@boddie.org.uk> |
paul@0 | 7 | |
paulb@260 | 8 | This program is free software; you can redistribute it and/or modify it under |
paulb@260 | 9 | the terms of the GNU General Public License as published by the Free Software |
paulb@260 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paulb@260 | 11 | version. |
paul@0 | 12 | |
paulb@260 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paulb@260 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paulb@260 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paulb@260 | 16 | details. |
paul@0 | 17 | |
paulb@260 | 18 | You should have received a copy of the GNU General Public License along with |
paulb@260 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paulb@249 | 20 | |
paulb@249 | 21 | -------- |
paulb@249 | 22 | |
paulb@255 | 23 | To use this module, an importer should be constructed and the load_from_file |
paulb@255 | 24 | method used. Here, the standard path for module searching is employed: |
paulb@249 | 25 | |
paulb@249 | 26 | importer = Importer(sys.path) |
paulb@249 | 27 | importer.load_from_file(builtins) |
paulb@249 | 28 | importer.load_from_file(filename, builtins) |
paulb@261 | 29 | |
paulb@261 | 30 | Such importer objects are the most convenient mechanism through which the |
paulb@261 | 31 | functionality of the simplify package may be accessed. |
paul@0 | 32 | """ |
paul@0 | 33 | |
paulb@249 | 34 | from simplify.simplified import * |
paulb@249 | 35 | import simplify.ast |
paulb@249 | 36 | import simplify.fixnames |
paulb@249 | 37 | import simplify.annotate |
paulb@249 | 38 | import simplify.fixinstances |
paulb@249 | 39 | |
paulb@249 | 40 | class Importer: |
paulb@249 | 41 | |
paulb@249 | 42 | "An import machine, searching for and loading modules." |
paulb@249 | 43 | |
paulb@261 | 44 | def __init__(self, path=None, annotate=1): |
paulb@249 | 45 | |
paulb@249 | 46 | """ |
paulb@249 | 47 | Initialise the importer with the given search 'path' - a list of |
paulb@249 | 48 | directories to search for Python modules. If the optional 'annotate' |
paulb@249 | 49 | parameter is set to a false value (unlike the default), no annotation |
paulb@249 | 50 | will be performed. |
paulb@249 | 51 | """ |
paulb@249 | 52 | |
paulb@249 | 53 | self.path = path or [os.getcwd()] |
paulb@249 | 54 | self.path.append(libdir) |
paulb@249 | 55 | self.annotate = annotate |
paulb@249 | 56 | self.modules = {} |
paulb@249 | 57 | |
paulb@249 | 58 | def get_modules(self): |
paulb@255 | 59 | |
paulb@255 | 60 | "Return all modules known to the importer." |
paulb@255 | 61 | |
paulb@249 | 62 | return self.modules.values() |
paulb@249 | 63 | |
paulb@249 | 64 | def find_in_path(self, name): |
paulb@249 | 65 | |
paulb@249 | 66 | """ |
paulb@249 | 67 | Find the given module 'name' in the search path, returning None where no |
paulb@249 | 68 | such module could be found, or a 2-tuple from the 'find' method |
paulb@249 | 69 | otherwise. |
paulb@249 | 70 | """ |
paulb@249 | 71 | |
paulb@249 | 72 | for d in self.path: |
paulb@249 | 73 | m = self.find(d, name) |
paulb@249 | 74 | if m: return m |
paulb@249 | 75 | return None |
paulb@249 | 76 | |
paulb@249 | 77 | def find(self, d, name): |
paulb@249 | 78 | |
paulb@249 | 79 | """ |
paulb@249 | 80 | In the directory 'd', find the given module 'name', where 'name' can |
paulb@249 | 81 | either refer to a single file module or to a package. Return None if the |
paulb@249 | 82 | 'name' cannot be associated with either a file or a package directory, |
paulb@249 | 83 | or a 2-tuple from '_find_package' or '_find_module' otherwise. |
paulb@249 | 84 | """ |
paulb@249 | 85 | |
paulb@249 | 86 | m = self._find_package(d, name) |
paulb@249 | 87 | if m: return m |
paulb@249 | 88 | m = self._find_module(d, name) |
paulb@249 | 89 | if m: return m |
paulb@249 | 90 | return None |
paulb@249 | 91 | |
paulb@249 | 92 | def _find_module(self, d, name): |
paulb@249 | 93 | |
paulb@249 | 94 | """ |
paulb@249 | 95 | In the directory 'd', find the given module 'name', returning None where |
paulb@249 | 96 | no suitable file exists in the directory, or a 2-tuple consisting of |
paulb@249 | 97 | None (indicating that no package directory is involved) and a filename |
paulb@249 | 98 | indicating the location of the module. |
paulb@249 | 99 | """ |
paulb@249 | 100 | |
paulb@249 | 101 | name_py = name + os.extsep + "py" |
paulb@249 | 102 | filename = self._find_file(d, name_py) |
paulb@249 | 103 | if filename: |
paulb@249 | 104 | return None, filename |
paulb@249 | 105 | return None |
paulb@249 | 106 | |
paulb@249 | 107 | def _find_package(self, d, name): |
paulb@249 | 108 | |
paulb@249 | 109 | """ |
paulb@249 | 110 | In the directory 'd', find the given package 'name', returning None |
paulb@249 | 111 | where no suitable package directory exists, or a 2-tuple consisting of |
paulb@249 | 112 | a directory (indicating the location of the package directory itself) |
paulb@249 | 113 | and a filename indicating the location of the __init__.py module which |
paulb@249 | 114 | declares the package's top-level contents. |
paulb@249 | 115 | """ |
paulb@249 | 116 | |
paulb@249 | 117 | filename = self._find_file(d, name) |
paulb@249 | 118 | if filename: |
paulb@249 | 119 | init_py = "__init__" + os.path.extsep + "py" |
paulb@249 | 120 | init_py_filename = self._find_file(filename, init_py) |
paulb@249 | 121 | if init_py_filename: |
paulb@249 | 122 | return filename, init_py_filename |
paulb@249 | 123 | return None |
paulb@249 | 124 | |
paulb@249 | 125 | def _find_file(self, d, filename): |
paulb@249 | 126 | |
paulb@249 | 127 | """ |
paulb@249 | 128 | Return the filename obtained when searching the directory 'd' for the |
paulb@249 | 129 | given 'filename', or None if no actual file exists for the filename. |
paulb@249 | 130 | """ |
paulb@249 | 131 | |
paulb@249 | 132 | filename = os.path.join(d, filename) |
paulb@249 | 133 | if os.path.exists(filename): |
paulb@249 | 134 | return filename |
paulb@249 | 135 | else: |
paulb@249 | 136 | return None |
paulb@249 | 137 | |
paulb@249 | 138 | def load(self, name, builtins=None, alias=None): |
paulb@249 | 139 | |
paulb@249 | 140 | """ |
paulb@249 | 141 | Load the module or package with the given 'name' and using the specified |
paulb@249 | 142 | 'builtins'. Return an Attribute object referencing the loaded module or |
paulb@249 | 143 | package, or None if no such module or package exists. |
paulb@249 | 144 | """ |
paulb@249 | 145 | |
paulb@249 | 146 | if self.modules.has_key(name): |
paulb@249 | 147 | return Attribute(None, self.modules[name]) |
paulb@249 | 148 | |
paulb@255 | 149 | # Split the name into path components, and try to find the uppermost in |
paulb@255 | 150 | # the search path. |
paulb@255 | 151 | |
paulb@249 | 152 | path = name.split(".") |
paulb@249 | 153 | m = self.find_in_path(path[0]) |
paulb@249 | 154 | if not m: |
paulb@249 | 155 | return None # NOTE: Import error. |
paulb@249 | 156 | d, filename = m |
paulb@249 | 157 | |
paulb@255 | 158 | # Either acquire a reference to an already-imported module, or load the |
paulb@255 | 159 | # module from a file. |
paulb@255 | 160 | |
paulb@249 | 161 | if self.modules.has_key(path[0]): |
paulb@249 | 162 | top = module = self.modules[path[0]] |
paulb@249 | 163 | else: |
paulb@249 | 164 | top = module = self.load_from_file(filename, builtins, path[0]) |
paulb@249 | 165 | |
paulb@255 | 166 | # For hierarchical names, traverse each path component... |
paulb@255 | 167 | |
paulb@249 | 168 | if len(path) > 1: |
paulb@249 | 169 | path_so_far = path[:1] |
paulb@249 | 170 | for p in path[1:]: |
paulb@249 | 171 | path_so_far.append(p) |
paulb@255 | 172 | |
paulb@255 | 173 | # Find the package or module concerned. |
paulb@255 | 174 | |
paulb@249 | 175 | m = self.find(d, p) |
paulb@249 | 176 | if not m: |
paulb@249 | 177 | return None # NOTE: Import error. |
paulb@249 | 178 | d, filename = m |
paulb@249 | 179 | module_name = ".".join(path_so_far) |
paulb@249 | 180 | |
paulb@255 | 181 | # Either reference an imported module or load one from a file. |
paulb@255 | 182 | |
paulb@249 | 183 | if self.modules.has_key(module_name): |
paulb@249 | 184 | submodule = self.modules[module_name] |
paulb@249 | 185 | else: |
paulb@249 | 186 | submodule = self.load_from_file(filename, builtins, module_name) |
paulb@249 | 187 | |
paulb@249 | 188 | # Store the submodule within its parent module. |
paulb@249 | 189 | |
paulb@249 | 190 | module.namespace[p] = [Attribute(None, submodule)] |
paulb@249 | 191 | module = submodule |
paulb@249 | 192 | |
paulb@255 | 193 | # Return either the deepest or the uppermost module. |
paulb@255 | 194 | |
paulb@249 | 195 | if alias: |
paulb@249 | 196 | return Attribute(None, module) |
paulb@249 | 197 | else: |
paulb@249 | 198 | return Attribute(None, top) |
paulb@249 | 199 | |
paulb@249 | 200 | def load_from_file(self, name, builtins=None, module_name=None): |
paulb@249 | 201 | |
paulb@249 | 202 | """ |
paulb@249 | 203 | Load the module with the given 'name' (which may be a full module path), |
paulb@249 | 204 | using the optional 'builtins' to resolve built-in names. |
paulb@249 | 205 | """ |
paulb@249 | 206 | |
paulb@249 | 207 | if module_name is None: |
paulb@249 | 208 | if builtins is None: |
paulb@249 | 209 | module_name = "__builtins__" |
paulb@249 | 210 | else: |
paulb@249 | 211 | module_name = "__main__" |
paulb@249 | 212 | |
paulb@255 | 213 | # Simplify, fix names, and annotate the module. |
paulb@255 | 214 | |
paulb@249 | 215 | module = simplify.ast.simplify(name, builtins is None, module_name) |
paulb@249 | 216 | simplify.fixnames.fix(module, builtins) |
paulb@249 | 217 | if self.annotate: |
paulb@249 | 218 | simplify.annotate.annotate(module, builtins, self) |
paulb@249 | 219 | |
paulb@249 | 220 | # Record the module. |
paulb@249 | 221 | |
paulb@249 | 222 | self.modules[module_name] = module |
paulb@249 | 223 | return module |
paulb@249 | 224 | |
paulb@249 | 225 | def fix_instances(self): |
paulb@249 | 226 | |
paulb@249 | 227 | "Fix instances for all modules loaded by this importer." |
paulb@249 | 228 | |
paul@284 | 229 | count = simplify.fixinstances.system.count |
paul@284 | 230 | while 1: |
paul@284 | 231 | for module in self.get_modules(): |
paul@284 | 232 | simplify.fixinstances.fix_structures(module) |
paul@284 | 233 | |
paul@284 | 234 | print simplify.fixinstances.system.count - count, "attributes changed" |
paul@284 | 235 | |
paul@284 | 236 | for module in self.get_modules(): |
paul@284 | 237 | simplify.fixinstances.fix_signatures(module) |
paul@284 | 238 | |
paul@284 | 239 | print simplify.fixinstances.system.count - count, "parameters changed" |
paul@284 | 240 | |
paul@284 | 241 | for module in self.get_modules(): |
paul@284 | 242 | simplify.fixinstances.fix(module) |
paul@284 | 243 | |
paul@284 | 244 | print simplify.fixinstances.system.count - count, "sites changed" |
paul@284 | 245 | |
paul@284 | 246 | if simplify.fixinstances.system.count == count: |
paul@284 | 247 | break |
paul@284 | 248 | else: |
paul@284 | 249 | print "Processing structures again after", simplify.fixinstances.system.count - count, "changes" |
paul@284 | 250 | count = simplify.fixinstances.system.count |
paulb@249 | 251 | |
paul@0 | 252 | # vim: tabstop=4 expandtab shiftwidth=4 |