Example #1
0
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}}
Example #2
0
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 _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)