Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 

244 rindas
8.3 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. import lxml.etree as ET
  40. _QName = ET.QName
  41. from functools import partial
  42. try:
  43. from types import GenericAlias as _GenericAlias
  44. except ImportError:
  45. # Python 3.8 - we only need this as return value from "__class_getitem__"
  46. def _GenericAlias(cls, item):
  47. return f"{cls.__name__}[{item.__name__}]"
  48. try:
  49. basestring
  50. except NameError:
  51. basestring = str
  52. try:
  53. unicode
  54. except NameError:
  55. unicode = str
  56. class ElementMaker:
  57. """Element generator factory.
  58. Unlike the ordinary Element factory, the E factory allows you to pass in
  59. more than just a tag and some optional attributes; you can also pass in
  60. text and other elements. The text is added as either text or tail
  61. attributes, and elements are inserted at the right spot. Some small
  62. examples::
  63. >>> from lxml import etree as ET
  64. >>> from lxml.builder import E
  65. >>> ET.tostring(E("tag"))
  66. '<tag/>'
  67. >>> ET.tostring(E("tag", "text"))
  68. '<tag>text</tag>'
  69. >>> ET.tostring(E("tag", "text", key="value"))
  70. '<tag key="value">text</tag>'
  71. >>> ET.tostring(E("tag", E("subtag", "text"), "tail"))
  72. '<tag><subtag>text</subtag>tail</tag>'
  73. For simple tags, the factory also allows you to write ``E.tag(...)`` instead
  74. of ``E('tag', ...)``::
  75. >>> ET.tostring(E.tag())
  76. '<tag/>'
  77. >>> ET.tostring(E.tag("text"))
  78. '<tag>text</tag>'
  79. >>> ET.tostring(E.tag(E.subtag("text"), "tail"))
  80. '<tag><subtag>text</subtag>tail</tag>'
  81. Here's a somewhat larger example; this shows how to generate HTML
  82. documents, using a mix of prepared factory functions for inline elements,
  83. nested ``E.tag`` calls, and embedded XHTML fragments::
  84. # some common inline elements
  85. A = E.a
  86. I = E.i
  87. B = E.b
  88. def CLASS(v):
  89. # helper function, 'class' is a reserved word
  90. return {'class': v}
  91. page = (
  92. E.html(
  93. E.head(
  94. E.title("This is a sample document")
  95. ),
  96. E.body(
  97. E.h1("Hello!", CLASS("title")),
  98. E.p("This is a paragraph with ", B("bold"), " text in it!"),
  99. E.p("This is another paragraph, with a ",
  100. A("link", href="http://www.python.org"), "."),
  101. E.p("Here are some reserved characters: <spam&egg>."),
  102. ET.XML("<p>And finally, here is an embedded XHTML fragment.</p>"),
  103. )
  104. )
  105. )
  106. print ET.tostring(page)
  107. Here's a prettyprinted version of the output from the above script::
  108. <html>
  109. <head>
  110. <title>This is a sample document</title>
  111. </head>
  112. <body>
  113. <h1 class="title">Hello!</h1>
  114. <p>This is a paragraph with <b>bold</b> text in it!</p>
  115. <p>This is another paragraph, with <a href="http://www.python.org">link</a>.</p>
  116. <p>Here are some reserved characters: &lt;spam&amp;egg&gt;.</p>
  117. <p>And finally, here is an embedded XHTML fragment.</p>
  118. </body>
  119. </html>
  120. For namespace support, you can pass a namespace map (``nsmap``)
  121. and/or a specific target ``namespace`` to the ElementMaker class::
  122. >>> E = ElementMaker(namespace="http://my.ns/")
  123. >>> print(ET.tostring( E.test ))
  124. <test xmlns="http://my.ns/"/>
  125. >>> E = ElementMaker(namespace="http://my.ns/", nsmap={'p':'http://my.ns/'})
  126. >>> print(ET.tostring( E.test ))
  127. <p:test xmlns:p="http://my.ns/"/>
  128. """
  129. def __init__(self, typemap=None,
  130. namespace=None, nsmap=None, makeelement=None):
  131. self._namespace = '{' + namespace + '}' if namespace is not None else None
  132. self._nsmap = dict(nsmap) if nsmap else None
  133. assert makeelement is None or callable(makeelement)
  134. self._makeelement = makeelement if makeelement is not None else ET.Element
  135. # initialize the default type map functions for this element factory
  136. typemap = dict(typemap) if typemap else {}
  137. def add_text(elem, item):
  138. try:
  139. last_child = elem[-1]
  140. except IndexError:
  141. elem.text = (elem.text or "") + item
  142. else:
  143. last_child.tail = (last_child.tail or "") + item
  144. def add_cdata(elem, cdata):
  145. if elem.text:
  146. raise ValueError("Can't add a CDATA section. Element already has some text: %r" % elem.text)
  147. elem.text = cdata
  148. if str not in typemap:
  149. typemap[str] = add_text
  150. if unicode not in typemap:
  151. typemap[unicode] = add_text
  152. if ET.CDATA not in typemap:
  153. typemap[ET.CDATA] = add_cdata
  154. def add_dict(elem, item):
  155. attrib = elem.attrib
  156. for k, v in item.items():
  157. if isinstance(v, basestring):
  158. attrib[k] = v
  159. else:
  160. attrib[k] = typemap[type(v)](None, v)
  161. if dict not in typemap:
  162. typemap[dict] = add_dict
  163. self._typemap = typemap
  164. def __call__(self, tag, *children, **attrib):
  165. typemap = self._typemap
  166. # We'll usually get a 'str', and the compiled type check is very fast.
  167. if not isinstance(tag, str) and isinstance(tag, _QName):
  168. # A QName is explicitly qualified, do not look at self._namespace.
  169. tag = tag.text
  170. elif 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. # Allow subscripting ElementMaker in type annotions (PEP 560)
  198. def __class_getitem__(cls, item):
  199. return _GenericAlias(cls, item)
  200. # create factory object
  201. E = ElementMaker()