add file format checker for parsing

This commit is contained in:
2026-04-03 15:00:08 +02:00
parent 5022cfe020
commit 6f503bdd36
+16
View File
@@ -1,3 +1,4 @@
from os.path import split
from mazegen import DepthFirstSearch, Kruskal from mazegen import DepthFirstSearch, Kruskal
from mazegen import AStar, DepthFirstSearchSolver from mazegen import AStar, DepthFirstSearchSolver
from typing import Any from typing import Any
@@ -183,6 +184,20 @@ class DataMaze:
return True return True
return False return False
@staticmethod
def test_file_format(file: str) -> None:
with open(file) as data_str:
for line in data_str:
if len(line.split("=", 1)) != 2:
raise Exception(
"config file format not respected. excpected format : "
"KEY=VALUE"
)
if not line.split("=", 1)[1] or line.split("=", 1)[1] == "\n":
raise Exception(
f"VALUE not provide for {line.split('=')[0]} key"
)
@staticmethod @staticmethod
def get_data_maze(name_file: str) -> dict[str, Any]: def get_data_maze(name_file: str) -> dict[str, Any]:
"""Load, validate, and convert maze configuration data from a file. """Load, validate, and convert maze configuration data from a file.
@@ -194,6 +209,7 @@ class DataMaze:
A dictionary of validated configuration values with lowercase keys. A dictionary of validated configuration values with lowercase keys.
""" """
try: try:
DataMaze.test_file_format(name_file)
data_str = DataMaze.get_file_data(name_file) data_str = DataMaze.get_file_data(name_file)
data_dict = DataMaze.transform_data(data_str) data_dict = DataMaze.transform_data(data_str)
DataMaze.verif_key_data(data_dict) DataMaze.verif_key_data(data_dict)