def test_index_filesystem(tmpdir): pkg = tmpdir.mkdir('pkg') pkg.join('__init__.py').write('class Cls:\n pass\n') pkg.join('submod.py').write(dedent(''' import sys import _private from os import path from other import _x def func(): pass ''')) with pkg.join('encoded.py').open('wb') as fp: fp.write(b('# encoding: latin1\ndef foo():\n print("\xff")')) # these should be ignored pkg.join('mytest_submod.py').write('def func2():\n pass\n') pkg.join('_submod.py').write('def func3():\n pass\n') pkg.join('syntaxerr.py').write('def func3():\n') tree = SymbolIndex(blacklist_re=re.compile('mytest_')) tree.build_index([str(tmpdir)]) subtree = tree._tree['pkg'] assert serialize(subtree) == { ".location": "L", ".score": 1.0, "Cls": 1.1, "submod": {".location": "L", ".score": 1.0, "func": 1.1, "sys": 0.25, "path": 0.25}, "encoded": {".location": "L", ".score": 1.0, "foo": 1.1}}
def test_index_filesystem(tmpdir): pkg = tmpdir.mkdir('pkg') pkg.join('__init__.py').write('class Cls:\n pass\n') pkg.join('submod.py').write(dedent(''' import sys import _private from os import path from other import _x def func(): pass ''')) # these should be ignored pkg.join('mytest_submod.py').write('def func2():\n pass\n') pkg.join('_submod.py').write('def func3():\n pass\n') pkg.join('syntaxerr.py').write('def func3():\n') tree = SymbolIndex(blacklist_re=re.compile('mytest_')) tree.build_index([str(tmpdir)]) subtree = tree._tree['pkg'] assert serialize(subtree) == { ".location": "L", ".score": 1.0, "Cls": 1.1, "submod": {".location": "L", ".score": 1.0, "func": 1.1, "sys": 0.25, "path": 0.25}}
def test_index_symbol_scores(): src = dedent(''' def walk(dir): pass ''') tree = SymbolIndex() with tree.enter('os') as os_tree: with os_tree.enter('path') as path_tree: path_tree.index_source('os.py', src) assert tree.symbol_scores('walk')[0][1:] == ('os.path', 'walk') assert tree.symbol_scores('os') == [(1.44, 'os', None)] assert tree.symbol_scores('os.path.walk') == [(4.2, 'os.path', None)]
def test_index_if_name_main(): src = dedent(''' if __name__ == '__main__': one = 1 else: two = 2 ''') tree = SymbolIndex() with tree.enter('test') as subtree: subtree.index_source('test.py', src) assert serialize(subtree) == {".location": "L", ".score": 1.0}
def test_index_file_with_all(): src = dedent(''' __all__ = ['one'] one = 1 two = 2 three = 3 ''') tree = SymbolIndex() with tree.enter('test') as subtree: subtree.index_source('test.py', src) assert serialize(subtree) == {".location": "L", ".score": 1.0, "one": 1.1}
def _indexer(self, root, index_file): # Delay indexing briefly so we don't hammer the system. print(os.environ) settings = sublime.load_settings('Python Import Magic.sublime-settings') locations = settings.get('python_path', LIB_LOCATIONS) print(locations) sublime.status_message('Loading index {0}'.format(root)) if os.path.exists(index_file): log('Loading index for {0}', root) with open(index_file) as fd: index = SymbolIndex.deserialize(fd) else: index = SymbolIndex(locations=locations) paths = self._make_python_path(root, locations=locations) log('Indexing {0} with paths {1}', root, os.path.pathsep.join(paths)) index.build_index(paths) with open(index_file, 'w') as fd: fd.write(index.serialize()) with self._lock: self._indexes[root] = index del self._threads[root] log('Ready for {0}', root, status=True)
def index(request): directory = os.path.dirname(__file__) with open(os.path.join(directory, '..', 'testing', 'test_index.json')) as fd: return SymbolIndex.deserialize(fd)
def index(request): dir = os.path.dirname(__file__) with open(os.path.join(dir, 'test_index.json')) as fd: return SymbolIndex.deserialize(fd)