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""" 37The ``E`` Element factory for generating XML documents. 38""" 39 40importlxml.etreeasET 41 42fromfunctoolsimportpartial 43 44try: 45basestring 46exceptNameError: 47basestring=str 48 49try: 50unicode 51exceptNameError: 52unicode=str 53 54
56"""Element generator factory. 57 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 64 >>> from lxml import etree as ET 65 >>> from lxml.builder import E 66 67 >>> ET.tostring(E("tag")) 68 '<tag/>' 69 >>> ET.tostring(E("tag", "text")) 70 '<tag>text</tag>' 71 >>> ET.tostring(E("tag", "text", key="value")) 72 '<tag key="value">text</tag>' 73 >>> ET.tostring(E("tag", E("subtag", "text"), "tail")) 74 '<tag><subtag>text</subtag>tail</tag>' 75 76 For simple tags, the factory also allows you to write ``E.tag(...)`` instead 77 of ``E('tag', ...)``:: 78 79 >>> ET.tostring(E.tag()) 80 '<tag/>' 81 >>> ET.tostring(E.tag("text")) 82 '<tag>text</tag>' 83 >>> ET.tostring(E.tag(E.subtag("text"), "tail")) 84 '<tag><subtag>text</subtag>tail</tag>' 85 86 Here's a somewhat larger example; this shows how to generate HTML 87 documents, using a mix of prepared factory functions for inline elements, 88 nested ``E.tag`` calls, and embedded XHTML fragments:: 89 90 # some common inline elements 91 A = E.a 92 I = E.i 93 B = E.b 94 95 def CLASS(v): 96 # helper function, 'class' is a reserved word 97 return {'class': v} 98 99 page = (100 E.html(101 E.head(102 E.title("This is a sample document")103 ),104 E.body(105 E.h1("Hello!", CLASS("title")),106 E.p("This is a paragraph with ", B("bold"), " text in it!"),107 E.p("This is another paragraph, with a ",108 A("link", href="http://www.python.org"), "."),109 E.p("Here are some reserved characters: <spam&egg>."),110 ET.XML("<p>And finally, here is an embedded XHTML fragment.</p>"),111 )112 )113 )114115 print ET.tostring(page)116117 Here's a prettyprinted version of the output from the above script::118119 <html>120 <head>121 <title>This is a sample document</title>122 </head>123 <body>124 <h1 class="title">Hello!</h1>125 <p>This is a paragraph with <b>bold</b> text in it!</p>126 <p>This is another paragraph, with <a href="http://www.python.org">link</a>.</p>127 <p>Here are some reserved characters: <spam&egg>.</p>128 <p>And finally, here is an embedded XHTML fragment.</p>129 </body>130 </html>131132 For namespace support, you can pass a namespace map (``nsmap``)133 and/or a specific target ``namespace`` to the ElementMaker class::134135 >>> E = ElementMaker(namespace="http://my.ns/")136 >>> print(ET.tostring( E.test ))137 <test xmlns="http://my.ns/"/>138139 >>> E = ElementMaker(namespace="http://my.ns/", nsmap={'p':'http://my.ns/'})140 >>> print(ET.tostring( E.test ))141 <p:test xmlns:p="http://my.ns/"/>142 """143
174175defadd_cdata(elem,cdata):176ifelem.text:177raiseValueError("Can't add a CDATA section. Element already has some text: %r"%elem.text)178elem.text=cdata
200typemap=self._typemap201202ifself._namespaceisnotNoneandtag[0]!='{':203tag=self._namespace+tag204elem=self._makeelement(tag,nsmap=self._nsmap)205ifattrib:206typemap[dict](elem,attrib)207208foriteminchildren:209ifcallable(item):210item=item()211t=typemap.get(type(item))212iftisNone:213ifET.iselement(item):214elem.append(item)215continue216forbasetypeintype(item).__mro__:217# See if the typemap knows of any of this type's bases.218t=typemap.get(basetype)219iftisnotNone:220break221else:222raiseTypeError("bad argument type: %s(%r)"%223(type(item).__name__,item))224v=t(elem,item)225ifv:226typemap.get(type(v))(elem,v)227228returnelem