o
    Óó·i£$  ã                   @   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   ú^C:\Users\psant\Downloads\PAF backend\school\venv\Lib\site-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.
        Nr)   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.
        Nr)   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_FunctionN)Úglobals)ÚfnameZ	klassnameÚcr   r   r	   ÚFunctionc   s   r4   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   r6   )r   r7   r   r   r	   Ú	setParamsp   r9   z_not_Function.setParamsc                 C   s   | j  |¡ S r   )r7   r
   r   r   r   r	   r
   s   ó   z_not_Function.valueN)r   r   r   r   r:   r
   r   r   r   r	   r5   l   s    r5   c                   @   r   )Ú_text_Functionc                 C   s   d S r   r   r8   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ÚchildLocationr8   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   )rD   ÚnamerC   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 rG   )rK   rE   ÚelementsÚmatches©r   r   r3   r   r   r	   rM   �   s   

ÿþz_Location.matchesc                 C   sJ   |   |¡sd S | jd kr| ¡ D ]	}| j ||¡ qd S | t|ƒ¡ d S r   )rK   rE   rL   ÚqueryForStringÚwriter=   )r   r   Ú	resultbufr3   r   r   r	   rO   œ   s   

ÿz_Location.queryForStringc                 C   sF   |   |¡sd S | jd kr| ¡ D ]	}| j ||¡ qd S | |¡ d S r   )rK   rE   rL   ÚqueryForNodesÚappend©r   r   Ú
resultlistr3   r   r   r	   rR   ¦   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   )rK   rE   rL   ÚqueryForStringListr   Ú
isinstancer=   rS   rT   r   r   r	   rV   °   s   

ÿ


€þz_Location.queryForStringListN)	r   r   r   r   rK   rM   rO   rR   rV   r   r   r   r	   r@      s    


r@   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   rA   r   rB   r8   r   r   r	   r   ¾   rF   z_AnyLocation.__init__c                 C   s    | j D ]
}| |¡s dS qdS rG   )rC   r
   rI   r   r   r	   rK   Ã   s
   

ÿz_AnyLocation.matchesPredicatesc                 C   s(   |j d kr|  |j |¡ | |j¡ d S r   )ÚparentÚlistParentsrS   rH   )r   r   Z
parentlistr   r   r	   rZ   É   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©rD   rH   rK   rE   rL   rM   rN   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\   rN   r   r   r	   ÚfindFirstRootMatchÚ   s$   ÿ
ÿþ
ÿ
ÿz_AnyLocation.findFirstRootMatchc                 C   s0   |   |¡rdS | ¡ D ]
}|  |¡r dS qdS r[   )r]   rL   rM   rN   r   r   r	   rM   ô   s   

ÿz_AnyLocation.matchesc                 C   s   t dƒ‚)Nz2queryForString is not implemented for any location)ÚNotImplementedError)r   r   rQ   r   r   r	   rO     r>   z_AnyLocation.queryForStringc                 C   s2   |   |¡r
| |¡ | ¡ D ]}|  ||¡ qd S r   )r]   rS   rL   rR   rT   r   r   r	   rR     s
   

ÿz_AnyLocation.queryForNodesc                 C   sH   |   |¡r|jD ]}t|tƒr| |¡ q| ¡ D ]}|  ||¡ qd S r   )r]   r   rW   r=   rS   rL   rV   rT   r   r   r	   rV     s   



€ÿz_AnyLocation.queryForStringListN)r   r   r   r   rK   rZ   r]   r^   rM   rO   rR   rV   r   r   r   r	   rX   ½   s    	rX   c                   @   r?   )Ú
XPathQueryc                 C   s4   || _ ddlm}m} |||ƒƒ}t|dƒƒ | _d S )Nr   )ÚXPathParserÚXPathParserScannerZXPATH)ÚqueryStrZtwisted.words.xish.xpathparserra   rb   ÚgetattrÚbaseLocation)r   rc   ra   rb   Úparserr   r   r	   r     s   zXPathQuery.__init__c                 C   s
   | j  ¡ S r   )rc   Ú__hash__r8   r   r   r	   rg     r9   zXPathQuery.__hash__c                 C   s   | j  |¡S r   )re   rM   r   r   r   r	   rM   "  r   zXPathQuery.matchesc                 C   s   t ƒ }| j ||¡ | ¡ S r   )r   re   rO   Úgetvalue©r   r   Úresultr   r   r	   rO   %  s   zXPathQuery.queryForStringc                 C   ó&   g }| j  ||¡ t|ƒdkrd S |S ©Nr   )re   rR   Úlenri   r   r   r	   rR   *  ó
   zXPathQuery.queryForNodesc                 C   rk   rl   )re   rV   rm   ri   r   r   r	   rV   2  rn   zXPathQuery.queryForStringListN)	r   r   r   r   rg   rM   rO   rR   rV   r   r   r   r	   r`     s    r`   c                 C   s   | t vr
t| ƒt | < t |  S r   )Ú__internedQueriesr`   )ZqueryStringr   r   r	   ÚinternQuery>  s   rp   c                 C   ó   t | ƒ |¡S r   )rp   rM   ©Zxpathstrr   r   r   r	   rM   D  r;   rM   c                 C   rq   r   )rp   rV   rr   r   r   r	   rV   H  r;   rV   c                 C   rq   r   )rp   rO   rr   r   r   r	   rO   L  r;   rO   c                 C   rq   r   )rp   rR   rr   r   r   r	   rR   P  r;   rR   N)r0   Úior   r=   r   r   r   r   r+   r4   r5   r<   r@   rX   r`   ro   rp   rM   rV   rO   rR   r   r   r   r	   Ú<module>   s&   	$	>Y%