1
0
forked from Mirrors/itch-dl

Sync settings and CLI arguments

Most CLI arguments can be now saved into the configuration profile JSON.
This also cleans up passing around some, but not all args, into various
classes and methods, instead of just passing all of settings.
This commit is contained in:
Ryszard Knop
2025-01-31 22:35:37 +01:00
parent 5a9cbf675f
commit 1cb57d0be4
3 changed files with 63 additions and 39 deletions

View File

@@ -2,6 +2,7 @@ import os
import json
import logging
import platform
import argparse
from typing import Optional
import requests
@@ -9,6 +10,18 @@ from pydantic import BaseModel
from . import __version__
OVERRIDABLE_SETTINGS = (
"api_key",
"user_agent",
"download_to",
"mirror_web",
"urls_only",
"parallel",
"filter_files_glob",
"filter_files_regex",
"verbose",
)
class Settings(BaseModel):
"""Available settings for itch-dl. Make sure all of them
@@ -17,9 +30,16 @@ class Settings(BaseModel):
api_key: Optional[str] = None
user_agent: str = f"python-requests/{requests.__version__} itch-dl/{__version__}"
download_to: Optional[str] = None
mirror_web: bool = False
urls_only: bool = False
parallel: int = 1
filter_files_glob: Optional[str] = None
filter_files_regex: Optional[str] = None
verbose: bool = False
def create_and_get_config_path() -> str:
"""Returns the configuration directory in the appropriate
@@ -37,7 +57,7 @@ def create_and_get_config_path() -> str:
return os.path.join(base_path, "itch-dl")
def load_config(profile: Optional[str] = None) -> Settings:
def load_config(args: argparse.Namespace, profile: Optional[str] = None) -> Settings:
"""Loads the configuration from the file system if it exists,
the returns a Settings object."""
config_path = create_and_get_config_path()
@@ -58,4 +78,13 @@ def load_config(profile: Optional[str] = None) -> Settings:
config_data.update(profile_data)
return Settings(**config_data)
# All settings from the base file:
settings = Settings(**config_data)
# Apply overrides from CLI args:
for key in OVERRIDABLE_SETTINGS:
value = getattr(args, key)
if value:
setattr(settings, key, value)
return settings