forked from VimPlug/jedi
Try to migrate to the new project API
This commit is contained in:
@@ -25,10 +25,10 @@ from jedi.api import interpreter
|
|||||||
from jedi.api import helpers
|
from jedi.api import helpers
|
||||||
from jedi.api.completion import Completion
|
from jedi.api.completion import Completion
|
||||||
from jedi.api.environment import InterpreterEnvironment
|
from jedi.api.environment import InterpreterEnvironment
|
||||||
|
from jedi.api.project import get_default_project
|
||||||
from jedi.evaluate import Evaluator
|
from jedi.evaluate import Evaluator
|
||||||
from jedi.evaluate import imports
|
from jedi.evaluate import imports
|
||||||
from jedi.evaluate import usages
|
from jedi.evaluate import usages
|
||||||
from jedi.evaluate.project import Project
|
|
||||||
from jedi.evaluate.arguments import try_iter_content
|
from jedi.evaluate.arguments import try_iter_content
|
||||||
from jedi.evaluate.helpers import get_module_names, evaluate_call_of_leaf
|
from jedi.evaluate.helpers import get_module_names, evaluate_call_of_leaf
|
||||||
from jedi.evaluate.sys_path import dotted_path_in_sys_path
|
from jedi.evaluate.sys_path import dotted_path_in_sys_path
|
||||||
@@ -82,10 +82,11 @@ class Script(object):
|
|||||||
:type sys_path: list
|
:type sys_path: list
|
||||||
:param environment: TODO
|
:param environment: TODO
|
||||||
:type sys_path: Environment
|
:type sys_path: Environment
|
||||||
|
:param project: TODO
|
||||||
"""
|
"""
|
||||||
def __init__(self, source=None, line=None, column=None, path=None,
|
def __init__(self, source=None, line=None, column=None, path=None,
|
||||||
encoding='utf-8', sys_path=None, environment=None):
|
encoding='utf-8', sys_path=None, environment=None,
|
||||||
|
project=None):
|
||||||
self._orig_path = path
|
self._orig_path = path
|
||||||
# An empty path (also empty string) should always result in no path.
|
# An empty path (also empty string) should always result in no path.
|
||||||
self.path = os.path.abspath(path) if path else None
|
self.path = os.path.abspath(path) if path else None
|
||||||
@@ -116,9 +117,12 @@ class Script(object):
|
|||||||
sys_path = list(map(force_unicode, sys_path))
|
sys_path = list(map(force_unicode, sys_path))
|
||||||
|
|
||||||
# Load the Python grammar of the current interpreter.
|
# Load the Python grammar of the current interpreter.
|
||||||
project = Project(sys_path=sys_path)
|
if project is None:
|
||||||
|
project = get_default_project()
|
||||||
|
# TODO deprecate and remove sys_path from the Script API.
|
||||||
|
project._sys_path = sys_path
|
||||||
self._evaluator = Evaluator(project, environment)
|
self._evaluator = Evaluator(project, environment)
|
||||||
project.add_script_path(self.path)
|
self._project = project
|
||||||
debug.speed('init')
|
debug.speed('init')
|
||||||
|
|
||||||
@cache.memoize_method
|
@cache.memoize_method
|
||||||
@@ -139,7 +143,7 @@ class Script(object):
|
|||||||
self.path
|
self.path
|
||||||
)
|
)
|
||||||
if self.path is not None:
|
if self.path is not None:
|
||||||
name = dotted_path_in_sys_path(self._evaluator.project.sys_path, self.path)
|
name = dotted_path_in_sys_path(self._evaluator.get_sys_path(), self.path)
|
||||||
if name is not None:
|
if name is not None:
|
||||||
imports.add_module(self._evaluator, name, module)
|
imports.add_module(self._evaluator, name, module)
|
||||||
return module
|
return module
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ from jedi.api.environment import DefaultEnvironment, \
|
|||||||
from jedi.api.exceptions import WrongVersion
|
from jedi.api.exceptions import WrongVersion
|
||||||
from jedi._compatibility import force_unicode
|
from jedi._compatibility import force_unicode
|
||||||
from jedi.evaluate.sys_path import detect_additional_paths
|
from jedi.evaluate.sys_path import detect_additional_paths
|
||||||
from jedi.evaluate.cache import evaluator_function_cache
|
from jedi.evaluate.cache import evaluator_as_method_param_cache
|
||||||
|
|
||||||
_CONFIG_FOLDER = '.jedi'
|
_CONFIG_FOLDER = '.jedi'
|
||||||
_CONTAINS_POTENTIAL_PROJECT = 'setup.py', '.git', '.hg', 'MANIFEST.in'
|
_CONTAINS_POTENTIAL_PROJECT = 'setup.py', '.git', '.hg', 'MANIFEST.in'
|
||||||
@@ -80,7 +80,7 @@ class Project(object):
|
|||||||
pass
|
pass
|
||||||
return sys_path
|
return sys_path
|
||||||
|
|
||||||
@evaluator_function_cache()
|
@evaluator_as_method_param_cache()
|
||||||
def _get_sys_path(self, evaluator, environment=None):
|
def _get_sys_path(self, evaluator, environment=None):
|
||||||
"""
|
"""
|
||||||
Keep this method private for all users of jedi. However internally this
|
Keep this method private for all users of jedi. However internally this
|
||||||
@@ -135,7 +135,7 @@ def get_default_project():
|
|||||||
|
|
||||||
def _get_default_project():
|
def _get_default_project():
|
||||||
previous = None
|
previous = None
|
||||||
curdir = dir = os.path.realpath(os.curdir())
|
curdir = dir = os.path.realpath(os.getcwd())
|
||||||
probable_path = None
|
probable_path = None
|
||||||
while dir != previous:
|
while dir != previous:
|
||||||
try:
|
try:
|
||||||
|
|||||||
@@ -104,7 +104,6 @@ class Evaluator(object):
|
|||||||
self.is_analysis = False
|
self.is_analysis = False
|
||||||
self.project = project
|
self.project = project
|
||||||
self.access_cache = {}
|
self.access_cache = {}
|
||||||
project.add_evaluator(self)
|
|
||||||
|
|
||||||
self.reset_recursion_limitations()
|
self.reset_recursion_limitations()
|
||||||
|
|
||||||
@@ -117,6 +116,10 @@ class Evaluator(object):
|
|||||||
self.recursion_detector = recursion.RecursionDetector()
|
self.recursion_detector = recursion.RecursionDetector()
|
||||||
self.execution_recursion_detector = recursion.ExecutionRecursionDetector(self)
|
self.execution_recursion_detector = recursion.ExecutionRecursionDetector(self)
|
||||||
|
|
||||||
|
def get_sys_path(self):
|
||||||
|
"""Convenience function"""
|
||||||
|
return self.project._get_sys_path(self, environment=self.environment)
|
||||||
|
|
||||||
def eval_element(self, context, element):
|
def eval_element(self, context, element):
|
||||||
if isinstance(context, CompForContext):
|
if isinstance(context, CompForContext):
|
||||||
return eval_node(context, element)
|
return eval_node(context, element)
|
||||||
|
|||||||
@@ -59,7 +59,7 @@ def evaluator_method_cache(default=_NO_DEFAULT):
|
|||||||
return decorator
|
return decorator
|
||||||
|
|
||||||
|
|
||||||
def _memoize_meta_class():
|
def evaluator_as_method_param_cache():
|
||||||
def decorator(call):
|
def decorator(call):
|
||||||
return _memoize_default(second_arg_is_evaluator=True)(call)
|
return _memoize_default(second_arg_is_evaluator=True)(call)
|
||||||
|
|
||||||
@@ -72,6 +72,6 @@ class CachedMetaClass(type):
|
|||||||
class initializations. Either you do it this way or with decorators, but
|
class initializations. Either you do it this way or with decorators, but
|
||||||
with decorators you lose class access (isinstance, etc).
|
with decorators you lose class access (isinstance, etc).
|
||||||
"""
|
"""
|
||||||
@_memoize_meta_class()
|
@evaluator_as_method_param_cache()
|
||||||
def __call__(self, *args, **kwargs):
|
def __call__(self, *args, **kwargs):
|
||||||
return super(CachedMetaClass, self).__call__(*args, **kwargs)
|
return super(CachedMetaClass, self).__call__(*args, **kwargs)
|
||||||
|
|||||||
@@ -135,7 +135,7 @@ def create_access(evaluator, obj):
|
|||||||
|
|
||||||
def load_module(evaluator, path=None, name=None, sys_path=None):
|
def load_module(evaluator, path=None, name=None, sys_path=None):
|
||||||
if sys_path is None:
|
if sys_path is None:
|
||||||
sys_path = list(evaluator.project.sys_path)
|
sys_path = list(evaluator.get_sys_path())
|
||||||
if path is not None:
|
if path is not None:
|
||||||
dotted_path = dotted_from_fs_path(path, sys_path=sys_path)
|
dotted_path = dotted_from_fs_path(path, sys_path=sys_path)
|
||||||
else:
|
else:
|
||||||
|
|||||||
@@ -230,7 +230,7 @@ class Listener(object):
|
|||||||
except KeyError:
|
except KeyError:
|
||||||
from jedi.api.environment import InterpreterEnvironment
|
from jedi.api.environment import InterpreterEnvironment
|
||||||
evaluator = Evaluator(
|
evaluator = Evaluator(
|
||||||
project=project.Project(),
|
project=None,
|
||||||
environment=InterpreterEnvironment()
|
environment=InterpreterEnvironment()
|
||||||
)
|
)
|
||||||
self._evaluators[evaluator_id] = evaluator
|
self._evaluators[evaluator_id] = evaluator
|
||||||
|
|||||||
@@ -137,7 +137,7 @@ class ModuleContext(use_metaclass(CachedMetaClass, TreeContext)):
|
|||||||
return self.py__name__()
|
return self.py__name__()
|
||||||
|
|
||||||
def _py__path__(self):
|
def _py__path__(self):
|
||||||
search_path = self.evaluator.project.sys_path
|
search_path = self.evaluator.get_sys_path()
|
||||||
init_path = self.py__file__()
|
init_path = self.py__file__()
|
||||||
if os.path.basename(init_path) == '__init__.py':
|
if os.path.basename(init_path) == '__init__.py':
|
||||||
with open(init_path, 'rb') as f:
|
with open(init_path, 'rb') as f:
|
||||||
|
|||||||
@@ -243,7 +243,7 @@ class Importer(object):
|
|||||||
|
|
||||||
def sys_path_with_modifications(self):
|
def sys_path_with_modifications(self):
|
||||||
in_path = []
|
in_path = []
|
||||||
sys_path_mod = self._evaluator.project.sys_path \
|
sys_path_mod = self._evaluator.get_sys_path() \
|
||||||
+ sys_path.check_sys_path_modifications(self.module_context)
|
+ sys_path.check_sys_path_modifications(self.module_context)
|
||||||
if self.file_path is not None:
|
if self.file_path is not None:
|
||||||
# If you edit e.g. gunicorn, there will be imports like this:
|
# If you edit e.g. gunicorn, there will be imports like this:
|
||||||
@@ -459,7 +459,7 @@ class Importer(object):
|
|||||||
|
|
||||||
def _load_module(evaluator, path=None, code=None, sys_path=None, parent_module=None):
|
def _load_module(evaluator, path=None, code=None, sys_path=None, parent_module=None):
|
||||||
if sys_path is None:
|
if sys_path is None:
|
||||||
sys_path = evaluator.project.sys_path
|
sys_path = evaluator.get_sys_path()
|
||||||
|
|
||||||
dotted_path = path and dotted_from_fs_path(path, sys_path)
|
dotted_path = path and dotted_from_fs_path(path, sys_path)
|
||||||
if path is not None and path.endswith(('.py', '.zip', '.egg')) \
|
if path is not None and path.endswith(('.py', '.zip', '.egg')) \
|
||||||
@@ -519,7 +519,9 @@ def get_modules_containing_name(evaluator, modules, name):
|
|||||||
if name in code:
|
if name in code:
|
||||||
module = _load_module(evaluator, path, code)
|
module = _load_module(evaluator, path, code)
|
||||||
|
|
||||||
module_name = sys_path.dotted_path_in_sys_path(evaluator.project.sys_path, path)
|
module_name = sys_path.dotted_path_in_sys_path(
|
||||||
|
evaluator.get_sys_path(), path
|
||||||
|
)
|
||||||
if module_name is not None:
|
if module_name is not None:
|
||||||
add_module(evaluator, module_name, module)
|
add_module(evaluator, module_name, module)
|
||||||
return module
|
return module
|
||||||
|
|||||||
@@ -130,7 +130,7 @@ def check_sys_path_modifications(module_context):
|
|||||||
|
|
||||||
|
|
||||||
def sys_path_with_modifications(evaluator, module_context):
|
def sys_path_with_modifications(evaluator, module_context):
|
||||||
return evaluator.project.sys_path + check_sys_path_modifications(module_context)
|
return evaluator.get_sys_path() + check_sys_path_modifications(module_context)
|
||||||
|
|
||||||
|
|
||||||
def detect_additional_paths(evaluator, script_path):
|
def detect_additional_paths(evaluator, script_path):
|
||||||
|
|||||||
@@ -63,7 +63,7 @@ def test_sys_path_with_modifications(Script):
|
|||||||
""")
|
""")
|
||||||
|
|
||||||
path = os.path.abspath(os.path.join(os.curdir, 'module_name.py'))
|
path = os.path.abspath(os.path.join(os.curdir, 'module_name.py'))
|
||||||
paths = Script(code, path=path)._evaluator.project.sys_path
|
paths = Script(code, path=path)._evaluator.get_sys_path()
|
||||||
assert '/tmp/.buildout/eggs/important_package.egg' in paths
|
assert '/tmp/.buildout/eggs/important_package.egg' in paths
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user