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 | |
paul@0 | 8 | This software is free software; you can redistribute it and/or |
paul@0 | 9 | modify it under the terms of the GNU General Public License as |
paul@0 | 10 | published by the Free Software Foundation; either version 2 of |
paul@0 | 11 | the License, or (at your option) any later version. |
paul@0 | 12 | |
paul@0 | 13 | This software is distributed in the hope that it will be useful, |
paul@0 | 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@0 | 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@0 | 16 | GNU General Public License for more details. |
paul@0 | 17 | |
paul@0 | 18 | You should have received a copy of the GNU General Public |
paul@0 | 19 | License along with this library; see the file LICENCE.txt |
paul@0 | 20 | If not, write to the Free Software Foundation, Inc., |
paul@0 | 21 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
paulb@249 | 22 | |
paulb@249 | 23 | -------- |
paulb@249 | 24 | |
paulb@249 | 25 | To use this module, an importer should be constructed and the load method used. |
paulb@249 | 26 | Here, the standard path for module searching is used: |
paulb@249 | 27 | |
paulb@249 | 28 | importer = Importer(sys.path) |
paulb@249 | 29 | importer.load_from_file(builtins) |
paulb@249 | 30 | importer.load_from_file(filename, builtins) |
paul@0 | 31 | """ |
paul@0 | 32 | |
paulb@249 | 33 | from simplify.simplified import * |
paulb@249 | 34 | import simplify.ast |
paulb@249 | 35 | import simplify.fixnames |
paulb@249 | 36 | import simplify.annotate |
paulb@249 | 37 | import simplify.fixinstances |
paulb@249 | 38 | |
paulb@249 | 39 | class Importer: |
paulb@249 | 40 | |
paulb@249 | 41 | "An import machine, searching for and loading modules." |
paulb@249 | 42 | |
paulb@249 | 43 | def __init__(self, path=None, annotate=1, fixinstances=1): |
paulb@249 | 44 | |
paulb@249 | 45 | """ |
paulb@249 | 46 | Initialise the importer with the given search 'path' - a list of |
paulb@249 | 47 | directories to search for Python modules. If the optional 'annotate' |
paulb@249 | 48 | parameter is set to a false value (unlike the default), no annotation |
paulb@249 | 49 | will be performed. |
paulb@249 | 50 | false value (unlike the default), no instance fixing 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@249 | 59 | return self.modules.values() |
paulb@249 | 60 | |
paulb@249 | 61 | def find_in_path(self, name): |
paulb@249 | 62 | |
paulb@249 | 63 | """ |
paulb@249 | 64 | Find the given module 'name' in the search path, returning None where no |
paulb@249 | 65 | such module could be found, or a 2-tuple from the 'find' method |
paulb@249 | 66 | otherwise. |
paulb@249 | 67 | """ |
paulb@249 | 68 | |
paulb@249 | 69 | for d in self.path: |
paulb@249 | 70 | m = self.find(d, name) |
paulb@249 | 71 | if m: return m |
paulb@249 | 72 | return None |
paulb@249 | 73 | |
paulb@249 | 74 | def find(self, d, name): |
paulb@249 | 75 | |
paulb@249 | 76 | """ |
paulb@249 | 77 | In the directory 'd', find the given module 'name', where 'name' can |
paulb@249 | 78 | either refer to a single file module or to a package. Return None if the |
paulb@249 | 79 | 'name' cannot be associated with either a file or a package directory, |
paulb@249 | 80 | or a 2-tuple from '_find_package' or '_find_module' otherwise. |
paulb@249 | 81 | """ |
paulb@249 | 82 | |
paulb@249 | 83 | m = self._find_package(d, name) |
paulb@249 | 84 | if m: return m |
paulb@249 | 85 | m = self._find_module(d, name) |
paulb@249 | 86 | if m: return m |
paulb@249 | 87 | return None |
paulb@249 | 88 | |
paulb@249 | 89 | def _find_module(self, d, name): |
paulb@249 | 90 | |
paulb@249 | 91 | """ |
paulb@249 | 92 | In the directory 'd', find the given module 'name', returning None where |
paulb@249 | 93 | no suitable file exists in the directory, or a 2-tuple consisting of |
paulb@249 | 94 | None (indicating that no package directory is involved) and a filename |
paulb@249 | 95 | indicating the location of the module. |
paulb@249 | 96 | """ |
paulb@249 | 97 | |
paulb@249 | 98 | name_py = name + os.extsep + "py" |
paulb@249 | 99 | filename = self._find_file(d, name_py) |
paulb@249 | 100 | if filename: |
paulb@249 | 101 | return None, filename |
paulb@249 | 102 | return None |
paulb@249 | 103 | |
paulb@249 | 104 | def _find_package(self, d, name): |
paulb@249 | 105 | |
paulb@249 | 106 | """ |
paulb@249 | 107 | In the directory 'd', find the given package 'name', returning None |
paulb@249 | 108 | where no suitable package directory exists, or a 2-tuple consisting of |
paulb@249 | 109 | a directory (indicating the location of the package directory itself) |
paulb@249 | 110 | and a filename indicating the location of the __init__.py module which |
paulb@249 | 111 | declares the package's top-level contents. |
paulb@249 | 112 | """ |
paulb@249 | 113 | |
paulb@249 | 114 | filename = self._find_file(d, name) |
paulb@249 | 115 | if filename: |
paulb@249 | 116 | init_py = "__init__" + os.path.extsep + "py" |
paulb@249 | 117 | init_py_filename = self._find_file(filename, init_py) |
paulb@249 | 118 | if init_py_filename: |
paulb@249 | 119 | return filename, init_py_filename |
paulb@249 | 120 | return None |
paulb@249 | 121 | |
paulb@249 | 122 | def _find_file(self, d, filename): |
paulb@249 | 123 | |
paulb@249 | 124 | """ |
paulb@249 | 125 | Return the filename obtained when searching the directory 'd' for the |
paulb@249 | 126 | given 'filename', or None if no actual file exists for the filename. |
paulb@249 | 127 | """ |
paulb@249 | 128 | |
paulb@249 | 129 | filename = os.path.join(d, filename) |
paulb@249 | 130 | if os.path.exists(filename): |
paulb@249 | 131 | return filename |
paulb@249 | 132 | else: |
paulb@249 | 133 | return None |
paulb@249 | 134 | |
paulb@249 | 135 | def load(self, name, builtins=None, alias=None): |
paulb@249 | 136 | |
paulb@249 | 137 | """ |
paulb@249 | 138 | Load the module or package with the given 'name' and using the specified |
paulb@249 | 139 | 'builtins'. Return an Attribute object referencing the loaded module or |
paulb@249 | 140 | package, or None if no such module or package exists. |
paulb@249 | 141 | """ |
paulb@249 | 142 | |
paulb@249 | 143 | if self.modules.has_key(name): |
paulb@249 | 144 | return Attribute(None, self.modules[name]) |
paulb@249 | 145 | |
paulb@249 | 146 | path = name.split(".") |
paulb@249 | 147 | m = self.find_in_path(path[0]) |
paulb@249 | 148 | if not m: |
paulb@249 | 149 | return None # NOTE: Import error. |
paulb@249 | 150 | d, filename = m |
paulb@249 | 151 | |
paulb@249 | 152 | if self.modules.has_key(path[0]): |
paulb@249 | 153 | top = module = self.modules[path[0]] |
paulb@249 | 154 | else: |
paulb@249 | 155 | top = module = self.load_from_file(filename, builtins, path[0]) |
paulb@249 | 156 | |
paulb@249 | 157 | if len(path) > 1: |
paulb@249 | 158 | path_so_far = path[:1] |
paulb@249 | 159 | for p in path[1:]: |
paulb@249 | 160 | path_so_far.append(p) |
paulb@249 | 161 | m = self.find(d, p) |
paulb@249 | 162 | if not m: |
paulb@249 | 163 | return None # NOTE: Import error. |
paulb@249 | 164 | d, filename = m |
paulb@249 | 165 | module_name = ".".join(path_so_far) |
paulb@249 | 166 | |
paulb@249 | 167 | if self.modules.has_key(module_name): |
paulb@249 | 168 | submodule = self.modules[module_name] |
paulb@249 | 169 | else: |
paulb@249 | 170 | submodule = self.load_from_file(filename, builtins, module_name) |
paulb@249 | 171 | |
paulb@249 | 172 | # Store the submodule within its parent module. |
paulb@249 | 173 | |
paulb@249 | 174 | module.namespace[p] = [Attribute(None, submodule)] |
paulb@249 | 175 | module = submodule |
paulb@249 | 176 | |
paulb@249 | 177 | if alias: |
paulb@249 | 178 | return Attribute(None, module) |
paulb@249 | 179 | else: |
paulb@249 | 180 | return Attribute(None, top) |
paulb@249 | 181 | |
paulb@249 | 182 | def load_from_file(self, name, builtins=None, module_name=None): |
paulb@249 | 183 | |
paulb@249 | 184 | """ |
paulb@249 | 185 | Load the module with the given 'name' (which may be a full module path), |
paulb@249 | 186 | using the optional 'builtins' to resolve built-in names. |
paulb@249 | 187 | """ |
paulb@249 | 188 | |
paulb@249 | 189 | if module_name is None: |
paulb@249 | 190 | if builtins is None: |
paulb@249 | 191 | module_name = "__builtins__" |
paulb@249 | 192 | else: |
paulb@249 | 193 | module_name = "__main__" |
paulb@249 | 194 | |
paulb@249 | 195 | module = simplify.ast.simplify(name, builtins is None, module_name) |
paulb@249 | 196 | simplify.fixnames.fix(module, builtins) |
paulb@249 | 197 | if self.annotate: |
paulb@249 | 198 | simplify.annotate.annotate(module, builtins, self) |
paulb@249 | 199 | |
paulb@249 | 200 | # Record the module. |
paulb@249 | 201 | |
paulb@249 | 202 | self.modules[module_name] = module |
paulb@249 | 203 | return module |
paulb@249 | 204 | |
paulb@249 | 205 | def fix_instances(self): |
paulb@249 | 206 | |
paulb@249 | 207 | "Fix instances for all modules loaded by this importer." |
paulb@249 | 208 | |
paulb@249 | 209 | for module in self.get_modules(): |
paulb@249 | 210 | simplify.fixinstances.fix_structures(module) |
paulb@249 | 211 | for module in self.get_modules(): |
paulb@249 | 212 | simplify.fixinstances.fix(module) |
paulb@249 | 213 | |
paul@0 | 214 | # vim: tabstop=4 expandtab shiftwidth=4 |