This commit is contained in:
Auric Vente 2024-07-25 21:23:38 -06:00
parent 788c5c698f
commit c79c61f79b
3 changed files with 36 additions and 8 deletions

View File

@ -10,6 +10,7 @@ from .argspec import ArgSpec
class Args: class Args:
names: Path | None = None names: Path | None = None
ants: Path | None = None
images: bool = True images: bool = True
header: bool = True header: bool = True
footer: bool = True footer: bool = True
@ -51,6 +52,7 @@ class Args:
paths = [ paths = [
"names", "names",
"ants",
] ]
for p_item in paths: for p_item in paths:

View File

@ -73,7 +73,13 @@ class ArgSpec:
ArgSpec.add_argument( ArgSpec.add_argument(
"names", "names",
type=str, type=str,
info="Path to a JSON file with a list of names. The game will use these names instead of the default ones", info="Path to a JSON file with a list of names. Use these instead of the default ones",
)
ArgSpec.add_argument(
"ants",
type=str,
info="Path to a JSON file with ants data. Use this instead of the default one",
) )
ArgSpec.add_argument( ArgSpec.add_argument(

View File

@ -2,6 +2,7 @@ from __future__ import annotations
import json import json
from typing import TYPE_CHECKING, Any from typing import TYPE_CHECKING, Any
from pathlib import Path
from .config import Config from .config import Config
@ -13,10 +14,32 @@ from .utils import Utils
class Storage: class Storage:
@staticmethod
def get_names_path() -> Path:
path = Config.names_json
if Args.names:
if Args.names.exists():
path = Args.names
return path
@staticmethod
def get_ants_path() -> Path:
path = Config.ants_json
if Args.ants:
if Args.ants.exists():
path = Args.ants
return path
@staticmethod @staticmethod
def get_ants() -> Any: def get_ants() -> Any:
try: try:
with Config.ants_json.open() as file: path = Storage.get_ants_path()
with path.open() as file:
return json.load(file) return json.load(file)
except Exception as e: except Exception as e:
Utils.print(str(e)) Utils.print(str(e))
@ -25,17 +48,14 @@ class Storage:
@staticmethod @staticmethod
def save_ants(ants: list[Ant]) -> None: def save_ants(ants: list[Ant]) -> None:
objs = [ant.to_dict() for ant in ants] objs = [ant.to_dict() for ant in ants]
path = Storage.get_ants_path()
with Config.ants_json.open("w") as file: with path.open("w") as file:
json.dump(objs, file) json.dump(objs, file)
@staticmethod @staticmethod
def get_names() -> Any: def get_names() -> Any:
path = Config.names_json path = Storage.get_names_path()
if Args.names:
if Args.names.exists():
path = Args.names
with path.open() as file: with path.open() as file:
return json.load(file) return json.load(file)