lxml.builder

Package lxml :: Module builder |
[hide private]
[frames] | no frames]

# Source Code for Module lxml.builder

      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  """
     37  The ``E`` Element factory for generating XML documents.
     38  """
     39
     40  import lxml.etree as ET
     41
     42  try:
     43      from functools import partial
     44  except ImportError:
     45      # fake it for pre-2.5 releases
     46 -    def partial(func, tag):
     47          return lambda *args, **kwargs: func(tag, *args, **kwargs)
     48
     49  try:
     50      callable
     51  except NameError:
     52      # Python 3
     53 -    def callable(f):
     54          return hasattr(f, '__call__')
     55
     56  try:
     57      basestring = __builtins__["basestring"]
     58  except (NameError, KeyError):
     59      basestring = str
     60
     61  try:
     62      unicode = __builtins__["unicode"]
     63  except (NameError, KeyError):
     64      unicode = str
     65
     66
     67 -class ElementMaker(object):
     68      """Element generator factory.
     69
     70      Unlike the ordinary Element factory, the E factory allows you to pass in
     71      more than just a tag and some optional attributes; you can also pass in
     72      text and other elements.  The text is added as either text or tail
     73      attributes, and elements are inserted at the right spot.  Some small
     74      examples::
     75
     76          >>> from lxml import etree as ET
     77          >>> from lxml.builder import E
     78
     79          >>> ET.tostring(E("tag"))
     80          '<tag/>'
     81          >>> ET.tostring(E("tag", "text"))
     82          '<tag>text</tag>'
     83          >>> ET.tostring(E("tag", "text", key="value"))
     84          '<tag key="value">text</tag>'
     85          >>> ET.tostring(E("tag", E("subtag", "text"), "tail"))
     86          '<tag><subtag>text</subtag>tail</tag>'
     87
     88      For simple tags, the factory also allows you to write ``E.tag(...)`` instead
     89      of ``E('tag', ...)``::
     90
     91          >>> ET.tostring(E.tag())
     92          '<tag/>'
     93          >>> ET.tostring(E.tag("text"))
     94          '<tag>text</tag>'
     95          >>> ET.tostring(E.tag(E.subtag("text"), "tail"))
     96          '<tag><subtag>text</subtag>tail</tag>'
     97
     98      Here's a somewhat larger example; this shows how to generate HTML
     99      documents, using a mix of prepared factory functions for inline elements,
    100      nested ``E.tag`` calls, and embedded XHTML fragments::
    101
    102          # some common inline elements
    103          A = E.a
    104          I = E.i
    105          B = E.b
    106
    107          def CLASS(v):
    108              # helper function, 'class' is a reserved word
    109              return {'class': v}
    110
    111          page = (
    112              E.html(
    113                  E.head(
    114                      E.title("This is a sample document")
    115                  ),
    116                  E.body(
    117                      E.h1("Hello!", CLASS("title")),
    118                      E.p("This is a paragraph with ", B("bold"), " text in it!"),
    119                      E.p("This is another paragraph, with a ",
    120                          A("link", href="http://www.python.org"), "."),
    121                      E.p("Here are some reservered characters: <spam&egg>."),
    122                      ET.XML("<p>And finally, here is an embedded XHTML fragment.</p>"),
    123                  )
    124              )
    125          )
    126
    127          print ET.tostring(page)
    128
    129      Here's a prettyprinted version of the output from the above script::
    130
    131          <html>
    132            <head>
    133              <title>This is a sample document</title>
    134            </head>
    135            <body>
    136              <h1 class="title">Hello!</h1>
    137              <p>This is a paragraph with <b>bold</b> text in it!</p>
    138              <p>This is another paragraph, with <a href="http://www.python.org">link</a>.</p>
    139              <p>Here are some reservered characters: &lt;spam&amp;egg&gt;.</p>
    140              <p>And finally, here is an embedded XHTML fragment.</p>
    141            </body>
    142          </html>
    143
    144      For namespace support, you can pass a namespace map (``nsmap``)
    145      and/or a specific target ``namespace`` to the ElementMaker class::
    146
    147      >>> E = ElementMaker(namespace="http://my.ns/")
    148      >>> print(ET.tostring( E.test ))
    149      <test xmlns="http://my.ns/"/>
    150
    151      >>> E = ElementMaker(namespace="http://my.ns/", nsmap={'p':'http://my.ns/'})
    152      >>> print(ET.tostring( E.test ))
    153      <p:test xmlns:p="http://my.ns/"/>
    154      """
    155
    156 -    def __init__(self, typemap=None,
    157                   namespace=None, nsmap=None, makeelement=None):
    158          if namespace is not None:
    159              self._namespace = '{' + namespace + '}'
    160          else:
    161              self._namespace = None
    162
    163          if nsmap:
    164              self._nsmap = dict(nsmap)
    165          else:
    166              self._nsmap = None
    167
    168          if makeelement is not None:
    169              assert callable(makeelement)
    170              self._makeelement = makeelement
    171          else:
    172              self._makeelement = ET.Element
    173
    174          # initialize type map for this element factory
    175
    176          if typemap:
    177              typemap = typemap.copy()
    178          else:
    179              typemap = {}
    180
    181          def add_text(elem, item):
    182              if len(elem):
    183                  elem[-1].tail = (elem[-1].tail or "") + item
    184              else:
    185                  elem.text = (elem.text or "") + item
    186          if str not in typemap:
    187              typemap[str] = add_text
    188          if unicode not in typemap:
    189              typemap[unicode] = add_text
    190
    191          def add_dict(elem, item):
    192              attrib = elem.attrib
    193              for k, v in item.items():
    194                  if isinstance(v, basestring):
    195                      attrib[k] = v
    196                  else:
    197                      attrib[k] = typemap[type(v)](None, v)
    198          if dict not in typemap:
    199              typemap[dict] = add_dict
    200
    201          self._typemap = typemap
    202
    203 -    def __call__(self, tag, *children, **attrib):
    204          get = self._typemap.get
    205
    206          if self._namespace is not None and tag[0] != '{':
    207              tag = self._namespace + tag
    208          elem = self._makeelement(tag, nsmap=self._nsmap)
    209          if attrib:
    210              get(dict)(elem, attrib)
    211
    212          for item in children:
    213              if callable(item):
    214                  item = item()
    215              t = get(type(item))
    216              if t is None:
    217                  if ET.iselement(item):
    218                      elem.append(item)
    219                      continue
    220                  raise TypeError("bad argument type: %r" % item)
    221              else:
    222                  v = t(elem, item)
    223                  if v:
    224                      get(type(v))(elem, v)
    225
    226          return elem
    227
    228 -    def __getattr__(self, tag):
    229          return partial(self, tag)
    230
    231  # create factory object
    232  E = ElementMaker()
    233