configuration-language-server/confls/config/config.py

57 lines
1.9 KiB
Python
Raw Normal View History

2021-09-09 18:31:23 +02:00
import logging
import logging.config
import pyls.config.config as config
import pyls.uris as uris
import pyls._utils as _utils
import pkg_resources
import pluggy
from confls import hookspecs, PYLS
log = logging.getLogger(__name__)
class ConfigurationConfig(config.Config):
def __init__(self, root_uri, init_opts, process_id, capabilities):
self._root_path = uris.to_fs_path(root_uri)
self._root_uri = root_uri
self._init_opts = init_opts
self._process_id = process_id
self._capabilities = capabilities
self._settings = {}
self._plugin_settings = {}
self._config_sources = {}
self._pm = pluggy.PluginManager(PYLS)
self._pm.trace.root.setwriter(log.debug)
self._pm.enable_tracing()
self._pm.add_hookspecs(hookspecs)
# Pluggy will skip loading a plugin if it throws a DistributionNotFound exception.
# However I don't want all plugins to have to catch ImportError and re-throw. So here we'll filter
# out any entry points that throw ImportError assuming one or more of their dependencies isn't present.
for entry_point in pkg_resources.iter_entry_points(PYLS):
try:
entry_point.load()
except ImportError as e:
log.warning("Failed to load %s entry point '%s': %s", PYLS, entry_point.name, e)
self._pm.set_blocked(entry_point.name)
# Load the entry points into pluggy, having blocked any failing ones
self._pm.load_setuptools_entrypoints(PYLS)
for name, plugin in self._pm.list_name_plugin():
if plugin is not None:
log.info("Loaded pyls plugin %s from %s", name, plugin)
for plugin_conf in self._pm.hook.pyls_settings(config=self):
self._plugin_settings = _utils.merge_dicts(self._plugin_settings, plugin_conf)
self._update_disabled_plugins()