From 1d971a5e04f4bd6acc7b09fd196694cd45159ed9 Mon Sep 17 00:00:00 2001 From: Julien Veyssier Date: Sun, 24 Mar 2013 18:23:23 +0100 Subject: [PATCH] [cookboob] export to KRecipesML much better and cleaner --- weboob/applications/cookboob/cookboob.py | 10 ++-- weboob/capabilities/recipe.py | 61 +++++++++++++++--------- 2 files changed, 44 insertions(+), 27 deletions(-) diff --git a/weboob/applications/cookboob/cookboob.py b/weboob/applications/cookboob/cookboob.py index 47ccf0c1..f76ac0df 100644 --- a/weboob/applications/cookboob/cookboob.py +++ b/weboob/applications/cookboob/cookboob.py @@ -114,7 +114,7 @@ class Cookboob(ReplApplication): """ 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 '-', the file is written to stdout. """ @@ -123,20 +123,22 @@ class Cookboob(ReplApplication): _id, backend_name = self.parse_id(id) if dest is None: - dest = '%s.mx2' % _id + dest = '%s.kreml' % _id recipe = self.get_object(id, 'get_recipe') if recipe: - xmlstring = recipe.toMasterCookXml(backend_name or None) + xmlstring = recipe.toKrecipesXml(backend_name or None) if dest == '-': print xmlstring else: + if not dest.endswith('.kreml'): + dest += '.kreml' try: with codecs.open(dest, 'w', 'utf-8') as f: f.write(xmlstring) 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 print >>sys.stderr, 'Recipe "%s" not found' % id diff --git a/weboob/capabilities/recipe.py b/weboob/capabilities/recipe.py index 07822f1a..df563069 100644 --- a/weboob/capabilities/recipe.py +++ b/weboob/capabilities/recipe.py @@ -18,7 +18,7 @@ # along with weboob. If not, see . -from .base import IBaseCap, CapBaseObject, StringField, IntField, Field +from .base import IBaseCap, CapBaseObject, StringField, IntField, Field, empty import xml.etree.ElementTree as ET @@ -44,35 +44,50 @@ class Recipe(CapBaseObject): CapBaseObject.__init__(self, id) 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: author = 'Cookboob' - header = '''\ - - -''' initial_xml = '''\ - -''' + + + +''' doc = ET.fromstring(initial_xml) - summ = ET.SubElement(doc,'Summ') - nam = ET.SubElement(summ,'Nam') - nam.text = self.title + recipe = doc.find('krecipes-recipe') + desc = ET.SubElement(recipe, 'krecipes-description') + 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}) - ET.SubElement(rcpe, 'Serv', {'qty': '0'}) - ET.SubElement(rcpe, 'PrpT', {'elapsed': '%s:%s' % (self.preparation_time / 60, self.preparation_time % 60)}) - ET.SubElement(rcpe, 'CatS') - for i in self.ingredients: - ing = ET.SubElement(rcpe, 'IngR', {'units': '', 'name': i, 'qty': ''}) - instr = ET.SubElement(rcpe, 'DirS') - sinstr = ET.SubElement(instr, 'DirT') - sinstr.text = self.instructions - ET.SubElement(rcpe, 'Yield', {'unit': 'persons', 'qty': '%s'%self.nb_person}) - return header + ET.tostring(doc) + if not empty(self.ingredients): + ings = ET.SubElement(recipe, 'krecipes-ingredients') + for i in self.ingredients: + ing = ET.SubElement(ings, 'ingredient') + am = ET.SubElement(ing, 'amount') + am.text = '' + unit = ET.SubElement(ing, 'unit') + unit.text = '' + name = ET.SubElement(ing, 'name') + name.text = '%s' % i + + 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):