diff --git a/src/generate.py b/src/generate.py new file mode 100755 index 0000000..4d579d9 --- /dev/null +++ b/src/generate.py @@ -0,0 +1,29 @@ +#!/bin/python + +import os +import warnings +import yaml + +import recipes.ingredients +from recipes.recipe import Recipe + +base_dir = ".." + +# load ingredients +ingredients = {} +with open(base_dir + "/ingredients.yaml") as file: + ingredients = recipes.ingredients.load(file) + +# load recipes +recipes = {} +for filename in os.listdir(base_dir + "/recipes"): + split_filename = filename.split(".") + if split_filename[-1] != "yaml": + warnings.warn("bad file format: " + split_filename) + continue + + with open(base_dir + "/recipes/" + filename) as file: + recipe_name = split_filename[0] + recipe_yaml = yaml.load(file, Loader=yaml.BaseLoader) + print(recipe_yaml) + recipes[recipe_name] = Recipe(recipe_name, recipe_yaml, ingredients) diff --git a/src/recipes/__init__.py b/src/recipes/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/recipes/ingredients.py b/src/recipes/ingredients.py new file mode 100644 index 0000000..c05ef51 --- /dev/null +++ b/src/recipes/ingredients.py @@ -0,0 +1,49 @@ +#!/bin/false + +import yaml +import warnings + +from .measure import MEASURE_TYPES + + +class Ingredient: + def __init__(self, name, object): + # ingredient name in different languages + self.name = {"en": name} + if "translations" in object: + for language in object["translations"]: + self.name[language] = object["translations"][language] + + if "types" in object: + self.types = object["types"] + else: + self.types = {} + + # initialize measurement types + self.measure = [] + if "measure" in object: + # ensure we have a list of measure types + measures = [] + if isinstance(object["measure"], str): + measures = [object["measure"]] + elif isinstance(object["measure"], list): + measures = object["measure"] + else: + warnings.warn(name + ".measure should be string or list") + + # transform measure name into measure objects + for measure_name in measures: + if measure_name not in MEASURE_TYPES: + warnings.warn("Unknown measure type: " + measure_name) + else: + self.measure.append(MEASURE_TYPES[measure_name]) + + +def load(stream): + ingredients = yaml.load(stream, Loader=yaml.BaseLoader) + ret_ingredients = {} + for name in ingredients: + if name in ret_ingredients: + warnings.warn("Duplicate ingredient: " + name) + ret_ingredients[name] = Ingredient(name, ingredients[name]) + return ret_ingredients diff --git a/src/recipes/measure.py b/src/recipes/measure.py new file mode 100644 index 0000000..14c7543 --- /dev/null +++ b/src/recipes/measure.py @@ -0,0 +1,22 @@ +#!/bin/false + + +class Measure: + def __init__(self): + pass + + +class Weight(Measure): + def __init__(self): + super().__init__() + + +class Volume(Measure): + def __init__(self): + super().__init__() + + +MEASURE_TYPES = { + "weight": Weight(), + "volume": Volume(), +} diff --git a/src/recipes/recipe.py b/src/recipes/recipe.py new file mode 100644 index 0000000..61e2272 --- /dev/null +++ b/src/recipes/recipe.py @@ -0,0 +1,36 @@ +#!/bin/false + + +def loadTitle(title_object): + if isinstance(title_object, str): + return {"en": title_object} + elif isinstance(title_object, dict): + return title_object + else: + pass + + +class Step: + def __init__(self, object: dict, ingredients: dict, isMain: bool): + if isMain or "title" not in object: + self.title = "" + else: + self.title = loadTitle(object["title"]) + + # TODO: finish loading + pass + + +class Recipe: + def __init__(self, name: str, object: dict, ingredients: dict): + if "title" not in object: + self.title = loadTitle(name) + else: + self.title = loadTitle(object["title"]) + + self.steps = [] + if "step" not in object: + self.steps = [Step(object, ingredients, isMain=True)] + else: + for step in object["step"]: + self.steps.append(Step(step, ingredients, isMain=False))