1
0
forked from VimPlug/jedi

Start using the parse function for caching as well.

This commit is contained in:
Dave Halter
2017-03-24 01:52:55 +01:00
parent fb4cff8ef9
commit ac0d0869c9
3 changed files with 33 additions and 33 deletions

View File

@@ -5,12 +5,11 @@ from jedi.evaluate.site import addsitedir
from jedi._compatibility import exec_function, unicode from jedi._compatibility import exec_function, unicode
from jedi.parser.python import tree from jedi.parser.python import tree
from jedi.parser import ParserWithRecovery from jedi.parser.python import parse
from jedi.evaluate.cache import memoize_default from jedi.evaluate.cache import memoize_default
from jedi import debug from jedi import debug
from jedi import common from jedi import common
from jedi.evaluate.compiled import CompiledObject from jedi.evaluate.compiled import CompiledObject
from jedi.parser.utils import load_parser, save_parser
def get_venv_path(venv): def get_venv_path(venv):
@@ -211,22 +210,15 @@ def sys_path_with_modifications(evaluator, module_context):
return list(result) + list(buildout_script_paths) return list(result) + list(buildout_script_paths)
def _get_paths_from_buildout_script(evaluator, buildout_script): def _get_paths_from_buildout_script(evaluator, buildout_script_path):
def load(buildout_script):
try: try:
with open(buildout_script, 'rb') as f: module_node = parse(
source = common.source_to_unicode(f.read()) path=buildout_script_path,
grammar=evaluator.grammar,
cache=True
)
except IOError: except IOError:
debug.dbg('Error trying to read buildout_script: %s', buildout_script) debug.warning('Error trying to read buildout_script: %s', buildout_script_path)
return
p = ParserWithRecovery(evaluator.grammar, source, buildout_script)
save_parser(evaluator.grammar, buildout_script, p)
return p.get_root_node()
cached = load_parser(evaluator.grammar, buildout_script)
module_node = cached and cached.module or load(buildout_script)
if module_node is None:
return return
from jedi.evaluate.representation import ModuleContext from jedi.evaluate.representation import ModuleContext

View File

@@ -8,6 +8,7 @@ from jedi.parser.pgen2.pgen import generate_grammar
from jedi.parser.python.parser import Parser, ParserWithRecovery, \ from jedi.parser.python.parser import Parser, ParserWithRecovery, \
_remove_last_newline _remove_last_newline
from jedi.parser.tokenize import source_tokens from jedi.parser.tokenize import source_tokens
from jedi.parser import utils
_loaded_grammars = {} _loaded_grammars = {}
@@ -46,7 +47,7 @@ def load_grammar(version=None):
def parse(code=None, path=None, grammar=None, error_recovery=True, def parse(code=None, path=None, grammar=None, error_recovery=True,
start_symbol='file_input', cache=False): start_symbol='file_input', cache=False, diff_cache=False):
""" """
If you want to parse a Python file you want to start here, most likely. If you want to parse a Python file you want to start here, most likely.
@@ -64,20 +65,30 @@ def parse(code=None, path=None, grammar=None, error_recovery=True,
:return: A syntax tree node. Typically the module. :return: A syntax tree node. Typically the module.
""" """
if start_symbol != 'file_input' and error_recovery: if code is None and path is None:
raise Exception( raise TypeError("Please provide either code or a path.")
'The start_symbol is only allowed when error recovery is disabled.')
if grammar is None:
grammar = load_grammar()
if path is not None:
path = os.path.expanduser(path)
use_cache = cache and path is not None and not code
if use_cache:
# In this case we do actual caching. We just try to load it.
p = utils.load_parser(grammar, path)
if p is not None:
return p.get_root_node()
if code is None:
with open(path) as f:
code = f.read()
added_newline = not code.endswith('\n') added_newline = not code.endswith('\n')
if added_newline: if added_newline:
code += '\n' code += '\n'
if grammar is None:
grammar = load_grammar()
if cache and path and not code:
# In this case we do actual caching
path = os.path.expanduser(path)
tokens = source_tokens(code, use_exact_op_types=True) tokens = source_tokens(code, use_exact_op_types=True)
kwargs = {} kwargs = {}
if error_recovery: if error_recovery:
@@ -90,4 +101,6 @@ def parse(code=None, path=None, grammar=None, error_recovery=True,
if added_newline: if added_newline:
_remove_last_newline(module) _remove_last_newline(module)
if use_cache:
utils.save_parser(grammar, path, parser)
return module return module

View File

@@ -119,7 +119,6 @@ class ParserPickling(object):
greater than the original pickling time. In which case the pickled greater than the original pickling time. In which case the pickled
parser is not up to date. parser is not up to date.
""" """
path = self._clean_path(path)
try: try:
pickle_changed_time = self._index[path] pickle_changed_time = self._index[path]
except KeyError: except KeyError:
@@ -144,7 +143,6 @@ class ParserPickling(object):
return parser_cache_item.parser return parser_cache_item.parser
def save_parser(self, grammar, path, parser_cache_item): def save_parser(self, grammar, path, parser_cache_item):
path = self._clean_path(path)
self.__index = None self.__index = None
try: try:
files = self._index files = self._index
@@ -191,9 +189,6 @@ class ParserPickling(object):
shutil.rmtree(self._cache_directory()) shutil.rmtree(self._cache_directory())
self.__index = {} self.__index = {}
def _clean_path(self, path):
return os.path.expanduser(path)
def _get_hashed_path(self, grammar, path): def _get_hashed_path(self, grammar, path):
file_hash = hashlib.sha256(path.encode("utf-8")).hexdigest() file_hash = hashlib.sha256(path.encode("utf-8")).hexdigest()
return self._get_path('%s-%s.pkl' % (grammar.sha256, file_hash)) return self._get_path('%s-%s.pkl' % (grammar.sha256, file_hash))