[cookboob] export to KRecipesML much better and cleaner

This commit is contained in:
Julien Veyssier 2013-03-24 18:23:23 +01:00 committed by Florent
commit 1d971a5e04
2 changed files with 45 additions and 28 deletions

View file

@ -114,7 +114,7 @@ class Cookboob(ReplApplication):
""" """
export ID [FILENAME] export ID [FILENAME]
Export the recipe to a mastercook XML file Export the recipe to a KRecipes XML file
FILENAME is where to write the file. If FILENAME is '-', FILENAME is where to write the file. If FILENAME is '-',
the file is written to stdout. the file is written to stdout.
""" """
@ -123,20 +123,22 @@ class Cookboob(ReplApplication):
_id, backend_name = self.parse_id(id) _id, backend_name = self.parse_id(id)
if dest is None: if dest is None:
dest = '%s.mx2' % _id dest = '%s.kreml' % _id
recipe = self.get_object(id, 'get_recipe') recipe = self.get_object(id, 'get_recipe')
if recipe: if recipe:
xmlstring = recipe.toMasterCookXml(backend_name or None) xmlstring = recipe.toKrecipesXml(backend_name or None)
if dest == '-': if dest == '-':
print xmlstring print xmlstring
else: else:
if not dest.endswith('.kreml'):
dest += '.kreml'
try: try:
with codecs.open(dest, 'w', 'utf-8') as f: with codecs.open(dest, 'w', 'utf-8') as f:
f.write(xmlstring) f.write(xmlstring)
except IOError, e: except IOError, e:
print >>sys.stderr, 'Unable to write .mx2 in "%s": %s' % (dest, e) print >>sys.stderr, 'Unable to write .kreml in "%s": %s' % (dest, e)
return 1 return 1
return return
print >>sys.stderr, 'Recipe "%s" not found' % id print >>sys.stderr, 'Recipe "%s" not found' % id

View file

@ -18,7 +18,7 @@
# along with weboob. If not, see <http://www.gnu.org/licenses/>. # along with weboob. If not, see <http://www.gnu.org/licenses/>.
from .base import IBaseCap, CapBaseObject, StringField, IntField, Field from .base import IBaseCap, CapBaseObject, StringField, IntField, Field, empty
import xml.etree.ElementTree as ET import xml.etree.ElementTree as ET
@ -44,35 +44,50 @@ class Recipe(CapBaseObject):
CapBaseObject.__init__(self, id) CapBaseObject.__init__(self, id)
self.title = title self.title = title
def toMasterCookXml(self, author=None): def toKrecipesXml(self, author=None):
""" """
Export recipe to mastercook pretty XML string Export recipe to KRecipes XML string
""" """
if author == None: if author == None:
author = 'Cookboob' author = 'Cookboob'
header = '''\
<?xml version="1.0" standalone="yes" encoding="UTF-8"?>
<!DOCTYPE mx2 SYSTEM "mx2.dtd">
'''
initial_xml = '''\ initial_xml = '''\
<mx2 source="cookboob"> <krecipes version='2.0-beta2' lang='fr' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation='krecipes.xsd'>
</mx2>''' <krecipes-recipe id='1'>
</krecipes-recipe>
</krecipes>'''
doc = ET.fromstring(initial_xml) doc = ET.fromstring(initial_xml)
summ = ET.SubElement(doc,'Summ') recipe = doc.find('krecipes-recipe')
nam = ET.SubElement(summ,'Nam') desc = ET.SubElement(recipe, 'krecipes-description')
nam.text = self.title title = ET.SubElement(desc, 'title')
title.text = self.title
authors = ET.SubElement(desc, 'author')
authors.text = author
eyield = ET.SubElement(desc, 'yield')
if not empty(self.nb_person):
amount = ET.SubElement(eyield, 'amount')
amount.text = '%s' % self.nb_person
etype = ET.SubElement(eyield, 'type')
etype.text = 'persons'
if not empty(self.preparation_time):
preptime = ET.SubElement(desc, 'preparation-time')
preptime.text = '%02d:%02d' % (self.preparation_time / 60, self.preparation_time % 60)
rcpe = ET.SubElement(doc, 'RcpE', {'author': author, 'name': self.title}) if not empty(self.ingredients):
ET.SubElement(rcpe, 'Serv', {'qty': '0'}) ings = ET.SubElement(recipe, 'krecipes-ingredients')
ET.SubElement(rcpe, 'PrpT', {'elapsed': '%s:%s' % (self.preparation_time / 60, self.preparation_time % 60)}) for i in self.ingredients:
ET.SubElement(rcpe, 'CatS') ing = ET.SubElement(ings, 'ingredient')
for i in self.ingredients: am = ET.SubElement(ing, 'amount')
ing = ET.SubElement(rcpe, 'IngR', {'units': '', 'name': i, 'qty': ''}) am.text = ''
instr = ET.SubElement(rcpe, 'DirS') unit = ET.SubElement(ing, 'unit')
sinstr = ET.SubElement(instr, 'DirT') unit.text = ''
sinstr.text = self.instructions name = ET.SubElement(ing, 'name')
ET.SubElement(rcpe, 'Yield', {'unit': 'persons', 'qty': '%s'%self.nb_person}) name.text = '%s' % i
return header + ET.tostring(doc)
if not empty(self.instructions):
instructions = ET.SubElement(recipe, 'krecipes-instructions')
instructions.text = self.instructions
return ET.tostring(doc, encoding='UTF-8').decode('utf-8')
class ICapRecipe(IBaseCap): class ICapRecipe(IBaseCap):