Skip to content

Commit

Permalink
FEAT: add tool.nbqa.addopts ruff setting
Browse files Browse the repository at this point in the history
  • Loading branch information
redeboer committed Jul 3, 2023
1 parent 8e5144c commit 908b4e9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 27 deletions.
29 changes: 2 additions & 27 deletions src/repoma/check_dev_files/black.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
from repoma.utilities import CONFIG_PATH
from repoma.utilities.executor import Executor
from repoma.utilities.precommit import (
find_repo,
load_round_trip_precommit_config,
update_precommit_hook,
update_single_hook_precommit_repo,
)
Expand All @@ -16,6 +14,7 @@
get_sub_table,
load_pyproject,
to_toml_array,
update_nbqa_settings,
write_pyproject,
)
from repoma.utilities.setup_cfg import get_supported_python_versions
Expand All @@ -27,9 +26,9 @@ def main() -> None:
executor = Executor()
executor(_remove_outdated_settings)
executor(_update_black_settings)
executor(_update_nbqa_settings)
executor(_update_precommit_repo)
executor(_update_precommit_nbqa_hook)
executor(update_nbqa_settings, "black", to_toml_array(["--line-length=85"]))
executor.finalize()


Expand Down Expand Up @@ -83,27 +82,3 @@ def _update_precommit_nbqa_hook() -> None:
additional_dependencies=["black>=22.1.0"],
),
)


def _update_nbqa_settings() -> None:
# cspell:ignore addopts
if not CONFIG_PATH.precommit.exists():
return
if not __has_nbqa_precommit_repo():
return
pyproject = load_pyproject()
nbqa_table = get_sub_table(pyproject, "tool.nbqa.addopts", create=True)
expected = to_toml_array(["--line-length=85"])
if nbqa_table.get("black") != expected:
nbqa_table["black"] = expected
write_pyproject(pyproject)
msg = "Added nbQA configuration for black"
raise PrecommitError(msg)


def __has_nbqa_precommit_repo() -> bool:
config, _ = load_round_trip_precommit_config()
nbqa_repo = find_repo(config, "https://github.com/nbQA-dev/nbQA")
if nbqa_repo is None:
return False
return True
2 changes: 2 additions & 0 deletions src/repoma/check_dev_files/ruff.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
get_sub_table,
load_pyproject,
to_toml_array,
update_nbqa_settings,
write_pyproject,
)
from repoma.utilities.readme import add_badge
Expand All @@ -40,6 +41,7 @@ def main() -> None:
executor(_update_precommit_hook)
executor(_update_precommit_nbqa_hook)
executor(_update_vscode_settings)
executor(update_nbqa_settings, "ruff", to_toml_array(["--line-length=85"]))
executor.finalize()


Expand Down
25 changes: 25 additions & 0 deletions src/repoma/utilities/pyproject.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
from tomlkit.items import Array, Table
from tomlkit.toml_document import TOMLDocument

from repoma.errors import PrecommitError
from repoma.utilities import CONFIG_PATH
from repoma.utilities.precommit import find_repo, load_round_trip_precommit_config


def complies_with_subset(settings: dict, minimal_settings: dict) -> bool:
Expand Down Expand Up @@ -51,3 +53,26 @@ def to_toml_array(items: Iterable[Any]) -> Array:
else:
array.multiline(False)
return array


def update_nbqa_settings(key: str, expected: Any) -> None:
# cspell:ignore addopts
if not CONFIG_PATH.precommit.exists():
return
if not __has_nbqa_precommit_repo():
return
pyproject = load_pyproject()
nbqa_table = get_sub_table(pyproject, "tool.nbqa.addopts", create=True)
if nbqa_table.get(key) != expected:
nbqa_table[key] = expected
write_pyproject(pyproject)
msg = f"Added nbQA configuration for {key!r}"
raise PrecommitError(msg)


def __has_nbqa_precommit_repo() -> bool:
config, _ = load_round_trip_precommit_config()
nbqa_repo = find_repo(config, "https://github.com/nbQA-dev/nbQA")
if nbqa_repo is None:
return False
return True

0 comments on commit 908b4e9

Please sign in to comment.