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
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/micropython/inspect.py Tue Oct 16 01:29:33 2007 +0200
2.3 @@ -0,0 +1,273 @@
2.4 +#!/usr/bin/env python
2.5 +
2.6 +"""
2.7 +Inspect source files, obtaining details of classes and attributes.
2.8 +
2.9 +Copyright (C) 2007 Paul Boddie <paul@boddie.org.uk>
2.10 +
2.11 +This program is free software; you can redistribute it and/or modify it under
2.12 +the terms of the GNU General Public License as published by the Free Software
2.13 +Foundation; either version 3 of the License, or (at your option) any later
2.14 +version.
2.15 +
2.16 +This program is distributed in the hope that it will be useful, but WITHOUT
2.17 +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
2.18 +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
2.19 +details.
2.20 +
2.21 +You should have received a copy of the GNU General Public License along with
2.22 +this program. If not, see <http://www.gnu.org/licenses/>.
2.23 +"""
2.24 +
2.25 +import compiler.ast
2.26 +from compiler.visitor import ASTVisitor
2.27 +try:
2.28 + set
2.29 +except NameError:
2.30 + from sets import Set as set
2.31 +
2.32 +class InspectError(Exception):
2.33 +
2.34 + "An inspection error."
2.35 +
2.36 + pass
2.37 +
2.38 +class Class:
2.39 +
2.40 + "An inspected class."
2.41 +
2.42 + def __init__(self, name):
2.43 + self.name = name
2.44 + self.bases = []
2.45 + self.namespace = {}
2.46 + self.instattr = set()
2.47 +
2.48 + def add_base(self, base):
2.49 + self.bases.append(base)
2.50 +
2.51 +class Function:
2.52 +
2.53 + "An inspected function."
2.54 +
2.55 + def __init__(self, name):
2.56 + self.name = name
2.57 +
2.58 +class Module(ASTVisitor):
2.59 +
2.60 + "An inspected module."
2.61 +
2.62 + def __init__(self, importer=None):
2.63 + ASTVisitor.__init__(self)
2.64 + self.visitor = self
2.65 +
2.66 + self.importer = importer
2.67 +
2.68 + self.namespace = {}
2.69 + self.namespace.update(builtins_namespace)
2.70 +
2.71 + self.in_global = 1
2.72 + self.in_init = 0
2.73 + self.expr = None
2.74 +
2.75 + self.classes = []
2.76 + self.module = None
2.77 +
2.78 + def parse(self, filename):
2.79 + module = compiler.parseFile(filename)
2.80 + self.process(module)
2.81 +
2.82 + def process(self, module):
2.83 + self.module = module
2.84 + return self.dispatch(module)
2.85 +
2.86 + def default(self, node, *args):
2.87 + raise InspectError, (None, node)
2.88 +
2.89 + def dispatch(self, node, *args):
2.90 + return ASTVisitor.dispatch(self, node, *args)
2.91 +
2.92 + def store(self, name, obj):
2.93 + if self.in_global:
2.94 + if not self.classes:
2.95 + self.namespace[name] = obj
2.96 + else:
2.97 + self.classes[-1].namespace[name] = obj
2.98 +
2.99 + def store_attr(self, name):
2.100 + if self.in_init:
2.101 + self.classes[-1].instattr.add(name)
2.102 +
2.103 + def NOP(self, node):
2.104 + return node
2.105 +
2.106 + visitAnd = NOP
2.107 +
2.108 + def visitAssign(self, node):
2.109 + self.expr = self.dispatch(node.expr)
2.110 + for n in node.nodes:
2.111 + self.dispatch(n)
2.112 + return None
2.113 +
2.114 + def visitAssAttr(self, node):
2.115 + expr = self.dispatch(node.expr)
2.116 + if expr is not None and isinstance(expr, Self):
2.117 + self.store_attr(node.attrname)
2.118 + return None
2.119 +
2.120 + def visitAssList(self, node):
2.121 + for n in node.nodes:
2.122 + self.dispatch(n)
2.123 + return None
2.124 +
2.125 + def visitAssName(self, node):
2.126 + self.store(node.name, self.expr)
2.127 + return None
2.128 +
2.129 + visitAssTuple = visitAssList
2.130 +
2.131 + visitCallFunc = NOP
2.132 +
2.133 + def visitClass(self, node):
2.134 + if not self.in_global:
2.135 + raise InspectError, "Class is %s not global: cannot handle this reasonably." % node.name
2.136 + else:
2.137 + cls = Class(node)
2.138 + for base in node.bases:
2.139 + base_ref = self.dispatch(base)
2.140 + if base_ref is None:
2.141 + raise InspectError, "Base class %s for class %s is not found: it may be hidden in some way." % (base, node.name)
2.142 + cls.add_base(base_ref)
2.143 +
2.144 + # Make an entry for the class.
2.145 +
2.146 + self.store(node.name, cls)
2.147 +
2.148 + self.classes.append(cls)
2.149 + self.dispatch(node.code)
2.150 + self.classes.pop()
2.151 +
2.152 + return node
2.153 +
2.154 + visitConst = NOP
2.155 +
2.156 + visitDict = NOP
2.157 +
2.158 + visitDiscard = NOP
2.159 +
2.160 + visitFor = NOP
2.161 +
2.162 + def visitFrom(self, node):
2.163 + if self.importer is None:
2.164 + raise InspectError, "Please use the micropython.Importer class for code which uses the 'from' statement."
2.165 +
2.166 + module = self.importer.load(node.modname, 1)
2.167 +
2.168 + if module is None:
2.169 + print "Warning:", node.modname, "not imported."
2.170 + return None
2.171 +
2.172 + for name, alias in node.names:
2.173 + if name != "*":
2.174 + self.namespace[alias] = module.namespace[name]
2.175 + else:
2.176 + for n in module.namespace.keys():
2.177 + self.namespace[n] = module.namespace[n]
2.178 +
2.179 + return None
2.180 +
2.181 + def visitFunction(self, node):
2.182 + self.store(node.name, Function(node.name))
2.183 +
2.184 + in_global = self.in_global
2.185 + self.in_global = 0
2.186 + if node.name == "__init__" and self.classes:
2.187 + self.in_init = 1
2.188 + self.dispatch(node.code)
2.189 + self.in_init = 0
2.190 + self.in_global = in_global
2.191 + return None
2.192 +
2.193 + def visitGetattr(self, node):
2.194 + expr = self.dispatch(node.expr)
2.195 + if expr is not None and isinstance(expr, Module):
2.196 + return expr.namespace.get(node.attrname)
2.197 + else:
2.198 + return None
2.199 +
2.200 + def visitIf(self, node):
2.201 + for test, body in node.tests:
2.202 + self.dispatch(body)
2.203 + if node.else_ is not None:
2.204 + self.dispatch(node.else_)
2.205 + return None
2.206 +
2.207 + def visitImport(self, node):
2.208 + if self.importer is None:
2.209 + raise InspectError, "Please use the micropython.Importer class for code which uses the 'import' statement."
2.210 +
2.211 + for name, alias in node.names:
2.212 + self.namespace[alias] = self.importer.load(name, 1)
2.213 +
2.214 + return None
2.215 +
2.216 + visitList = NOP
2.217 +
2.218 + def visitModule(self, node):
2.219 + return self.dispatch(node.node)
2.220 +
2.221 + def visitName(self, node):
2.222 + name = node.name
2.223 + if name == "self":
2.224 + return Self()
2.225 + elif self.namespace.has_key(name):
2.226 + return self.namespace[name]
2.227 + else:
2.228 + return None
2.229 +
2.230 + visitNot = NOP
2.231 +
2.232 + visitOr = NOP
2.233 +
2.234 + visitPass = NOP
2.235 +
2.236 + visitRaise = NOP
2.237 +
2.238 + def visitStmt(self, node):
2.239 + for n in node.nodes:
2.240 + self.dispatch(n)
2.241 + return None
2.242 +
2.243 + visitSubscript = NOP
2.244 +
2.245 + def visitTryExcept(self, node):
2.246 + self.dispatch(node.body)
2.247 + for name, var, n in node.handlers:
2.248 + self.dispatch(n)
2.249 + if node.else_ is not None:
2.250 + self.dispatch(node.else_)
2.251 + return None
2.252 +
2.253 +class Self:
2.254 +
2.255 + "A reference to an object within a method."
2.256 +
2.257 + pass
2.258 +
2.259 +builtins_namespace = {}
2.260 +for key in ['ArithmeticError', 'AssertionError', 'AttributeError',
2.261 + 'BaseException', 'DeprecationWarning', 'EOFError', 'Ellipsis',
2.262 + 'EnvironmentError', 'Exception', 'False', 'FloatingPointError',
2.263 + 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError',
2.264 + 'ImportWarning', 'IndentationError', 'IndexError', 'KeyError',
2.265 + 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'NameError',
2.266 + 'None', 'NotImplemented', 'NotImplementedError', 'OSError',
2.267 + 'OverflowError', 'PendingDeprecationWarning', 'ReferenceError',
2.268 + 'RuntimeError', 'RuntimeWarning', 'StandardError', 'StopIteration',
2.269 + 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit',
2.270 + 'TabError', 'True', 'TypeError', 'UnboundLocalError',
2.271 + 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError',
2.272 + 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning',
2.273 + 'ValueError', 'Warning', 'ZeroDivisionError']:
2.274 + builtins_namespace[key] = Class(key)
2.275 +
2.276 +# vim: tabstop=4 expandtab shiftwidth=4