mirror of
https://github.com/nathom/streamrip.git
synced 2025-05-13 06:34:45 -04:00
163 lines
4.9 KiB
Python
163 lines
4.9 KiB
Python
"""A config class that manages arguments between the config file and CLI."""
|
|
|
|
import copy
|
|
import logging
|
|
import os
|
|
import shutil
|
|
from pprint import pformat
|
|
from typing import Any, Dict
|
|
|
|
import tomlkit
|
|
from click import secho
|
|
|
|
from streamrip.exceptions import InvalidSourceError
|
|
|
|
from .constants import CONFIG_DIR, CONFIG_PATH, DOWNLOADS_DIR
|
|
|
|
logger = logging.getLogger("streamrip")
|
|
|
|
|
|
class Config:
|
|
"""Config class that handles command line args and config files.
|
|
|
|
Usage:
|
|
|
|
>>> config = Config('test_config.toml')
|
|
>>> config.defaults['qobuz']['quality']
|
|
3
|
|
|
|
If test_config was already initialized with values, this will load them
|
|
into `config`. Otherwise, a new config file is created with the default
|
|
values.
|
|
"""
|
|
|
|
default_config_path = os.path.join(os.path.dirname(__file__), "config.toml")
|
|
|
|
with open(default_config_path) as cfg:
|
|
defaults: Dict[str, Any] = tomlkit.parse(cfg.read().strip())
|
|
|
|
def __init__(self, path: str = None):
|
|
"""Create a Config object with state.
|
|
|
|
A TOML file is created at `path` if there is none.
|
|
|
|
:param path:
|
|
:type path: str
|
|
"""
|
|
# to access settings loaded from toml file
|
|
self.file: Dict[str, Any] = copy.deepcopy(self.defaults)
|
|
self.session: Dict[str, Any] = copy.deepcopy(self.defaults)
|
|
|
|
if path is None:
|
|
self._path = CONFIG_PATH
|
|
else:
|
|
self._path = path
|
|
|
|
if os.path.isfile(self._path):
|
|
self.load()
|
|
if self.file["misc"]["version"] != self.defaults["misc"]["version"]:
|
|
secho(
|
|
"Updating config file to new version. Some settings may be lost.",
|
|
fg="yellow",
|
|
)
|
|
self.update()
|
|
self.load()
|
|
else:
|
|
logger.debug("Creating toml config file at '%s'", self._path)
|
|
os.makedirs(os.path.dirname(self._path), exist_ok=True)
|
|
shutil.copy(self.default_config_path, self._path)
|
|
self.load()
|
|
self.file["downloads"]["folder"] = DOWNLOADS_DIR
|
|
|
|
def update(self):
|
|
"""Reset the config file except for credentials."""
|
|
self.reset()
|
|
# Save original credentials
|
|
qobuz_creds = self.file["qobuz"]
|
|
tidal_creds = self.file["tidal"]
|
|
|
|
# Reset and load config file
|
|
shutil.copy(self.default_config_path, self._path)
|
|
self.load()
|
|
|
|
# Set credentials and download directory, then save
|
|
self.file["qobuz"].update(qobuz_creds)
|
|
self.file["tidal"].update(tidal_creds)
|
|
self.file["downloads"]["folder"] = DOWNLOADS_DIR
|
|
self.save()
|
|
|
|
def save(self):
|
|
"""Save the config state to file."""
|
|
self.dump(self.file)
|
|
|
|
def reset(self):
|
|
"""Reset the config file."""
|
|
if not os.path.isdir(CONFIG_DIR):
|
|
os.makedirs(CONFIG_DIR, exist_ok=True)
|
|
|
|
shutil.copy(self.default_config_path, self._path)
|
|
self.load()
|
|
self.file["downloads"]["folder"] = DOWNLOADS_DIR
|
|
self.save()
|
|
|
|
def load(self):
|
|
"""Load infomation from the config files, making a deepcopy."""
|
|
with open(self._path) as cfg:
|
|
for k, v in tomlkit.loads(cfg.read().strip()).items():
|
|
self.file[k] = v
|
|
if hasattr(v, "copy"):
|
|
self.session[k] = v.copy()
|
|
else:
|
|
self.session[k] = v
|
|
|
|
logger.debug("Config loaded")
|
|
self.__loaded = True
|
|
|
|
def dump(self, info):
|
|
"""Given a state of the config, save it to the file.
|
|
|
|
:param info:
|
|
"""
|
|
with open(self._path, "w") as cfg:
|
|
logger.debug("Config saved: %s", self._path)
|
|
cfg.write(tomlkit.dumps(info))
|
|
|
|
@property
|
|
def tidal_creds(self):
|
|
"""Return a TidalClient compatible dict of credentials."""
|
|
creds = dict(self.file["tidal"])
|
|
logger.debug(creds)
|
|
del creds["quality"] # should not be included in creds
|
|
del creds["download_videos"]
|
|
return creds
|
|
|
|
@property
|
|
def qobuz_creds(self):
|
|
"""Return a QobuzClient compatible dict of credentials."""
|
|
return {
|
|
"email": self.file["qobuz"]["email"],
|
|
"pwd": self.file["qobuz"]["password"],
|
|
"app_id": self.file["qobuz"]["app_id"],
|
|
"secrets": self.file["qobuz"]["secrets"],
|
|
}
|
|
|
|
def creds(self, source: str):
|
|
"""Return a Client compatible dict of credentials.
|
|
|
|
:param source:
|
|
:type source: str
|
|
"""
|
|
if source == "qobuz":
|
|
return self.qobuz_creds
|
|
if source == "tidal":
|
|
return self.tidal_creds
|
|
if source == "deezer":
|
|
return {"arl": self.file["deezer"]["arl"]}
|
|
if source == "soundcloud":
|
|
return {}
|
|
|
|
raise InvalidSourceError(source)
|
|
|
|
def __repr__(self):
|
|
"""Return a string representation of the config."""
|
|
return f"Config({pformat(self.session)})"
|