mirror of
https://github.com/davidhalter/jedi.git
synced 2026-05-25 01:38:36 +08:00
move CachedModule and get_sys_path from builtin.py to modules.py
This commit is contained in:
+3
-50
@@ -9,61 +9,14 @@ if is_py3k:
|
|||||||
import types
|
import types
|
||||||
import inspect
|
import inspect
|
||||||
|
|
||||||
import cache
|
|
||||||
import common
|
import common
|
||||||
import debug
|
import debug
|
||||||
import parsing
|
import parsing
|
||||||
import fast_parser
|
|
||||||
import evaluate
|
import evaluate
|
||||||
|
import modules
|
||||||
|
|
||||||
|
|
||||||
def get_sys_path():
|
class Parser(modules.CachedModule):
|
||||||
def check_virtual_env(sys_path):
|
|
||||||
""" Add virtualenv's site-packages to the `sys.path`."""
|
|
||||||
venv = os.getenv('VIRTUAL_ENV')
|
|
||||||
if not venv:
|
|
||||||
return
|
|
||||||
venv = os.path.abspath(venv)
|
|
||||||
p = os.path.join(
|
|
||||||
venv, 'lib', 'python%d.%d' % sys.version_info[:2], 'site-packages')
|
|
||||||
sys_path.insert(0, p)
|
|
||||||
|
|
||||||
p = sys.path[1:]
|
|
||||||
check_virtual_env(p)
|
|
||||||
return p
|
|
||||||
|
|
||||||
|
|
||||||
class CachedModule(object):
|
|
||||||
"""
|
|
||||||
The base type for all modules, which is not to be confused with
|
|
||||||
`parsing_representation.Module`. Caching happens here.
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(self, path=None, name=None):
|
|
||||||
self.path = path and os.path.abspath(path)
|
|
||||||
self.name = name
|
|
||||||
self._parser = None
|
|
||||||
|
|
||||||
@property
|
|
||||||
def parser(self):
|
|
||||||
""" get the parser lazy """
|
|
||||||
if self._parser is None:
|
|
||||||
self._parser = cache.load_module(self.path, self.name) \
|
|
||||||
or self._load_module()
|
|
||||||
return self._parser
|
|
||||||
|
|
||||||
def _get_source(self):
|
|
||||||
raise NotImplementedError()
|
|
||||||
|
|
||||||
def _load_module(self):
|
|
||||||
source = self._get_source()
|
|
||||||
p = self.path or self.name
|
|
||||||
p = fast_parser.FastParser(source, p)
|
|
||||||
cache.save_module(self.path, self.name, p)
|
|
||||||
return p
|
|
||||||
|
|
||||||
|
|
||||||
class Parser(CachedModule):
|
|
||||||
"""
|
"""
|
||||||
This module is a parser for all builtin modules, which are programmed in
|
This module is a parser for all builtin modules, which are programmed in
|
||||||
C/C++. It should also work on third party modules.
|
C/C++. It should also work on third party modules.
|
||||||
@@ -93,7 +46,7 @@ class Parser(CachedModule):
|
|||||||
|
|
||||||
def __init__(self, path=None, name=None, sys_path=None):
|
def __init__(self, path=None, name=None, sys_path=None):
|
||||||
if sys_path is None:
|
if sys_path is None:
|
||||||
sys_path = get_sys_path()
|
sys_path = modules.get_sys_path()
|
||||||
if not name:
|
if not name:
|
||||||
name = os.path.basename(path)
|
name = os.path.basename(path)
|
||||||
name = name.rpartition('.')[0] # cut file type (normally .so)
|
name = name.rpartition('.')[0] # cut file type (normally .so)
|
||||||
|
|||||||
+1
-1
@@ -241,7 +241,7 @@ class ImportPath(pr.Base):
|
|||||||
sys_path_mod = list(self.sys_path_with_modifications())
|
sys_path_mod = list(self.sys_path_with_modifications())
|
||||||
sys_path_mod.insert(0, self.file_path)
|
sys_path_mod.insert(0, self.file_path)
|
||||||
else:
|
else:
|
||||||
sys_path_mod = list(builtin.get_sys_path())
|
sys_path_mod = list(modules.get_sys_path())
|
||||||
|
|
||||||
current_namespace = (None, None, None)
|
current_namespace = (None, None, None)
|
||||||
# now execute those paths
|
# now execute those paths
|
||||||
|
|||||||
+49
-4
@@ -11,12 +11,41 @@ import cache
|
|||||||
import parsing
|
import parsing
|
||||||
import parsing_representation as pr
|
import parsing_representation as pr
|
||||||
import fast_parser
|
import fast_parser
|
||||||
import builtin
|
|
||||||
import debug
|
import debug
|
||||||
import settings
|
import settings
|
||||||
|
|
||||||
|
|
||||||
class Module(builtin.CachedModule):
|
class CachedModule(object):
|
||||||
|
"""
|
||||||
|
The base type for all modules, which is not to be confused with
|
||||||
|
`parsing_representation.Module`. Caching happens here.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, path=None, name=None):
|
||||||
|
self.path = path and os.path.abspath(path)
|
||||||
|
self.name = name
|
||||||
|
self._parser = None
|
||||||
|
|
||||||
|
@property
|
||||||
|
def parser(self):
|
||||||
|
""" get the parser lazy """
|
||||||
|
if self._parser is None:
|
||||||
|
self._parser = cache.load_module(self.path, self.name) \
|
||||||
|
or self._load_module()
|
||||||
|
return self._parser
|
||||||
|
|
||||||
|
def _get_source(self):
|
||||||
|
raise NotImplementedError()
|
||||||
|
|
||||||
|
def _load_module(self):
|
||||||
|
source = self._get_source()
|
||||||
|
p = self.path or self.name
|
||||||
|
p = fast_parser.FastParser(source, p)
|
||||||
|
cache.save_module(self.path, self.name, p)
|
||||||
|
return p
|
||||||
|
|
||||||
|
|
||||||
|
class Module(CachedModule):
|
||||||
"""
|
"""
|
||||||
Manages all files, that are parsed and caches them.
|
Manages all files, that are parsed and caches them.
|
||||||
|
|
||||||
@@ -224,6 +253,22 @@ class ModuleWithCursor(Module):
|
|||||||
return self._part_parser
|
return self._part_parser
|
||||||
|
|
||||||
|
|
||||||
|
def get_sys_path():
|
||||||
|
def check_virtual_env(sys_path):
|
||||||
|
""" Add virtualenv's site-packages to the `sys.path`."""
|
||||||
|
venv = os.getenv('VIRTUAL_ENV')
|
||||||
|
if not venv:
|
||||||
|
return
|
||||||
|
venv = os.path.abspath(venv)
|
||||||
|
p = os.path.join(
|
||||||
|
venv, 'lib', 'python%d.%d' % sys.version_info[:2], 'site-packages')
|
||||||
|
sys_path.insert(0, p)
|
||||||
|
|
||||||
|
p = sys.path[1:]
|
||||||
|
check_virtual_env(p)
|
||||||
|
return p
|
||||||
|
|
||||||
|
|
||||||
@cache.memoize_default([])
|
@cache.memoize_default([])
|
||||||
def sys_path_with_modifications(module):
|
def sys_path_with_modifications(module):
|
||||||
def execute_code(code):
|
def execute_code(code):
|
||||||
@@ -247,9 +292,9 @@ def sys_path_with_modifications(module):
|
|||||||
try:
|
try:
|
||||||
possible_stmts = module.used_names['path']
|
possible_stmts = module.used_names['path']
|
||||||
except KeyError:
|
except KeyError:
|
||||||
return builtin.get_sys_path()
|
return get_sys_path()
|
||||||
|
|
||||||
sys_path = list(builtin.get_sys_path()) # copy
|
sys_path = list(get_sys_path()) # copy
|
||||||
for p in possible_stmts:
|
for p in possible_stmts:
|
||||||
try:
|
try:
|
||||||
call = p.get_assignment_calls().get_only_subelement()
|
call = p.get_assignment_calls().get_only_subelement()
|
||||||
|
|||||||
Reference in New Issue
Block a user