Skip to content

comicbox.config.online.build

Online (tagging) settings assembly.

Precedence for the online block is CLI > the confuse view, which already carries env-over-file layering. Only the CLI layer is hand-rolled here instead of going through confuse because:

  • Parse failures must raise typed ValueErrors with flag-specific messages (--match: unknown name ...); confuse templates would surface generic validation errors instead.
  • The --id/--series-id maps and the per-source auth and tuning shapes are keyed by dynamic source names, which confuse’s fixed MappingTemplates can’t express.

build_online_settings(online_block, runtime, *, cns=None)

Build the nested OnlineSettings from the confuse view + CLI overrides.

Source code in comicbox/config/online/build.py
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
def build_online_settings(
    online_block: Any,
    runtime: _RuntimeOnlineInputs,
    *,
    cns: Namespace | None = None,
) -> OnlineSettings:
    """Build the nested `OnlineSettings` from the confuse view + CLI overrides."""

    def _cli(field: str) -> Any:
        return getattr(cns, field, None) if cns is not None else None

    return OnlineSettings(
        lookup=_build_lookup(online_block, runtime, _cli),
        auth=_build_auth_settings(online_block, runtime),
        cache=_build_cache(online_block, runtime, _cli),
        tuning=_build_tuning(online_block, _cli),
    )

cns_for_overrides(args)

Locate the inner args.comicbox Namespace for CLI overrides, when present.

Source code in comicbox/config/online/build.py
493
494
495
496
497
498
def cns_for_overrides(args: Namespace | Mapping | None) -> Namespace | None:
    """Locate the inner `args.comicbox` Namespace for CLI overrides, when present."""
    if not isinstance(args, Namespace):
        return None
    candidate = getattr(args, "comicbox", args)
    return candidate if isinstance(candidate, Namespace) else args

parse_ttl(raw, *, default=_DEFAULT_CACHE_TTL, label='cache.ttl')

Parse a duration like ‘7d’, ‘24h’, ‘60m’ or ‘0’, or a number of seconds.

A number is accepted as YAML writes it, so ttl: 0 works as the warning below suggests. A missing or unparsable value falls back to default; the warning names the setting by label, so each caller’s lifetime stays its own.

Source code in comicbox/config/online/build.py
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
def parse_ttl(
    raw: str | float | None,
    *,
    default: timedelta = _DEFAULT_CACHE_TTL,
    label: str = "cache.ttl",
) -> timedelta:
    """
    Parse a duration like '7d', '24h', '60m' or '0', or a number of seconds.

    A number is accepted as YAML writes it, so ``ttl: 0`` works as the
    warning below suggests. A missing or unparsable value falls back to
    ``default``; the warning names the setting by ``label``, so each
    caller's lifetime stays its own.
    """
    if raw is None:
        return default
    if isinstance(raw, int | float) and not isinstance(raw, bool):
        return _ttl_from_seconds(raw, default, label)
    text = str(raw).strip().lower()
    if not text or text == "0":
        return timedelta(0)
    suffix = text[-1]
    try:
        if suffix in _TTL_UNIT_SECONDS:
            return timedelta(seconds=int(text[:-1]) * _TTL_UNIT_SECONDS[suffix])
        return timedelta(seconds=int(text))
    except ValueError:
        return _unparsable_ttl(text, default, label)

runtime_online_inputs(args)

Extract online-related runtime values from CLI args (Namespace only).

Source code in comicbox/config/online/build.py
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
def runtime_online_inputs(
    args: Namespace | Mapping | None,
) -> _RuntimeOnlineInputs:
    """Extract online-related runtime values from CLI args (Namespace only)."""
    if not isinstance(args, Namespace):
        return _RuntimeOnlineInputs()

    cns: Any = getattr(args, "comicbox", args)
    if not isinstance(cns, Namespace):
        cns = args

    ids = _parse_db_id_list(
        getattr(cns, "explicit_ids", None), "--id", _parse_explicit_id
    )
    series_ids = _parse_db_id_list(
        getattr(cns, "explicit_series_ids", None),
        "--series-id",
        _parse_explicit_series_id,
    )
    explicit_id_sources = tuple(dict.fromkeys((*ids, *series_ids)))
    runtime_enabled, selected = _resolve_runtime_sources(
        getattr(cns, "online_sources", None), explicit_id_sources
    )

    cli_overrides = CliOverrides.from_auth_list(getattr(cns, "auth", None) or ())
    cache_cli = getattr(cns, "cache", None)
    cache_mode_cli = (
        parse_enum(CacheMode, "--cache", str(cache_cli), noun="value")
        if cache_cli
        else None
    )

    return _RuntimeOnlineInputs(
        cli_overrides=cli_overrides,
        enabled=runtime_enabled,
        sources=selected,
        ids=ids,
        series_ids=series_ids,
        cache_mode_cli=cache_mode_cli,
    )