load cards from json

This commit is contained in:
2024-03-29 13:59:57 +01:00
parent a0ab6a051f
commit cc7e2ad93e
10 changed files with 167 additions and 99 deletions
+26
View File
@@ -0,0 +1,26 @@
from json import JSONDecodeError
from pathlib import Path
from pydantic import BaseModel, ValidationError
from typing import List
from model.card_validator import CardParser
class CardContainerFile(BaseModel):
cards: List[CardParser]
def load_cards():
cards = []
for p in Path("./src/cards").glob("**/*.json"):
print("loading " + p.name)
with open(p, encoding="UTF-8") as f_in:
try:
data = CardContainerFile.model_validate_json(f_in.read())
for c in data.cards:
cards.append(c)
except JSONDecodeError as e:
print("Not a valid JSON file : " + p.name + " : " + str(e))
except ValidationError as e:
print("Incorrect or missing JSON data : " + p.name + " : " + str(e))
return cards