o
    ¯b£$  ã                   @   sÖ   d Z ddlmZ G dd„ deƒZG dd„ dƒZG dd„ dƒZG d	d
„ d
ƒZG dd„ dƒZdd„ Z	G dd„ dƒZ
G dd„ dƒZG dd„ dƒZG dd„ dƒZG dd„ dƒZi Zdd„ Zdd„ Zdd„ Zdd „ Zd!d"„ Zd#S )$z¢
XPath query support.

This module provides L{XPathQuery} to match
L{domish.Element<twisted.words.xish.domish.Element>} instances against
XPath-like expressions.
é    )ÚStringIOc                   @   s   e Zd Zdd„ ZdS )ÚLiteralValuec                 C   s   | S ©N© ©ÚselfÚelemr   r   ú:/usr/lib/python3/dist-packages/twisted/words/xish/xpath.pyÚvalue   ó   zLiteralValue.valueN)Ú__name__Ú
__module__Ú__qualname__r
   r   r   r   r	   r      s    r   c                   @   ó   e Zd Zdd„ Zdd„ ZdS )Ú
IndexValuec                 C   s   t |ƒd | _d S )Né   )ÚintÚindex)r   r   r   r   r	   Ú__init__   s   zIndexValue.__init__c                 C   s   |j | j S r   )Úchildrenr   r   r   r   r	   r
      ó   zIndexValue.valueN)r   r   r   r   r
   r   r   r   r	   r      ó    r   c                   @   ó$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚAttribValuec                 C   s    || _ | j dkr| j| _d S d S )NÚxmlns)Ú
attribnameÚvalue_nsr
   )r   r   r   r   r	   r       s   
ÿzAttribValue.__init__c                 C   s   |j S r   )Úurir   r   r   r	   r   %   s   zAttribValue.value_nsc                 C   s   | j |jv r|j| j  S d S r   )r   Ú
attributesr   r   r   r	   r
   (   s   zAttribValue.valueN)r   r   r   r   r   r
   r   r   r   r	   r      s    r   c                   @   r   )ÚCompareValuec                 C   ó,   || _ || _|dkr| j| _d S | j| _d S )Nú=)ÚlhsÚrhsÚ_compareEqualr
   Ú_compareNotEqual©r   r"   Úopr#   r   r   r	   r   0   ó
   zCompareValue.__init__c                 C   s   | j  |¡| j |¡kS r   ©r"   r
   r#   r   r   r   r	   r$   8   ó   zCompareValue._compareEqualc                 C   s   | j  |¡| j |¡kS r   r)   r   r   r   r	   r%   ;   r*   zCompareValue._compareNotEqualN)r   r   r   r   r$   r%   r   r   r   r	   r   /   s    r   c                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚBooleanValuea^  
    Provide boolean XPath expression operators.

    @ivar lhs: Left hand side expression of the operator.
    @ivar op: The operator. One of C{'and'}, C{'or'}.
    @ivar rhs: Right hand side expression of the operator.
    @ivar value: Reference to the method that will calculate the value of
                 this expression given an element.
    c                 C   r    )NÚand)r"   r#   Ú_booleanAndr
   Ú
_booleanOrr&   r   r   r	   r   J   r(   zBooleanValue.__init__c                 C   s   | j  |¡o| j |¡S )z£
        Calculate boolean and of the given expressions given an element.

        @param elem: The element to calculate the value of the expression from.
        r)   r   r   r   r	   r-   R   ó   zBooleanValue._booleanAndc                 C   s   | j  |¡p| j |¡S )z¢
        Calculate boolean or of the given expressions given an element.

        @param elem: The element to calculate the value of the expression from.
        r)   r   r   r   r	   r.   Z   r/   zBooleanValue._booleanOrN)r   r   r   Ú__doc__r   r-   r.   r   r   r   r	   r+   ?   s
    
r+   c                 C   s   d|  }t ƒ | ƒ }|S )z;
    Internal method which selects the function object
    z_%s_Function)Úglobals)ÚfnameÚ	klassnameÚcr   r   r	   ÚFunctionc   s   r5   c                   @   r   )Ú_not_Functionc                 C   s
   d | _ d S r   ©Ú	baseValue©r   r   r   r	   r   m   ó   
z_not_Function.__init__c                 C   s
   || _ d S r   r7   )r   r8   r   r   r	   Ú	setParamsp   r:   z_not_Function.setParamsc                 C   s   | j  |¡ S r   )r8   r
   r   r   r   r	   r
   s   ó   z_not_Function.valueN)r   r   r   r   r;   r
   r   r   r   r	   r6   l   s    r6   c                   @   r   )Ú_text_Functionc                 C   s   d S r   r   r9   r   r   r	   r;   x   r   z_text_Function.setParamsc                 C   s   t |ƒS r   )Ústrr   r   r   r	   r
   {   ó   z_text_Function.valueN)r   r   r   r;   r
   r   r   r   r	   r=   w   r   r=   c                   @   ó<   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ ZdS )Ú	_Locationc                 C   ó   g | _ d | _d | _d S r   ©Ú
predicatesÚelementNameÚchildLocationr9   r   r   r	   r   €   ó   
z_Location.__init__c                 C   s:   | j d kr| j |jkrdS | jD ]
}| |¡s dS qdS ©Nr   r   )rE   ÚnamerD   r
   ©r   r   Úpr   r   r	   ÚmatchesPredicates…   s   

ÿz_Location.matchesPredicatesc                 C   s@   |   |¡sdS | jd kr| ¡ D ]}| j |¡r dS qdS dS rH   )rL   rF   ÚelementsÚmatches©r   r   r4   r   r   r	   rN      s   

ÿþz_Location.matchesc                 C   sJ   |   |¡sd S | jd kr| ¡ D ]	}| j ||¡ qd S | t|ƒ¡ d S r   )rL   rF   rM   ÚqueryForStringÚwriter>   )r   r   Ú	resultbufr4   r   r   r	   rP   œ   s   

ÿz_Location.queryForStringc                 C   sF   |   |¡sd S | jd kr| ¡ D ]	}| j ||¡ qd S | |¡ d S r   )rL   rF   rM   ÚqueryForNodesÚappend©r   r   Ú
resultlistr4   r   r   r	   rS   ¦   s   

ÿz_Location.queryForNodesc                 C   s\   |   |¡sd S | jd kr| ¡ D ]	}| j ||¡ qd S |jD ]}t|tƒr+| |¡ qd S r   )rL   rF   rM   ÚqueryForStringListr   Ú
isinstancer>   rT   rU   r   r   r	   rW   °   s   

ÿ


€þz_Location.queryForStringListN)	r   r   r   r   rL   rN   rP   rS   rW   r   r   r   r	   rA      s    


rA   c                   @   sT   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dd„ ZdS )Ú_AnyLocationc                 C   rB   r   rC   r9   r   r   r	   r   ¾   rG   z_AnyLocation.__init__c                 C   s    | j D ]
}| |¡s dS qdS rH   )rD   r
   rJ   r   r   r	   rL   Ã   s
   

ÿz_AnyLocation.matchesPredicatesc                 C   s(   |j d kr|  |j |¡ | |j¡ d S r   )ÚparentÚlistParentsrT   rI   )r   r   Ú
parentlistr   r   r	   r[   É   s   
z_AnyLocation.listParentsc                 C   sV   | j d ks| j |jkr)|  |¡r)| jd kr'| ¡ D ]}| j |¡r$ dS qdS dS dS ©NTF©rE   rI   rL   rF   rM   rN   rO   r   r   r	   ÚisRootMatchÎ   s   ÿ
ÿÿz_AnyLocation.isRootMatchc                 C   sx   | j d ks| j |jkr*|  |¡r*| jd kr(| ¡ D ]}| j |¡r%|  S qd S |S | ¡ D ]}|  |¡r9|  S q.d S r   r^   rO   r   r   r	   ÚfindFirstRootMatchÚ   s   ÿ
ÿ
ÿz_AnyLocation.findFirstRootMatchc                 C   s0   |   |¡rdS | ¡ D ]
}|  |¡r dS qdS r]   )r_   rM   rN   rO   r   r   r	   rN   ô   s   

ÿz_AnyLocation.matchesc                 C   s   t dƒ‚)Nz2queryForString is not implemented for any location)ÚNotImplementedError)r   r   rR   r   r   r	   rP     r?   z_AnyLocation.queryForStringc                 C   s2   |   |¡r
| |¡ | ¡ D ]}|  ||¡ qd S r   )r_   rT   rM   rS   rU   r   r   r	   rS     s
   

ÿz_AnyLocation.queryForNodesc                 C   sH   |   |¡r|jD ]}t|tƒr| |¡ q| ¡ D ]}|  ||¡ qd S r   )r_   r   rX   r>   rT   rM   rW   rU   r   r   r	   rW     s   



€ÿz_AnyLocation.queryForStringListN)r   r   r   r   rL   r[   r_   r`   rN   rP   rS   rW   r   r   r   r	   rY   ½   s    	rY   c                   @   r@   )Ú
XPathQueryc                 C   s4   || _ ddlm}m} |||ƒƒ}t|dƒƒ | _d S )Nr   )ÚXPathParserÚXPathParserScannerÚXPATH)ÚqueryStrÚtwisted.words.xish.xpathparserrc   rd   ÚgetattrÚbaseLocation)r   rf   rc   rd   Úparserr   r   r	   r     s   zXPathQuery.__init__c                 C   s
   | j  ¡ S r   )rf   Ú__hash__r9   r   r   r	   rk     r:   zXPathQuery.__hash__c                 C   s   | j  |¡S r   )ri   rN   r   r   r   r	   rN   "  r   zXPathQuery.matchesc                 C   s   t ƒ }| j ||¡ | ¡ S r   )r   ri   rP   Úgetvalue©r   r   Úresultr   r   r	   rP   %  s   zXPathQuery.queryForStringc                 C   ó&   g }| j  ||¡ t|ƒdkrd S |S ©Nr   )ri   rS   Úlenrm   r   r   r	   rS   *  ó
   zXPathQuery.queryForNodesc                 C   ro   rp   )ri   rW   rq   rm   r   r   r	   rW   2  rr   zXPathQuery.queryForStringListN)	r   r   r   r   rk   rN   rP   rS   rW   r   r   r   r	   rb     s    rb   c                 C   s   | t vr
t| ƒt | < t |  S r   )Ú__internedQueriesrb   )ÚqueryStringr   r   r	   ÚinternQuery>  s   ru   c                 C   ó   t | ƒ |¡S r   )ru   rN   ©Úxpathstrr   r   r   r	   rN   D  r<   rN   c                 C   rv   r   )ru   rW   rw   r   r   r	   rW   H  r<   rW   c                 C   rv   r   )ru   rP   rw   r   r   r	   rP   L  r<   rP   c                 C   rv   r   )ru   rS   rw   r   r   r	   rS   P  r<   rS   N)r0   Úior   r>   r   r   r   r   r+   r5   r6   r=   rA   rY   rb   rs   ru   rN   rW   rP   rS   r   r   r   r	   Ú<module>   s&   	$	>Y%