1
0
forked from VimPlug/jedi

move star_import_cache to cache.py

This commit is contained in:
David Halter
2012-12-22 01:39:22 +01:00
parent ed8845c343
commit 0d6d84402d
2 changed files with 41 additions and 42 deletions

View File

@@ -6,6 +6,8 @@ memoize_caches = []
time_caches = [] time_caches = []
star_import_cache = {}
def clear_caches(): def clear_caches():
""" Jedi caches many things, that should be completed after each completion """ Jedi caches many things, that should be completed after each completion
@@ -90,3 +92,40 @@ def time_cache(time_add_setting):
def cache_get_in_function_call(stmt): def cache_get_in_function_call(stmt):
module_path = stmt.get_parent_until().path module_path = stmt.get_parent_until().path
return None if module_path is None else (module_path, stmt.start_pos) return None if module_path is None else (module_path, stmt.start_pos)
def cache_star_import(func):
def wrapper(scope, *args, **kwargs):
try:
mods = star_import_cache[scope]
if mods[0] + settings.star_import_cache_validity > time.time():
return mods[1]
except KeyError:
pass
# cache is too old and therefore invalid or not available
invalidate_star_import_cache(scope)
mods = func(scope, *args, **kwargs)
star_import_cache[scope] = time.time(), mods
return mods
return wrapper
def invalidate_star_import_cache(module, only_main=False):
""" Important if some new modules are being reparsed """
try:
t, mods = star_import_cache[module]
del star_import_cache[module]
for m in mods:
invalidate_star_import_cache(m, only_main=True)
except KeyError:
pass
if not only_main:
# We need a list here because otherwise the list is being changed
# during the iteration in py3k: iteritems -> items.
for key, (t, mods) in list(star_import_cache.items()):
if module in mods:
invalidate_star_import_cache(key)

View File

@@ -4,7 +4,6 @@ import os
import pkgutil import pkgutil
import imp import imp
import sys import sys
import time
import builtin import builtin
import modules import modules
@@ -12,13 +11,11 @@ import debug
import parsing import parsing
import evaluate import evaluate
import itertools import itertools
import settings import cache
# for debugging purposes only # for debugging purposes only
imports_processed = 0 imports_processed = 0
star_import_cache = {}
class ModuleNotFound(Exception): class ModuleNotFound(Exception):
pass pass
@@ -276,44 +273,7 @@ def strip_imports(scopes):
return result return result
def cache_star_import(func): @cache.cache_star_import
def wrapper(scope, *args, **kwargs):
try:
mods = star_import_cache[scope]
if mods[0] + settings.star_import_cache_validity > time.time():
return mods[1]
except KeyError:
pass
# cache is too old and therefore invalid or not available
invalidate_star_import_cache(scope)
mods = func(scope, *args, **kwargs)
star_import_cache[scope] = time.time(), mods
return mods
return wrapper
def invalidate_star_import_cache(module, only_main=False):
""" Important if some new modules are being reparsed """
try:
t, mods = star_import_cache[module]
del star_import_cache[module]
for m in mods:
invalidate_star_import_cache(m, only_main=True)
except KeyError:
pass
if not only_main:
# We need a list here because otherwise the list is being changed
# during the iteration in py3k: iteritems -> items.
for key, (t, mods) in list(star_import_cache.items()):
if module in mods:
invalidate_star_import_cache(key)
@cache_star_import
def remove_star_imports(scope, ignored_modules=[]): def remove_star_imports(scope, ignored_modules=[]):
""" """
Check a module for star imports: Check a module for star imports: