2022-06-12 19:50:43 +02:00
|
|
|
import os
|
|
|
|
import json
|
|
|
|
import logging
|
|
|
|
import platform
|
2025-01-31 22:35:37 +01:00
|
|
|
import argparse
|
2025-02-14 15:41:14 +01:00
|
|
|
from dataclasses import dataclass, fields
|
2025-02-14 14:55:54 +01:00
|
|
|
from typing import Any, get_type_hints
|
2022-06-12 19:50:43 +02:00
|
|
|
|
|
|
|
import requests
|
|
|
|
|
|
|
|
from . import __version__
|
|
|
|
|
2025-01-31 22:35:37 +01:00
|
|
|
|
2025-02-14 15:41:14 +01:00
|
|
|
@dataclass
|
|
|
|
class Settings:
|
2022-06-12 19:50:43 +02:00
|
|
|
"""Available settings for itch-dl. Make sure all of them
|
|
|
|
have default values, as the config file may not exist."""
|
2024-03-17 01:17:19 +01:00
|
|
|
|
2025-02-14 14:55:54 +01:00
|
|
|
api_key: str | None = None
|
2022-06-12 19:50:43 +02:00
|
|
|
user_agent: str = f"python-requests/{requests.__version__} itch-dl/{__version__}"
|
|
|
|
|
2025-02-14 14:55:54 +01:00
|
|
|
download_to: str | None = None
|
2025-01-31 22:35:37 +01:00
|
|
|
mirror_web: bool = False
|
|
|
|
urls_only: bool = False
|
|
|
|
parallel: int = 1
|
|
|
|
|
2025-02-14 14:55:54 +01:00
|
|
|
filter_files_glob: str | None = None
|
|
|
|
filter_files_regex: str | None = None
|
2025-01-27 12:13:47 +01:00
|
|
|
|
2025-01-31 22:35:37 +01:00
|
|
|
verbose: bool = False
|
|
|
|
|
2022-06-12 19:50:43 +02:00
|
|
|
|
|
|
|
def create_and_get_config_path() -> str:
|
|
|
|
"""Returns the configuration directory in the appropriate
|
|
|
|
location for the current OS. The directory may not exist."""
|
|
|
|
system = platform.system()
|
|
|
|
if system == "Linux":
|
2024-03-17 01:17:19 +01:00
|
|
|
base_path = os.environ.get("XDG_CONFIG_HOME") or os.path.expanduser("~/.config/")
|
2022-06-12 19:50:43 +02:00
|
|
|
elif system == "Darwin":
|
2024-03-17 01:17:19 +01:00
|
|
|
base_path = os.path.expanduser("~/Library/Application Support/")
|
2022-06-12 19:50:43 +02:00
|
|
|
elif system == "Windows":
|
2024-03-17 01:17:19 +01:00
|
|
|
base_path = os.environ.get("APPDATA") or os.path.expanduser("~/AppData/Roaming/")
|
2022-06-12 19:50:43 +02:00
|
|
|
else:
|
|
|
|
raise NotImplementedError(f"Unknown platform: {system}")
|
|
|
|
|
|
|
|
return os.path.join(base_path, "itch-dl")
|
|
|
|
|
|
|
|
|
2025-02-14 15:41:14 +01:00
|
|
|
def clean_config(config_data: dict[str, Any]) -> dict[str, Any]:
|
|
|
|
cleaned_config = {}
|
|
|
|
settings_invalid = False
|
|
|
|
type_hints = get_type_hints(Settings)
|
|
|
|
|
|
|
|
# Complain about invalid types, if any:
|
|
|
|
for key, value in config_data.items():
|
|
|
|
if not (expected_type := type_hints.get(key)):
|
|
|
|
logging.warning("Settings contain an unknown item, ignoring: '%s'", key)
|
|
|
|
continue
|
|
|
|
|
|
|
|
if not isinstance(value, expected_type):
|
|
|
|
logging.fatal("Settings.%s has invalid type '%s', expected '%s'", key, type(value), expected_type)
|
|
|
|
|
|
|
|
# Keep iterating to look up all the bad keys:
|
|
|
|
settings_invalid = True
|
|
|
|
continue
|
|
|
|
|
|
|
|
cleaned_config[key] = value
|
|
|
|
|
|
|
|
if settings_invalid:
|
|
|
|
logging.fatal("Settings invalid, bailing out!")
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
return cleaned_config
|
|
|
|
|
|
|
|
|
2025-02-14 14:55:54 +01:00
|
|
|
def load_config(args: argparse.Namespace, profile: str | None = None) -> Settings:
|
2022-06-12 19:50:43 +02:00
|
|
|
"""Loads the configuration from the file system if it exists,
|
|
|
|
the returns a Settings object."""
|
|
|
|
config_path = create_and_get_config_path()
|
|
|
|
config_file_path = os.path.join(config_path, "config.json")
|
|
|
|
profile_file_path = os.path.join(config_path, "profiles", profile or "")
|
|
|
|
|
|
|
|
if os.path.isfile(config_file_path):
|
2025-01-31 23:40:40 +01:00
|
|
|
logging.debug("Found config file: %s", config_file_path)
|
2022-06-12 19:50:43 +02:00
|
|
|
with open(config_file_path) as f:
|
|
|
|
config_data = json.load(f)
|
|
|
|
else:
|
|
|
|
config_data = {}
|
|
|
|
|
|
|
|
if os.path.isfile(profile_file_path):
|
2025-01-31 23:40:40 +01:00
|
|
|
logging.debug("Found profile: %s", profile_file_path)
|
2022-06-12 19:50:43 +02:00
|
|
|
with open(config_file_path) as f:
|
|
|
|
profile_data = json.load(f)
|
|
|
|
|
|
|
|
config_data.update(profile_data)
|
|
|
|
|
2025-01-31 22:35:37 +01:00
|
|
|
# All settings from the base file:
|
2025-02-14 15:41:14 +01:00
|
|
|
settings = Settings(**clean_config(config_data))
|
2025-01-31 22:35:37 +01:00
|
|
|
|
2025-02-14 15:41:14 +01:00
|
|
|
# Apply overrides from CLI args on each field in Settings:
|
|
|
|
for field in fields(Settings):
|
|
|
|
key = field.name
|
|
|
|
if value := getattr(args, key):
|
2025-01-31 22:35:37 +01:00
|
|
|
setattr(settings, key, value)
|
|
|
|
|
|
|
|
return settings
|