okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-0211-nvdual into f-0211-int55a2bcd47

main
3.0 KiB59 linesraw
1"""``strata split-engine apply``: the all-cards split engine for a run config the app wrote (the Terminal does the same inside ``strata launch``).
2
3The desktop app builds its run config itself and starts it as a saved profile, so the step that turns a layer-split run config into the split engine's
4(strata_control/split_engine.py ``apply_run_config``) is asked of Control here, with the rule, the engine package and the draft layer checked on this computer.
5``--config`` is read and never changed; ``--out`` receives the split engine's run config only when the answer is yes. Nothing is downloaded.
6"""
7
8from __future__ import annotations
9
10import json
11import os
12import sys
13from pathlib import Path
14
15
16def add_parser(sp) -> None:
17    p = sp.add_parser("split-engine", help="the all-cards split engine for a run config (the app's start asks it; the Terminal does the same in `strata launch`)")
18    s_sp = p.add_subparsers(dest="split_engine_command", required=True)
19    one = s_sp.add_parser("apply", help="write the split engine's run config for this one when the rule says yes; the config itself is not changed")
20    one.add_argument("--config", required=True, metavar="FILE", help="the run config the app is about to start")
21    one.add_argument("--out", metavar="FILE", help="where the split engine's run config goes when the answer is yes")
22    one.add_argument("--output", choices=("human", "json"), default="human")
23
24
25def run(a) -> int:
26    machine = a.output == "json"
27
28    def fail(message: str) -> int:
29        if machine:
30            print(json.dumps({"schema_version": 1, "ok": False, "error": {"code": "split_engine_failed", "message": message}}, ensure_ascii=False))
31        else:
32            print(f"strata: {message}", file=sys.stderr)
33        return 5
34
35    try:
36        cfg = json.loads(Path(a.config).read_text(encoding="utf-8-sig"))
37        if not isinstance(cfg, dict):
38            raise ValueError("not an object")
39    except (OSError, ValueError) as exc:
40        return fail(f"The run config could not be read ({type(exc).__name__}).")
41    from strata_control import split_engine as SE
42    work = json.loads(json.dumps(cfg))                       # a copy: apply_run_config rewrites it in place
43    decision, line, warn = SE.apply_run_config(work, allow_untested=os.environ.get("STRATA_SPLIT_ALLOW_UNTESTED") == "1")
44    wrote = None
45    if decision.use_split and a.out:
46        out = Path(a.out)
47        tmp = out.with_name(out.name + ".tmp")
48        fd = os.open(tmp, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600)
49        with os.fdopen(fd, "w", encoding="utf-8") as f:
50            json.dump(work, f, indent=2)
51            f.write("\n")
52        os.replace(tmp, out)
53        wrote = str(out)
54    if machine:
55        print(json.dumps({"schema_version": 1, "ok": True, "split": bool(decision.use_split), "line": line, "warn": bool(warn), "reasons": list(decision.reasons),
56                          "cards": decision.cards, "written": wrote}, ensure_ascii=False))
57    else:
58        print(line or ("Using the normal layer split." if not decision.use_split else decision.line))
59    return 0