|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import argparse |
| 4 | + |
| 5 | +from _common import print_json, run_sync_demo, step |
| 6 | +from pyritone import BridgeError |
| 7 | + |
| 8 | + |
| 9 | +MODES: dict[str, dict[str, bool]] = { |
| 10 | + "builder": { |
| 11 | + "allowPlace": True, |
| 12 | + "allowSprint": True, |
| 13 | + }, |
| 14 | + "careful": { |
| 15 | + "allowPlace": False, |
| 16 | + "allowSprint": False, |
| 17 | + }, |
| 18 | +} |
| 19 | + |
| 20 | + |
| 21 | +parser = argparse.ArgumentParser(description="Show settings API and mode presets") |
| 22 | +parser.add_argument("--mode", choices=sorted(MODES.keys()), default="builder") |
| 23 | +args = parser.parse_args() |
| 24 | + |
| 25 | + |
| 26 | +def _apply_mode(client, mode_name: str) -> None: |
| 27 | + mode = MODES[mode_name] |
| 28 | + step(f"Applying mode '{mode_name}' with {len(mode)} settings") |
| 29 | + |
| 30 | + for setting_name, value in mode.items(): |
| 31 | + try: |
| 32 | + dispatch = client.settings.set(setting_name, value) |
| 33 | + print_json(f"set {setting_name} {value}", dispatch) |
| 34 | + except BridgeError as error: |
| 35 | + print( |
| 36 | + f"[warn] Could not set {setting_name!r}: {error.code} - {error.message}. " |
| 37 | + "Continuing with remaining settings." |
| 38 | + ) |
| 39 | + |
| 40 | + |
| 41 | +def demo(client): |
| 42 | + step("Property assignment style (sync): client.settings.allowPlace = True") |
| 43 | + client.settings.allowPlace = True |
| 44 | + if client.settings.last_dispatch is not None: |
| 45 | + print_json("last_dispatch", client.settings.last_dispatch) |
| 46 | + |
| 47 | + step("Handle methods: get/toggle/reset") |
| 48 | + print_json("allowPlace.get()", client.settings.allowPlace.get()) |
| 49 | + print_json("allowPlace.toggle()", client.settings.allowPlace.toggle()) |
| 50 | + print_json("allowPlace.reset()", client.settings.allowPlace.reset()) |
| 51 | + |
| 52 | + _apply_mode(client, args.mode) |
| 53 | + |
| 54 | + step("Final checks") |
| 55 | + print_json("allowPlace.get()", client.settings.allowPlace.get()) |
| 56 | + print_json("allowSprint.get()", client.settings.allowSprint.get()) |
| 57 | + return 0 |
| 58 | + |
| 59 | + |
| 60 | +if __name__ == "__main__": |
| 61 | + raise SystemExit(run_sync_demo("06 - Settings Mode Switch", demo)) |
0 commit comments