
    Ng                         d Z ddlmZ ej        ZddlmZ 	 e n# e	$ r e
ZY nw xY w	 e n# e	$ r e
ZY nw xY w G d d          Z e            ZdS )z9
The ``E`` Element factory for generating XML documents.
    Npartialc                   *    e Zd ZdZ	 	 ddZd Z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                    |d|z   dz   nd | _         |rt          |          nd | _        |t          |          sJ ||nt          j        | _        rt                    ni d }d }t          vr
|t          <   t          vr
|t          <   t          j	        vr|t          j	        <   fd}t          vr
|t          <   | _
        d S )N{}c                 ~    	 | d         }|j         pd|z   |_         d S # t          $ r | j        pd|z   | _        Y d S w xY w)N )tail
IndexErrortext)elemitem
last_childs      H/var/www/html/ai-engine/env/lib/python3.11/site-packages/lxml/builder.pyadd_textz'ElementMaker.__init__.<locals>.add_text   s_    A!"X
 $.?#8bD"@
  5 5 5!Y_"4				5s    <<c                 P    | j         rt          d| j         z            || _         d S )Nz<Can't add a CDATA section. Element already has some text: %r)r   
ValueError)r   cdatas     r   	add_cdataz(ElementMaker.__init__.<locals>.add_cdata   s0    y m !_bfbk!klllDIII    c                     | j         }|                                D ]B\  }}t          |t                    r|||<     t	          |                   d |          ||<   Cd S N)attribitems
isinstance
basestringtype)r   r   r   kvtypemaps        r   add_dictz'ElementMaker.__init__.<locals>.add_dict   sn    [F

 : :1a,, : !F1II 0Q 0q 9 9F1II	: :r   )
_namespacedict_nsmapcallableETElement_makeelementstrunicodeCDATA_typemap)selfr#   	namespacensmapmakeelementr   r   r$   s    `      r   __init__zElementMaker.__init__   s   3<3H#	/C//d%*4d5kkk"h{&;&;"""+6+BKK
 $+2$w---	A 	A 	A	 	 	
 g#GCL'!!'GG87"" )GBH	: 	: 	: 	: 	: w$GDMr   c                    | j         }t          |t                    st          |t                    r|j        }n| j        |d         dk    r
| j        |z   }|                     || j                  }|r |t                   ||           |D ]}t          |          r
 |            }|
                    t          |                    }|t          j        |          r|                    |           it          |          j        D ]}|
                    |          }| n)t!          dt          |          j        d|d           |||          }	|	r, |
                    t          |	                    ||	           |S )Nr   r   )r2   zbad argument type: ())r/   r   r,   _QNamer   r%   r+   r'   r&   r(   getr    r)   	iselementappend__mro__	TypeError__name__)
r0   tagchildrenr   r#   r   r   tbasetyper"   s
             r   __call__zElementMaker.__call__   s   - #s## 	(
3(?(? 	((CC_(SVs]]/C'C  DK 88 	(GDM$''' 	. 	.D~~ tvvDJJ''Ay<%% KK%%% $T

 2 A AHH--A} % $)%)$ZZ%8%8%8$$$%@ A A A$A .$DGG$$T1---r   c                 "    t          | |          S r   r   )r0   r?   s     r   __getattr__zElementMaker.__getattr__   s    tS!!!r   )NNNN)r>   
__module____qualname____doc__r4   rC   rE    r   r   r   r   ;   s]        V Vp  $9=*  *  *  * X! ! !F" " " " "r   r   )rH   
lxml.etreeetreer)   QNamer8   	functoolsr   r   	NameErrorr,   r-   r   ErI   r   r   <module>rP      s   L 
      	      JJ   JJJGG   GGGi" i" i" i" i" i" i" i"Z LNNs    $$+ 55