feat(config): add workers field and --workers CLI flag
Add parallel download worker-count configuration through all three config layers: Config dataclass (workers=3 default), YAML parsing with InvalidWorkersError validation, and -w/--workers CLI flag with three-layer resolution (CLI > config > hardcoded 3). 6 new tests. Closes #12
This commit is contained in:
1 parent
b02884fe46
commit
dbdcc72260
10 files changed
+326
-2
No files matched your search
@@ -20,6 +20,7 @@ def run(
|
||||
history_path: str = "download_history.json",
|
||||
verbose: bool = False,
|
||||
limit: int = 0,
|
||||
workers: int = 0,
|
||||
) -> int:
|
||||
"""Main pipeline: load config → auth → fetch library → filter → download → summary.
|
||||
|
||||
@@ -28,10 +29,12 @@ def run(
|
||||
secrets_path: Path to secrets.yaml (gitignored).
|
||||
history_path: Path to download_history.json.
|
||||
verbose: Print extra detail (URLs, paths, API responses).
|
||||
limit: Maximum number of books to download (0 = no limit).
|
||||
workers: Parallel download worker count (0 = use config value).
|
||||
|
||||
Returns:
|
||||
Exit code: 0 on success, 1 on fatal error, 130 on Ctrl+C interrupt.
|
||||
"""
|
||||
"""
|
||||
|
||||
try:
|
||||
if verbose:
|
||||
@@ -54,6 +57,12 @@ def run(
|
||||
console.print(f" covers: {config.download_covers}")
|
||||
console.print(f" user: {config.username}")
|
||||
|
||||
# Resolve workers: CLI flag (>0?) → config.workers → default 3
|
||||
resolved_workers = workers if workers > 0 else config.workers
|
||||
if resolved_workers < 1:
|
||||
resolved_workers = 3
|
||||
console.print(f" user: {config.username}")
|
||||
|
||||
# 2. Authenticate
|
||||
if verbose:
|
||||
console.print("[dim]── auth ────────────────────────────────────────[/dim]")
|
||||
@@ -199,6 +208,14 @@ if __name__ == "__main__":
|
||||
metavar="N",
|
||||
help="Maximum number of books to download (0 = no limit). Useful for testing.",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-w",
|
||||
"--workers",
|
||||
type=int,
|
||||
default=0,
|
||||
metavar="N",
|
||||
help="Parallel download workers (0 = use config value). Default: 3.",
|
||||
)
|
||||
args = parser.parse_args()
|
||||
|
||||
sys.exit(run(
|
||||
@@ -207,4 +224,5 @@ if __name__ == "__main__":
|
||||
history_path=args.history,
|
||||
verbose=args.verbose,
|
||||
limit=args.limit,
|
||||
workers=args.workers,
|
||||
))
|
||||
@@ -16,6 +16,7 @@ class Config:
|
||||
output_dir: str
|
||||
download_extras: bool
|
||||
download_covers: bool
|
||||
workers: int = 3
|
||||
|
||||
|
||||
class ConfigError(Exception):
|
||||
@@ -34,6 +35,10 @@ class InvalidFormatError(ConfigError):
|
||||
"""Invalid audio format value."""
|
||||
|
||||
|
||||
class InvalidWorkersError(ConfigError):
|
||||
"""Invalid parallel download worker count."""
|
||||
|
||||
|
||||
def load_config(config_path: Path | str, secrets_path: Path | str) -> Config:
|
||||
"""Load config.yaml + secrets.yaml, merge, validate, and return Config."""
|
||||
config_path = Path(config_path)
|
||||
@@ -59,6 +64,10 @@ def load_config(config_path: Path | str, secrets_path: Path | str) -> Config:
|
||||
# Validate format
|
||||
_validate_format(librofm.get("format", "m4b_mp3_fallback"))
|
||||
|
||||
# Validate workers (if explicitly set)
|
||||
if "workers" in librofm:
|
||||
_validate_workers(librofm["workers"])
|
||||
|
||||
return Config(
|
||||
username=librofm["username"],
|
||||
password=librofm["password"],
|
||||
@@ -66,6 +75,7 @@ def load_config(config_path: Path | str, secrets_path: Path | str) -> Config:
|
||||
output_dir=librofm.get("output_dir", "./audiobooks"),
|
||||
download_extras=librofm.get("download_extras", True),
|
||||
download_covers=librofm.get("download_covers", True),
|
||||
workers=librofm.get("workers", 3),
|
||||
)
|
||||
|
||||
|
||||
@@ -110,4 +120,12 @@ def _validate_format(fmt: str) -> None:
|
||||
if fmt not in VALID_FORMATS:
|
||||
raise InvalidFormatError(
|
||||
f"Invalid format '{fmt}'. Valid formats: {', '.join(sorted(VALID_FORMATS))}."
|
||||
)
|
||||
|
||||
|
||||
def _validate_workers(workers: int) -> None:
|
||||
"""Raise InvalidWorkersError if workers is less than 1."""
|
||||
if not isinstance(workers, int) or workers < 1:
|
||||
raise InvalidWorkersError(
|
||||
f"Invalid workers value '{workers}'. Must be an integer >= 1."
|
||||
)
|
||||
Reference in new issue
Block a user