black format
This commit is contained in:
parent
5d9ddf60b7
commit
363cfdd692
1 changed files with 16 additions and 9 deletions
21
recipes.py
21
recipes.py
|
@ -16,11 +16,13 @@ ISSUE_UNKNOWN_INGREDIENT = "unknown-ingredient"
|
||||||
ISSUE_DUPLICATE_UNITS = "duplicate-units"
|
ISSUE_DUPLICATE_UNITS = "duplicate-units"
|
||||||
ISSUE_KNOWN_PRICE_UNKNOWN_CONVERSION = "known-price-unknown-conversion"
|
ISSUE_KNOWN_PRICE_UNKNOWN_CONVERSION = "known-price-unknown-conversion"
|
||||||
|
|
||||||
|
|
||||||
class Issue:
|
class Issue:
|
||||||
def __init__(self, id: str, msg: str) -> None:
|
def __init__(self, id: str, msg: str) -> None:
|
||||||
self.id = id
|
self.id = id
|
||||||
self.msg = msg
|
self.msg = msg
|
||||||
|
|
||||||
|
|
||||||
class Issues:
|
class Issues:
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
self.errors: List[Issue] = []
|
self.errors: List[Issue] = []
|
||||||
|
@ -44,6 +46,7 @@ class Issues:
|
||||||
self.warnings.clear()
|
self.warnings.clear()
|
||||||
return retcode
|
return retcode
|
||||||
|
|
||||||
|
|
||||||
class Context:
|
class Context:
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
self.units = Units(self)
|
self.units = Units(self)
|
||||||
|
@ -142,7 +145,10 @@ class Units:
|
||||||
unitnames.append(unit["name"])
|
unitnames.append(unit["name"])
|
||||||
for unitname, num in collections.Counter(unitnames).items():
|
for unitname, num in collections.Counter(unitnames).items():
|
||||||
if num > 1:
|
if num > 1:
|
||||||
self.ctx.issues.error(ISSUE_DUPLICATE_UNITS, f"units.yaml: {unitname} should only have one entry, found {num}")
|
self.ctx.issues.error(
|
||||||
|
ISSUE_DUPLICATE_UNITS,
|
||||||
|
f"units.yaml: {unitname} should only have one entry, found {num}",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class Ingredient(Element):
|
class Ingredient(Element):
|
||||||
|
@ -179,9 +185,7 @@ class Ingredient(Element):
|
||||||
return result
|
return result
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def convert(
|
def convert(self, amount: float, unitfrom: Unit, unitto: Unit) -> Optional[float]:
|
||||||
self, amount: float, unitfrom: Unit, unitto: Unit
|
|
||||||
) -> Optional[float]:
|
|
||||||
conversions: Dict[str, Dict[str, float]] = defaultdict(dict)
|
conversions: Dict[str, Dict[str, float]] = defaultdict(dict)
|
||||||
# construct node tree
|
# construct node tree
|
||||||
convs = self["conversions"]
|
convs = self["conversions"]
|
||||||
|
@ -196,8 +200,9 @@ class Ingredient(Element):
|
||||||
# find path between conversions
|
# find path between conversions
|
||||||
path = self.dfs(conversions, unitfrom["name"], unitto["name"])
|
path = self.dfs(conversions, unitfrom["name"], unitto["name"])
|
||||||
if path is None:
|
if path is None:
|
||||||
self.ctx.issues.warn(ISSUE_KNOWN_PRICE_UNKNOWN_CONVERSION,
|
self.ctx.issues.warn(
|
||||||
f'{self["name"]} has a known price, but conversion {unitfrom["name"]} -> {unitto["name"]} not known'
|
ISSUE_KNOWN_PRICE_UNKNOWN_CONVERSION,
|
||||||
|
f'{self["name"]} has a known price, but conversion {unitfrom["name"]} -> {unitto["name"]} not known',
|
||||||
)
|
)
|
||||||
return None
|
return None
|
||||||
assert len(path) != 0
|
assert len(path) != 0
|
||||||
|
@ -278,7 +283,9 @@ class IngredientInstance(Element):
|
||||||
def load(self, dct: Dict[str, Any]) -> None:
|
def load(self, dct: Dict[str, Any]) -> None:
|
||||||
ingredient = self.ctx.ingredients.get(dct["name"])
|
ingredient = self.ctx.ingredients.get(dct["name"])
|
||||||
if ingredient is None:
|
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
|
self["ingredient"] = ingredient
|
||||||
|
|
||||||
if "amount" not in dct:
|
if "amount" not in dct:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue