1.1 --- a/simplify/__init__.py Sun Jun 24 20:05:24 2007 +0200
1.2 +++ b/simplify/__init__.py Sun Jun 24 20:06:07 2007 +0200
1.3 @@ -22,8 +22,8 @@
1.4
1.5 --------
1.6
1.7 -To use this module, an importer should be constructed and the load method used.
1.8 -Here, the standard path for module searching is used:
1.9 +To use this module, an importer should be constructed and the load_from_file
1.10 +method used. Here, the standard path for module searching is employed:
1.11
1.12 importer = Importer(sys.path)
1.13 importer.load_from_file(builtins)
1.14 @@ -56,6 +56,9 @@
1.15 self.modules = {}
1.16
1.17 def get_modules(self):
1.18 +
1.19 + "Return all modules known to the importer."
1.20 +
1.21 return self.modules.values()
1.22
1.23 def find_in_path(self, name):
1.24 @@ -143,27 +146,40 @@
1.25 if self.modules.has_key(name):
1.26 return Attribute(None, self.modules[name])
1.27
1.28 + # Split the name into path components, and try to find the uppermost in
1.29 + # the search path.
1.30 +
1.31 path = name.split(".")
1.32 m = self.find_in_path(path[0])
1.33 if not m:
1.34 return None # NOTE: Import error.
1.35 d, filename = m
1.36
1.37 + # Either acquire a reference to an already-imported module, or load the
1.38 + # module from a file.
1.39 +
1.40 if self.modules.has_key(path[0]):
1.41 top = module = self.modules[path[0]]
1.42 else:
1.43 top = module = self.load_from_file(filename, builtins, path[0])
1.44
1.45 + # For hierarchical names, traverse each path component...
1.46 +
1.47 if len(path) > 1:
1.48 path_so_far = path[:1]
1.49 for p in path[1:]:
1.50 path_so_far.append(p)
1.51 +
1.52 + # Find the package or module concerned.
1.53 +
1.54 m = self.find(d, p)
1.55 if not m:
1.56 return None # NOTE: Import error.
1.57 d, filename = m
1.58 module_name = ".".join(path_so_far)
1.59
1.60 + # Either reference an imported module or load one from a file.
1.61 +
1.62 if self.modules.has_key(module_name):
1.63 submodule = self.modules[module_name]
1.64 else:
1.65 @@ -174,6 +190,8 @@
1.66 module.namespace[p] = [Attribute(None, submodule)]
1.67 module = submodule
1.68
1.69 + # Return either the deepest or the uppermost module.
1.70 +
1.71 if alias:
1.72 return Attribute(None, module)
1.73 else:
1.74 @@ -192,6 +210,8 @@
1.75 else:
1.76 module_name = "__main__"
1.77
1.78 + # Simplify, fix names, and annotate the module.
1.79 +
1.80 module = simplify.ast.simplify(name, builtins is None, module_name)
1.81 simplify.fixnames.fix(module, builtins)
1.82 if self.annotate:
2.1 --- a/simplify/ast.py Sun Jun 24 20:05:24 2007 +0200
2.2 +++ b/simplify/ast.py Sun Jun 24 20:06:07 2007 +0200
2.3 @@ -85,12 +85,24 @@
2.4
2.5 self.visitor = self
2.6
2.7 - def process(self, node, name):
2.8 - result = self.dispatch(node, name)
2.9 + def process(self, module, name):
2.10 +
2.11 + """
2.12 + Process the 'module' having the given 'name'. Return the simplified node
2.13 + representing the 'module'.
2.14 + """
2.15 +
2.16 + result = self.dispatch(module, name)
2.17 result.simplifier = self
2.18 return result
2.19
2.20 def dispatch_or_none(self, node, *args):
2.21 +
2.22 + """
2.23 + Dispatch to a handler for 'node', returning the result, or if 'node' is
2.24 + None then return a node which loads None in the simplified program.
2.25 + """
2.26 +
2.27 if node is not None:
2.28 return self.dispatch(node, *args)
2.29 else: