
    ZTh                        d Z ddlmZ ddlZddlmZ ddlm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dZddZddZddZddZy)zCEL (Common Expression Language) utilities for generating CEL expressions.

This module provides helper functions to generate CEL expressions for building filters commonly used in Sift.
    )annotationsN)datetime)Anyc                f    |sy|D cg c]  }d| d
 }}|  ddj                  |       dS c c}w )a  Generates a CEL expression that checks for `field` membership in `vals`.

    Args:
        field: The field name to check
        vals: List of string values to check membership against

    Returns:
        A CEL expression string or empty string if vals is empty
     'z in [,])join)fieldvalsvalquoted_valss       _/home/www/backend.miabetepe.com/venv/lib/python3.12/site-packages/sift_client/util/cel_utils.pyin_r      sG     )-.#Qse1:.K.WE#((;/022 /s   .c                    d|  dS )zWraps the given expression in parentheses.

    Args:
        expr: The expression to wrap in parentheses

    Returns:
        A CEL expression string with parentheses
    () )exprs    r   parensr      s     tfA;    c                N    ||  dS t        |t              r|  d| dS |  d| S )zGenerates a CEL expression that checks for equality.

    Args:
        key: The field name
        value: The value to compare against

    Returns:
        A CEL expression string
     == nullz == 'r   z == )
isinstancestrkeyvalues     r   equalsr    *   sD     }h	E3	eE7!$$d5'""r   c                v    | j                         D cg c]  \  }}t        ||       }}}t        | S c c}}w )zGenerates a CEL expression that checks for equality of all key-value pairs.

    Args:
        values: Dictionary of field names and values to check for equality

    Returns:
        A CEL expression string with all equality checks joined by AND
    )itemsr    and_valuesr   r   clausess       r   
equals_allr'   <   s8     5;LLNCjc5vc5!CGC> D   5c                v    | j                         D cg c]  \  }}t        ||       }}}t        | S c c}}w )zGenerates a CEL expression that checks for equality of any key-value pairs.

    Args:
        values: Dictionary of field names and values to check for equality

    Returns:
        A CEL expression string with all equality checks joined by OR
    )r"   r    or_r$   s       r   
equals_anyr+   I   s8     5;LLNCjc5vc5!CGC= Dr(   c                     ||  dS |  d| dS )zGenerates a CEL expression that checks for equality with a double value.

    Args:
        key: The field name
        value: The value to compare against as a double

    Returns:
        A CEL expression string
    r   z == double(r   r   r   s     r   equals_doubler-   V   s)     }hU+eWA&&r   c                    |  dS )zGenerates a CEL expression that checks for equality with null.

    Args:
        key: The field name

    Returns:
        A CEL expression string
    r   r   )r   s    r   equals_nullr/   e   s     U(r   c                 P    | syt        |       dk(  r| d   S dj                  |       S )zGenerates a CEL expression that joins all clauses with an AND operator.

    Args:
        *clauses: Variable number of CEL expression strings

    Returns:
        A CEL expression string with all clauses joined by AND
    r      r   z && lenr   r&   s    r   r#   r#   q   /     
7|qqz;;wr   c                 P    | syt        |       dk(  r| d   S dj                  |       S )zGenerates a CEL expression that joins all clauses with an OR operator.

    Args:
        *clauses: Variable number of CEL expression strings

    Returns:
        A CEL expression string with all clauses joined by OR
    r   r1   r   z || r2   r4   s    r   r*   r*      r5   r   c                    d|  dS )zGenerates a CEL expression that negates the given clause.

    Args:
        clause: The CEL expression to negate

    Returns:
        A negated CEL expression string
    z!(r   r   )clauses    r   not_r9      s     xq>r   c                    |  d| dS )zGenerates a CEL expression that checks whether a string field contains a given value.

    Args:
        field: The field name
        value: The substring to check for

    Returns:
        A CEL expression string
    z.contains('')r   )r   r   s     r   containsr<      s     WKwb))r   c                    t        |t        j                        rt        |j                        }|j                  dd      }|  d| dS )zGenerates a CEL expression that checks for a match on the specified field.

    Args:
        field: The field name
        query: The regex pattern to match against

    Returns:
        A CEL expression string
    \z\\z
.matches('r;   )r   rePatternr   patternreplace)r   queryescaped_regexs      r   matchrE      sC     %$EMM"MM$/MWJ}oR00r   c                h    t        |t              r|j                         }nt        |      }|  d| S )zGenerates a CEL expression that checks whether a numeric or datetime field is greater than a given value.

    Args:
        field: The field name
        value: The value to compare against

    Returns:
        A CEL expression string
    z > r   r   	isoformatr   r   r   	as_strings      r   greater_thanrK      4     %"OO%	J	WC	{##r   c                h    t        |t              r|j                         }nt        |      }|  d| S )zGenerates a CEL expression that checks whether a numeric or datetime field is less than a given value.

    Args:
        field: The field name
        value: The value to compare against

    Returns:
        A CEL expression string
    z < rG   rI   s      r   	less_thanrN      rL   r   )r   r   r   z	list[str]returnr   )r   r   rO   r   )r   r   r   r   rO   r   )r%   zdict[str, Any]rO   r   )r   r   rO   r   )r&   r   rO   r   )r8   r   rO   r   )r   r   r   r   rO   r   )r   r   rC   zstr | re.PatternrO   r   )r   r   r   zint | float | datetimerO   r   )__doc__
__future__r   r?   r   typingr   r   r   r    r'   r+   r-   r/   r#   r*   r9   r<   rE   rK   rN   r   r   r   <module>rS      s^   
 # 	  3"	#$

'	    	
*1"$"$r   