1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/micropython/__init__.py Tue Oct 16 01:29:33 2007 +0200
1.3 @@ -0,0 +1,227 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 +"""
1.7 +The micropython package for processing Python source code. The code originates
1.8 +from the simplify package but has had various details related to that package
1.9 +removed.
1.10 +
1.11 +Copyright (C) 2006, 2007 Paul Boddie <paul@boddie.org.uk>
1.12 +
1.13 +This program is free software; you can redistribute it and/or modify it under
1.14 +the terms of the GNU General Public License as published by the Free Software
1.15 +Foundation; either version 3 of the License, or (at your option) any later
1.16 +version.
1.17 +
1.18 +This program is distributed in the hope that it will be useful, but WITHOUT
1.19 +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.20 +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
1.21 +details.
1.22 +
1.23 +You should have received a copy of the GNU General Public License along with
1.24 +this program. If not, see <http://www.gnu.org/licenses/>.
1.25 +
1.26 +--------
1.27 +
1.28 +To use this module, an importer should be constructed and the load_from_file
1.29 +method used. Here, the standard path for module searching is employed:
1.30 +
1.31 +importer = Importer(sys.path)
1.32 +importer.load_from_file(filename)
1.33 +
1.34 +Such importer objects are the most convenient mechanism through which the
1.35 +functionality of the micropython package may be accessed.
1.36 +"""
1.37 +
1.38 +import micropython.inspect
1.39 +import os
1.40 +try:
1.41 + set
1.42 +except NameError:
1.43 + from sets import Set as set
1.44 +
1.45 +class Importer:
1.46 +
1.47 + "An import machine, searching for and loading modules."
1.48 +
1.49 + def __init__(self, path=None):
1.50 +
1.51 + """
1.52 + Initialise the importer with the given search 'path' - a list of
1.53 + directories to search for Python modules.
1.54 + """
1.55 +
1.56 + self.path = path or [os.getcwd()]
1.57 + self.modules = {}
1.58 + self.loaded = set()
1.59 +
1.60 + def get_modules(self):
1.61 +
1.62 + "Return all modules known to the importer."
1.63 +
1.64 + return self.modules.values()
1.65 +
1.66 + def find_in_path(self, name):
1.67 +
1.68 + """
1.69 + Find the given module 'name' in the search path, returning None where no
1.70 + such module could be found, or a 2-tuple from the 'find' method
1.71 + otherwise.
1.72 + """
1.73 +
1.74 + for d in self.path:
1.75 + m = self.find(d, name)
1.76 + if m: return m
1.77 + return None
1.78 +
1.79 + def find(self, d, name):
1.80 +
1.81 + """
1.82 + In the directory 'd', find the given module 'name', where 'name' can
1.83 + either refer to a single file module or to a package. Return None if the
1.84 + 'name' cannot be associated with either a file or a package directory,
1.85 + or a 2-tuple from '_find_package' or '_find_module' otherwise.
1.86 + """
1.87 +
1.88 + m = self._find_package(d, name)
1.89 + if m: return m
1.90 + m = self._find_module(d, name)
1.91 + if m: return m
1.92 + return None
1.93 +
1.94 + def _find_module(self, d, name):
1.95 +
1.96 + """
1.97 + In the directory 'd', find the given module 'name', returning None where
1.98 + no suitable file exists in the directory, or a 2-tuple consisting of
1.99 + None (indicating that no package directory is involved) and a filename
1.100 + indicating the location of the module.
1.101 + """
1.102 +
1.103 + name_py = name + os.extsep + "py"
1.104 + filename = self._find_file(d, name_py)
1.105 + if filename:
1.106 + return None, filename
1.107 + return None
1.108 +
1.109 + def _find_package(self, d, name):
1.110 +
1.111 + """
1.112 + In the directory 'd', find the given package 'name', returning None
1.113 + where no suitable package directory exists, or a 2-tuple consisting of
1.114 + a directory (indicating the location of the package directory itself)
1.115 + and a filename indicating the location of the __init__.py module which
1.116 + declares the package's top-level contents.
1.117 + """
1.118 +
1.119 + filename = self._find_file(d, name)
1.120 + if filename:
1.121 + init_py = "__init__" + os.path.extsep + "py"
1.122 + init_py_filename = self._find_file(filename, init_py)
1.123 + if init_py_filename:
1.124 + return filename, init_py_filename
1.125 + return None
1.126 +
1.127 + def _find_file(self, d, filename):
1.128 +
1.129 + """
1.130 + Return the filename obtained when searching the directory 'd' for the
1.131 + given 'filename', or None if no actual file exists for the filename.
1.132 + """
1.133 +
1.134 + filename = os.path.join(d, filename)
1.135 + if os.path.exists(filename):
1.136 + return filename
1.137 + else:
1.138 + return None
1.139 +
1.140 + def load(self, name, return_leaf=0):
1.141 +
1.142 + """
1.143 + Load the module or package with the given 'name'. Return an object
1.144 + referencing the loaded module or package, or None if no such module or
1.145 + package exists.
1.146 + """
1.147 +
1.148 + print "Loading", name
1.149 + if self.modules.has_key(name) and self.modules[name] in self.loaded:
1.150 + return self.modules[name]
1.151 +
1.152 + # Split the name into path components, and try to find the uppermost in
1.153 + # the search path.
1.154 +
1.155 + path = name.split(".")
1.156 + m = self.find_in_path(path[0])
1.157 + if not m:
1.158 + return None # NOTE: Import error.
1.159 + d, filename = m
1.160 +
1.161 + # Either acquire a reference to an already-imported module, or load the
1.162 + # module from a file.
1.163 +
1.164 + if self.modules.has_key(path[0]):
1.165 + top = module = self.modules[path[0]]
1.166 + else:
1.167 + top = module = self.load_from_file(filename, path[0])
1.168 +
1.169 + # For hierarchical names, traverse each path component...
1.170 +
1.171 + if len(path) > 1:
1.172 + if not d:
1.173 + return None # NOTE: Import error (package not found).
1.174 +
1.175 + path_so_far = path[:1]
1.176 + for p in path[1:]:
1.177 + path_so_far.append(p)
1.178 +
1.179 + # Find the package or module concerned.
1.180 +
1.181 + m = self.find(d, p)
1.182 + if not m:
1.183 + return None # NOTE: Import error.
1.184 + d, filename = m
1.185 + module_name = ".".join(path_so_far)
1.186 +
1.187 + # Either reference an imported module or load one from a file.
1.188 +
1.189 + if self.modules.has_key(module_name):
1.190 + submodule = self.modules[module_name]
1.191 + else:
1.192 + submodule = self.load_from_file(filename, module_name)
1.193 +
1.194 + # Store the submodule within its parent module.
1.195 +
1.196 + module.namespace[p] = submodule
1.197 + module = submodule
1.198 +
1.199 + # Return either the deepest or the uppermost module.
1.200 +
1.201 + if return_leaf:
1.202 + return module
1.203 + else:
1.204 + return top
1.205 +
1.206 + def load_from_file(self, name, module_name=None):
1.207 +
1.208 + """
1.209 + Load the module with the given 'name' (which may be a full module path).
1.210 + """
1.211 +
1.212 + if module_name is None:
1.213 + module_name = "__main__"
1.214 +
1.215 + if not self.modules.has_key(module_name):
1.216 + self.modules[module_name] = module = micropython.inspect.Module(self)
1.217 + else:
1.218 + module = self.modules[module_name]
1.219 +
1.220 + print "Parsing", name
1.221 + module.parse(name)
1.222 + print "Done", name
1.223 +
1.224 + # Record the module.
1.225 +
1.226 + self.loaded.add(module)
1.227 + #print "Loaded", module_name, "with namespace", module.namespace.keys()
1.228 + return module
1.229 +
1.230 +# vim: tabstop=4 expandtab shiftwidth=4