okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
55a2bcd47
main
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 : 17p = 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`)" ) 18s_sp = p .add_subparsers (dest = "split_engine_command" ,required = True ) 19one = 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" ) 20one .add_argument ("--config" ,required = True ,metavar = "FILE" ,help = "the run config the app is about to start" ) 21one .add_argument ("--out" ,metavar = "FILE" ,help = "where the split engine's run config goes when the answer is yes" ) 22one .add_argument ("--output" ,choices = ("human" ,"json" ),default = "human" ) 23 24 25def run (a )-> int : 26machine = a .output == "json" 27 28def fail (message :str )-> int : 29if machine : 30json .dumps ({"schema_version" :1 ,"ok" :False ,"error" : {"code" :"split_engine_failed" ,"message" :message }},ensure_ascii = False )) 31else : 32f"strata: { message } " ,file = sys .stderr ) 33return 5 34 35try : 36cfg = json .loads (Path (a .config ).read_text (encoding = "utf-8-sig" )) 37if not isinstance (cfg ,dict ): 38raise ValueError ("not an object" ) 39except (OSError ,ValueError )as exc : 40return fail (f"The run config could not be read ( { type ( exc ). __name__ } )." ) 41from strata_control import split_engine as SE 42work = json .loads (json .dumps (cfg ))# a copy: apply_run_config rewrites it in place 43decision ,line ,warn = SE .apply_run_config (work ,allow_untested = os .environ .get ("STRATA_SPLIT_ALLOW_UNTESTED" )== "1" ) 44wrote = None 45if decision .use_split and a .out : 46out = Path (a .out ) 47tmp = out .with_name (out .name + ".tmp" ) 48fd = os .open (tmp ,os .O_WRONLY | os .O_CREAT | os .O_TRUNC ,0o600 ) 49with os .fdopen (fd ,"w" ,encoding = "utf-8" )as f : 50json .dump (work ,f ,indent = 2 ) 51f .write ("\n" ) 52os .replace (tmp ,out ) 53wrote = str (out ) 54if machine : 55json .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 )) 57else : 58line or ("Using the normal layer split." if not decision .use_split else decision .line )) 59return 0