Skip to content

Bulk asynchronous downloader for multiple file hosts

tests: add more config tests

NTFSvolume72dda41f7499
diff --git a/cyberdrop_dl/config/__init__.py b/cyberdrop_dl/config/__init__.py
index 841f5f094..192eb938d 100644
--- a/cyberdrop_dl/config/__init__.py
+++ b/cyberdrop_dl/config/__init__.py
@@ -86,9 +86,19 @@ class Config(BaseModel):
def source(self) -> Path | None:
return self._source
- @classmethod
- def from_file(cls, config_file: Path) -> Config:
- return _load_config_file(config_file)
+ @staticmethod
+ def from_file(file: Path, *, _save_if_not_found: bool = False) -> Config:
+ try:
+ content = yaml.load(file)
+ except FileNotFoundError:
+ default = Config()
+ if _save_if_not_found:
+ yaml.save(file, default.model_dump())
+ return default
+
+ config = Config.model_validate(content, extra="forbid")
+ config._source = file
+ return config
def __or__(self, other: Self) -> Self:
return merge_models(self, other)
@@ -135,31 +145,16 @@ def _resolve_paths(model: BaseModel) -> None:
_resolve_paths(value)
-def _load_config_file(file: Path, *, save_if_not_found: bool = False) -> Config:
- try:
- content = yaml.load(file)
- except FileNotFoundError:
- default = Config()
- if save_if_not_found:
- yaml.save(file, default.model_dump())
- return default
- else:
- config = Config.model_validate(content, extra="forbid")
- config._source = file
- return config
-
-
-def add_or_remove_lists(cli_values: list[str], config_values: list[str]) -> None:
+def merge_additive_args[T: list[str] | tuple[str, ...]](cli_values: T, config_values: Iterable[str]) -> T:
match cli_values:
case ["+", *_]:
- new_values = set(config_values + cli_values)
+ new_values = set(config_values).union(cli_values)
case ["-", *_]:
new_values = set(config_values) - set(cli_values)
case _:
- return
+ return cli_values
- cli_values.clear()
- cli_values.extend(sorted(new_values - {"+", "-"}))
+ return type(cli_values)(sorted(new_values - {"+", "-"}))
def _coerce(*, config: Config | None = None) -> Config:
diff --git a/tests/test_config.py b/tests/test_config.py
index 444a86fae..f3d893027 100644
--- a/tests/test_config.py
+++ b/tests/test_config.py
@@ -8,7 +8,7 @@ from cyclopts.exceptions import UnknownOptionError
from pydantic import BaseModel
import cyberdrop_dl.cli.download
-from cyberdrop_dl.config import Config, _resolve_paths, settings
+from cyberdrop_dl.config import Config, _resolve_paths, merge_additive_args, settings
from cyberdrop_dl.exceptions import CDLConfigRuntimeErrorsGroup
from cyberdrop_dl.models import merge_dicts
@@ -193,3 +193,64 @@ def test_resolve_paths(tmp_cwd: Path) -> None:
config.child.path = Path("URLs.txt")
_resolve_paths(config)
assert config.child.path == tmp_cwd / "URLs.txt"
+
+
+ ("config_args", "cli_args", "expected"),
+ [
+ (
+ [],
+ ["a", "b", "c"],
+ ["a", "b", "c"],
+ ),
+ (
+ ("e"),
+ ("a", "b", "c"),
+ ("a", "b", "c"),
+ ),
+ (
+ ("b", "d", "e"),
+ ("+", "a", "b", "c"),
+ ("a", "b", "c", "d", "e"),
+ ),
+ (
+ ("a", "d", "e"),
+ ("-", "a", "b", "c"),
+ ("d", "e"),
+ ),
+ (
+ ("drive.google.com", "facebook.com", "youtube.com"),
+ ("instagram.com",),
+ ("instagram.com",),
+ ),
+ (
+ ("drive.google.com", "facebook.com", "youtube.com"),
+ ("+", "instagram.com"),
+ ("drive.google.com", "facebook.com", "instagram.com", "youtube.com"),
+ ),
+ (
+ ("drive.google.com", "facebook.com", "youtube.com"),
+ ("-", "youtube.com"),
+ ("drive.google.com", "facebook.com"),
+ ),
+ ],
+)
+def test_additive_args(
+ config_args: list[str] | tuple[str, ...],
+ cli_args: list[str] | tuple[str, ...],
+ expected: list[str] | tuple[str, ...],
+) -> None:
+ result = merge_additive_args(cli_args, config_args)
+ assert type(result) is type(expected)
+ assert result == expected
+
+
+def test_config_from_file(tmp_cwd: Path) -> None:
+ config_file = tmp_cwd / "cdl_config.txt"
+ config_1 = Config.from_file(config_file)
+ assert config_1.source is None
+ config_file.touch()
+ config_2 = Config.from_file(config_file)
+ assert config_2.source == config_file
+ config_2._source = None
+ assert config_1 == config_2