ó
'°	Tc           @   sÒ   d  Z  d d l j Z y d d l m Z Wn e k
 rE d „  Z n Xy e Wn e k
 rj d „  Z n Xy e	 Wn e k
 rŒ e
 Z	 n Xy e Wn e k
 r® e
 Z n Xd e f d „  ƒ  YZ e ƒ  Z d S(   s9   
The ``E`` Element factory for generating XML documents.
iÿÿÿÿN(   t   partialc            s   ‡  ‡ f d †  S(   Nc             s   ˆ  ˆ |  | Ž S(   N(    (   t   argst   kwargs(   t   funct   tag(    s0   /usr/lib/python2.7/dist-packages/lxml/builder.pyt   <lambda>/   s    (    (   R   R   (    (   R   R   s0   /usr/lib/python2.7/dist-packages/lxml/builder.pyR    .   s    c         C   s   t  |  d ƒ S(   Nt   __call__(   t   hasattr(   t   f(    (    s0   /usr/lib/python2.7/dist-packages/lxml/builder.pyt   callable5   s    t   ElementMakerc           B   s5   e  Z d  Z d d d d d „ Z d „  Z d „  Z RS(   sg  Element generator factory.

    Unlike the ordinary Element factory, the E factory allows you to pass in
    more than just a tag and some optional attributes; you can also pass in
    text and other elements.  The text is added as either text or tail
    attributes, and elements are inserted at the right spot.  Some small
    examples::

        >>> from lxml import etree as ET
        >>> from lxml.builder import E

        >>> ET.tostring(E("tag"))
        '<tag/>'
        >>> ET.tostring(E("tag", "text"))
        '<tag>text</tag>'
        >>> ET.tostring(E("tag", "text", key="value"))
        '<tag key="value">text</tag>'
        >>> ET.tostring(E("tag", E("subtag", "text"), "tail"))
        '<tag><subtag>text</subtag>tail</tag>'

    For simple tags, the factory also allows you to write ``E.tag(...)`` instead
    of ``E('tag', ...)``::

        >>> ET.tostring(E.tag())
        '<tag/>'
        >>> ET.tostring(E.tag("text"))
        '<tag>text</tag>'
        >>> ET.tostring(E.tag(E.subtag("text"), "tail"))
        '<tag><subtag>text</subtag>tail</tag>'

    Here's a somewhat larger example; this shows how to generate HTML
    documents, using a mix of prepared factory functions for inline elements,
    nested ``E.tag`` calls, and embedded XHTML fragments::

        # some common inline elements
        A = E.a
        I = E.i
        B = E.b

        def CLASS(v):
            # helper function, 'class' is a reserved word
            return {'class': v}

        page = (
            E.html(
                E.head(
                    E.title("This is a sample document")
                ),
                E.body(
                    E.h1("Hello!", CLASS("title")),
                    E.p("This is a paragraph with ", B("bold"), " text in it!"),
                    E.p("This is another paragraph, with a ",
                        A("link", href="http://www.python.org"), "."),
                    E.p("Here are some reservered characters: <spam&egg>."),
                    ET.XML("<p>And finally, here is an embedded XHTML fragment.</p>"),
                )
            )
        )

        print ET.tostring(page)

    Here's a prettyprinted version of the output from the above script::

        <html>
          <head>
            <title>This is a sample document</title>
          </head>
          <body>
            <h1 class="title">Hello!</h1>
            <p>This is a paragraph with <b>bold</b> text in it!</p>
            <p>This is another paragraph, with <a href="http://www.python.org">link</a>.</p>
            <p>Here are some reservered characters: &lt;spam&amp;egg&gt;.</p>
            <p>And finally, here is an embedded XHTML fragment.</p>
          </body>
        </html>

    For namespace support, you can pass a namespace map (``nsmap``)
    and/or a specific target ``namespace`` to the ElementMaker class::

        >>> E = ElementMaker(namespace="http://my.ns/")
        >>> print(ET.tostring( E.test ))
        <test xmlns="http://my.ns/"/>

        >>> E = ElementMaker(namespace="http://my.ns/", nsmap={'p':'http://my.ns/'})
        >>> print(ET.tostring( E.test ))
        <p:test xmlns:p="http://my.ns/"/>
    c            s3  | d  k	 r  d | d |  _ n	 d  |  _ | rA t | ƒ |  _ n	 d  |  _ | d  k	 rt t | ƒ sh t ‚ | |  _ n t j |  _ ˆ  r• ˆ  j	 ƒ  ‰  n i  ‰  d „  } d „  } t
 ˆ  k rÆ | ˆ  t
 <n  t ˆ  k rß | ˆ  t <n  t j ˆ  k rþ | ˆ  t j <n  ‡  f d †  } t ˆ  k r&| ˆ  t <n  ˆ  |  _ d  S(   Nt   {t   }c         S   sP   y" |  d j  p d | |  d _  Wn' t k
 rK |  j p> d | |  _ n Xd  S(   Niÿÿÿÿt    (   t   tailt
   IndexErrort   text(   t   elemt   item(    (    s0   /usr/lib/python2.7/dist-packages/lxml/builder.pyt   add_textµ   s    "c         S   s,   |  j  r t d |  j  ƒ ‚ n  | |  _  d  S(   Ns<   Can't add a CDATA section. Element already has some text: %r(   R   t
   ValueError(   R   t   cdata(    (    s0   /usr/lib/python2.7/dist-packages/lxml/builder.pyt	   add_cdata»   s    	c            sc   |  j  } xS | j ƒ  D]E \ } } t | t ƒ r> | | | <q ˆ  t | ƒ d  | ƒ | | <q Wd  S(   N(   t   attribt   itemst
   isinstancet
   basestringt   typet   None(   R   R   R   t   kt   v(   t   typemap(    s0   /usr/lib/python2.7/dist-packages/lxml/builder.pyt   add_dictÇ   s
    	(   R   t
   _namespacet   dictt   _nsmapR	   t   AssertionErrort   _makeelementt   ETt   Elementt   copyt   strt   unicodet   CDATAt   _typemap(   t   selfR   t	   namespacet   nsmapt   makeelementR   R   R    (    (   R   s0   /usr/lib/python2.7/dist-packages/lxml/builder.pyt   __init__œ   s2    				c   
      O   se  |  j  j } |  j d  k	 r; | d d k r; |  j | } n  |  j | d |  j ƒ} | ro | t ƒ | | ƒ n  xï | D]ç } t | ƒ r” | ƒ  } n  | t | ƒ ƒ } | d  k r,t	 j
 | ƒ rÔ | j | ƒ qv n  xU t | ƒ j D]" } | | ƒ } | d  k	 rä Pqä qä Wt d t | ƒ j | f ƒ ‚ n  | | | ƒ }	 |	 rv | t |	 ƒ ƒ | |	 ƒ qv qv W| S(   Ni    R   R/   s   bad argument type: %s(%r)(   R,   t   getR!   R   R%   R#   R"   R	   R   R&   t	   iselementt   appendt   __mro__t	   TypeErrort   __name__(
   R-   R   t   childrenR   R2   R   R   t   tt   basetypeR   (    (    s0   /usr/lib/python2.7/dist-packages/lxml/builder.pyR   Ó   s0     c         C   s   t  |  | ƒ S(   N(   R    (   R-   R   (    (    s0   /usr/lib/python2.7/dist-packages/lxml/builder.pyt   __getattr__ò   s    N(   R7   t
   __module__t   __doc__R   R1   R   R;   (    (    (    s0   /usr/lib/python2.7/dist-packages/lxml/builder.pyR
   C   s
   W6	(   R=   t
   lxml.etreet   etreeR&   t	   functoolsR    t   ImportErrorR	   t	   NameErrorR   R)   R*   t   objectR
   t   E(    (    (    s0   /usr/lib/python2.7/dist-packages/lxml/builder.pyt   <module>&   s&   

³