From ad749a2b5e0727b122d1ef64b4bbdf69412fcf25 Mon Sep 17 00:00:00 2001 From: laundmo Date: Mon, 2 Aug 2021 12:29:20 +0200 Subject: [PATCH 1/2] Allow tidypy to run with a file, not just a folder --- src/tidypy/finder.py | 44 +++++++++++++++++++++++--------------- src/tidypy/reports/base.py | 13 ++++++----- 2 files changed, 35 insertions(+), 22 deletions(-) diff --git a/src/tidypy/finder.py b/src/tidypy/finder.py index 1a9ac0c..49d391c 100644 --- a/src/tidypy/finder.py +++ b/src/tidypy/finder.py @@ -22,19 +22,23 @@ class Finder(object): be analyzed. """ - def __init__(self, base_path, config): + def __init__(self, path, config): """ - :param base_path: the path to the base of the project - :type base_path: str + :param path: the path to the base of the project or a python file + :type path: str :param config: the configuration to use when searching the project :type config: dict """ - - self.base_path = Path(base_path).resolve() + + path = Path(path).resolve() + if path.is_file(): + self.base_path = Path.cwd() + else: + self.base_path = path self.excludes = compile_masks(config['exclude']) self._found = dict() - self._find(self.base_path) + self._find(path) self._found = dict([ (dirname, files) for dirname, files in self._found.items() @@ -61,17 +65,23 @@ def relative_to_project(self, filepath): return str(Path(filepath).relative_to(self.base_path)) def _find(self, path): - for subpath in path.iterdir(): - if subpath.is_dir(): - if not self.is_excluded_dir(subpath): - self._found[str(subpath)] = [] - self._find(subpath) - - elif subpath.is_file(): - if not self.is_excluded(subpath): - if str(path) not in self._found: - self._found[str(path)] = [] - self._found[str(path)].append(str(subpath)) + if path.is_file(): + if not self.is_excluded(path): + if str(path) not in self._found: + self._found[str(self.base_path)] = [] + self._found[str(self.base_path)].append(str(path)) + else: + for subpath in path.iterdir(): + if subpath.is_dir(): + if not self.is_excluded_dir(subpath): + self._found[str(subpath)] = [] + self._find(subpath) + + elif subpath.is_file(): + if not self.is_excluded(subpath): + if str(path) not in self._found: + self._found[str(path)] = [] + self._found[str(path)].append(str(subpath)) def is_excluded(self, path): """ diff --git a/src/tidypy/reports/base.py b/src/tidypy/reports/base.py index a6c215a..1944082 100644 --- a/src/tidypy/reports/base.py +++ b/src/tidypy/reports/base.py @@ -9,17 +9,21 @@ class Report(object): The base class for TidyPy issue reporters. """ - def __init__(self, config, base_path, output_file=None): + def __init__(self, config, path, output_file=None): """ :param config: the configuration used during the analysis of the project :type config: dict - :param base_path: the path to the project base directory - :type base_path: str + :param path: the path to the project base directory or file + :type path: str """ self.config = config - self.base_path = base_path + path = Path(path) + if path.is_file(): + self.base_path = Path.cwd() + else: + self.base_path = path self._output_needs_closing = False if output_file: self.output_file = output_file @@ -57,7 +61,6 @@ def relative_filename(self, filename): :type filename: str :rtype: str """ - return Path(filename).relative_to(self.base_path).as_posix() def output(self, msg, newline=True): From a741dee194fd0b7733a2014dcec99ad954641600 Mon Sep 17 00:00:00 2001 From: laundmo Date: Tue, 3 Aug 2021 03:32:37 +0200 Subject: [PATCH 2/2] pyproject.toml fix --- src/tidypy/config.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/tidypy/config.py b/src/tidypy/config.py index 70671cf..733445e 100644 --- a/src/tidypy/config.py +++ b/src/tidypy/config.py @@ -2,6 +2,7 @@ import os import shutil import sys +import pathlib from hashlib import sha512 @@ -295,11 +296,10 @@ def get_local_config(project_path, use_cache=True): :type use_cache: bool :rtype: dict """ + pyproject_path = pathlib.Path(project_path) / 'pyproject.toml' - pyproject_path = os.path.join(project_path, 'pyproject.toml') - - if os.path.exists(pyproject_path): - with open(pyproject_path, 'r') as config_file: + if pyproject_path.exists(): + with pyproject_path.open() as config_file: config = toml.load(config_file) config = config.get('tool', {}).get('tidypy', {}) @@ -326,7 +326,12 @@ def get_project_config(project_path, use_cache=True): :type use_cache: bool :rtype: dict """ - + path = pathlib.Path(project_path) + if path.is_file(): + project_path = pathlib.Path.cwd() + else: + project_path = path + return get_local_config(project_path, use_cache=use_cache) \ or get_user_config(project_path, use_cache=use_cache) \ or get_default_config()