forked from samjavner/recipeformats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmx2.py
272 lines (216 loc) · 8.29 KB
/
mx2.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
"""Recipe parsing for MasterCook 5+ format (.mx2) files.
Use parse_file to parse a file.
"""
import xml.etree.ElementTree as ET
__all__ = ['Info', 'Recipe', 'Rating', 'Ingredient', 'parse_file']
class Info:
"""Represents file-level information for a MasterCook 5+ .mx2 file.
Attributes:
source: File source application (e.g. 'MasterCook').
date: Date string (e.g. 'September 19, 2014').
"""
def __init__(self, source, date):
"""Initializes Info with provided values."""
self.source = source
self.date = date
class Recipe:
"""Represents a recipe in a MasterCook 5+ .mx2 file.
Attributes:
name: Recipe name string.
author: Author string.
source: Source string.
copyright: Copyright string.
servings: Number of servings string.
preparation_time: Preparation time string.
total_time: Total recipe time string.
cuisine: Type of cuisine string (e.g. 'Chinese').
description: Recipe description string.
note: Recipe note string.
serving_ideas: Recipe serving ideas string.
suggested_wine: Wine suggestion string.
yield_: Recipe yield string (e.g. '24 muffins').
alternate_source: Source other than source/author (e.g. 'www.example.com').
alternate_source_label: Label for alternate source (e.g. 'Web Page').
alternate_time: Amount of time other than preparation/total.
alternate_time_label: Label for alternate time (e.g. 'Cooking Time').
categories: List of category strings.
ratings: List of Rating objects.
ingredients: List of Ingredient objects.
directions: List of direction strings.
"""
def __init__(self):
"""Initializes Recipe with default values."""
self.name = ''
self.author = ''
self.source = ''
self.copyright = ''
self.servings = ''
self.preparation_time = ''
self.total_time = ''
self.cuisine = ''
self.description = ''
self.note = ''
self.serving_ideas = ''
self.suggested_wine = ''
self.yield_ = ''
self.alternate_source = ''
self.alternate_source_label = ''
self.alternate_time = ''
self.alternate_time_label = ''
self.categories = []
self.ratings = []
self.ingredients = []
self.directions = []
class Rating:
"""Represents a recipe rating.
Attributes:
name: Name of the rating (e.g. 'Difficulty', 'Taste').
value: Integer value (0-10).
"""
def __init__(self, name, value):
"""Initializes Rating with provided values."""
self.name = name
self.value = value
def __repr__(self):
"""Provides default string representation of Rating."""
return self.name + ': ' + str(self.value)
class Ingredient:
"""Represents a recipe ingredient.
Attributes:
quantity: Amount string (e.g. '1/2', '2', '').
unit: Measure of unit string (e.g. 'cup', 'tablespoons', '').
name: Ingredient name (e.g. 'apple', 'salt').
preparation: Preparation method (e.g. 'chopped', '').
code: Ingredient code:
'I' or '' = Ingredient
'R' = Recipe
'S' = Subtitle
'T' = Text
"""
def __init__(self, quantity, unit, name, preparation, code):
"""Initializes Ingredient with provided values."""
self.quantity = quantity
self.unit = unit
self.name = name
self.preparation = preparation
self.code = code
def __repr__(self):
"""Provides default string representation of Ingredient."""
return '{%s} {%s} {%s} {%s} {%s}' % (self.quantity, self.unit, self.name, self.preparation, self.code)
def parse_file(filename):
"""Parses a .mx2 file.
Args:
filename: File name of the .mx2 file to parse.
Returns:
A tuple containing an Info object and a list of Recipe objects.
"""
with open(filename) as f:
s = _load_mx2_into_string(f)
return _parse_string(s)
def _load_mx2_into_string(file):
"""Loads a .mx2 file into a string. Fixes the XML declaration if necessary."""
return ''.join(_get_mx2_iterator(file))
def _get_mx2_iterator(file):
"""Fixes the XML declaration on the first line of the file and then returns
the rest of the lines unchanged.
MasterCook outputs files with an XML declaration that is not well-formed.
"""
first = True
for line in file:
if first and line == '<?xml version="1.0" standalone="yes" encoding="ISO-8859-1"?>\n':
yield '<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>\n'
else:
first = False
yield line
def _parse_string(string):
"""Parses a mx2 XML string.
Args:
string: XML content to parse with root 'mx2' element.
Returns:
A tuple containing an Info object and a list of Recipe objects.
"""
mx2 = ET.fromstring(string)
return _parse_info(mx2), _parse_recipes(mx2)
def _parse_info(mx2):
"""Parses Info from an 'mx2' Element."""
source = mx2.get('source', '')
date = mx2.get('date', '')
return Info(source, date)
def _parse_recipes(mx2):
"""Parses a list of Recipe objects from an 'mx2' Element."""
recipes = []
for rcpe in mx2.iter('RcpE'):
# TODO: Next recipe could be embedded recipe
recipes.append(_parse_recipe(rcpe))
return recipes
def _parse_recipe(rcpe):
"""Parses a Recipe from a 'RcpE' Element."""
recipe = Recipe()
recipe.name = rcpe.get('name', '')
recipe.author = rcpe.get('author', '')
recipe.source = rcpe.findtext('Srce', '').strip()
recipe.copyright = rcpe.findtext('CpyR', '').strip()
recipe.cuisine = rcpe.findtext('Natn', '').strip()
recipe.description = rcpe.findtext('Desc', '').strip()
recipe.note = rcpe.findtext('Note', '').strip()
recipe.serving_ideas = rcpe.findtext('SrvI', '').strip()
recipe.suggested_wine = rcpe.findtext('Wine', '').strip()
recipe.servings = _findget(rcpe, 'Serv', 'qty', '')
recipe.preparation_time = _findget(rcpe, 'PrpT', 'elapsed', '')
recipe.total_time = _findget(rcpe, 'TTim', 'elapsed', '')
yield_ = _find(rcpe, 'Yield')
qty = yield_.get('qty', '')
unit = yield_.get('unit', '')
recipe.yield_ = (qty + ' ' + unit).strip()
alts = _find(rcpe, 'AltS')
recipe.alternate_source = alts.get('source', '')
recipe.alternate_source_label = alts.get('label', '')
altt = _find(rcpe, 'AltT')
recipe.alternate_time = altt.get('elapsed', '')
recipe.alternate_time_label = altt.get('label', '')
for catt in _find(rcpe, 'CatS').iter('CatT'):
recipe.categories.append(catt.text.strip())
for rats in _find(rcpe, 'RatS').iter('RatE'):
name = rats.get('name', '')
try:
value = int(rats.get('value', ''))
except ValueError:
pass
else:
recipe.ratings.append(Rating(name, value))
for ingr in rcpe.iter('IngR'):
quantity = ingr.get('qty', '')
unit = ingr.get('unit', '')
name = ingr.get('name', '')
preparation = ingr.findtext('IPrp', '').strip()
code = ingr.get('code', '')
# TODO: INtI, IERp
recipe.ingredients.append(Ingredient(quantity, unit, name, preparation, code))
for dirt in _find(rcpe, 'DirS').iter('DirT'):
# TODO: img
# TODO: replace '\r\n' with '\n' ??
recipe.directions.append(dirt.text.strip())
# TODO: img
# TODO: Nutr
return recipe
def _find(element, tag):
"""Finds the first subelement with the given tag name.
Returns the subelement, or an empty element with no attributes or
subelements if no subelement with the given tag name was found.
"""
subelement = element.find(tag)
if subelement is None:
return ET.Element(tag)
else:
return subelement
def _findget(element, tag, key, default=None):
"""Finds the first subelement with the given tag name and gets its
element attribute named key.
Returns the attribute value, or default if the subelement with the given
tag name was not found or the attribute was not found.
"""
subelement = element.find(tag)
if subelement is None:
return default
else:
return subelement.get(key, default)