You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

240 lines
7.8 KiB

  1. # cython: language_level=2
  2. #
  3. # Element generator factory by Fredrik Lundh.
  4. #
  5. # Source:
  6. # http://online.effbot.org/2006_11_01_archive.htm#et-builder
  7. # http://effbot.python-hosting.com/file/stuff/sandbox/elementlib/builder.py
  8. #
  9. # --------------------------------------------------------------------
  10. # The ElementTree toolkit is
  11. #
  12. # Copyright (c) 1999-2004 by Fredrik Lundh
  13. #
  14. # By obtaining, using, and/or copying this software and/or its
  15. # associated documentation, you agree that you have read, understood,
  16. # and will comply with the following terms and conditions:
  17. #
  18. # Permission to use, copy, modify, and distribute this software and
  19. # its associated documentation for any purpose and without fee is
  20. # hereby granted, provided that the above copyright notice appears in
  21. # all copies, and that both that copyright notice and this permission
  22. # notice appear in supporting documentation, and that the name of
  23. # Secret Labs AB or the author not be used in advertising or publicity
  24. # pertaining to distribution of the software without specific, written
  25. # prior permission.
  26. #
  27. # SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  28. # TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANT-
  29. # ABILITY AND FITNESS. IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR
  30. # BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
  31. # DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  32. # WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  33. # ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  34. # OF THIS SOFTWARE.
  35. # --------------------------------------------------------------------
  36. """
  37. The ``E`` Element factory for generating XML documents.
  38. """
  39. from __future__ import absolute_import
  40. import lxml.etree as ET
  41. from functools import partial
  42. try:
  43. basestring
  44. except NameError:
  45. basestring = str
  46. try:
  47. unicode
  48. except NameError:
  49. unicode = str
  50. class ElementMaker(object):
  51. """Element generator factory.
  52. Unlike the ordinary Element factory, the E factory allows you to pass in
  53. more than just a tag and some optional attributes; you can also pass in
  54. text and other elements. The text is added as either text or tail
  55. attributes, and elements are inserted at the right spot. Some small
  56. examples::
  57. >>> from lxml import etree as ET
  58. >>> from lxml.builder import E
  59. >>> ET.tostring(E("tag"))
  60. '<tag/>'
  61. >>> ET.tostring(E("tag", "text"))
  62. '<tag>text</tag>'
  63. >>> ET.tostring(E("tag", "text", key="value"))
  64. '<tag key="value">text</tag>'
  65. >>> ET.tostring(E("tag", E("subtag", "text"), "tail"))
  66. '<tag><subtag>text</subtag>tail</tag>'
  67. For simple tags, the factory also allows you to write ``E.tag(...)`` instead
  68. of ``E('tag', ...)``::
  69. >>> ET.tostring(E.tag())
  70. '<tag/>'
  71. >>> ET.tostring(E.tag("text"))
  72. '<tag>text</tag>'
  73. >>> ET.tostring(E.tag(E.subtag("text"), "tail"))
  74. '<tag><subtag>text</subtag>tail</tag>'
  75. Here's a somewhat larger example; this shows how to generate HTML
  76. documents, using a mix of prepared factory functions for inline elements,
  77. nested ``E.tag`` calls, and embedded XHTML fragments::
  78. # some common inline elements
  79. A = E.a
  80. I = E.i
  81. B = E.b
  82. def CLASS(v):
  83. # helper function, 'class' is a reserved word
  84. return {'class': v}
  85. page = (
  86. E.html(
  87. E.head(
  88. E.title("This is a sample document")
  89. ),
  90. E.body(
  91. E.h1("Hello!", CLASS("title")),
  92. E.p("This is a paragraph with ", B("bold"), " text in it!"),
  93. E.p("This is another paragraph, with a ",
  94. A("link", href="http://www.python.org"), "."),
  95. E.p("Here are some reserved characters: <spam&egg>."),
  96. ET.XML("<p>And finally, here is an embedded XHTML fragment.</p>"),
  97. )
  98. )
  99. )
  100. print ET.tostring(page)
  101. Here's a prettyprinted version of the output from the above script::
  102. <html>
  103. <head>
  104. <title>This is a sample document</title>
  105. </head>
  106. <body>
  107. <h1 class="title">Hello!</h1>
  108. <p>This is a paragraph with <b>bold</b> text in it!</p>
  109. <p>This is another paragraph, with <a href="http://www.python.org">link</a>.</p>
  110. <p>Here are some reserved characters: &lt;spam&amp;egg&gt;.</p>
  111. <p>And finally, here is an embedded XHTML fragment.</p>
  112. </body>
  113. </html>
  114. For namespace support, you can pass a namespace map (``nsmap``)
  115. and/or a specific target ``namespace`` to the ElementMaker class::
  116. >>> E = ElementMaker(namespace="http://my.ns/")
  117. >>> print(ET.tostring( E.test ))
  118. <test xmlns="http://my.ns/"/>
  119. >>> E = ElementMaker(namespace="http://my.ns/", nsmap={'p':'http://my.ns/'})
  120. >>> print(ET.tostring( E.test ))
  121. <p:test xmlns:p="http://my.ns/"/>
  122. """
  123. def __init__(self, typemap=None,
  124. namespace=None, nsmap=None, makeelement=None):
  125. if namespace is not None:
  126. self._namespace = '{' + namespace + '}'
  127. else:
  128. self._namespace = None
  129. if nsmap:
  130. self._nsmap = dict(nsmap)
  131. else:
  132. self._nsmap = None
  133. if makeelement is not None:
  134. assert callable(makeelement)
  135. self._makeelement = makeelement
  136. else:
  137. self._makeelement = ET.Element
  138. # initialize type map for this element factory
  139. if typemap:
  140. typemap = dict(typemap)
  141. else:
  142. typemap = {}
  143. def add_text(elem, item):
  144. try:
  145. elem[-1].tail = (elem[-1].tail or "") + item
  146. except IndexError:
  147. elem.text = (elem.text or "") + item
  148. def add_cdata(elem, cdata):
  149. if elem.text:
  150. raise ValueError("Can't add a CDATA section. Element already has some text: %r" % elem.text)
  151. elem.text = cdata
  152. if str not in typemap:
  153. typemap[str] = add_text
  154. if unicode not in typemap:
  155. typemap[unicode] = add_text
  156. if ET.CDATA not in typemap:
  157. typemap[ET.CDATA] = add_cdata
  158. def add_dict(elem, item):
  159. attrib = elem.attrib
  160. for k, v in item.items():
  161. if isinstance(v, basestring):
  162. attrib[k] = v
  163. else:
  164. attrib[k] = typemap[type(v)](None, v)
  165. if dict not in typemap:
  166. typemap[dict] = add_dict
  167. self._typemap = typemap
  168. def __call__(self, tag, *children, **attrib):
  169. typemap = self._typemap
  170. if self._namespace is not None and tag[0] != '{':
  171. tag = self._namespace + tag
  172. elem = self._makeelement(tag, nsmap=self._nsmap)
  173. if attrib:
  174. typemap[dict](elem, attrib)
  175. for item in children:
  176. if callable(item):
  177. item = item()
  178. t = typemap.get(type(item))
  179. if t is None:
  180. if ET.iselement(item):
  181. elem.append(item)
  182. continue
  183. for basetype in type(item).__mro__:
  184. # See if the typemap knows of any of this type's bases.
  185. t = typemap.get(basetype)
  186. if t is not None:
  187. break
  188. else:
  189. raise TypeError("bad argument type: %s(%r)" %
  190. (type(item).__name__, item))
  191. v = t(elem, item)
  192. if v:
  193. typemap.get(type(v))(elem, v)
  194. return elem
  195. def __getattr__(self, tag):
  196. return partial(self, tag)
  197. # create factory object
  198. E = ElementMaker()