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.

235 lines
7.7 KiB

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