U
    b+d                     @   s,  d d Z d5ddZdd Zdd Zd	d
 Zdd Zdd Zd6ddZG dd dZG dd deZ	G dd deZ
G dd deZG dd deZG dd dZG dd deZG d d! d!eZG d"d# d#eZG d$d% d%eZG d&d' d'eZG d(d) d)eZd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Zd4S )7c                  G   s   | st dt|  S )zx
    Indicate that the values should be matched to a tag field

    ### Parameters

    - **t**: Tags to search for
    z"At least one tag must be specified)
ValueErrorTagValue)t r   E/tmp/pip-unpacked-wheel-cdsyf3nb/redis/commands/search/querystring.pytags   s    r   Tc                 C   s   t | |||dS )z0
    Indicate that value is a numeric range
    )inclusive_mininclusive_max)
RangeValue)abr   r   r   r   r   between   s    r   c                 C   s
   t | | S )z
    Match a numeric value
    r   nr   r   r   equal   s    r   c                 C   s   t d| ddS )z%
    Match any value less than n
    NFr   r   r   r   r   r   lt   s    r   c                 C   s   t d| ddS )z,
    Match any value less or equal to n
    NTr   r   r   r   r   r   le#   s    r   c                 C   s   t | dddS )z(
    Match any value greater than n
    NFr   r   r   r   r   r   gt*   s    r   c                 C   s   t | dddS )z/
    Match any value greater or equal to n
    NTr   r   r   r   r   r   ge1   s    r   kmc                 C   s   t | |||S )z-
    Indicate that value is a geo region
    )GeoValue)latlonradiusunitr   r   r   geo8   s    r   c                   @   s4   e Zd Zedd Zedd Zdd Zdd Zd	S )
Valuec                 C   s   dS )z
        Whether this type of value may be combined with other values
        for the same field. This makes the filter potentially more efficient
        Fr   selfr   r   r   
combinable@   s    zValue.combinablec                 C   s   t | tr| S t| S )zL
        Convert an object to a value, if it is not a value already
        )
isinstancer   ScalarValuevr   r   r   
make_valueH   s    
zValue.make_valuec                 C   s
   t  d S NNotImplementedErrorr   r   r   r   	to_stringQ   s    zValue.to_stringc                 C   s   |   S r'   r*   r   r   r   r   __str__T   s    zValue.__str__N)	__name__
__module____qualname__propertyr!   staticmethodr&   r*   r,   r   r   r   r   r   ?   s   

r   c                   @   s"   e Zd ZdZdddZdd ZdS )r	   Fc                 C   s:   |d krd}|d krd}t |t |g| _|| _|| _d S )Nz-infinf)strranger   r   )r    r
   r   r   r   r   r   r   __init__[   s    zRangeValue.__init__c                 C   s$   d | j| jsdnd| jsdndS )Nz[{1}{0[0]} {2}{0[1]}]( )formatr4   r   r   r   r   r   r   r*   d   s
    zRangeValue.to_stringN)FFr-   r.   r/   r!   r5   r*   r   r   r   r   r	   X   s   
	r	   c                   @   s    e Zd ZdZdd Zdd ZdS )r#   Tc                 C   s   t || _d S r'   )r3   r%   )r    r%   r   r   r   r5   o   s    zScalarValue.__init__c                 C   s   | j S r'   r$   r   r   r   r   r*   r   s    zScalarValue.to_stringNr9   r   r   r   r   r#   l   s   r#   c                   @   s    e Zd ZdZdd Zdd ZdS )r   Fc                 G   s
   || _ d S r'   )r   )r    r   r   r   r   r5   y   s    zTagValue.__init__c                 C   s   dd dd | jD  d S )N{z | c                 s   s   | ]}t |V  qd S r'   )r3   ).0r   r   r   r   	<genexpr>}   s     z%TagValue.to_string.<locals>.<genexpr>})joinr   r   r   r   r   r*   |   s    zTagValue.to_stringNr9   r   r   r   r   r   v   s   r   c                   @   s   e Zd ZdddZdd ZdS )r   r   c                 C   s   || _ || _|| _|| _d S r'   r   r   r   r   )r    r   r   r   r   r   r   r   r5      s    zGeoValue.__init__c              	   C   s&   d| j  d| j d| j d| j d	S )N[ ]r?   r   r   r   r   r*      s    zGeoValue.to_stringN)r   )r-   r.   r/   r5   r*   r   r   r   r   r      s   
r   c                   @   sN   e Zd Zdd Zdd Zedd Zedd Zdd
dZ	dd Z
dd Zd	S )Nodec                 O   s   g | _ i }| D ]`\}}||g }t|tttfrH|t	| qt|tr^|| q|
dd |D  q|  j dd |D 7  _ | D ]\}}| j 
| || qdS )a  
        Create a node

        ### Parameters

        - **children**: One or more sub-conditions. These can be additional
            `intersect`, `disjunct`, `union`, `optional`, or any other `Node`
            type.

            The semantics of multiple conditions are dependent on the type of
            query. For an `intersection` node, this amounts to a logical AND,
            for a `union` node, this amounts to a logical `OR`.

        - **kwparams**: key-value parameters. Each key is the name of a field,
            and the value should be a field value. This can be one of the
            following:

            - Simple string (for text field matches)
            - value returned by one of the helper functions
            - list of either a string or a value


        ### Examples

        Field `num` should be between 1 and 10
        ```
        intersect(num=between(1, 10)
        ```

        Name can either be `bob` or `john`

        ```
        union(name=("bob", "john"))
        ```

        Don't select countries in Israel, Japan, or US

        ```
        disjunct_union(country=("il", "jp", "us"))
        ```
        c                 s   s   | ]}t |V  qd S r'   )r   r&   )r;   Zsubvr   r   r   r<      s     z Node.__init__.<locals>.<genexpr>c                 S   s   g | ]}t |qS r   )rC   to_node)r;   pr   r   r   
<listcomp>   s     z!Node.__init__.<locals>.<listcomp>N)paramsitems
setdefaultr"   r3   intfloatappendr   r&   extendjoin_fields)r    childrenZkwparamsZkvparamskr%   Zcurvalsr   r   r   r5      s    +
zNode.__init__c                    st   t |dkr*td  d|d   gS |d jsF fdd|D S td  d| jdd	 |D  d
}|gS )N   @:    c                    s$   g | ]}t d   d|  qS )rR   rS   )BaseNoder*   r;   r%   keyr   r   rF      s     z$Node.join_fields.<locals>.<listcomp>z:(c                 s   s   | ]}|  V  qd S r'   r+   rV   r   r   r   r<      s     z#Node.join_fields.<locals>.<genexpr>))lenrU   r*   r!   JOINSTRr>   )r    rX   valssr   rW   r   rN      s    
(zNode.join_fieldsc                 C   s   t |tr|S t|S r'   )r"   rC   rU   )clsobjr   r   r   rD      s    
zNode.to_nodec                 C   s
   t  d S r'   r(   r   r   r   r   r[      s    zNode.JOINSTRNc                 C   s>   |  |}|rdnd\}}| | jdd | jD  | S )N)r6   rY   )r7   r7   c                 s   s   | ]}|  V  qd S r'   r+   )r;   r   r   r   r   r<      s     z!Node.to_string.<locals>.<genexpr>)_should_use_parenr[   r>   rG   )r    with_parensprepostr   r   r   r*      s    
zNode.to_stringc                 C   s   |d k	r|S t | jdkS )NrQ   )rZ   rG   )r    optvalr   r   r   r`      s    zNode._should_use_parenc                 C   s   |   S r'   r+   r   r   r   r   r,      s    zNode.__str__)N)r-   r.   r/   r5   rN   classmethodrD   r0   r[   r*   r`   r,   r   r   r   r   rC      s   <


rC   c                       s&   e Zd Z fddZdddZ  ZS )rU   c                    s   t    t|| _d S r'   )superr5   r3   r]   )r    r]   	__class__r   r   r5      s    
zBaseNode.__init__Nc                 C   s   | j S r'   )r]   )r    ra   r   r   r   r*      s    zBaseNode.to_string)N)r-   r.   r/   r5   r*   __classcell__r   r   rg   r   rU      s   rU   c                   @   s   e Zd ZdZdZdS )IntersectNodezw
    Create an intersection node. All children need to be satisfied in order for
    this node to evaluate as true
    rA   Nr-   r.   r/   __doc__r[   r   r   r   r   rj      s   rj   c                   @   s   e Zd ZdZdZdS )	UnionNodezv
    Create a union node. Any of the children need to be satisfied in order for
    this node to evaluate as true
    |Nrk   r   r   r   r   rm      s   rm   c                       s"   e Zd ZdZd fdd	Z  ZS )DisjunctNodezs
    Create a disjunct node. In order for this node to be true, all of its
    children must evaluate to false
    Nc                    s4   |  |}t jdd}|r(d| d S d| S d S )NFra   z(-rY   -r`   rf   r*   r    ra   retrg   r   r   r*   	  s
    
zDisjunctNode.to_string)Nr-   r.   r/   rl   r*   ri   r   r   rg   r   ro     s   ro   c                   @   s   e Zd ZdZdZdS )DistjunctUnionz~
    This node is true if *all* of its children are false. This is equivalent to
    ```
    disjunct(union(...))
    ```
    rn   Nrk   r   r   r   r   rv     s   rv   c                       s"   e Zd ZdZd fdd	Z  ZS )OptionalNodez}
    Create an optional node. If this nodes evaluates to true, then the document
    will be rated higher in score/rank.
    Nc                    s4   |  |}t jdd}|r(d| d S d| S d S )NFrp   z(~rY   ~rr   rs   rg   r   r   r*   #  s
    
zOptionalNode.to_string)Nru   r   r   rg   r   rw     s   rw   c                  O   s
   t | |S r'   )rj   argskwargsr   r   r   	intersect,  s    r|   c                  O   s
   t | |S r'   )rm   ry   r   r   r   union0  s    r}   c                  O   s
   t | |S r'   )ro   ry   r   r   r   disjunct4  s    r~   c                  O   s
   t | |S r'   )rv   ry   r   r   r   disjunct_union8  s    r   c                  O   s   t | | S r'   )r|   r*   ry   r   r   r   querystring<  s    r   N)TT)r   )r   r   r   r   r   r   r   r   r   r	   r#   r   r   rC   rU   rj   rm   ro   rv   rw   r|   r}   r~   r   r   r   r   r   r   <module>   s0   



]			