Skip to content

Commit

Permalink
refactor: load fixed and dynamic classes on BaseFile
Browse files Browse the repository at this point in the history
  • Loading branch information
andreoliwa committed Aug 21, 2019
1 parent 83a8f89 commit af6d0e5
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 18 deletions.
6 changes: 3 additions & 3 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/nitpick/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def __init__(self) -> None:
def create_app(cls) -> "Nitpick":
"""Create a single application."""
from nitpick.config import Config # pylint: disable=redefined-outer-name
from nitpick.files.base import BaseFile

app = cls()
cls._current_app = app
Expand All @@ -46,6 +47,7 @@ def create_app(cls) -> "Nitpick":

app.main_python_file = app.find_main_python_file()
app.config = Config()
BaseFile.load_fixed_dynamic_classes()
except (NoRootDir, NoPythonFile) as err:
app.init_errors.append(err)

Expand Down
18 changes: 16 additions & 2 deletions src/nitpick/files/base.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
"""Base class for file checkers."""
import abc
from pathlib import Path
from typing import Generator, List
from typing import Generator, List, Set, Type

import jmespath

from nitpick import Nitpick
from nitpick.formats import TomlFormat
from nitpick.generic import search_dict
from nitpick.generic import get_subclasses, search_dict
from nitpick.mixin import NitpickMixin
from nitpick.typedefs import JsonDict, YieldFlake8Error

Expand All @@ -24,6 +24,9 @@ class BaseFile(NitpickMixin, metaclass=abc.ABCMeta):
file_dict = {} # type: JsonDict
nitpick_file_dict = {} # type: JsonDict

fixed_name_classes = set() # type: Set[Type[BaseFile]]
dynamic_name_classes = set() # type: Set[Type[BaseFile]]

def __init__(self) -> None:
if self.has_multiple_files:
key = "{}.file_names".format(self.__class__.__name__)
Expand All @@ -32,6 +35,17 @@ def __init__(self) -> None:
self._multiple_files = [self.file_name]
self._set_current_data(self.file_name)

@classmethod
def load_fixed_dynamic_classes(cls) -> None:
"""Separate classes with fixed file names from classes with dynamic files names."""
cls.fixed_name_classes = set()
cls.dynamic_name_classes = set()
for subclass in get_subclasses(BaseFile):
if subclass.file_name:
cls.fixed_name_classes.add(subclass)
else:
cls.dynamic_name_classes.add(subclass)

def _set_current_data(self, file_name: str) -> None:
"""Set data for the current file name, either if there are multiple or single files."""
if self.has_multiple_files:
Expand Down
16 changes: 3 additions & 13 deletions src/nitpick/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from nitpick.files.base import BaseFile
from nitpick.files.pyproject_toml import PyProjectTomlFile
from nitpick.formats import TomlFormat
from nitpick.generic import MergeDict, climb_directory_tree, get_subclasses, is_url, search_dict
from nitpick.generic import MergeDict, climb_directory_tree, is_url, search_dict
from nitpick.schemas import BaseStyleSchema, flatten_marshmallow_errors
from nitpick.typedefs import JsonDict, StrOrList

Expand All @@ -35,16 +35,6 @@ def __init__(self) -> None:
self._already_included = set() # type: Set[str]
self._first_full_path = "" # type: str

# Separate classes with fixed file names from classes with dynamic files names.
# FIXME do only once on app init (Nitpick.__init__(); or create_app(), mimicking Flask)
self.files_predetermined_names = set() # type: Set[Type[BaseFile]]
self.files_dynamic_names = set() # type: Set[Type[BaseFile]]
for subclass in get_subclasses(BaseFile):
if subclass.file_name:
self.files_predetermined_names.add(subclass)
else:
self.files_dynamic_names.add(subclass)

self._dynamic_schema_class = BaseStyleSchema # type: type
self.rebuild_dynamic_schema()

Expand Down Expand Up @@ -217,12 +207,12 @@ def rebuild_dynamic_schema(self, data: JsonDict = None) -> None:
# Data is empty; so this is the first time the dynamic class is being rebuilt.
# Loop on classes with predetermined names, and add fields for them on the dynamic validation schema.
# E.g.: setup.cfg, pre-commit, pyproject.toml: files whose names we already know at this point.
for subclass in self.files_predetermined_names:
for subclass in BaseFile.fixed_name_classes:
self.append_field_from_file(new_files_found, subclass, subclass.file_name)
else:
# Data was provided; search it to find new dynamic files to add to the validation schema).
# E.g.: JSON files that were configured on some TOML style file.
for subclass in self.files_dynamic_names:
for subclass in BaseFile.dynamic_name_classes:
jmex = subclass.get_compiled_jmespath_file_names()
for file_name in search_dict(jmex, data, []):
self.append_field_from_file(new_files_found, subclass, file_name)
Expand Down

0 comments on commit af6d0e5

Please sign in to comment.