mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-09 23:34:45 +08:00
module caching
This commit is contained in:
18
modules.py
18
modules.py
@@ -10,7 +10,7 @@ files = {}
|
|||||||
load_module_cb = None
|
load_module_cb = None
|
||||||
module_find_path = sys.path[1:]
|
module_find_path = sys.path[1:]
|
||||||
|
|
||||||
# TODO we need module caching
|
|
||||||
|
|
||||||
class ModuleNotFound(Exception):
|
class ModuleNotFound(Exception):
|
||||||
pass
|
pass
|
||||||
@@ -23,6 +23,8 @@ class File(object):
|
|||||||
:param source: The source code of the file.
|
:param source: The source code of the file.
|
||||||
:param module_path: The module path of the file.
|
:param module_path: The module path of the file.
|
||||||
"""
|
"""
|
||||||
|
module_cache = {}
|
||||||
|
|
||||||
def __init__(self, module_path, source):
|
def __init__(self, module_path, source):
|
||||||
self.source = source
|
self.source = source
|
||||||
self.module_path = module_path
|
self.module_path = module_path
|
||||||
@@ -36,11 +38,24 @@ class File(object):
|
|||||||
if not self.module_path and not self.source:
|
if not self.module_path and not self.source:
|
||||||
raise AttributeError("Submit a module name or the source code")
|
raise AttributeError("Submit a module name or the source code")
|
||||||
elif self.module_path:
|
elif self.module_path:
|
||||||
|
# check the cache
|
||||||
|
try:
|
||||||
|
timestamp, _parser = File.module_cache[self.module_path]
|
||||||
|
if timestamp == os.path.getmtime(self.module_path):
|
||||||
|
debug.dbg('hit cache')
|
||||||
|
return _parser
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
return self._load_module()
|
return self._load_module()
|
||||||
|
|
||||||
def _load_module(self):
|
def _load_module(self):
|
||||||
self._parser = parsing.PyFuzzyParser(self.source, self.module_path)
|
self._parser = parsing.PyFuzzyParser(self.source, self.module_path)
|
||||||
del self.source # efficiency
|
del self.source # efficiency
|
||||||
|
|
||||||
|
# insert into cache
|
||||||
|
to_cache = (os.path.getmtime(self.module_path), self._parser)
|
||||||
|
File.module_cache[self.module_path] = to_cache
|
||||||
return self._parser
|
return self._parser
|
||||||
|
|
||||||
def find_module(current_module, point_path):
|
def find_module(current_module, point_path):
|
||||||
@@ -59,7 +74,6 @@ def find_module(current_module, point_path):
|
|||||||
if ns:
|
if ns:
|
||||||
path = [ns[1]]
|
path = [ns[1]]
|
||||||
else:
|
else:
|
||||||
# TODO modules can be system modules, without '.' in path
|
|
||||||
path = None
|
path = None
|
||||||
debug.dbg('search_module', string, path)
|
debug.dbg('search_module', string, path)
|
||||||
try:
|
try:
|
||||||
|
|||||||
Reference in New Issue
Block a user