basic imports rewriting, which has still it's rough edges

This commit is contained in:
David Halter
2012-07-16 02:19:48 +02:00
parent e6741c2dd6
commit e779cc8c97
6 changed files with 79 additions and 151 deletions

View File

@@ -144,67 +144,3 @@ class ModuleWithCursor(Module):
return self._line_cache[line - 1]
except IndexError:
raise StopIteration()
def find_module(current_module, point_path):
"""
Find a module with a path (of the module, like usb.backend.libusb10).
Relative imports: http://www.python.org/dev/peps/pep-0328
are only used like this (py3000): from .module import name.
:param current_ns_path: A path to the current namespace.
:param point_path: A name from the parser.
:return: The rest of the path, and the module top scope.
"""
def follow_str(ns, string):
debug.dbg('follow_module', ns, string)
if ns:
path = [ns[1]]
else:
path = None
debug.dbg('search_module', string, path,
current_module.path)
try:
i = imp.find_module(string, path)
except ImportError:
# find builtins (ommit path):
i = imp.find_module(string, builtin.module_find_path)
return i
# TODO handle relative paths - they are included in the import object
current_namespace = None
builtin.module_find_path.insert(0, os.path.dirname(current_module.path))
# now execute those paths
rest = []
for i, s in enumerate(point_path):
try:
current_namespace = follow_str(current_namespace, s)
except ImportError:
if current_namespace:
rest = point_path[i:]
else:
raise ModuleNotFound(
'The module you searched has not been found')
builtin.module_find_path.pop(0)
path = current_namespace[1]
is_package_directory = current_namespace[2][2] == imp.PKG_DIRECTORY
f = None
if is_package_directory or current_namespace[0]:
# is a directory module
if is_package_directory:
path += '/__init__.py'
with open(path) as f:
source = f.read()
else:
source = current_namespace[0].read()
if path.endswith('.py'):
f = Module(path, source)
else:
f = builtin.Parser(path=path)
else:
f = builtin.Parser(name=path)
return f.parser.top, rest