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