a
    %ñ»j"  ã                   @   s¦   d Z ddlmZ ejZddlmZ zddlm	Z
 W n eyL   dd„ Z
Y n0 ze W n eyl   eZY n0 ze W n eyŒ   eZY n0 G dd„ dƒZeƒ ZdS )	z9
The ``E`` Element factory for generating XML documents.
é    N©Úpartial)ÚGenericAliasc                 C   s   | j › d|j › d�S )Nú[ú])Ú__name__©ÚclsÚitem© r   ú2E:\soccer\ml_env\Lib\site-packages\lxml/builder.pyÚ_GenericAlias4   s    r   c                   @   s2   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ ZdS )ÚElementMakerac  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 reserved 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 reserved 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/"/>
    Nc                    sÎ   |d urd| d nd | _ |r&t|ƒnd | _|d u s@t|ƒs@J ‚|d urL|ntj| _ˆ r`tˆ ƒni ‰ dd„ }dd„ }tˆ vr„|ˆ t< tˆ vr”|ˆ t< tj	ˆ vr¨|ˆ tj	< ‡ fdd„}tˆ vrÄ|ˆ t< ˆ | _
d S )	NÚ{Ú}c                 S   sD   z| d }W n" t y.   | jp"d| | _Y n0 |jp8d| |_d S )NéÿÿÿÿÚ )Ú
IndexErrorÚtextÚtail)Úelemr
   Z
last_childr   r   r   Úadd_text¦   s
    z'ElementMaker.__init__.<locals>.add_textc                 S   s   | j rtd| j  ƒ‚|| _ d S )Nz<Can't add a CDATA section. Element already has some text: %r)r   Ú
ValueError)r   Úcdatar   r   r   Ú	add_cdata®   s    z(ElementMaker.__init__.<locals>.add_cdatac                    sF   | j }| ¡ D ]2\}}t|tƒr*|||< qˆ t|ƒ d |ƒ||< qd S ©N)ÚattribÚitemsÚ
isinstanceÚ
basestringÚtype)r   r
   r   ÚkÚv©Útypemapr   r   Úadd_dictº   s
    

z'ElementMaker.__init__.<locals>.add_dict)Ú
_namespaceÚdictÚ_nsmapÚcallableÚETZElementÚ_makeelementÚstrÚunicodeZCDATAÚ_typemap)Úselfr$   Ú	namespaceÚnsmapZmakeelementr   r   r%   r   r#   r   Ú__init__›   s"    

zElementMaker.__init__c           
      O   s
  | j }t|tƒs"t|tƒr"|j}n | jd urB|d dkrB| j| }| j|| jd�}|rd|t ||ƒ |D ]œ}t	|ƒrz|ƒ }| 
t|ƒ¡}|d u rât |¡r¦| |¡ qht|ƒjD ]}| 
|¡}|d ur° qâq°tdt|ƒj|f ƒ‚|||ƒ}	|	rh| 
t|	ƒ¡||	ƒ qh|S )Nr   r   )r1   zbad argument type: %s(%r))r.   r   r,   Ú_QNamer   r&   r+   r(   r'   r)   Úgetr    r*   Z	iselementÚappendÚ__mro__Ú	TypeErrorr   )
r/   ÚtagÚchildrenr   r$   r   r
   ÚtZbasetyper"   r   r   r   Ú__call__Ç   s6    



ÿ
zElementMaker.__call__c                 C   s
   t | |ƒS r   r   )r/   r8   r   r   r   Ú__getattr__ê   s    zElementMaker.__getattr__c                 C   s
   t | |ƒS r   )r   r   r   r   r   Ú__class_getitem__î   s    zElementMaker.__class_getitem__)NNNN)r   Ú
__module__Ú__qualname__Ú__doc__r2   r;   r<   r=   r   r   r   r   r   B   s   X  ÿ
,#r   )r@   Z
lxml.etreeÚetreer*   ZQNamer3   Ú	functoolsr   Útypesr   r   ÚImportErrorr   Ú	NameErrorr,   r-   r   ÚEr   r   r   r   Ú<module>&   s$   

 2