PageRenderTime 11ms CodeModel.GetById 1ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/appy/pod/elements.py

https://code.google.com/
Python | 88 lines | 53 code | 12 blank | 23 comment | 1 complexity | 4f80aff7c411dd270005f35fb84c573b MD5 | raw file
 1# ------------------------------------------------------------------------------
 2# Appy is a framework for building applications in the Python language.
 3# Copyright (C) 2007 Gaetan Delannay
 4
 5# This program is free software; you can redistribute it and/or
 6# modify it under the terms of the GNU General Public License
 7# as published by the Free Software Foundation; either version 2
 8# of the License, or (at your option) any later version.
 9
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU General Public License for more details.
14
15# You should have received a copy of the GNU General Public License
16# along with this program; if not, write to the Free Software
17# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,USA.
18
19# ------------------------------------------------------------------------------
20from appy.shared.xml_parser import XmlElement
21from appy.pod.odf_parser import OdfEnvironment as ns
22from appy.pod import PodError
23
24# ------------------------------------------------------------------------------
25class PodElement:
26    OD_TO_POD = {'p': 'Text', 'h': 'Title', 'section': 'Section',
27                 'table': 'Table', 'table-row': 'Row', 'table-cell': 'Cell',
28                 None: 'Expression'}
29    POD_ELEMS = ('text', 'title', 'section', 'table', 'row', 'cell')
30    MINUS_ELEMS = ('section', 'table') # Elements for which the '-' operator can
31    # be applied
32    def create(elem):
33        '''Used to create any POD elem that has a equivalent OD element. Not
34           for creating expressions, for example.'''
35        return eval(PodElement.OD_TO_POD[elem])()
36    create = staticmethod(create)
37
38class Text(PodElement):
39    OD = XmlElement('p', nsUri=ns.NS_TEXT)
40    subTags = [] # When generating an error we may need to surround the error
41    # with a given tag and subtags
42
43class Title(PodElement):
44    OD = XmlElement('h', nsUri=ns.NS_TEXT)
45    subTags = []
46
47class Section(PodElement):
48    OD = XmlElement('section', nsUri=ns.NS_TEXT)
49    subTags = [Text.OD]
50    DEEPEST_TO_REMOVE = OD # When we must remove the Section element from a
51    # buffer, the deepest element to remove is the Section element itself
52
53class Cell(PodElement):
54    OD = XmlElement('table-cell', nsUri=ns.NS_TABLE)
55    subTags = [Text.OD]
56    def __init__(self):
57        self.tableInfo = None # ~OdTable~
58        self.colIndex = None # The column index for this cell, within its table.
59
60class Row(PodElement):
61    OD = XmlElement('table-row', nsUri=ns.NS_TABLE)
62    subTags = [Cell.OD, Text.OD]
63
64class Table(PodElement):
65    OD = XmlElement('table', nsUri=ns.NS_TABLE)
66    subTags = [Row.OD, Cell.OD, Text.OD]
67    DEEPEST_TO_REMOVE = Cell.OD # When we must remove the Table element from a
68    # buffer, the deepest element to remove is the Cell (it can only be done for
69    # one-row, one-cell tables
70    def __init__(self):
71        self.tableInfo = None # ~OdTable~
72
73class Expression(PodElement):
74    OD = None
75    def __init__(self, pyExpr):
76        self.expr = pyExpr
77    def evaluate(self, context):
78        res = eval(self.expr, context)
79        if res == None:
80            res = u''
81        elif isinstance(res, str):
82            res = unicode(res.decode('utf-8'))
83        elif isinstance(res, unicode):
84            pass
85        else:
86            res = unicode(res)
87        return res
88# ------------------------------------------------------------------------------