[cookboob] basic amount parsing for better export

This commit is contained in:
Julien Veyssier 2013-03-24 22:02:37 +01:00 committed by Florent
commit b0d9a43a76

View file

@ -20,8 +20,7 @@
from .base import IBaseCap, CapBaseObject, StringField, IntField, Field, empty from .base import IBaseCap, CapBaseObject, StringField, IntField, Field, empty
import lxml.etree as ET import lxml.etree as ET
import base64 import base64, re, urllib
import urllib
__all__ = ['Recipe', 'ICapRecipe'] __all__ = ['Recipe', 'ICapRecipe']
@ -84,14 +83,23 @@ class Recipe(CapBaseObject):
if not empty(self.ingredients): if not empty(self.ingredients):
ings = ET.SubElement(recipe, 'krecipes-ingredients') ings = ET.SubElement(recipe, 'krecipes-ingredients')
pat = re.compile('^[0-9]*')
for i in self.ingredients: for i in self.ingredients:
sname = u'%s' % i
samount = ''
sunit = ''
first_nums = pat.match(i).group()
if first_nums != '':
samount = first_nums
sname = i.lstrip('0123456789 ')
ing = ET.SubElement(ings, 'ingredient') ing = ET.SubElement(ings, 'ingredient')
am = ET.SubElement(ing, 'amount') am = ET.SubElement(ing, 'amount')
am.text = '' am.text = samount
unit = ET.SubElement(ing, 'unit') unit = ET.SubElement(ing, 'unit')
unit.text = '' unit.text = sunit
name = ET.SubElement(ing, 'name') name = ET.SubElement(ing, 'name')
name.text = '%s' % i name.text = sname
if not empty(self.instructions): if not empty(self.instructions):
instructions = ET.SubElement(recipe, 'krecipes-instructions') instructions = ET.SubElement(recipe, 'krecipes-instructions')