~vorlon/ubuntu/saucy/gourmet/trunk

« back to all changes in this revision

Viewing changes to src/lib/exporters/gxml2_exporter.py

  • Committer: Bazaar Package Importer
  • Author(s): Rolf Leggewie
  • Date: 2008-07-26 13:29:41 UTC
  • Revision ID: james.westby@ubuntu.com-20080726132941-6ldd73qmacrzz0bn
Tags: upstream-0.14.0
ImportĀ upstreamĀ versionĀ 0.14.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import exporter
 
2
import sys, xml.sax.saxutils, base64
 
3
from gourmet.gdebug import *
 
4
from gourmet.gglobals import *
 
5
from xml_exporter import XmlExporter
 
6
 
 
7
class rec_to_xml (XmlExporter):
 
8
    """A vastly simplified recipe XML exporter.
 
9
 
 
10
    The previous XML format was written as a format designed for
 
11
    itself (its design predated gourmet). This one is really written
 
12
    as a simple way to save, load and exchange Gourmet recipes. As a
 
13
    result, the code is much simpler, and the format should be quicker
 
14
    to write and more convenient for exchanging single recipes.
 
15
 
 
16
    This implementation actually uses the DOM to ensure correctness.
 
17
    """
 
18
 
 
19
    doc_element = 'gourmetDoc'
 
20
    doctype_desc = ''
 
21
    dtd_path = ''
 
22
 
 
23
    def write_head (self):
 
24
        self.rec_el = self.create_element_with_attrs("recipe",{'id':self.r.id})
 
25
        self.top_element.appendChild(self.rec_el)
 
26
 
 
27
    def write_attr (self, attr, text):
 
28
        self.rec_el.appendChild(self.create_text_element(attr,text))
 
29
        
 
30
    def write_text (self, attr, text):
 
31
        self.rec_el.appendChild(
 
32
            self.create_text_element(attr,text)
 
33
            )
 
34
 
 
35
    def write_image (self, image):
 
36
        image_el = self.create_element_with_attrs('image',{'format':'jpeg'})
 
37
        image_el.appendChild(
 
38
            self.xmlDoc.createCDATASection(base64.b64encode(image))
 
39
            )
 
40
        self.rec_el.appendChild(image_el)
 
41
    
 
42
    def handle_italic (self, chunk): return '<i>'+chunk+'</i>'
 
43
    def handle_bold (self, chunk): return '<b>'+chunk+'</b>'    
 
44
    def handle_underline (self, chunk): return '<u>'+chunk+'</u>'    
 
45
        
 
46
    def write_foot (self):
 
47
        if self.i_created_this_document:
 
48
            self.xmlDoc.writexml(self.ofi, newl = '\n', addindent = "\t", 
 
49
                                 encoding = "UTF-8")
 
50
 
 
51
    def write_inghead (self):
 
52
        self.inglist_el = self.xmlDoc.createElement('ingredient-list')
 
53
        self.top_inglist = self.inglist_el # because groups will let us nest...
 
54
        self.rec_el.appendChild(self.inglist_el)
 
55
 
 
56
    def write_ingref (self, amount=1, unit=None, item=None, refid=None, optional=False):
 
57
        self.inglist_el.appendChild(
 
58
            self.create_text_element('ingref',
 
59
                                     item,
 
60
                                     {'refid':str(refid),
 
61
                                      'amount':amount}
 
62
                                     )
 
63
            )
 
64
        
 
65
    def write_ing (self, amount=1, unit=None, item=None, key=None, optional=False):
 
66
        if optional:
 
67
            ing_el = self.create_element_with_attrs('ingredient',{'optional':'yes'})
 
68
        else:
 
69
            ing_el = self.create_element_with_attrs('ingredient',{})
 
70
        self.inglist_el.appendChild(ing_el)
 
71
        if amount:
 
72
            ing_el.appendChild(
 
73
                self.create_text_element('amount',amount)
 
74
                )
 
75
        if unit:
 
76
           ing_el.appendChild(
 
77
               self.create_text_element('unit',unit)
 
78
               )
 
79
        if item:
 
80
            ing_el.appendChild(
 
81
                self.create_text_element('item',item)
 
82
                )
 
83
        if key:
 
84
            ing_el.appendChild(
 
85
                self.create_text_element('key',key)
 
86
                )
 
87
 
 
88
    def write_grouphead (self, name):
 
89
        group_el = self.xmlDoc.createElement('inggroup')
 
90
        group_el.appendChild(
 
91
            self.create_text_element('groupname',name)
 
92
            )
 
93
        self.inglist_el = group_el
 
94
        
 
95
    def write_groupfoot (self):
 
96
        self.top_inglist.appendChild(self.inglist_el)
 
97
        self.inglist_el = self.top_inglist
 
98
    
 
99
 
 
100
class recipe_table_to_xml (exporter.ExporterMultirec, XmlExporter):
 
101
    doc_element = 'gourmetDoc'
 
102
    doctype_desc = ''
 
103
    dtd_path = ''
 
104
    def __init__ (self, rd, recipe_table, out, one_file=True, progress_func=None, change_units=False,
 
105
                  mult=1):
 
106
        self.create_xmldoc()
 
107
        exporter.ExporterMultirec.__init__(
 
108
            self, rd, recipe_table, out, one_file=True, ext='xml', exporter=rec_to_xml,
 
109
            progress_func=progress_func,
 
110
            exporter_kwargs={'change_units':change_units,
 
111
                             'mult':mult,
 
112
                             'xmlDoc':self.xmlDoc,
 
113
                             # This order is now in our DTD so we'd
 
114
                             # better make it solid.
 
115
                             'attr_order':('title',
 
116
                                           'category','cuisine',
 
117
                                           'source','link',
 
118
                                           'rating',
 
119
                                           'preptime','cooktime',
 
120
                                           'servings',
 
121
                                           ),
 
122
                             'order':['attr','image','ings','text'],
 
123
                             }
 
124
            )
 
125
        
 
126
 
 
127
    def write_footer (self, *args):
 
128
        self.xmlDoc.writexml(self.ofi, newl = '\n', addindent = "\t", 
 
129
                             encoding = "UTF-8")
 
130
        
 
131
def quoteattr (str):
 
132
    return xml.sax.saxutils.quoteattr(xml.sax.saxutils.escape(str))