31 lines
1.1 KiB
Python
31 lines
1.1 KiB
Python
"""Load settings.yaml. One place for every tunable value."""
|
|
import os
|
|
import yaml
|
|
|
|
PIPELINE_DIR = os.path.dirname(os.path.abspath(__file__))
|
|
REPO_ROOT = os.path.dirname(PIPELINE_DIR)
|
|
|
|
|
|
def load(path=None):
|
|
path = path or os.environ.get("AUDIOBOOK_SETTINGS") or os.path.join(PIPELINE_DIR, "settings.yaml")
|
|
with open(path, encoding="utf-8") as f:
|
|
cfg = yaml.safe_load(f)
|
|
cfg["_path"] = path
|
|
return cfg
|
|
|
|
|
|
def resolve(cfg, key):
|
|
"""A path setting as an absolute path (relative values are relative to the repo root)."""
|
|
value = cfg[key]
|
|
return value if os.path.isabs(value) else os.path.join(REPO_ROOT, value)
|
|
|
|
|
|
def audio_dir(cfg, book_dir):
|
|
"""Where audio for a book goes: the book folder by default, else under archive_dir."""
|
|
if not cfg.get("archive_dir"):
|
|
return os.path.join(book_dir, "audio")
|
|
base = cfg["archive_dir"]
|
|
base = base if os.path.isabs(base) else os.path.join(REPO_ROOT, base)
|
|
parts = os.path.normpath(book_dir).split(os.sep)[-2:] # <author>/<book>
|
|
return os.path.join(base, *parts, "audio")
|