8000 Configurable preloaded modules (#367) · python-lsp/python-lsp-server@d294fe1 · GitHub
[go: up one dir, main page]

Skip to content

Commit d294fe1

Browse files
authored
Configurable preloaded modules (#367)
1 parent 077e1ae commit d294fe1

File tree

5 files changed

+62
-57
lines changed

5 files changed

+62
-57
lines changed

pyls/plugins/preload_imports.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Copyright 2017 Palantir Technologies, Inc.
2+
import logging
3+
from pyls import hookimpl
4+
5+
log = logging.getLogger(__name__)
6+
7+
MODULES = [
8+
"OpenGL", "PIL",
9+
"array", "audioop", "binascii", "cPickle", "cStringIO", "cmath", "collections",
10+
"datetime", "errno", "exceptions", "gc", "imageop", "imp", "itertools",
11+
"marshal", "math", "matplotlib", "mmap", "mpmath", "msvcrt", "networkx", "nose", "nt",
12+
"numpy", "operator", "os", "os.path", "pandas", "parser", "rgbimg", "scipy", "signal",
13+
"skimage", "sklearn", "statsmodels", "strop", "sympy", "sys", "thread", "time",
14+
"wx", "xxsubtype", "zipimport", "zlib"
15+
]
16+
17+
18+
@hookimpl
19+
def pyls_settings():
20+
# Setup default modules to preload, and rope extension modules
21+
return {
22+
'plugins': {'preload': {'modules': MODULES}},
23+
'rope': {'extensionModules': MODULES}
24+
}
25+
26+
27+
@hookimpl
28+
def pyls_initialize(config):
29+
for mod_name in config.plugin_settings('preload').get('modules', []):
30+
try:
31+
__import__(mo 10000 d_name)
32+
log.debug("Preloaded module %s", mod_name)
33+
except ImportError:
34+
pass

pyls/workspace.py

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
import logging
44
import os
55
import re
6-
import imp
7-
import pkgutil
86

97
import jedi
108

@@ -17,59 +15,11 @@
1715
RE_END_WORD = re.compile('^[A-Za-z_0-9]*')
1816

1917

20-
def get_submodules(mod):
21-
"""Get all submodules of a given module"""
22-
def catch_exceptions(_module):
23-
pass
24-
25-
try:
26-
m = __import__(mod)
27-
submodules = [mod]
28-
submods = pkgutil.walk_packages(m.__path__, m.__name__ + '.', catch_exceptions)
29-
for sm in submods:
30-
sm_name = sm[1]
31-
submodules.append(sm_name)
32-
except ImportError:
33-
return []
34-
except: # pylint: disable=bare-except
35-
return [mod]
36-
return submodules
37-
38-
39-
def get_preferred_submodules():
40-
mods = ['numpy', 'scipy', 'sympy', 'pandas',
41-
'networkx', 'statsmodels', 'matplotlib', 'sklearn',
42-
'skimage', 'mpmath', 'os', 'PIL',
43-
'OpenGL', 'array', 'audioop', 'binascii', 'cPickle',
44-
'cStringIO', 'cmath', 'collections', 'datetime',
45-
'errno', 'exceptions', 'gc', 'imageop', 'imp',
46-
'itertools', 'marshal', 'math', 'mmap', 'msvcrt',
47-
'nt', 'operator', 'parser', 'rgbimg', 'signal',
48-
'strop', 'sys', 'thread', 'time', 'wx', 'xxsubtype',
49-
'zipimport', 'zlib', 'nose', 'os.path']
50-
51-
submodules = []
52-
for mod in mods:
53-
submods = get_submodules(mod)
54-
submodules += submods
55-
56-
actual = []
57-
for submod in submodules:
58-
try:
59-
imp.find_module(submod)
60-
actual.append(submod)
61-
except ImportError:
62-
pass
63-
64-
return actual
65-
66-
6718
class Workspace(object):
6819

6920
M_PUBLISH_DIAGNOSTICS = 'textDocument/publishDiagnostics'
7021
M_APPLY_EDIT = 'workspace/applyEdit'
7122
M_SHOW_MESSAGE = 'window/showMessage'
72-
PRELOADED_MODULES = get_preferred_submodules()
7323

7424
def __init__(self, root_uri, endpoint):
7525
self._root_uri = root_uri
@@ -89,7 +39,7 @@ def _rope_project_builder(self, rope_config):
8939
if self.__rope is None or self.__rope_config != rope_config:
9040
rope_folder = rope_config.get('ropeFolder')
9141
self.__rope = Project(self._root_path, ropefolder=rope_folder)
92-
self.__rope.prefs.set('extension_modules', self.PRELOADED_MODULES)
42+
self.__rope.prefs.set('extension_modules', rope_config.get('extensionModules', []))
9343
self.__rope.prefs.set('ignore_syntax_errors', True)
9444
self.__rope.prefs.set('ignore_bad_imports', True)
9545
self.__rope.validate()

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
'jedi_signature_help = pyls.plugins.signature',
8181
'jedi_symbols = pyls.plugins.symbols',
8282
'mccabe = pyls.plugins.mccabe_lint',
83+
'preload = pyls.plugins.preload_imports',
8384
'pycodestyle = pyls.plugins.pycodestyle_lint',
8485
'pydocstyle = pyls.plugins.pydocstyle_lint',
8586
'pyflakes = pyls.plugins.pyflakes_lint',

test/plugins/test_completion.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
# Copyright 2017 Palantir Technologies, Inc.
22
import os
3-
from rope.base.project import Project
43

54
from pyls import uris
6-
from pyls.workspace import Document, get_preferred_submodules
5+
from pyls.workspace import Document
76
from pyls.plugins.jedi_completion import pyls_completions as pyls_jedi_completions
87
from pyls.plugins.rope_completion import pyls_completions as pyls_rope_completions
98

@@ -47,8 +46,6 @@ def test_jedi_completion():
4746
def test_rope_completion(config, workspace):
4847
# Over 'i' in os.path.isabs(...)
4948
com_position = {'line': 1, 'character': 15}
50-
rope = Project(LOCATION)
51-
rope.prefs.set('extension_modules', get_preferred_submodules())
5249
workspace.put_document(DOC_URI, source=DOC)
5350
doc = workspace.get_document(DOC_URI)
5451
items = pyls_rope_completions(config, workspace, doc, com_position)

vscode-client/package.json

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,20 @@
7575
"default": 15,
7676
"description": "The minimum threshold that triggers warnings about cyclomatic complexity."
7777
},
78+
"pyls.plugins.preload.enabled": {
79+
"type": "boolean",
80+
"default": true,
81+
"description": "Enable or disable the plugin."
82+
},
83+
"pyls.plugins.preload.modules": {
84+
"type": "array",
85+
"default": null,
86+
"items": {
87+
"type": "string"
88+
},
89+
"uniqueItems": true,
90+
"description": "List of modules to import on startup"
91+
},
7892
"pyls.plugins.pycodestyle.enabled": {
7993
"type": "boolean",
8094
"default": true,
@@ -201,10 +215,19 @@
201215
"default": true,
202216
"description": "Enable or disable the plugin."
203217
},
204-
"pyls.rope.ropeFolder": {
218+
"pyls.rope.extensionModules": {
205219
"type": "string",
206-
"default": ".ropeproject",
220+
"default": null,
207221
"description": "The name of the folder in which rope stores project configurations and data. Pass `null` for not using such a folder at all."
222+
},
223+
"pyls.rope.ropeFolder": {
224+
"type": "array",
225+
"default": null,
226+
"items": {
227+
"type": "string"
228+
},
229+
"uniqueItems": true,
230+
"description": "Builtin and c-extension modules that are allowed to be imported and inspected by rope."
208231
}
209232
}
210233
}

0 commit comments

Comments
 (0)
0