From 0ea099ba348a5fb88d9749f34b654d9774d2b092 Mon Sep 17 00:00:00 2001 From: Axel Gilliard--Marthey Date: Tue, 27 Oct 2026 14:27:59 +0100 Subject: [PATCH] Better config arg handling --- .gitignore | 3 +-- Makefile | 4 ++-- README.md | 2 +- src/pacman/game/__init__.py | 22 ++++++++++++++++++---- 4 files changed, 22 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index df4adea..1ce533d 100644 --- a/.gitignore +++ b/.gitignore @@ -2,5 +2,4 @@ .venv **/__pycache__ .ruff_cache -/pacman -/*.tgz +/pacman* diff --git a/Makefile b/Makefile index cda17ce..d0f1127 100644 --- a/Makefile +++ b/Makefile @@ -37,5 +37,5 @@ check: build: uv pip compile pyproject.toml -o requirements.txt - uv run shiv -o pacman -e pacman:main -r requirements.txt . - tar czf "$$(uv version).tgz" pacman config.json + uv run shiv -o "$$(uv version)" -e pacman:main -r requirements.txt . + tar czf "$$(uv version).tgz" "$$(uv version)" config.json diff --git a/README.md b/README.md index aec2b0d..a07be87 100644 --- a/README.md +++ b/README.md @@ -100,4 +100,4 @@ The whole is packaged as an executable for distribution using `shiv`. ![Project Management](./Project%20Management.png) -![Project Status](./Project%20Status.png) \ No newline at end of file +![Project Status](./Project%20Status.png) diff --git a/src/pacman/game/__init__.py b/src/pacman/game/__init__.py index f81c7fd..46a383a 100644 --- a/src/pacman/game/__init__.py +++ b/src/pacman/game/__init__.py @@ -5,6 +5,7 @@ import sys from collections.abc import Callable from dataclasses import dataclass from enum import Enum, auto +from pathlib import Path from typing import cast, override from mazegenerator import ( # type: ignore[import-untyped, unused-ignore] @@ -291,13 +292,26 @@ def remove_comments(s: str) -> str: def load_config(world: World) -> None: """Load the configuration json.""" - if len(sys.argv) != 2: + if len(sys.argv) > 2: raise Exception( - f"Expected exactly 1 argument, got {len(sys.argv) - 1}" + f"Expected at most 1 argument, the config, got {len(sys.argv) - 1}" ) + s = Config().model_dump_json() + if len(sys.argv) == 1: + for path in [ + str(Path(sys.argv[0]).parent) + "/config.json", + "./config.json", + ]: + try: + with open(path) as f: + s = f.read() + except Exception: + continue + else: + with open(sys.argv[1]) as f: + s = f.read() - with open(sys.argv[1]) as f: - config = Config.model_validate_json(remove_comments(f.read())) + config = Config.model_validate_json(remove_comments(s)) world.spawn_resource(config) -- 2.53.0