steam-backlog-enforcer/steam_installer/config.py
Krzysztof kuhy Rudnicki a76e726e36 Merge steam-game-installer into the repo
The installer ships as the steam_installer package beside
steam_backlog_enforcer, with its tests, docs and Proton lock. The
enforcer's fast install now starts the installer from this checkout.

Both appinfo.vdf readers share one key-value decoder
(steam_installer.appinfo_kv), and the duplicated test fixtures and
helpers are folded into conftest and _cli_helpers so the whole-tree
duplicate-code check is clean. check_requirements_sync treats sibling
packages as first-party.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WzxdWZCxnNWLtpVwFLVk9w
2026-11-22 20:25:39 +01:00

67 lines
2.2 KiB
Python

"""Remember the Steam username so every run doesn't ask for it again.
Only the username is ever persisted. Passwords and Steam Guard codes stay
where they belong: with depotdownloader's `-remember-password` login token
(prompted through the pty in `runlog.run_logged`). Writing them to a dotfile
would be a downgrade in security over what Steam's own tooling already does.
"""
from __future__ import annotations
import os
from pathlib import Path
from steam_installer import errors
CONFIG_DIR = Path.home() / ".config" / "steam-game-installer"
CONFIG_PATH = CONFIG_DIR / "config"
USERNAME_KEY = "username"
ENV_VAR = "STEAM_USERNAME"
class ConfigError(errors.InstallerError):
"""The saved config is unusable or a username is missing."""
def load_username() -> str | None:
"""The saved username, or None if nothing has been saved yet."""
if not CONFIG_PATH.is_file():
return None
for line in CONFIG_PATH.read_text().splitlines():
key, sep, value = line.partition("=")
if sep and key.strip() == USERNAME_KEY:
return value.strip() or None
return None
def save_username(username: str) -> None:
"""Persist the username at 0600, replacing any previous value."""
CONFIG_DIR.mkdir(parents=True, exist_ok=True)
CONFIG_PATH.write_text(f"{USERNAME_KEY}={username}\n")
# Not a secret, but it names an account; no reason for it to be world-readable.
CONFIG_PATH.chmod(0o600)
def resolve_username(cli_value: str | None, *, interactive: bool) -> str:
"""--username > $STEAM_USERNAME > saved config > prompt (and save the answer)."""
if cli_value:
return cli_value
env_value = os.environ.get(ENV_VAR, "").strip()
if env_value:
return env_value
saved = load_username()
if saved:
return saved
if not interactive:
msg = (
"No Steam username available. Pass --username, set "
f"${ENV_VAR}, or run once interactively to save one."
)
raise ConfigError(msg)
entered = input("Steam username: ").strip()
if not entered:
msg = "No Steam username entered."
raise ConfigError(msg)
save_username(entered)
return entered