Archived
load cards from json
This commit is contained in:
@@ -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
|
||||
Reference in New Issue
Block a user