25 literal names covered 34 modules. plex/client.py, plex/stream.py, plex/subtitles.py, plex/sync.py and two Plex route modules all called themselves `siftlode.plex`; downloads/pipeline.py called itself `siftlode.worker`; routes/channels.py called itself `siftlode.api`. The `logger` column is in every stored row so E22.6's console can filter on it, and so an operator can pin a level to one part of the system — neither works when six modules share a name. `logs.get_logger(__name__)` derives it: app/plex/sync.py -> siftlode.plex.sync. One name per module, and it cannot drift from the file it sits in. tests/test_logger_names.py pins the derivation, that everything stays under the one `siftlode` tree the handler and the tests attach to, and that no module hand-rolls a name again unless the line says why (`# logger-name-ok: <why>`). Two do: get_logger's own call, and `siftlode.http`, which is named for the traffic it carries rather than the module it lives in — it replaced uvicorn's access log and is what an operator pins when they want request lines and nothing else. app/scheduler.py called its logger `logger`; every other module calls it `log`. ruff gains LOG, G, T20, TRY400, TRY401 and S112. Four findings cleared with them: worker.py logged `str(exc)[:120]` where the traceback was the whole question, and three prints/f-strings that are deliberate now say so on the line. S110 and BLE001 stay off until S3 — 23 handlers are outstanding and each is a judgement about one of them; enabling the rules first would just mean 23 blanket noqas.
91 lines
2.7 KiB
Python
91 lines
2.7 KiB
Python
"""Global admin-controlled app state (e.g. pausing background sync, first-run setup)."""
|
|
|
|
import secrets
|
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app import logs
|
|
from app.models import AppState
|
|
from app.security import hash_token
|
|
|
|
log = logs.get_logger(__name__)
|
|
# Once configuration completes in this process it never reverts, so cache it to skip the per-request
|
|
# DB hit in the setup-mode gate (see main.setup_gate). Starts False; refreshed from the DB.
|
|
_configured_cache = False
|
|
|
|
|
|
def _row(db: Session) -> AppState:
|
|
row = db.get(AppState, 1)
|
|
if row is None:
|
|
row = AppState(id=1, sync_paused=False)
|
|
db.add(row)
|
|
db.commit()
|
|
return row
|
|
|
|
|
|
def is_sync_paused(db: Session) -> bool:
|
|
return _row(db).sync_paused
|
|
|
|
|
|
def set_sync_paused(db: Session, paused: bool) -> None:
|
|
# Flushes, doesn't commit — the caller owns the transaction so the change lands with its audit
|
|
# row in one transaction (B-core-10).
|
|
row = _row(db)
|
|
row.sync_paused = paused
|
|
db.add(row)
|
|
db.flush()
|
|
log.info("Background sync %s", "paused" if paused else "resumed")
|
|
|
|
|
|
# --- First-run install wizard ------------------------------------------------------------
|
|
|
|
|
|
def is_configured(db: Session) -> bool:
|
|
return _row(db).configured
|
|
|
|
|
|
def setup_complete() -> bool:
|
|
"""Fast, DB-free check for the request gate: True once configuration finished this process."""
|
|
return _configured_cache
|
|
|
|
|
|
def is_configured_cached(db: Session) -> bool:
|
|
"""Configured check that promotes the module cache once true (configuration is one-way)."""
|
|
global _configured_cache
|
|
if _configured_cache:
|
|
return True
|
|
if is_configured(db):
|
|
_configured_cache = True
|
|
return _configured_cache
|
|
|
|
|
|
def mark_configured(db: Session) -> None:
|
|
"""Finish setup: flip the instance to configured and invalidate the setup token."""
|
|
global _configured_cache
|
|
row = _row(db)
|
|
row.configured = True
|
|
row.setup_token_hash = None
|
|
db.add(row)
|
|
db.commit()
|
|
_configured_cache = True
|
|
log.info("Instance marked configured; setup wizard disabled.")
|
|
|
|
|
|
def rotate_setup_token(db: Session) -> str:
|
|
"""Generate a fresh one-time setup token, persist only its hash, and return the raw token
|
|
(logged at startup so the admin can open the wizard)."""
|
|
raw = secrets.token_urlsafe(24)
|
|
row = _row(db)
|
|
row.setup_token_hash = hash_token(raw)
|
|
db.add(row)
|
|
db.commit()
|
|
return raw
|
|
|
|
|
|
def check_setup_token(db: Session, raw: str | None) -> bool:
|
|
"""Whether `raw` matches the stored setup-token hash (constant-time)."""
|
|
if not raw:
|
|
return False
|
|
stored = _row(db).setup_token_hash
|
|
return stored is not None and secrets.compare_digest(stored, hash_token(raw))
|