a
    ¥Q•hB5  ã                   @   sV   d dl mZ d dlmZ d dlmZ ddlmZmZ G dd„ dƒZ	dd	„ Z
d
d„ ZdS )é    )Úcached_property)Úwarn)Úascii_loweré   )Úcompile_selector_listÚsplit_whitespacec                   @   s$  e Zd ZdZed:dd„ƒZed;dd„ƒZed<dd	„ƒZd=d
d„Zdd„ Z	dd„ Z
dd„ Zdd„ Zedd„ ƒZedd„ ƒZdd„ Zdd„ Zdd„ Zdd„ Zd d!„ Zd"d#„ Zed$d%„ ƒZd&d'„ Zd(d)„ Zd*d+„ Zed,d-„ ƒZed.d/„ ƒZed0d1„ ƒZed2d3„ ƒZed4d5„ ƒZ ed6d7„ ƒZ!ed8d9„ ƒZ"dS )>ÚElementWrapperaí  Wrapper of :class:`xml.etree.ElementTree.Element` for Selector matching.

    This class should not be instanciated directly. :meth:`from_xml_root` or
    :meth:`from_html_root` should be used for the root element of a document,
    and other elements should be accessed (and wrappers generated) using
    methods such as :meth:`iter_children` and :meth:`iter_subtree`.

    :class:`ElementWrapper` objects compare equal if their underlying
    :class:`xml.etree.ElementTree.Element` do.

    Nc                 C   s   | j ||dd�S )a.  Wrap for selector matching the root of an XML or XHTML document.

        :param root:
            An ElementTree :class:`xml.etree.ElementTree.Element`
            for the root element of a document.
            If the given element is not the root,
            selector matching will behave is if it were.
            In other words, selectors will be not be `scoped`_
            to the subtree rooted at that element.
        :returns:
            A new :class:`ElementWrapper`

        .. _scoped: https://drafts.csswg.org/selectors-4/#scoping

        F©Úin_html_document©Ú
_from_root©ÚclsÚrootÚcontent_language© r   úH/var/www/sistema_ama/venv/lib/python3.9/site-packages/cssselect2/tree.pyÚfrom_xml_root   s    zElementWrapper.from_xml_rootc                 C   s   | j ||dd�S )zæSame as :meth:`from_xml_root` with case-insensitive attribute names.

        Useful for documents parsed with an HTML parser like html5lib, which
        should be the case of documents with the ``text/html`` MIME type.

        Tr	   r   r   r   r   r   Úfrom_html_root(   s    zElementWrapper.from_html_rootTc                 C   s&   t |dƒr| ¡ }| |d dd ||d�S )NÚgetrootr   )ÚparentÚindexÚpreviousr
   r   )Úhasattrr   )r   r   r   r
   r   r   r   r   2   s    
þzElementWrapper._from_rootc                 C   sN   || _ || _|| _|d ur$|j| _n|g| _|| _|| _|| _d | _d | _	d S ©N)
Úetree_elementr   r   Úetree_childrenZetree_siblingsr   r
   Útransport_content_languageÚ
_ancestorsÚ_previous_siblings)Úselfr   r   r   r   r
   r   r   r   r   Ú__init__:   s    
zElementWrapper.__init__c                 C   s   t | ƒt |ƒu o| j|jkS r   )Útyper   ©r    Úotherr   r   r   Ú__eq__V   s    
þzElementWrapper.__eq__c                 C   s
   | |k S r   r   r#   r   r   r   Ú__ne__[   s    zElementWrapper.__ne__c                 C   s   t t| ƒ| jfƒS r   )Úhashr"   r   ©r    r   r   r   Ú__hash__^   s    zElementWrapper.__hash__c                 c   s   |   ¡ E d H  d S r   )Úiter_childrenr(   r   r   r   Ú__iter__a   s    zElementWrapper.__iter__c                 C   s4   | j du r.| jdu rdng | jj¢| j‘R | _ | j S )uÂ   Tuple of existing ancestors.

        Tuple of existing :class:`ElementWrapper` objects for this elementâ€™s
        ancestors, in reversed tree order, from :attr:`parent` to the root.

        Nr   )r   r   Ú	ancestorsr(   r   r   r   r,   d   s    
 ÿzElementWrapper.ancestorsc                 C   s4   | j du r.| jdu rdng | jj¢| j‘R | _ | j S )u¨   Tuple of previous siblings.

        Tuple of existing :class:`ElementWrapper` objects for this elementâ€™s
        previous siblings, in reversed tree order.

        Nr   )r   r   Úprevious_siblingsr(   r   r   r   r-   q   s
    
þz ElementWrapper.previous_siblingsc                 c   s   t dtƒ | jE dH  dS )u¨  Iterate over ancestors.

        Return an iterator of existing :class:`ElementWrapper` objects for this
        elementâ€™s ancestors, in reversed tree order (from :attr:`parent` to the
        root).

        The element itself is not included, this is an empty sequence for the
        root element.

        This method is deprecated and will be removed in version 0.7.0. Use
        :attr:`ancestors` instead.

        zfThis method is deprecated and will be removed in version 0.7.0. Use the "ancestors" attribute instead.N)r   ÚDeprecationWarningr,   r(   r   r   r   Úiter_ancestors   s
    ýzElementWrapper.iter_ancestorsc                 c   s   t dtƒ | jE dH  dS )u§  Iterate over previous siblings.

        Return an iterator of existing :class:`ElementWrapper` objects for this
        elementâ€™s previous siblings, in reversed tree order.

        The element itself is not included, this is an empty sequence for a
        first child or the root element.

        This method is deprecated and will be removed in version 0.7.0. Use
        :attr:`previous_siblings` instead.

        znThis method is deprecated and will be removed in version 0.7.0. Use the "previous_siblings" attribute instead.N)r   r.   r-   r(   r   r   r   Úiter_previous_siblings“   s
    ýz%ElementWrapper.iter_previous_siblingsc                 c   s&   | j du r| V  n| j  ¡ E dH  dS )u£   Iterate over siblings.

        Return an iterator of newly-created :class:`ElementWrapper` objects for
        this elementâ€™s siblings, in tree order.

        N)r   r*   r(   r   r   r   Úiter_siblings¦   s    
zElementWrapper.iter_siblingsc                 c   s,   d}|   ¡ D ]}|r|V  || krd}qdS )u­   Iterate over next siblings.

        Return an iterator of newly-created :class:`ElementWrapper` objects for
        this elementâ€™s next siblings, in tree order.

        FTN)r1   )r    ÚfoundÚsiblingr   r   r   Úiter_next_siblings²   s    z!ElementWrapper.iter_next_siblingsc                 c   s:   d}t | jƒD ]&\}}t| ƒ|| ||| jd�}|V  qdS )u©   Iterate over children.

        Return an iterator of newly-created :class:`ElementWrapper` objects for
        this elementâ€™s child elements, in tree order.

        N)r   r   r   r
   )Ú	enumerater   r"   r
   )r    ÚchildÚiZetree_childr   r   r   r*   À   s    þzElementWrapper.iter_childrenc                 c   sJ   t | gƒg}|rFt|d dƒ}|du r0| ¡  q|V  | | ¡ ¡ qdS )a¨  Iterate over subtree.

        Return an iterator of newly-created :class:`ElementWrapper` objects for
        the entire subtree rooted at this element, in tree order.

        Unlike in other methods, the element itself *is* included.

        This loops over an entire document:

        .. code-block:: python

            for element in ElementWrapper.from_root(root_etree).iter_subtree():
                ...

        éÿÿÿÿN)ÚiterÚnextÚpopÚappendr*   )r    ÚstackÚelementr   r   r   Úiter_subtreeÎ   s    
zElementWrapper.iter_subtreec                 C   s   dd„ | D ƒS )Nc                 S   s@   g | ]8}t |d ƒr|gnt|ƒD ]}|jdu r |js |j‘q qS )ÚtestN)r   r   Úpseudo_elementÚnever_matchesr@   )Ú.0ÚselectorZcompiled_selectorr   r   r   Ú
<listcomp>é   s   
þ
úz+ElementWrapper._compile.<locals>.<listcomp>r   )Ú	selectorsr   r   r   Ú_compileç   s    þzElementWrapper._compilec                    s   t ‡ fdd„ˆ  |¡D ƒƒS )zðReturn wether this elememt matches any of the given selectors.

        :param selectors:
            Each given selector is either a :class:`compiler.CompiledSelector`,
            or an argument to :func:`compile_selector_list`.

        c                 3   s   | ]}|ˆ ƒV  qd S r   r   ©rC   r@   r(   r   r   Ú	<genexpr>ú   ó    z)ElementWrapper.matches.<locals>.<genexpr>)ÚanyrG   ©r    rF   r   r(   r   Úmatchesò   s    zElementWrapper.matchesc                    sN   |   |¡‰ tˆ ƒdkr(tˆ d |  ¡ ƒS |rB‡ fdd„|  ¡ D ƒS tdƒS dS )aÛ  Return elements, in tree order, that match any of given selectors.

        Selectors are `scoped`_ to the subtree rooted at this element.

        .. _scoped: https://drafts.csswg.org/selectors-4/#scoping

        :param selectors:
            Each given selector is either a :class:`compiler.CompiledSelector`,
            or an argument to :func:`compile_selector_list`.
        :returns:
            An iterator of newly-created :class:`ElementWrapper` objects.

        r   r   c                 3   s(   | ] ‰ t ‡ fd d„ˆD ƒƒrˆ V  qdS )c                 3   s   | ]}|ˆ ƒV  qd S r   r   rH   ©r>   r   r   rI     rJ   z5ElementWrapper.query_all.<locals>.<genexpr>.<genexpr>N)rK   )rC   ©ÚtestsrN   r   rI     s   ÿz+ElementWrapper.query_all.<locals>.<genexpr>r   N)rG   ÚlenÚfilterr?   r9   rL   r   rO   r   Ú	query_allü   s    

ÿzElementWrapper.query_allc                 G   s   t | j|Ž dƒS )aj  Return first element that matches any of given selectors.

        :param selectors:
            Each given selector is either a :class:`compiler.CompiledSelector`,
            or an argument to :func:`compile_selector_list`.
        :returns:
            A newly-created :class:`ElementWrapper` object,
            or :obj:`None` if there is no match.

        N)r:   rS   rL   r   r   r   Úquery  s    zElementWrapper.queryc                 C   s   dd„ | j D ƒS )a%  Children as a list of :class:`xml.etree.ElementTree.Element`.

        Other ElementTree nodes such as
        :func:`comments <xml.etree.ElementTree.Comment>` and
        :func:`processing instructions
        <xml.etree.ElementTree.ProcessingInstruction>`
        are not included.

        c                 S   s   g | ]}t |jtƒr|‘qS r   )Ú
isinstanceÚtagÚstr)rC   r>   r   r   r   rE   ,  s   ÿz1ElementWrapper.etree_children.<locals>.<listcomp>)r   r(   r   r   r   r   !  s    ÿzElementWrapper.etree_childrenc                 C   s"   t | jjƒ\}}|| jtdƒ< |S )z,The local name of this element, as a string.Únamespace_url©Ú_split_etree_tagr   rV   Ú__dict__rW   ©r    rX   Ú
local_namer   r   r   r]   0  s    zElementWrapper.local_namec                 C   s"   t | jjƒ\}}|| jtdƒ< |S )z/The namespace URL of this element, as a string.r]   rY   r\   r   r   r   rX   7  s    zElementWrapper.namespace_urlc                 C   s   | j  d¡S )z$The ID of this element, as a string.Úid)r   Úgetr(   r   r   r   r^   >  s    zElementWrapper.idc                 C   s   t t| j dd¡ƒƒS )z:The classes of this element, as a :class:`set` of strings.ÚclassÚ )Úsetr   r   r_   r(   r   r   r   ÚclassesC  s    zElementWrapper.classesc                 C   sÀ   | j  d¡}|durt|ƒS | jp*| jdk}|rL| j  d¡}|durLt|ƒS | jdur^| jjS |r²d}| j  d¡}|D ]*}| dd¡}t|ƒdkrvt| d	¡ƒ}qv|dur²t|ƒS t| j	ƒp¾dS )
z*The language of this element, as a string.z*{http://www.w3.org/XML/1998/namespace}langNzhttp://www.w3.org/1999/xhtmlÚlangz"{http://www.w3.org/1999/xhtml}metaz
http-equivra   zcontent-languageÚcontent)
r   r_   r   r
   rX   r   rd   r9   Ú_parse_content_languager   )r    Zxml_langZis_htmlrd   r   ÚiteratorÚmetaZ
http_equivr   r   r   rd   H  s,    þ
zElementWrapper.langc                    sh   | j d u rdS d}d‰ | j jj|koZ| j j d¡d uoZ| jjˆ kpZt‡ fdd„|  ¡ D ƒƒ}|pf| j jS )NFz&{http://www.w3.org/1999/xhtml}fieldsetz${http://www.w3.org/1999/xhtml}legendÚdisabledc                 3   s   | ]}|j jˆ kV  qd S r   )r   rV   )rC   r3   ©Úlegendr   r   rI   n  s   ÿz6ElementWrapper.in_disabled_fieldset.<locals>.<genexpr>)r   r   rV   r_   rK   r0   Úin_disabled_fieldset)r    ÚfieldsetZdisabled_fieldsetr   rj   r   rl   e  s    
ÿþýz#ElementWrapper.in_disabled_fieldset)N)N)T)N)#Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úclassmethodr   r   r   r!   r%   r&   r)   r+   Úpropertyr,   r-   r/   r0   r1   r4   r*   r?   ÚstaticmethodrG   rM   rS   rT   r   r   r]   rX   r^   rc   rd   rl   r   r   r   r   r   	   sT   	 ÿ











r   c                 C   sJ   |   d¡}|dkrd| fS | d dks*J ‚| d|… | |d d … fS d S )NÚ}r8   ra   r   Ú{r   )Úrfind)rV   Úpositionr   r   r   rZ   t  s
    
rZ   c                 C   s0   | d ur,d| vr,t | ƒ}t|ƒdkr,|d S d S )Nú,r   r   )r   rQ   )ÚvalueÚpartsr   r   r   rf   }  s    rf   N)Ú	functoolsr   Úwarningsr   Úwebencodingsr   Úcompilerr   r   r   rZ   rf   r   r   r   r   Ú<module>   s     m	