[cookboob] export to mastercook XML
This commit is contained in:
parent
278902f909
commit
4d661fd759
2 changed files with 71 additions and 1 deletions
|
|
@ -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]
|
||||
|
|
|
|||
|
|
@ -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 = '''\
|
||||
<?xml version="1.0" standalone="yes" encoding="UTF-8"?>
|
||||
<!DOCTYPE mx2 SYSTEM "mx2.dtd">
|
||||
'''
|
||||
initial_xml = '''\
|
||||
<mx2 source="cookboob">
|
||||
</mx2>'''
|
||||
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):
|
||||
"""
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue