mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-07 14:34:31 +08:00
module imports are working now
This commit is contained in:
43
modules.py
43
modules.py
@@ -1,5 +1,6 @@
|
||||
import imp
|
||||
import sys
|
||||
import os
|
||||
|
||||
import debug
|
||||
import parsing
|
||||
@@ -20,11 +21,11 @@ class File(object):
|
||||
Manages all files, that are parsed and caches them.
|
||||
|
||||
:param source: The source code of the file.
|
||||
:param module_name: The module name of the file.
|
||||
:param module_path: The module path of the file.
|
||||
"""
|
||||
def __init__(self, module_name, source):
|
||||
def __init__(self, module_path, source):
|
||||
self.source = source
|
||||
self.module_name = module_name
|
||||
self.module_path = module_path
|
||||
self._line_cache = None
|
||||
self._parser = None
|
||||
|
||||
@@ -32,30 +33,34 @@ class File(object):
|
||||
def parser(self):
|
||||
if self._parser:
|
||||
return self._parser
|
||||
if not self.module_name and not self.source:
|
||||
if not self.module_path and not self.source:
|
||||
raise AttributeError("Submit a module name or the source code")
|
||||
elif self.module_name:
|
||||
elif self.module_path:
|
||||
return self._load_module()
|
||||
|
||||
def _load_module(self):
|
||||
self._parser = parsing.PyFuzzyParser(self.source)
|
||||
self._parser = parsing.PyFuzzyParser(self.source, self.module_path)
|
||||
del self.source # efficiency
|
||||
return self._parser
|
||||
|
||||
def find_module(point_path):
|
||||
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]
|
||||
path = [ns[1]]
|
||||
else:
|
||||
# TODO modules can be system modules, without '.' in path
|
||||
path = module_find_path
|
||||
path = None
|
||||
debug.dbg('search_module', string, path)
|
||||
try:
|
||||
i = imp.find_module(string, path)
|
||||
@@ -67,8 +72,9 @@ def find_module(point_path):
|
||||
raise
|
||||
return i
|
||||
|
||||
# now execute those paths
|
||||
current_namespace = None
|
||||
sys.path.insert(0, os.path.dirname(current_module.module_path))
|
||||
# now execute those paths
|
||||
rest = []
|
||||
for i, s in enumerate(point_path):
|
||||
try:
|
||||
@@ -80,8 +86,19 @@ def find_module(point_path):
|
||||
raise ModuleNotFound(
|
||||
'The module you searched has not been found')
|
||||
|
||||
if current_namespace[0]:
|
||||
f = File(current_namespace[2], current_namespace[0].read())
|
||||
sys.path.pop(0)
|
||||
path = current_namespace[1]
|
||||
is_package_directory = current_namespace[2][2] == imp.PKG_DIRECTORY
|
||||
|
||||
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()
|
||||
f = File(path, source)
|
||||
else:
|
||||
f = builtin.Parser(current_namespace[1])
|
||||
f = builtin.Parser(path)
|
||||
return f.parser.top, rest
|
||||
|
||||
Reference in New Issue
Block a user