-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathread_from_yaml.py
180 lines (160 loc) · 6.53 KB
/
read_from_yaml.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import os
import yaml
from data import *
def create_sorted_ingredient_overview(recipe):
ingredients = dict()
for step in recipe.steps:
for ingr in step.ingredients_used:
name = ingr.name
amount = 0
if ingr.amount:
amount = ingr.amount
unit = ingr.unit
comment = ''
if ingr.comment:
comment = ingr.comment
recipe_link = ''
if ingr.recipe_link:
recipe_link = ingr.recipe_link
if not name in ingredients:
ingredients[name] = [(amount, unit, comment, recipe_link)]
else:
added = False
for index,item in enumerate(ingredients[name]):
if unit == item[1]:
ingredients[name][index] = (item[0]+amount, unit, item[2]+comment, recipe_link)
added = True
if not added:
index = 0
for item in ingredients[name]:
if item[0] > amount:
index += 1
else:
break
ingredients[name].insert(index, (amount, unit, comment, recipe_link))
sorted_ingredients = sorted(ingredients.items(), key=lambda kv: kv[1][0], reverse=True)
for kv in sorted_ingredients:
name = kv[0]
for value in kv[1]:
amount = value[0]
if amount == 0:
amount = ''
ingr_overview = IngredientsOverview(name=name, total_amount=amount, unit=value[1], comment=value[2], recipe_link=value[3])
recipe.ingredients.append(ingr_overview)
def read_steps(yaml, recipe):
if 'steps' in yaml:
id = 0
for step in yaml['steps']:
active_time = 0
passive_time = 0
if 'active-time' in step:
active_time = step['active-time']
if 'passive-time' in step:
passive_time = step['passive-time']
time = StepTime(active=active_time, passive=passive_time)
type = step['type']
temperature = step['temp']
step_data = Step(id=id, time=time, step_type=type, temperature=temperature)
if 'short' in step:
step_data.short = step['short']
if 'long' in step:
step_data.long = step['long']
if 'ingredients' in step:
add_ingredients_from_step_to_step_data(step, step_data)
if 'refined_ingredients' in step:
add_refined_ingredients_from_step_to_step_data(step, step_data)
if 'dependencies' in step:
add_dependencies_to_step_data(step, step_data)
recipe.steps.append(step_data)
id += 1
else:
print(f'Recipe {recipe.name} has no steps.')
def add_ingredients_from_step_to_step_data(step, step_data):
for ingr in step['ingredients']:
name = ingr['name']
amount = ingr['amount'] # might fail?
ingredient = Ingredient(name=name, amount=amount)
#if not name in ingredients:
#ingredients[name] = IngredientsOverview(name,
if 'unit' in ingr:
ingredient.unit = ingr['unit']
if 'comment' in ingr:
ingredient.comment = ingr['comment']
#ingredients[name].comment = ingredient.comment
if 'recipe-link' in ingr:
ingredient.recipe_link = ingr['recipe-link']
step_data.ingredients_used.append(ingredient)
def add_refined_ingredients_from_step_to_step_data(step, step_data):
for ingr in step['refined_ingredients']:
ingredient = RefinedIngredient(name=ingr['name'])
if 'amount' in ingr:
ingredient.amount = ingr['amount']
if 'unit' in ingr:
ingredient.unit = ingr['unit']
step_data.refined_ingredients_used.append(ingredient)
def add_dependencies_to_step_data(step, step_data):
for dep in step['dependencies']:
step_data.depends_on.append( step_data.id + int(dep) )
# Sort the list to try to make the generated graph look a bit prettier.
# The desired effect is that the steps occurring earlier are processed earlier, so that earlier entries get smaller y values.
step_data.depends_on.sort()
def read_source(yaml_source):
name = yaml_source['name']
source = Source(name)
if 'type' in yaml_source:
source.originality = yaml_source['type']
if 'author' in yaml_source:
source.author = yaml_source['author']
if 'publication' in yaml_source:
source.publication = yaml_source['publication']
if 'url' in yaml_source:
source.url = yaml_source['url']
if 'date' in yaml_source:
source.date = yaml_source['date']
return source
def read_recipe(yaml):
name = yaml['recipe']
created = yaml['date_created']
if 'last_updated' in yaml:
updated = yaml['last_updated']
else:
updated = created
recipe = Recipe(name, created, updated)
if 'not_ready_for_publish' in yaml:
recipe.not_ready_for_publish = yaml['not_ready_for_publish']
else:
recipe.not_ready_for_publish = False
if 'image' in yaml:
recipe.image = yaml['image']
if 'description' in yaml:
recipe.description = yaml['description']
if 'source' in yaml:
recipe.source = read_source(yaml['source'])
if 'yield' in yaml:
# recipe.yields = Yield(yaml['yield']['amount'], yaml['yield']['name'])
for yield_data_source in yaml['yield']:
yield_data = Yield(yield_data_source['amount'], yield_data_source['unit'])
if 'qualification' in yield_data_source:
yield_data.qualification = yield_data_source['qualification']
recipe.yields.append(yield_data)
return recipe
def read_recipe_into_data(name):
path = os.path.join('recipes', name+'.yaml')
with open(path, 'r', encoding='utf-8') as recipe_file:
yaml_data = yaml.load(recipe_file, Loader=yaml.FullLoader)
recipe = read_recipe(yaml_data)
read_steps(yaml_data, recipe)
create_sorted_ingredient_overview(recipe)
return recipe
if __name__ == "__main__":
#r = read_recipe_into_data("simons-burger")
#r = read_recipe_into_data("muffins-blueberry-and-basil")
r = read_recipe_into_data("saffron-knots")
print(r)
for s in r.steps:
print(s.short)
for i in r.ingredients:
print(f"{i.total_amount} {i.unit} {i.name}")
import timeline
timeline.generate_svg(r)
timeline.print_nodes(r)