Skip to content

Commit

Permalink
refactor: remove unneeded attributes from BuiltinStyle
Browse files Browse the repository at this point in the history
  • Loading branch information
andreoliwa committed Nov 19, 2023
1 parent 57c7f70 commit 8db6be4
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 21 deletions.
5 changes: 3 additions & 2 deletions docs/autofix_docs.py
Expand Up @@ -30,7 +30,7 @@
EmojiEnum,
)
from nitpick.core import Nitpick
from nitpick.style import BuiltinStyle, builtin_styles
from nitpick.style import BuiltinStyle, builtin_styles, repo_root

RST_DIVIDER_FROM_HERE = ".. auto-generated-from-here"
RST_DIVIDER_START = ".. auto-generated-start-{}"
Expand Down Expand Up @@ -296,7 +296,8 @@ def _build_library(gitref: bool = True) -> list[str]:
style = BuiltinStyle.from_path(path)

# When run with tox (invoke ci-build), the path starts with "site-packages"
clean_root = style.path_from_repo_root.replace("site-packages/", "src/")
path_from_repo_root = path.relative_to(repo_root()).as_posix()
clean_root = path_from_repo_root.replace("site-packages/", "src/")

row = StyleLibraryRow(
style=f"{pre}`{style.py_url_without_ext} <{clean_root}>`{post}",
Expand Down
2 changes: 1 addition & 1 deletion src/nitpick/generic.py
Expand Up @@ -106,7 +106,7 @@ def get_global_gitignore_path() -> Path | None:
"""Get the path to the global Git ignore file."""
try:
output = subprocess.check_output(
# TODO(AA): fix: this command might not work on Windows; maybe read ~/.gitconfig directly instead?
# TODO: fix: this command might not work on Windows; maybe read ~/.gitconfig directly instead?
["git", "config", "--get", GIT_CORE_EXCLUDES_FILE], # noqa: S603,S607
universal_newlines=True,
).strip()
Expand Down
22 changes: 4 additions & 18 deletions src/nitpick/style.py
Expand Up @@ -787,44 +787,30 @@ def fetch(self, url: furl) -> str:
class BuiltinStyle: # pylint: disable=too-few-public-methods
"""A built-in style file in TOML format."""

py_url: furl
py_url_without_ext: furl
path_from_repo_root: str
path_from_resources_root: str

pypackage_url: PythonPackageURL = attr.field(init=False)
identify_tag: str = attr.field(init=False)
name: str = attr.field(init=False)
url: str = attr.field(init=False)
files: list[str] = attr.field(init=False)

@classmethod
def from_path(cls, resource_path: Path) -> BuiltinStyle: # pylint: disable=too-many-locals
"""Create a built-in style from a resource path."""

def from_path(cls, resource_path: Path) -> BuiltinStyle:
"""Create a style from its path."""
without_suffix = resource_path.with_suffix("")
src_path = builtin_resources_root().parent.parent
package_path = resource_path.relative_to(src_path)
package_path = resource_path.relative_to(builtin_resources_root().parent.parent)
from_resources_root = without_suffix.relative_to(builtin_resources_root())

root, *path_remainder = package_path.parts
path_remainder_without_suffix = (*path_remainder[:-1], without_suffix.parts[-1])

# TODO(AA): fix: rename this class to Style, handle styles that are not resources,
# then remove this ugly hack. This will be a massive refactoring...
# https://doc.pytest.org/en/latest/example/simple.html#pytest-current-test-environment-variable
running_pytest = "PYTEST_CURRENT_TEST" in os.environ
bis = BuiltinStyle(
py_url=furl(scheme=Scheme.PY, host=root, path=path_remainder),
py_url_without_ext=furl(scheme=Scheme.PY, host=root, path=path_remainder_without_suffix),
path_from_repo_root="" if running_pytest else resource_path.relative_to(repo_root()).as_posix(),
path_from_resources_root=from_resources_root.as_posix(),
)
bis.pypackage_url = PythonPackageURL.from_furl(bis.py_url)
bis.identify_tag = from_resources_root.parts[0]

content_path = resource_path if running_pytest else bis.pypackage_url.content_path
toml_dict = tomlkit.loads(content_path.read_text(encoding="UTF-8"))
toml_dict = tomlkit.loads(resource_path.read_text(encoding="UTF-8"))

keys = list(toml_dict.keys())
keys.remove(PROJECT_NAME)
Expand Down

0 comments on commit 8db6be4

Please sign in to comment.