Skip to content

Commit

Permalink
Rearrange pre-commit-config.yaml creator.
Browse files Browse the repository at this point in the history
  • Loading branch information
domdfcoding committed Oct 29, 2024
1 parent 2277366 commit 67b3bcf
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 30 deletions.
64 changes: 35 additions & 29 deletions repo_helper/files/pre_commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@

# 3rd party
import attr
import ruamel.yaml
from apeye.url import URL
from domdf_python_tools.paths import PathPlus
from domdf_python_tools.stringlist import StringList
Expand Down Expand Up @@ -150,6 +149,15 @@ def to_dict(self) -> MutableMapping[str, Union[str, List[Hook]]]:
"hooks": self.hooks,
}

def replace_hooks(self: "Repo", hooks: Iterable[Union[str, Hook]]) -> "Repo":
"""
Return a new instance of this repo with the new hooks provided.
:param hooks:
"""

return Repo(self.repo, self.rev, hooks)


pre_commit_hooks = Repo(
repo=make_github_url("pre-commit", "pre-commit-hooks"),
Expand Down Expand Up @@ -209,17 +217,27 @@ def to_dict(self) -> MutableMapping[str, Union[str, List[Hook]]]:
hooks=["reformat-pyproject"], # TODO: add check-pyproject
)

# shellcheck = Repo(
# repo=make_github_url("shellcheck-py", "shellcheck-py"),
# rev="v0.7.1.1",
# hooks=["shellcheck"]
# )
#
# yamllint = Repo(
# repo=make_github_url("adrienverge", "yamllint"),
# rev="v1.23.0",
# hooks=["yamllint"]
# )
snippet_fmt = Repo(
repo=make_github_url("python-formate", "snippet-fmt"),
rev="v0.1.5",
hooks=["snippet-fmt"],
)

domdfcoding_hooks = Repo(
repo=make_github_url("domdfcoding", "pre-commit-hooks"),
rev="v0.4.0",
hooks=[
{"id": "requirements-txt-sorter", "args": ["--allow-git"]},
{"id": "check-docstring-first", "exclude": fr"^(doc-source/conf|__pkginfo__|setup|tests/.*)\.py$"},
"bind-requirements",
]
)

formate = Repo(
repo=make_github_url("python-formate", "formate"),
rev="v0.7.0",
hooks=[{"id": "formate", "exclude": r"^(doc-source/conf|__pkginfo__|setup)\.(_)?py$"}],
)


@management.register("pre-commit", ["enable_pre_commit"])
Expand All @@ -242,9 +260,7 @@ def make_pre_commit(repo_path: pathlib.Path, templates: Environment) -> List[str

non_source_files = [posixpath.join(docs_dir, "conf"), "__pkginfo__", "setup"]

domdfcoding_hooks = Repo(
repo=make_github_url("domdfcoding", "pre-commit-hooks"),
rev="v0.4.0",
domdfcoding_hooks_custom = domdfcoding_hooks.replace_hooks(
hooks=[
{"id": "requirements-txt-sorter", "args": ["--allow-git"]},
{
Expand All @@ -264,19 +280,9 @@ def make_pre_commit(repo_path: pathlib.Path, templates: Environment) -> List[str
}]
)

snippet_fmt = Repo(
repo=make_github_url("python-formate", "snippet-fmt"),
rev="v0.1.5",
hooks=["snippet-fmt"],
)

formate_excludes = fr"^({'|'.join([*templates.globals['yapf_exclude'], *non_source_files])})\.(_)?py$"

formate = Repo(
repo=make_github_url("python-formate", "formate"),
rev="v0.7.0",
hooks=[{"id": "formate", "exclude": formate_excludes}],
)
formate_custom = formate.replace_hooks(hooks=[{"id": "formate", "exclude": formate_excludes}])

dep_checker_args = [templates.globals["import_name"].replace('.', '/')]

Expand All @@ -299,7 +305,7 @@ def make_pre_commit(repo_path: pathlib.Path, templates: Environment) -> List[str
if not pre_commit_file.is_file():
pre_commit_file.touch()

dumper = ruamel.yaml.YAML()
dumper = YAML()
dumper.indent(mapping=2, sequence=3, offset=1)

output = StringList([
Expand All @@ -319,14 +325,14 @@ def make_pre_commit(repo_path: pathlib.Path, templates: Environment) -> List[str
managed_hooks = [
pyproject_parser,
pre_commit_hooks,
domdfcoding_hooks,
domdfcoding_hooks_custom,
flake8_dunder_all,
flake2lint,
pygrep_hooks,
pyupgrade,
lucas_c_hooks,
snippet_fmt,
formate,
formate_custom,
]

if not templates.globals["stubs_package"]:
Expand Down
1 change: 0 additions & 1 deletion repo_helper/files/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -990,7 +990,6 @@ def normalise_underscore(name: str) -> str:
formate_config["hooks"] = hooks
formate_config["config"] = config

formate_file = PathPlus(repo_path / "formate.toml")
dom_toml.dump(formate_config, formate_file, encoder=dom_toml.TomlEncoder)

return [formate_file.name, isort_file.name]
Expand Down

0 comments on commit 67b3bcf

Please sign in to comment.