From 4701b868de341bd34c257a20b1cbe7fed4fce0ab Mon Sep 17 00:00:00 2001 From: Augusto Wagner Andreoli Date: Sun, 23 Dec 2018 18:16:24 +0100 Subject: [PATCH] feat: Use nitpick's own default style file if none is provided --- flake8_nitpick/__init__.py | 34 +++++++++++++++++++--------------- nitpick-style.toml | 3 +++ setup.py | 2 +- 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/flake8_nitpick/__init__.py b/flake8_nitpick/__init__.py index 160951e2..c2d70844 100644 --- a/flake8_nitpick/__init__.py +++ b/flake8_nitpick/__init__.py @@ -25,6 +25,7 @@ CACHE_DIR: Path = Path(os.getcwd()) / ".cache" / NAME PYPROJECT_TOML = "pyproject.toml" NITPICK_STYLE_TOML = "nitpick-style.toml" +DEFAULT_NITPICK_STYLE_URL = "https://raw.githubusercontent.com/andreoliwa/flake8-nitpick/master/nitpick-style.toml" ROOT_PYTHON_FILES = ("setup.py", "manage.py", "autoapp.py") ROOT_FILES = (PYPROJECT_TOML, "setup.cfg", "requirements*.txt", "Pipfile") + ROOT_PYTHON_FILES @@ -102,33 +103,36 @@ def find_style(self) -> Optional[Path]: style: str = self.pyproject_toml.get("style", "") if style.startswith("http"): # If the style is a URL, save the contents in the cache dir - response = requests.get(style) - if not response.ok: - LOG.error("Error %r fetching style URL %s", response, style) - raise RuntimeError(f"Error {response} fetching style URL {style}") - contents = response.text - style_path = CACHE_DIR / "style.toml" - style_path.write_text(contents) + style_path = self.load_style_from_url(style) LOG.info("Loading style from URL: %s", style_path) elif style: style_path = Path(style) if not style_path.exists(): - LOG.error("Style file not found %s", style_path) raise RuntimeError(f"Style file does not exist: {style}") LOG.info("Loading style from file: %s", style_path) else: paths = climb_directory_tree(self.root_dir, [NITPICK_STYLE_TOML]) - if not paths: - LOG.error("Style file not found on the directory tree above %s", self.root_dir) - raise RuntimeError( - f"Style not configured on {PYPROJECT_TOML} and {NITPICK_STYLE_TOML} not found in directory tree" - ) - style_path = paths[0] - LOG.info("Loading style from directory tree: %s", style_path) + if paths: + style_path = paths[0] + LOG.info("Loading style from directory tree: %s", style_path) + else: + style_path = self.load_style_from_url(DEFAULT_NITPICK_STYLE_URL) + LOG.info("Loading default Nitpick style %s into local file %s", DEFAULT_NITPICK_STYLE_URL, style_path) cache.dump_path(style_path) return style_path + @staticmethod + def load_style_from_url(url: str) -> Path: + """Load a style file from a URL.""" + response = requests.get(url) + if not response.ok: + raise RuntimeError(f"Error {response} fetching style URL {url}") + contents = response.text + style_path = CACHE_DIR / "style.toml" + style_path.write_text(contents) + return style_path + @attr.s(hash=False) class NitpickChecker: diff --git a/nitpick-style.toml b/nitpick-style.toml index 41e8f935..59e05d67 100644 --- a/nitpick-style.toml +++ b/nitpick-style.toml @@ -1,3 +1,6 @@ +# Default style file for flake8-nitpick +# https://raw.githubusercontent.com/andreoliwa/flake8-nitpick/master/nitpick-style.toml + ["pyproject.toml".tool.black] line-length = 120 diff --git a/setup.py b/setup.py index f4f3ea12..bac93f64 100644 --- a/setup.py +++ b/setup.py @@ -22,7 +22,7 @@ VERSION = None # What packages are required for this module to be executed? -REQUIRED = ["flake8 > 3.0.0", "attrs", "toml", "requests"] +REQUIRED = ["flake8 > 3.0.0", "attrs", "toml", "requests", "dictdiffer"] # The rest you shouldn't have to touch too much :) # ------------------------------------------------