Browse Source

add recipe parser

Daniel Sheffield 2 years ago
parent
commit
60a13c0745
1 changed files with 32 additions and 0 deletions
  1. 32 0
      app/parse_recipe.py

+ 32 - 0
app/parse_recipe.py

@@ -0,0 +1,32 @@
+#
+# Copyright (c) Daniel Sheffield 2023
+#
+# All rights reserved
+#
+# THIS SOFTWARE IS PROVIDED AS IS WITHOUT WARRANTY
+import yaml
+from yaml.loader import SafeLoader
+from .db_utils import QueryManager
+import re
+
+def parse_recipe(fh, query_manager: QueryManager):
+    contents = yaml.load(fh, Loader=SafeLoader)
+    recipe = {
+        'ingredients': [],
+        'feeds': None,
+        'instructions': None,
+    }
+    for ingredient in contents['ingredients']:
+        units = query_manager.unique_suggestions('unit', unit='')
+        for u in units:
+            match = re.search(
+                r'(?P<product>.*) (?P<quantity>[0-9\.]+)(?P<unit>' f'{u}' r')', ingredient
+            )
+            if not match:
+                continue
+            recipe['ingredients'].append(
+                [match.group('product'), match.group('quantity'), match.group('unit')]
+            )
+    return recipe
+    
+