forked from VimPlug/jedi
preparations to merge parts of builtin/modules
This commit is contained in:
79
builtin.py
79
builtin.py
@@ -5,8 +5,53 @@ import os
|
|||||||
import debug
|
import debug
|
||||||
import parsing
|
import parsing
|
||||||
|
|
||||||
|
class CachedModule(object):
|
||||||
|
cache = {}
|
||||||
|
|
||||||
class Parser(object):
|
def __init__(self, name=None, path=None, sys_path=sys.path):
|
||||||
|
self.path = path
|
||||||
|
if name:
|
||||||
|
self.name = name
|
||||||
|
else:
|
||||||
|
name = os.path.basename(self.path)
|
||||||
|
self.name = name.rpartition('.')[0] # cut file type (normally .so)
|
||||||
|
self.path = os.path.dirname(self.path)
|
||||||
|
#print self.name, self.path
|
||||||
|
self._content = {}
|
||||||
|
self._parser = None
|
||||||
|
self._module = None
|
||||||
|
self.sys_path = sys_path
|
||||||
|
|
||||||
|
@property
|
||||||
|
def parser(self):
|
||||||
|
""" get the parser lazy """
|
||||||
|
if not self._parser:
|
||||||
|
try:
|
||||||
|
timestamp, parser = self.cache[self.path or self.name]
|
||||||
|
if not self.path or timestamp == os.path.getmtime(self.path):
|
||||||
|
self._parser = parser
|
||||||
|
else:
|
||||||
|
raise KeyError
|
||||||
|
except KeyError:
|
||||||
|
self._load_module()
|
||||||
|
return self._parser
|
||||||
|
|
||||||
|
def _get_source(self):
|
||||||
|
raise NotImplementedError()
|
||||||
|
|
||||||
|
def _load_module(self):
|
||||||
|
source = self._get_source()
|
||||||
|
self._parser = parsing.PyFuzzyParser(source, self.name)
|
||||||
|
#except:
|
||||||
|
# debug.warning('not possible to resolve', self.name, source)
|
||||||
|
#open('builtin_fail', 'w').write(code)
|
||||||
|
# raise
|
||||||
|
p_time = None if not self.path else os.path.getmtime(self.path)
|
||||||
|
|
||||||
|
self.cache[self.path or self.name] = p_time, self._parser
|
||||||
|
|
||||||
|
|
||||||
|
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.
|
||||||
@@ -32,9 +77,11 @@ class Parser(object):
|
|||||||
'file object': 'file("")',
|
'file object': 'file("")',
|
||||||
# TODO things like dbg: ('not working', 'tuple of integers')
|
# TODO things like dbg: ('not working', 'tuple of integers')
|
||||||
}
|
}
|
||||||
cache = {}
|
module_cache = {}
|
||||||
|
|
||||||
def __init__(self, name=None, path=None, sys_path=sys.path):
|
def __init__(self, name=None, path=None, sys_path=sys.path):
|
||||||
|
super(Parser, self).__init__(name, path)
|
||||||
|
|
||||||
self.path = path
|
self.path = path
|
||||||
if name:
|
if name:
|
||||||
self.name = name
|
self.name = name
|
||||||
@@ -64,32 +111,8 @@ class Parser(object):
|
|||||||
#print 'mod', self._content['module']
|
#print 'mod', self._content['module']
|
||||||
return self._module
|
return self._module
|
||||||
|
|
||||||
@property
|
def _get_source(self):
|
||||||
def parser(self):
|
return self._generate_code(self.module)
|
||||||
""" get the parser lazy """
|
|
||||||
if not self._parser:
|
|
||||||
try:
|
|
||||||
timestamp, parser = Parser.cache[self.name, self.path]
|
|
||||||
if not self.path or timestamp == os.path.getmtime(self.path):
|
|
||||||
debug.dbg('hit builtin cache')
|
|
||||||
self._parser = parser
|
|
||||||
else:
|
|
||||||
raise KeyError
|
|
||||||
except KeyError:
|
|
||||||
code = self._generate_code(self.module)
|
|
||||||
try:
|
|
||||||
self._parser = parsing.PyFuzzyParser(code, self.name)
|
|
||||||
except:
|
|
||||||
debug.warning('not possible to resolve', self.name, code)
|
|
||||||
#open('builtin_fail', 'w').write(code)
|
|
||||||
raise
|
|
||||||
else:
|
|
||||||
if self.path:
|
|
||||||
p_time = os.path.getmtime(self.path)
|
|
||||||
else:
|
|
||||||
p_time = None
|
|
||||||
Parser.cache[self.name, self.path] = p_time, self._parser
|
|
||||||
return self._parser
|
|
||||||
|
|
||||||
def _generate_code(self, scope, depth=0):
|
def _generate_code(self, scope, depth=0):
|
||||||
"""
|
"""
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import debug
|
|||||||
__all__ = ['complete', 'get_completion_parts', 'set_debug_function']
|
__all__ = ['complete', 'get_completion_parts', 'set_debug_function']
|
||||||
|
|
||||||
|
|
||||||
class FileWithCursor(modules.File):
|
class FileWithCursor(modules.Module):
|
||||||
"""
|
"""
|
||||||
Manages all files, that are parsed and caches them.
|
Manages all files, that are parsed and caches them.
|
||||||
Important are the params source and module_path, one of them has to
|
Important are the params source and module_path, one of them has to
|
||||||
|
|||||||
23
modules.py
23
modules.py
@@ -15,7 +15,7 @@ class ModuleNotFound(Exception):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class File(object):
|
class Module(builtin.CachedModule):
|
||||||
"""
|
"""
|
||||||
Manages all files, that are parsed and caches them.
|
Manages all files, that are parsed and caches them.
|
||||||
|
|
||||||
@@ -32,21 +32,19 @@ class File(object):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def parser(self):
|
def parser(self):
|
||||||
if self._parser:
|
if not self._parser:
|
||||||
return self._parser
|
|
||||||
if not self.module_path and not self.source:
|
|
||||||
raise AttributeError("Submit a module name or the source code")
|
|
||||||
elif self.module_path:
|
|
||||||
# check the cache
|
# check the cache
|
||||||
try:
|
try:
|
||||||
timestamp, _parser = File.module_cache[self.module_path]
|
timestamp, _parser = Module.module_cache[self.module_path]
|
||||||
if timestamp == os.path.getmtime(self.module_path):
|
if timestamp == os.path.getmtime(self.module_path):
|
||||||
debug.dbg('hit module cache')
|
debug.dbg('hit module cache')
|
||||||
return _parser
|
return _parser
|
||||||
except:
|
except KeyError:
|
||||||
pass
|
self._load_module()
|
||||||
|
return self._parser
|
||||||
|
|
||||||
return self._load_module()
|
def _get_source(self):
|
||||||
|
return self.source
|
||||||
|
|
||||||
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)
|
||||||
@@ -54,8 +52,7 @@ class File(object):
|
|||||||
|
|
||||||
# insert into cache
|
# insert into cache
|
||||||
to_cache = (os.path.getmtime(self.module_path), self._parser)
|
to_cache = (os.path.getmtime(self.module_path), self._parser)
|
||||||
File.module_cache[self.module_path] = to_cache
|
Module.module_cache[self.module_path] = to_cache
|
||||||
return self._parser
|
|
||||||
|
|
||||||
|
|
||||||
def find_module(current_module, point_path):
|
def find_module(current_module, point_path):
|
||||||
@@ -115,7 +112,7 @@ def find_module(current_module, point_path):
|
|||||||
else:
|
else:
|
||||||
source = current_namespace[0].read()
|
source = current_namespace[0].read()
|
||||||
if path.endswith('.py'):
|
if path.endswith('.py'):
|
||||||
f = File(path, source)
|
f = Module(path, source)
|
||||||
else:
|
else:
|
||||||
f = builtin.Parser(path=path)
|
f = builtin.Parser(path=path)
|
||||||
else:
|
else:
|
||||||
|
|||||||
@@ -162,7 +162,7 @@ def gen():
|
|||||||
yield 1
|
yield 1
|
||||||
yield ""
|
yield ""
|
||||||
|
|
||||||
gen_exe = gen()
|
gen_exe = t12343 + gen()
|
||||||
|
|
||||||
def dec(func):
|
def dec(func):
|
||||||
def wrapper(*args, **kwargs):
|
def wrapper(*args, **kwargs):
|
||||||
|
|||||||
@@ -236,6 +236,6 @@ def gen():
|
|||||||
yield 1
|
yield 1
|
||||||
yield ""
|
yield ""
|
||||||
|
|
||||||
exe = gen()
|
exe = next(gen())
|
||||||
#? ['upper']
|
#? ['upper']
|
||||||
exe.upper
|
exe.upper
|
||||||
|
|||||||
Reference in New Issue
Block a user