Files
spotify-cli/spotify_cli/config.py
T

47 lines
1.1 KiB
Python

import json
import os
from pathlib import Path
CONFIG_DIR = Path.home() / ".config" / "spotify-cli"
CONFIG_FILE = CONFIG_DIR / "config.json"
TOKEN_FILE = CONFIG_DIR / "token.json"
DEFAULT_CONFIG = {
"client_id": "",
"default_device": None,
"volume_step": 5,
}
def ensure_config_dir():
CONFIG_DIR.mkdir(parents=True, exist_ok=True)
def load_config():
ensure_config_dir()
if CONFIG_FILE.exists():
with open(CONFIG_FILE) as f:
user_cfg = json.load(f)
cfg = {**DEFAULT_CONFIG, **user_cfg}
return cfg
return DEFAULT_CONFIG.copy()
def save_config(cfg: dict):
ensure_config_dir()
with open(CONFIG_FILE, "w") as f:
json.dump(cfg, f, indent=2)
def load_token():
ensure_config_dir()
if TOKEN_FILE.exists():
with open(TOKEN_FILE) as f:
return json.load(f)
return None
def save_token(token: dict):
ensure_config_dir()
with open(TOKEN_FILE, "w") as f:
json.dump(token, f, indent=2)
def clear_token():
if TOKEN_FILE.exists():
TOKEN_FILE.unlink()