1.1 --- a/micropython/__init__.py Tue Oct 16 01:29:33 2007 +0200
1.2 +++ b/micropython/__init__.py Wed Oct 17 01:00:30 2007 +0200
1.3 @@ -168,6 +168,8 @@
1.4 if len(path) > 1:
1.5 if not d:
1.6 return None # NOTE: Import error (package not found).
1.7 + else:
1.8 + self.add_submodules(d, module)
1.9
1.10 path_so_far = path[:1]
1.11 for p in path[1:]:
1.12 @@ -188,6 +190,9 @@
1.13 else:
1.14 submodule = self.load_from_file(filename, module_name)
1.15
1.16 + if d:
1.17 + self.add_submodules(d, module)
1.18 +
1.19 # Store the submodule within its parent module.
1.20
1.21 module.namespace[p] = submodule
1.22 @@ -209,10 +214,7 @@
1.23 if module_name is None:
1.24 module_name = "__main__"
1.25
1.26 - if not self.modules.has_key(module_name):
1.27 - self.modules[module_name] = module = micropython.inspect.Module(self)
1.28 - else:
1.29 - module = self.modules[module_name]
1.30 + module = self.add_module(module_name)
1.31
1.32 print "Parsing", name
1.33 module.parse(name)
1.34 @@ -224,4 +226,28 @@
1.35 #print "Loaded", module_name, "with namespace", module.namespace.keys()
1.36 return module
1.37
1.38 + def add_module(self, module_name):
1.39 +
1.40 + """
1.41 + Return the module with the given 'module_name', adding a new module
1.42 + object if one does not already exist.
1.43 + """
1.44 +
1.45 + if not self.modules.has_key(module_name):
1.46 + self.modules[module_name] = module = micropython.inspect.Module(module_name, self)
1.47 + else:
1.48 + module = self.modules[module_name]
1.49 + return module
1.50 +
1.51 + def add_submodules(self, pathname, module):
1.52 +
1.53 + """
1.54 + Work around insufficient __all__ declarations and examine the directory
1.55 + with the given 'pathname', adding submodules to the given 'module'.
1.56 + """
1.57 +
1.58 + for filename in os.listdir(pathname):
1.59 + submodule = os.path.splitext(filename)[0]
1.60 + module.namespace[submodule] = self.add_module(module.name + "." + submodule)
1.61 +
1.62 # vim: tabstop=4 expandtab shiftwidth=4
2.1 --- a/micropython/inspect.py Tue Oct 16 01:29:33 2007 +0200
2.2 +++ b/micropython/inspect.py Wed Oct 17 01:00:30 2007 +0200
2.3 @@ -56,19 +56,26 @@
2.4
2.5 "An inspected module."
2.6
2.7 - def __init__(self, importer=None):
2.8 + def __init__(self, name, importer=None):
2.9 ASTVisitor.__init__(self)
2.10 self.visitor = self
2.11
2.12 + self.name = name
2.13 self.importer = importer
2.14
2.15 + # Module namespace.
2.16 +
2.17 self.namespace = {}
2.18 self.namespace.update(builtins_namespace)
2.19
2.20 + # Current expression state.
2.21 +
2.22 + self.expr = None
2.23 +
2.24 + # Namespace state.
2.25 +
2.26 self.in_global = 1
2.27 self.in_init = 0
2.28 - self.expr = None
2.29 -
2.30 self.classes = []
2.31 self.module = None
2.32
2.33 @@ -78,10 +85,16 @@
2.34
2.35 def process(self, module):
2.36 self.module = module
2.37 - return self.dispatch(module)
2.38 + processed = self.dispatch(module)
2.39 + if self.namespace.has_key("__all__"):
2.40 + all = self.namespace["__all__"]
2.41 + if isinstance(all, compiler.ast.List):
2.42 + for n in all.nodes:
2.43 + self.namespace[n.value] = self.importer.add_module(self.name + "." + n.value)
2.44 + return processed
2.45
2.46 def default(self, node, *args):
2.47 - raise InspectError, (None, node)
2.48 + raise InspectError, node.__class__
2.49
2.50 def dispatch(self, node, *args):
2.51 return ASTVisitor.dispatch(self, node, *args)
2.52 @@ -100,6 +113,8 @@
2.53 def NOP(self, node):
2.54 return node
2.55
2.56 + visitAdd = NOP
2.57 +
2.58 visitAnd = NOP
2.59
2.60 def visitAssign(self, node):
2.61 @@ -131,7 +146,7 @@
2.62 if not self.in_global:
2.63 raise InspectError, "Class is %s not global: cannot handle this reasonably." % node.name
2.64 else:
2.65 - cls = Class(node)
2.66 + cls = Class(node.name)
2.67 for base in node.bases:
2.68 base_ref = self.dispatch(base)
2.69 if base_ref is None:
2.70 @@ -168,7 +183,7 @@
2.71
2.72 for name, alias in node.names:
2.73 if name != "*":
2.74 - self.namespace[alias] = module.namespace[name]
2.75 + self.namespace[alias or name] = module.namespace[name]
2.76 else:
2.77 for n in module.namespace.keys():
2.78 self.namespace[n] = module.namespace[n]
2.79 @@ -206,10 +221,12 @@
2.80 raise InspectError, "Please use the micropython.Importer class for code which uses the 'import' statement."
2.81
2.82 for name, alias in node.names:
2.83 - self.namespace[alias] = self.importer.load(name, 1)
2.84 + self.namespace[alias or name] = self.importer.load(name, 1)
2.85
2.86 return None
2.87
2.88 + visitLeftShift = NOP
2.89 +
2.90 visitList = NOP
2.91
2.92 def visitModule(self, node):
2.93 @@ -232,6 +249,8 @@
2.94
2.95 visitRaise = NOP
2.96
2.97 + visitRightShift = NOP
2.98 +
2.99 def visitStmt(self, node):
2.100 for n in node.nodes:
2.101 self.dispatch(n)
2.102 @@ -247,6 +266,8 @@
2.103 self.dispatch(node.else_)
2.104 return None
2.105
2.106 + visitTuple = NOP
2.107 +
2.108 class Self:
2.109
2.110 "A reference to an object within a method."
2.111 @@ -267,7 +288,7 @@
2.112 'TabError', 'True', 'TypeError', 'UnboundLocalError',
2.113 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError',
2.114 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning',
2.115 - 'ValueError', 'Warning', 'ZeroDivisionError']:
2.116 + 'ValueError', 'Warning', 'ZeroDivisionError', 'object']:
2.117 builtins_namespace[key] = Class(key)
2.118
2.119 # vim: tabstop=4 expandtab shiftwidth=4