Skip to content

Commit

Permalink
MAINT: split add_dependency() function
Browse files Browse the repository at this point in the history
  • Loading branch information
redeboer committed Oct 28, 2024
1 parent 5728283 commit 096351f
Showing 1 changed file with 47 additions and 27 deletions.
74 changes: 47 additions & 27 deletions src/compwa_policy/utilities/pyproject/setters.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,41 +22,61 @@
from compwa_policy.utilities.pyproject._struct import IncludeGroup, PyprojectTOML


def add_dependency( # noqa: C901, PLR0911, PLR0912
def add_dependency(
pyproject: PyprojectTOML,
package: str,
dependency_group: str | Sequence[str] | None = None,
optional_key: str | Sequence[str] | None = None,
) -> bool:
if optional_key is None and dependency_group is None:
return _add_direct_dependency(pyproject, package)
if dependency_group is not None:
if "dependency-groups" not in pyproject:
pyproject["dependency-groups"] = tomlkit.table(is_super_table=False)
dependency_groups = pyproject["dependency-groups"]
if isinstance(dependency_group, str):
dependencies = dependency_groups.get(dependency_group, [])
if package in dependencies:
return False
dependencies.append(package)
dependency_groups[dependency_group] = to_toml_array(dependencies)
return True
if isinstance(dependency_group, abc.Sequence) and len(dependency_group):
updated = add_dependency(pyproject, package, dependency_group[0])
for previous, current in itertools.pairwise(dependency_group):
dependencies = dependency_groups.get(current, [])
expected: IncludeGroup = {"include-group": previous}
if expected in dependencies:
continue
updated &= True
dependencies.append(expected)
return updated
if optional_key is None:
project = get_sub_table(pyproject, "project")
existing_dependencies = set(project.get("dependencies", []))
if package in existing_dependencies:
return _add_to_dependency_group(pyproject, package, dependency_group)
if optional_key is not None:
return _add_to_optional_dependencies(pyproject, package, optional_key)
return False


def _add_direct_dependency(pyproject: PyprojectTOML, package: str) -> bool:
project = get_sub_table(pyproject, "project")
existing_dependencies = set(project.get("dependencies", []))
if package in existing_dependencies:
return False
existing_dependencies.add(package)
project["dependencies"] = to_toml_array(_sort_taplo(existing_dependencies))
return True


def _add_to_dependency_group(
pyproject: PyprojectTOML, package: str, dependency_group: str | Sequence[str]
) -> bool:
if "dependency-groups" not in pyproject:
pyproject["dependency-groups"] = tomlkit.table(is_super_table=False)
dependency_groups = pyproject["dependency-groups"]
if isinstance(dependency_group, str):
dependencies = dependency_groups.get(dependency_group, [])
if package in dependencies:
return False
existing_dependencies.add(package)
project["dependencies"] = to_toml_array(_sort_taplo(existing_dependencies)) # type:ignore[arg-type]
dependencies.append(package)
dependency_groups[dependency_group] = to_toml_array(dependencies)
return True
if isinstance(dependency_group, abc.Sequence) and len(dependency_group):
updated = add_dependency(pyproject, package, dependency_group[0])
for previous, current in itertools.pairwise(dependency_group):
dependencies = dependency_groups.get(current, [])
expected: IncludeGroup = {"include-group": previous}
if expected in dependencies:
continue
updated &= True
dependencies.append(expected)
return updated
msg = f"Unsupported type for dependency group: {type(dependency_group)}"
raise NotImplementedError(msg)


def _add_to_optional_dependencies(
pyproject: PyprojectTOML, package: str, optional_key: str | Sequence[str]
) -> bool:
if isinstance(optional_key, str):
table_key = "project.optional-dependencies"
optional_dependencies = get_sub_table(pyproject, table_key)
Expand Down

0 comments on commit 096351f

Please sign in to comment.