From 0f876f5f43874e7e6d70070fd317c1a82bfa15a2 Mon Sep 17 00:00:00 2001 From: Christopher Pickering Date: Fri, 14 Oct 2022 10:09:54 +0200 Subject: [PATCH 1/2] added function to find project root --- linter.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/linter.py b/linter.py index 4c2af06..b99ee0d 100644 --- a/linter.py +++ b/linter.py @@ -1,5 +1,6 @@ import logging import re +from pathlib import Path from SublimeLinter.lint import PythonLinter @@ -23,6 +24,26 @@ class Pylint(PythonLinter): '--init-hook=;': None } + def find_project_root(self, src): + """Attempt to get the project root.""" + directory = src.parent + + for directory in list(src.resolve().parents): + + if (directory / ".pylintrc").is_file(): + return directory + + if (directory / "pyproject.toml").is_file(): + return directory + + if (directory / ".git").exists(): + return directory + + if (directory / ".hg").is_dir(): + return directory + + return directory + def on_stderr(self, stderr): stderr = re.sub( 'No config file found, using default configuration\n', '', stderr) @@ -44,6 +65,8 @@ def cmd(self): ] settings['init-hook'] = commands + self.context['project_root'] = str(self.find_project_root(Path(self.view.file_name()))) + return ( 'pylint', '--msg-template=\'{line}:{column}:{msg_id}: {msg} ({symbol})\'', From 2d9a7027958bd2683848fb6f3e239c29f5eed2e1 Mon Sep 17 00:00:00 2001 From: Christopher Pickering Date: Fri, 14 Oct 2022 11:17:04 +0200 Subject: [PATCH 2/2] changed from pathlib to os.path --- linter.py | 64 ++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 42 insertions(+), 22 deletions(-) diff --git a/linter.py b/linter.py index b99ee0d..841dda1 100644 --- a/linter.py +++ b/linter.py @@ -1,11 +1,51 @@ import logging import re -from pathlib import Path +import os +from itertools import chain, takewhile from SublimeLinter.lint import PythonLinter logger = logging.getLogger('SublimeLinter.plugins.pylint') +HOME = os.path.expanduser('~') + + +def paths_upwards(path): + while True: + yield path + + next_path = os.path.dirname(path) + # Stop just before root in *nix systems + if next_path == '/': + return + + if next_path == path: + return + + path = next_path + + +def paths_upwards_until_home(path): + return chain(takewhile(lambda p: p != HOME, paths_upwards(path)), [HOME]) + + +def find_project_root(src): + """Attempt to get the project root.""" + for src in paths_upwards_until_home(src): + if os.path.exists(os.path.join(src, ".pylintrc")): + return src + + if os.path.exists(os.path.join(src, "pyproject.toml")): + return src + + if os.path.exists(os.path.join(src, ".git")): + return src + + if os.path.exists(os.path.join(src, ".hg")): + return src + + return src + class Pylint(PythonLinter): regex = ( @@ -24,26 +64,6 @@ class Pylint(PythonLinter): '--init-hook=;': None } - def find_project_root(self, src): - """Attempt to get the project root.""" - directory = src.parent - - for directory in list(src.resolve().parents): - - if (directory / ".pylintrc").is_file(): - return directory - - if (directory / "pyproject.toml").is_file(): - return directory - - if (directory / ".git").exists(): - return directory - - if (directory / ".hg").is_dir(): - return directory - - return directory - def on_stderr(self, stderr): stderr = re.sub( 'No config file found, using default configuration\n', '', stderr) @@ -65,7 +85,7 @@ def cmd(self): ] settings['init-hook'] = commands - self.context['project_root'] = str(self.find_project_root(Path(self.view.file_name()))) + self.context['project_root'] = find_project_root(self.view.file_name()) return ( 'pylint',