From 4d661fd759785d5ff416d9ea7930ccd808a90ca7 Mon Sep 17 00:00:00 2001 From: Julien Veyssier Date: Sat, 23 Mar 2013 11:49:16 +0100 Subject: [PATCH] [cookboob] export to mastercook XML --- weboob/applications/cookboob/cookboob.py | 41 +++++++++++++++++++++++- weboob/capabilities/recipe.py | 31 ++++++++++++++++++ 2 files changed, 71 insertions(+), 1 deletion(-) diff --git a/weboob/applications/cookboob/cookboob.py b/weboob/applications/cookboob/cookboob.py index 43eb5f87..47ccf0c1 100644 --- a/weboob/applications/cookboob/cookboob.py +++ b/weboob/applications/cookboob/cookboob.py @@ -20,6 +20,7 @@ from __future__ import with_statement import sys +import codecs from weboob.capabilities.recipe import ICapRecipe from weboob.capabilities.base import empty @@ -93,7 +94,6 @@ class Cookboob(ReplApplication): Get information about a recipe. """ - recipe = self.get_object(id, 'get_recipe') if not recipe: print >>sys.stderr, 'Recipe not found: %s' % id @@ -103,6 +103,45 @@ class Cookboob(ReplApplication): self.format(recipe) self.flush() + def complete_export(self, text, line, *ignored): + args = line.split(' ', 2) + if len(args) == 2: + return self._complete_object() + elif len(args) >= 3: + return self.path_completer(args[2]) + + def do_export(self, line): + """ + export ID [FILENAME] + + Export the recipe to a mastercook XML file + FILENAME is where to write the file. If FILENAME is '-', + the file is written to stdout. + """ + id, dest = self.parse_command_args(line, 2, 1) + + _id, backend_name = self.parse_id(id) + + if dest is None: + dest = '%s.mx2' % _id + + recipe = self.get_object(id, 'get_recipe') + + if recipe: + xmlstring = recipe.toMasterCookXml(backend_name or None) + if dest == '-': + print xmlstring + else: + 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) + return 1 + return + print >>sys.stderr, 'Recipe "%s" not found' % id + return 3 + def do_search(self, pattern): """ search [PATTERN] diff --git a/weboob/capabilities/recipe.py b/weboob/capabilities/recipe.py index 5bc51716..07822f1a 100644 --- a/weboob/capabilities/recipe.py +++ b/weboob/capabilities/recipe.py @@ -19,6 +19,7 @@ from .base import IBaseCap, CapBaseObject, StringField, IntField, Field +import xml.etree.ElementTree as ET __all__ = ['Recipe', 'ICapRecipe'] @@ -43,6 +44,36 @@ class Recipe(CapBaseObject): CapBaseObject.__init__(self, id) self.title = title + def toMasterCookXml(self, author=None): + """ + Export recipe to mastercook pretty 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 + + 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) + class ICapRecipe(IBaseCap): """