Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
0675ca7
Rename `pyproject` -> `pyproject_toml` for clarity
nathanjmcdougall Feb 23, 2025
7c7a0f2
Start TOML integration abstraction
nathanjmcdougall Feb 23, 2025
ed7da7c
Pass the tests while offline
nathanjmcdougall Feb 23, 2025
aec3046
Bring `set_config_value` into a dedicated TOML integration layer
nathanjmcdougall Feb 27, 2025
482b2f0
Move more functions into the TOML layer
nathanjmcdougall Feb 27, 2025
1adc675
Move do-keys-exist check to TOML layer
nathanjmcdougall Feb 27, 2025
5595ee3
Rename pyproject_toml layer core functions
nathanjmcdougall Feb 27, 2025
ae833fd
Merge branch 'main' into 219-properly-handle-other-ruff-configuration…
nathanjmcdougall Feb 28, 2025
c799cb1
Merge branch 'main' into 219-properly-handle-other-ruff-configuration…
nathanjmcdougall Mar 9, 2025
c09c0ef
Refactor the way Tools define pyproject.toml configuration to make it…
nathanjmcdougall Mar 9, 2025
62e0156
First draft of major refactor in the way file config is handled
nathanjmcdougall Mar 15, 2025
315c3fd
Temporarily skip failing test
nathanjmcdougall Mar 15, 2025
7c5ae0b
Merge branch 'main' into 219-properly-handle-other-ruff-configuration…
nathanjmcdougall Mar 15, 2025
58fedda
Rename id_keys -> keys
nathanjmcdougall Mar 15, 2025
52e9ce2
do_keys_exist -> contains
nathanjmcdougall Mar 15, 2025
9ae8066
Update docstring
nathanjmcdougall Mar 15, 2025
9342fb1
Update docstring
nathanjmcdougall Mar 15, 2025
9753d26
Add setitem and delitem methods to the KeyValueFileManager
nathanjmcdougall Mar 16, 2025
2ee4d93
Addressing various TODOs
nathanjmcdougall Mar 16, 2025
2f0fa73
Migrate tests into correct file
nathanjmcdougall Mar 16, 2025
6c402e5
Migrate tests into the correct file
nathanjmcdougall Mar 16, 2025
884c079
Uncomment tests
nathanjmcdougall Mar 16, 2025
1d18465
Refactor tests for new structure
nathanjmcdougall Mar 16, 2025
c22e88c
Fix a few bugs
nathanjmcdougall Mar 16, 2025
9ec5420
Re-enable temporarily disabled ruff rules
nathanjmcdougall Mar 16, 2025
bfe38dd
Add edge case tests for toml file manager
nathanjmcdougall Mar 16, 2025
045478a
Test the UsethisFileManager setter
nathanjmcdougall Mar 16, 2025
00ba2fc
Support alternative ruff config files
nathanjmcdougall Mar 16, 2025
7ca9fae
Fix bug in remove_configs logic for non-pyproject.toml files
nathanjmcdougall Mar 16, 2025
ca65935
Test dunder methods for TestUsethisFileManager
nathanjmcdougall Mar 16, 2025
26511fb
Add tests for various edge cases
nathanjmcdougall Mar 16, 2025
234cb46
Remove unused method
nathanjmcdougall Mar 16, 2025
a6d4769
Fix test which doesn't test what is intended
nathanjmcdougall Mar 16, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ layers = [
"_interface",
"_core",
"_tool | _ci",
"_config_file",
"_integrations",
"_console",
"_config | _io",
Expand All @@ -186,7 +187,7 @@ name = "Integrations Modular Design"
type = "layers"
layers = [
"ci | pre_commit",
"uv | pytest | ruff | pydantic | sonarqube",
"uv | pytest | pydantic | sonarqube",
"project | file | python",
]
containers = [ "usethis._integrations" ]
Expand Down
37 changes: 37 additions & 0 deletions src/usethis/_config_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from __future__ import annotations

import contextlib
from pathlib import Path
from typing import TYPE_CHECKING

from usethis._integrations.file.pyproject_toml.io_ import PyprojectTOMLManager
from usethis._integrations.file.toml.io_ import TOMLFileManager

if TYPE_CHECKING:
from collections.abc import Iterator


@contextlib.contextmanager
def files_manager() -> Iterator[None]:
with (
PyprojectTOMLManager(),
DotRuffTOMLManager(),
RuffTOMLManager(),
):
yield


class DotRuffTOMLManager(TOMLFileManager):
"""Class to manage the .ruff.toml file."""

@property
def relative_path(self) -> Path:
return Path(".ruff.toml")


class RuffTOMLManager(TOMLFileManager):
"""Class to manage the ruff.toml file."""

@property
def relative_path(self) -> Path:
return Path("ruff.toml")
39 changes: 17 additions & 22 deletions src/usethis/_core/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@
get_hook_names,
)
from usethis._integrations.pytest.core import add_pytest_dir, remove_pytest_dir
from usethis._integrations.ruff.rules import (
deselect_ruff_rules,
ignore_ruff_rules,
select_ruff_rules,
)
from usethis._integrations.uv.call import call_uv_subprocess
from usethis._integrations.uv.init import ensure_pyproject_toml
from usethis._tool import (
Expand Down Expand Up @@ -62,11 +57,11 @@ def use_codespell(*, remove: bool = False) -> None:
else:
tool.add_pre_commit_repo_configs()

tool.add_pyproject_configs()
tool.add_configs()
tool.print_how_to_use()
else:
remove_bitbucket_steps_from_default(tool.get_bitbucket_steps())
tool.remove_pyproject_configs()
tool.remove_configs()
tool.remove_pre_commit_repo_configs()
tool.remove_dev_deps()
tool.remove_managed_files()
Expand All @@ -79,10 +74,10 @@ def use_coverage(*, remove: bool = False) -> None:

if not remove:
tool.add_test_deps()
tool.add_pyproject_configs()
tool.add_configs()
tool.print_how_to_use()
else:
tool.remove_pyproject_configs()
tool.remove_configs()
tool.remove_test_deps()
tool.remove_managed_files()

Expand All @@ -102,7 +97,7 @@ def use_deptry(*, remove: bool = False) -> None:
tool.print_how_to_use()
else:
tool.remove_pre_commit_repo_configs()
tool.remove_pyproject_configs()
tool.remove_configs()
remove_bitbucket_steps_from_default(tool.get_bitbucket_steps())
tool.remove_dev_deps()
tool.remove_managed_files()
Expand All @@ -123,11 +118,11 @@ def use_pre_commit(*, remove: bool = False) -> None:
# We will use pre-commit instead of project-installed dependencies:
if pyproject_fmt_tool.is_used():
pyproject_fmt_tool.remove_dev_deps()
pyproject_fmt_tool.add_pyproject_configs()
pyproject_fmt_tool.add_configs()
pyproject_fmt_tool.print_how_to_use()
if codespell_tool.is_used():
codespell_tool.remove_dev_deps()
codespell_tool.add_pyproject_configs()
codespell_tool.add_configs()
codespell_tool.print_how_to_use()

if requirements_txt_tool.is_used():
Expand Down Expand Up @@ -206,11 +201,11 @@ def use_pyproject_fmt(*, remove: bool = False) -> None:
else:
tool.add_pre_commit_repo_configs()

tool.add_pyproject_configs()
tool.add_configs()
tool.print_how_to_use()
else:
remove_bitbucket_steps_from_default(tool.get_bitbucket_steps())
tool.remove_pyproject_configs()
tool.remove_configs()
tool.remove_pre_commit_repo_configs()
tool.remove_dev_deps()
tool.remove_managed_files()
Expand All @@ -236,9 +231,9 @@ def use_pytest(*, remove: bool = False) -> None:

if not remove:
tool.add_test_deps()
tool.add_pyproject_configs()
tool.add_configs()
if RuffTool().is_used():
select_ruff_rules(tool.get_associated_ruff_rules())
RuffTool().select_rules(tool.get_associated_ruff_rules())

# deptry currently can't scan the tests folder for dev deps
# https://github.com/fpgmaas/deptry/issues/302
Expand All @@ -256,8 +251,8 @@ def use_pytest(*, remove: bool = False) -> None:
remove_bitbucket_pytest_steps()

if RuffTool().is_used():
deselect_ruff_rules(tool.get_associated_ruff_rules())
tool.remove_pyproject_configs()
RuffTool().deselect_rules(tool.get_associated_ruff_rules())
tool.remove_configs()
tool.remove_test_deps()
remove_pytest_dir() # Last, since this is a manual step

Expand Down Expand Up @@ -335,9 +330,9 @@ def use_ruff(*, remove: bool = False) -> None:

if not remove:
tool.add_dev_deps()
tool.add_pyproject_configs()
select_ruff_rules(rules)
ignore_ruff_rules(ignored_rules)
tool.add_configs()
tool.select_rules(rules)
tool.ignore_rules(ignored_rules)
if PreCommitTool().is_used():
tool.add_pre_commit_repo_configs()
elif is_bitbucket_used():
Expand All @@ -347,6 +342,6 @@ def use_ruff(*, remove: bool = False) -> None:
else:
tool.remove_pre_commit_repo_configs()
remove_bitbucket_steps_from_default(tool.get_bitbucket_steps())
tool.remove_pyproject_configs()
tool.remove_configs()
tool.remove_dev_deps()
tool.remove_managed_files()
10 changes: 0 additions & 10 deletions src/usethis/_integrations/file/pyproject_toml/config.py

This file was deleted.

101 changes: 0 additions & 101 deletions src/usethis/_integrations/file/pyproject_toml/core.py

This file was deleted.

22 changes: 22 additions & 0 deletions src/usethis/_integrations/file/pyproject_toml/io_.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,24 @@
from usethis._integrations.file.pyproject_toml.errors import (
PyprojectTOMLDecodeError,
PyprojectTOMLNotFoundError,
PyprojectTOMLValueAlreadySetError,
PyprojectTOMLValueMissingError,
UnexpectedPyprojectTOMLIOError,
UnexpectedPyprojectTOMLOpenError,
)
from usethis._integrations.file.toml.errors import (
TOMLDecodeError,
TOMLNotFoundError,
TOMLValueAlreadySetError,
TOMLValueMissingError,
UnexpectedTOMLIOError,
UnexpectedTOMLOpenError,
)
from usethis._integrations.file.toml.io_ import TOMLFileManager

if TYPE_CHECKING:
from typing import Any

from typing_extensions import Self


Expand Down Expand Up @@ -49,3 +55,19 @@ def _validate_lock(self) -> None:
super()._validate_lock()
except UnexpectedTOMLIOError as err:
raise UnexpectedPyprojectTOMLIOError(err) from None

def set_value(
self, *, keys: list[str], value: Any, exists_ok: bool = False
) -> None:
"""Set a value in the pyproject.toml configuration file."""
try:
super().set_value(keys=keys, value=value, exists_ok=exists_ok)
except TOMLValueAlreadySetError as err:
raise PyprojectTOMLValueAlreadySetError(err) from None

def __delitem__(self, keys: list[str]) -> None:
"""Remove a value from the pyproject.toml configuration file."""
try:
super().__delitem__(keys)
except TOMLValueMissingError as err:
raise PyprojectTOMLValueMissingError(err) from None
Loading