diff --git a/recipes.py b/recipes.py index b2bb808..6994dd5 100644 --- a/recipes.py +++ b/recipes.py @@ -16,11 +16,13 @@ ISSUE_UNKNOWN_INGREDIENT = "unknown-ingredient" ISSUE_DUPLICATE_UNITS = "duplicate-units" ISSUE_KNOWN_PRICE_UNKNOWN_CONVERSION = "known-price-unknown-conversion" + class Issue: def __init__(self, id: str, msg: str) -> None: self.id = id self.msg = msg + class Issues: def __init__(self) -> None: self.errors: List[Issue] = [] @@ -44,6 +46,7 @@ class Issues: self.warnings.clear() return retcode + class Context: def __init__(self) -> None: self.units = Units(self) @@ -141,8 +144,11 @@ class Units: for unit in self.units: unitnames.append(unit["name"]) for unitname, num in collections.Counter(unitnames).items(): - if num>1: - self.ctx.issues.error(ISSUE_DUPLICATE_UNITS, f"units.yaml: {unitname} should only have one entry, found {num}") + if num > 1: + self.ctx.issues.error( + ISSUE_DUPLICATE_UNITS, + f"units.yaml: {unitname} should only have one entry, found {num}", + ) class Ingredient(Element): @@ -174,14 +180,12 @@ class Ingredient(Element): for nextunit in conversions[startname].keys(): if nextunit in visited: continue - result = self.dfs(conversions, nextunit, endname, visited+[startname]) + result = self.dfs(conversions, nextunit, endname, visited + [startname]) if result is not None: return result return None - def convert( - self, amount: float, unitfrom: Unit, unitto: Unit - ) -> Optional[float]: + def convert(self, amount: float, unitfrom: Unit, unitto: Unit) -> Optional[float]: conversions: Dict[str, Dict[str, float]] = defaultdict(dict) # construct node tree convs = self["conversions"] @@ -196,8 +200,9 @@ class Ingredient(Element): # find path between conversions path = self.dfs(conversions, unitfrom["name"], unitto["name"]) if path is None: - self.ctx.issues.warn(ISSUE_KNOWN_PRICE_UNKNOWN_CONVERSION, - f'{self["name"]} has a known price, but conversion {unitfrom["name"]} -> {unitto["name"]} not known' + self.ctx.issues.warn( + ISSUE_KNOWN_PRICE_UNKNOWN_CONVERSION, + f'{self["name"]} has a known price, but conversion {unitfrom["name"]} -> {unitto["name"]} not known', ) return None assert len(path) != 0 @@ -278,7 +283,9 @@ class IngredientInstance(Element): def load(self, dct: Dict[str, Any]) -> None: ingredient = self.ctx.ingredients.get(dct["name"]) if ingredient is None: - self.ctx.issues.error(ISSUE_UNKNOWN_INGREDIENT, f"unknown ingredient {dct['name']}") + self.ctx.issues.error( + ISSUE_UNKNOWN_INGREDIENT, f"unknown ingredient {dct['name']}" + ) self["ingredient"] = ingredient if "amount" not in dct: