Fix mypy issues in faction code.

This commit is contained in:
Dan Albert
2020-10-25 16:18:03 -07:00
parent ec7f8f5710
commit b405c3ab32
3 changed files with 78 additions and 37 deletions

View File

@@ -2,30 +2,27 @@ from __future__ import annotations
import json
import logging
from pathlib import Path
from typing import Type
from typing import Dict, Type
from game.factions.faction import Faction
FACTION_DIRECTORY = "./resources/factions/"
FACTION_DIRECTORY = Path("./resources/factions/")
class FactionLoader:
@classmethod
def load_factions(cls: Type[FactionLoader]) -> {str, Faction}:
path = Path(FACTION_DIRECTORY)
files = [f for f in path.glob("*.json") if f.is_file()]
def load_factions(cls: Type[FactionLoader]) -> Dict[str, Faction]:
files = [f for f in FACTION_DIRECTORY.glob("*.json") if f.is_file()]
factions = {}
for f in files:
logging.info("Loading faction" + str(f))
try:
with open(f, "r", encoding="utf-8") as fdata:
with f.open("r", encoding="utf-8") as fdata:
data = json.load(fdata, encoding="utf-8")
factions[data["name"]] = Faction.from_json(data)
logging.info("Loaded faction : " + str(f))
except Exception as e:
logging.error("Unable to load faction : " + path, e)
except Exception:
logging.exception(f"Unable to load faction : {f}")
return factions