U
    P|j£$  ã                   @   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   ú</tmp/pip-unpacked-wheel-audpp6lv/twisted/words/xish/xpath.pyÚvalue   s    zLiteralValue.valueN)Ú__name__Ú
__module__Ú__qualname__r
   r   r   r   r	   r      s   r   c                   @   s   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
      s    zIndexValue.valueN)r   r   r   r   r
   r   r   r   r	   r      s   r   c                   @   s$   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 )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kr|j| j  S d 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                   @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚCompareValuec                 C   s*   || _ || _|dkr| j| _n| j| _d S )Nú=)ÚlhsÚrhsÚ_compareEqualr
   Ú_compareNotEqual©r   r   Úopr   r   r   r	   r   0   s
    
zCompareValue.__init__c                 C   s   | j  |¡| j |¡kS r   ©r   r
   r   r   r   r   r	   r   8   s    zCompareValue._compareEqualc                 C   s   | j  |¡| j |¡kS r   r"   r   r   r   r	   r   ;   s    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   s*   || _ || _|dkr| j| _n| j| _d S )NÚand)r   r   Ú_booleanAndr
   Ú
_booleanOrr    r   r   r	   r   J   s
    
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   s    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   s    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)ÚfnameZ	klassnameÚcr   r   r	   ÚFunctionc   s    r+   c                   @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )Ú_not_Functionc                 C   s
   d | _ d S r   ©Ú	baseValue©r   r   r   r	   r   m   s    z_not_Function.__init__c                 C   s
   || _ d S r   r-   )r   r.   r   r   r	   Ú	setParamsp   s    z_not_Function.setParamsc                 C   s   | j  |¡ S r   )r.   r
   r   r   r   r	   r
   s   s    z_not_Function.valueN)r   r   r   r   r0   r
   r   r   r   r	   r,   l   s   r,   c                   @   s   e Zd Zdd„ Zdd„ ZdS )Ú_text_Functionc                 C   s   d S r   r   r/   r   r   r	   r0   x   s    z_text_Function.setParamsc                 C   s   t |ƒS r   )Ústrr   r   r   r	   r
   {   s    z_text_Function.valueN)r   r   r   r0   r
   r   r   r   r	   r1   w   s   r1   c                   @   s<   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   s   g | _ d | _d | _d S r   ©Ú
predicatesÚelementNameÚchildLocationr/   r   r   r	   r   €   s    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   )r6   Únamer5   r
   ©r   r   Úpr   r   r	   ÚmatchesPredicates…   s    

z_Location.matchesPredicatesc                 C   sB   |   |¡sdS | jd kr:| ¡ D ]}| j |¡r  dS q ndS dS r8   )r<   r7   ÚelementsÚmatches©r   r   r*   r   r   r	   r>   �   s    


z_Location.matchesc                 C   sH   |   |¡sd S | jd kr6| ¡ D ]}| j ||¡ q n| t|ƒ¡ d S r   )r<   r7   r=   ÚqueryForStringÚwriter2   )r   r   Ú	resultbufr*   r   r   r	   r@   œ   s    

z_Location.queryForStringc                 C   sD   |   |¡sd S | jd kr6| ¡ D ]}| j ||¡ q n
| |¡ d S r   )r<   r7   r=   ÚqueryForNodesÚappend©r   r   Ú
resultlistr*   r   r   r	   rC   ¦   s    

z_Location.queryForNodesc                 C   sZ   |   |¡sd S | jd kr6| ¡ D ]}| j ||¡ q n |jD ]}t|tƒr<| |¡ q<d S r   )r<   r7   r=   ÚqueryForStringListr   Ú
isinstancer2   rD   rE   r   r   r	   rG   °   s    



z_Location.queryForStringListN)	r   r   r   r   r<   r>   r@   rC   rG   r   r   r   r	   r3      s   


r3   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   s   g | _ d | _d | _d S r   r4   r/   r   r   r	   r   ¾   s    z_AnyLocation.__init__c                 C   s    | j D ]}| |¡s dS qdS r8   )r5   r
   r:   r   r   r	   r<   Ã   s    

z_AnyLocation.matchesPredicatesc                 C   s(   |j d kr|  |j |¡ | |j¡ d S r   )ÚparentÚlistParentsrD   r9   )r   r   Z
parentlistr   r   r	   rK   É   s    
z_AnyLocation.listParentsc                 C   sT   | j d ks| j |jkrP|  |¡rP| jd krL| ¡ D ]}| j |¡r2 dS q2ndS dS ©NTF©r6   r9   r<   r7   r=   r>   r?   r   r   r	   ÚisRootMatchÎ   s    ÿ
ÿþ

z_AnyLocation.isRootMatchc                 C   s~   | j d ks| j |jkrV|  |¡rV| jd krP| ¡ D ]}| j |¡r2|  S q2d S |S n$| ¡ D ]}|  |¡r^|  S q^d S d S r   rM   r?   r   r   r	   ÚfindFirstRootMatchÚ   s     ÿ
ÿþ



z_AnyLocation.findFirstRootMatchc                 C   s4   |   |¡rdS | ¡ D ]}|  |¡r dS qdS d S rL   )rN   r=   r>   r?   r   r   r	   r>   ô   s    

z_AnyLocation.matchesc                 C   s   t dƒ‚d S )Nz2queryForString is not implemented for any location)ÚNotImplementedError)r   r   rB   r   r   r	   r@     s    z_AnyLocation.queryForStringc                 C   s2   |   |¡r| |¡ | ¡ D ]}|  ||¡ qd S r   )rN   rD   r=   rC   rE   r   r   r	   rC     s    

z_AnyLocation.queryForNodesc                 C   sH   |   |¡r*|jD ]}t|tƒr| |¡ q| ¡ D ]}|  ||¡ q2d S r   )rN   r   rH   r2   rD   r=   rG   rE   r   r   r	   rG     s    


z_AnyLocation.queryForStringListN)r   r   r   r   r<   rK   rN   rO   r>   r@   rC   rG   r   r   r   r	   rI   ½   s   	rI   c                   @   s<   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ ZdS )Ú
XPathQueryc                 C   s4   || _ ddlm}m} |||ƒƒ}t|dƒƒ | _d S )Nr   )ÚXPathParserÚXPathParserScannerZXPATH)ÚqueryStrZtwisted.words.xish.xpathparserrR   rS   ÚgetattrÚbaseLocation)r   rT   rR   rS   Úparserr   r   r	   r     s    zXPathQuery.__init__c                 C   s
   | j  ¡ S r   )rT   Ú__hash__r/   r   r   r	   rX     s    zXPathQuery.__hash__c                 C   s   | j  |¡S r   )rV   r>   r   r   r   r	   r>   "  s    zXPathQuery.matchesc                 C   s   t ƒ }| j ||¡ | ¡ S r   )r   rV   r@   Úgetvalue©r   r   Úresultr   r   r	   r@   %  s    zXPathQuery.queryForStringc                 C   s*   g }| j  ||¡ t|ƒdkr"d S |S d S ©Nr   )rV   rC   ÚlenrZ   r   r   r	   rC   *  s
    zXPathQuery.queryForNodesc                 C   s*   g }| j  ||¡ t|ƒdkr"d S |S d S r\   )rV   rG   r]   rZ   r   r   r	   rG   2  s
    zXPathQuery.queryForStringListN)	r   r   r   r   rX   r>   r@   rC   rG   r   r   r   r	   rQ     s   rQ   c                 C   s   | t krt| ƒt | < t |  S r   )Ú__internedQueriesrQ   )ZqueryStringr   r   r	   ÚinternQuery>  s    r_   c                 C   s   t | ƒ |¡S r   )r_   r>   ©Zxpathstrr   r   r   r	   r>   D  s    r>   c                 C   s   t | ƒ |¡S r   )r_   rG   r`   r   r   r	   rG   H  s    rG   c                 C   s   t | ƒ |¡S r   )r_   r@   r`   r   r   r	   r@   L  s    r@   c                 C   s   t | ƒ |¡S r   )r_   rC   r`   r   r   r	   rC   P  s    rC   N)r'   Úior   r2   r   r   r   r   r#   r+   r,   r1   r3   rI   rQ   r^   r_   r>   rG   r@   rC   r   r   r   r	   Ú<module>   s$   	$	>Y%