Skip to content

Commit

Permalink
lsp: handle missing modules gracefully
Browse files Browse the repository at this point in the history
Also removed hard install requirement of pygls where users are not
interested in using pyang as an LSP server.

Signed-off-by: Siddharth Sharma <[email protected]>
  • Loading branch information
esmasth committed May 16, 2024
1 parent e3d3f19 commit f4e96a0
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 10 deletions.
1 change: 0 additions & 1 deletion dev-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@ pyflakes
flake8
pylint
virtualenv
pygls
3 changes: 0 additions & 3 deletions pyang/lsp/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
"""pyang library to serve Microsoft LSP"""

__version__ = "0.0.1"
__date__ = "2024-05-10"
9 changes: 7 additions & 2 deletions pyang/lsp/server.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""pyang LSP handling"""

from __future__ import absolute_import
import io
import optparse
import tempfile
from pathlib import Path
Expand All @@ -14,9 +13,15 @@
from pyang.translators import yang

from lsprotocol import types as lsp

from pygls.server import LanguageServer

import importlib
ext_deps = ['lsprotocol.types', 'pygls.server']
def try_import_deps():
"""Throws ModuleNotFoundError if external module dependencies are missing"""
for dep in ext_deps:
importlib.import_module(dep)

SERVER_NAME = "pyangls"
SERVER_VERSION = "v0.1"

Expand Down
11 changes: 9 additions & 2 deletions pyang/scripts/pyang_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,8 +341,15 @@ def run():
p.pre_load_modules(ctx)

if o.lsp:
pyangls.start_server(o, ctx, fmts)
sys.exit(0)
try:
pyangls.try_import_deps()
pyangls.start_server(o, ctx, fmts)
sys.exit(0)
except ModuleNotFoundError as e:
print("LSP feature required external dependencies are missing")
print(str(e))
print("Please resolve dependencies to use pyang as an LSP server")
sys.exit(1)

exit_code = 0
modules = []
Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
lxml
pygls
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def run_commands(self):
" Provides a framework for plugins that can convert YANG modules" + \
"to other formats.",
url='https://github.com/mbj4668/pyang',
install_requires = ["lxml", "pygls"],
install_requires = ["lxml"],
license='BSD',
classifiers=[
'Development Status :: 5 - Production/Stable',
Expand Down

0 comments on commit f4e96a0

Please sign in to comment.