Compare commits

...
Sign in to create a new pull request.

1 commit

5 changed files with 59 additions and 17 deletions

View file

@ -3,4 +3,4 @@ disable=
missing-module-docstring, missing-module-docstring,
missing-class-docstring, missing-class-docstring,
missing-function-docstring, missing-function-docstring,
too-few-public-methods too-few-public-methods

View file

@ -139,7 +139,8 @@ class Builder:
def collect_unitnames(rec: parsing.Recipe) -> List[str]: def collect_unitnames(rec: parsing.Recipe) -> List[str]:
results: List[str] = [] results: List[str] = []
for ing in rec.ingredients: for ing in rec.ingredients:
results.append(ing.unit.name) if ing.unit is not None:
results.append(ing.unit.name)
return results return results
unitnamelists = self.foreach_recipe(collect_unitnames) unitnamelists = self.foreach_recipe(collect_unitnames)

View file

@ -1,5 +1,6 @@
from abc import abstractmethod from abc import abstractmethod
import collections import collections
import re
from typing import Any, Dict, List, Optional, Self from typing import Any, Dict, List, Optional, Self
import comfyrecipes.settings as settings import comfyrecipes.settings as settings
@ -178,8 +179,8 @@ class IngredientInstance(Element):
self, self,
ctx: Context, ctx: Context,
name: str, name: str,
amount: float, amount: Optional[float],
unit: Unit, unit: Optional[Unit],
alternatives: List["IngredientInstance"], alternatives: List["IngredientInstance"],
note: str, note: str,
price: Optional["PriceDB"], price: Optional["PriceDB"],
@ -193,7 +194,37 @@ class IngredientInstance(Element):
self.price = price self.price = price
@classmethod @classmethod
def from_dict(cls, ctx: Context, dct: Dict[str, Any]) -> Self: def from_dict(cls, ctx: Context, dct: str | Dict[str, Any]) -> Self:
if isinstance(dct, str):
string = dct.strip()
p = re.compile(r"^(?:([0-9\.]+) ([a-zA-Z]+) )?([\w ]+)(?: \((.*)\))?$")
match = p.match(string)
if match is None:
raise RuntimeError(
"ingredient {string} regex not matched, it should be in the format [amount(num) unit(string, one word)] name(string, any number of words) [(note(string))]"
)
amount = float(match.group(1))
unitstr = match.group(2)
unit = ctx.default_unit
if unit is not None:
unitx = ctx.units.get(unitstr)
if unitx is None:
ctx.issues.error(issues.ISSUE_UNKNOWN_UNIT, "unknown unit {unitstr}")
else:
unit = unitx
name = match.group(3)
note = match.group(4)
if note is None:
note = ""
return cls(
ctx=ctx,
name=dct,
amount=amount,
unit=unit,
alternatives=[],
note=note,
price=None,
)
name = dct["name"] name = dct["name"]
ingredient = ctx.ingredients.get(name) ingredient = ctx.ingredients.get(name)

View file

@ -13,17 +13,22 @@
"ingredients": { "ingredients": {
"type": "array", "type": "array",
"items": { "items": {
"$id": "https://example.com/ingredient.json", "oneOf": [
"type": "object", { "type": "string" },
"additionalProperties": false, {
"required": [ "name" ], "$id": "https://example.com/ingredient.json",
"properties": { "type": "object",
"name": { "type": "string" }, "additionalProperties": false,
"amount": { "type": "number" }, "required": [ "name" ],
"unit": { "type": "string" }, "properties": {
"or": { "items": { "$ref": "/ingredient.json" } }, "name": { "type": "string" },
"note": { "type": "string" } "amount": { "type": "number" },
} "unit": { "type": "string" },
"or": { "items": { "$ref": "/ingredient.json" } },
"note": { "type": "string" }
}
}
]
} }
}, },
"steps": { "steps": {

View file

@ -1,6 +1,11 @@
{% extends "base.html" %} {% extends "base.html" %}
{% macro ingredientpart(ing) -%} {% macro ingredientpart(ing) -%}
{% if recipe.price != None %}{{price(ing.price)}} {%endif%}{{ing.amount|amountprint}} {{ing["unit"].name}} {{ ing.name }} {{ing.note}} {% if recipe.price != None %}{{price(ing.price)}} {%endif%}
{% if ing.amount != None %} {{ing.amount|amountprint}}
{% if ing.unit != None %} {{ing.unit.name}}{% endif %}
{%endif%}
{{ ing.name }}
{% if ing.note != "" %} {{ing.note}}{% endif %}
{%- endmacro %} {%- endmacro %}
{% macro ingredient(ing) -%} {% macro ingredient(ing) -%}