-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrun_slice05_contract.py
More file actions
57 lines (40 loc) · 1.56 KB
/
run_slice05_contract.py
File metadata and controls
57 lines (40 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from __future__ import annotations
import json
import sys
from pathlib import Path
from Implementations.Reference.common import BackendContract
from Implementations.Reference.Runtime.reference_runtime import create_runtime_for_family
DEFAULT_CONTRACT_PATH = (
Path(__file__).resolve().parents[1]
/ "ContractEmitter"
/ "examples"
/ "05_bounded_ui_accumulator.reference_host_runtime_ui_binding.contract.json"
)
def _parse_control_value(argv: list[str]) -> int:
if len(argv) >= 2:
try:
value = int(argv[1])
except ValueError as exc:
raise SystemExit("control_value must be an integer.") from exc
else:
value = 3
if not (0 <= value <= 65535):
raise SystemExit("control_value must remain within the u16 domain.")
return value
def _parse_contract_path(argv: list[str]) -> Path:
if len(argv) >= 3:
return Path(argv[2]).resolve()
return DEFAULT_CONTRACT_PATH
def main(argv: list[str]) -> int:
control_value = _parse_control_value(argv)
contract_path = _parse_contract_path(argv)
if not contract_path.exists():
raise SystemExit(f"Contract file not found: {contract_path}")
artifact = json.loads(contract_path.read_text(encoding="utf-8"))
contract = BackendContract(artifact=artifact)
runtime = create_runtime_for_family("reference_host_runtime_ui_binding")
result = runtime.execute(contract, {"input_value": control_value})
print(json.dumps(result.artifact, indent=2))
return 0
if __name__ == "__main__":
raise SystemExit(main(sys.argv))