
    3fip!              	       j   d dl Z d dlZd dlmZ d dlmZmZ d dlmZ d dl	m
Z
 	  edd       d d	lmZmZmZ d
Zd dlmZmZmZmZmZ dZ G d de
      Z G d de
      Z ed
       G d de             Z	 	 	 ddee   dz  dee   dz  dee   dz  defdZy# e$ r dededefdZeZeZdZY ww xY w)    N)Sequence)AnyLiteral)check_package_version)	TypedDictlarkz1.1.5)gte_version)LarkTransformerv_argsT___returnc                      d S )z/Dummy decorator for when lark is not installed.c                      y )N )r   s    o/var/www/auto_recruiter/arenv/lib/python3.12/site-packages/langchain_classic/chains/query_constructor/parser.py<lambda>zv_args.<locals>.<lambda>   s        r   )r   r   s     r   r   r      s    r   F)
Comparator
ComparisonFilterDirective	OperationOperatora  
    ?program: func_call
    ?expr: func_call
        | value

    func_call: CNAME "(" [args] ")"

    ?value: SIGNED_INT -> int
        | SIGNED_FLOAT -> float
        | DATE -> date
        | DATETIME -> datetime
        | list
        | string
        | ("false" | "False" | "FALSE") -> false
        | ("true" | "True" | "TRUE") -> true

    args: expr ("," expr)*
    DATE.2: /["']?(\d{4}-[01]\d-[0-3]\d)["']?/
    DATETIME.2: /["']?\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d[Zz]?["']?/
    string: /'[^']*'/ | ESCAPED_STRING
    list: "[" [args] "]"

    %import common.CNAME
    %import common.ESCAPED_STRING
    %import common.SIGNED_FLOAT
    %import common.SIGNED_INT
    %import common.WS
    %ignore WS
c                   ,    e Zd ZU dZeed<   ed   ed<   y)ISO8601Datez'A date in ISO 8601 format (YYYY-MM-DD).datetypeN__name__
__module____qualname____doc__str__annotations__r   r   r   r   r   r   ?   s    1
I
&/r   r   c                   ,    e Zd ZU dZeed<   ed   ed<   y)ISO8601DateTimez4A datetime in ISO 8601 format (YYYY-MM-DDTHH:MM:SS).datetimer   Nr   r   r   r   r'   r'   F   s    >M
*
r   r'   )inlinec                   (    e Zd ZdZdddddedee   dz  dee   dz  dee   dz  def
 fd	Z	d
ede
fdZdededefdZdedeez  fdZd
ede
fdZdefdZdefdZdedefdZdedefdZdedefdZdedefdZdedefdZdedefdZ xZS )QueryTransformerz=Transform a query string into an intermediate representation.Nallowed_comparatorsallowed_operatorsallowed_attributesargsr-   r.   r/   kwargsc                N    t        |   |i | || _        || _        || _        y)a  Initialize the QueryTransformer.

        Args:
            *args: Positional arguments.
            allowed_comparators: Optional sequence of allowed comparators.
            allowed_operators: Optional sequence of allowed operators.
            allowed_attributes: Optional sequence of allowed attributes for comparators.
            **kwargs: Additional keyword arguments.
        N)super__init__r-   r.   r/   )selfr-   r.   r/   r0   r1   	__class__s         r   r4   zQueryTransformer.__init__Q   s/    " 	$)&)#6 !2"4r   itemsr   c                     |S )z!Transform the items into a tuple.r   r5   r7   s     r   programzQueryTransformer.programg   s    r   	func_namec                 |   | j                  t        |            }t        |t              rQ| j                  r1|d   | j                  vr d|d    d| j                   }t        |      t        ||d   |d         S t        |      dk(  r'|t        j                  t        j                  fv r|d   S t        ||      S )ax  Transform a function name and args into a FilterDirective.

        Args:
            func_name: The name of the function.
            args: The arguments passed to the function.

        Returns:
            The filter directive.

        Raises:
            ValueError: If the function is a comparator and the first arg is not in the
            allowed attributes.
        r   zReceived invalid attributes z. Allowed attributes are    )
comparator	attributevalue)operator	arguments)_match_func_namer$   
isinstancer   r/   
ValueErrorr   lenr   ANDORr   )r5   r;   r0   funcmsgs        r   	func_callzQueryTransformer.func_callk   s     $$S^4dJ'&&47$:Q:Q+Q247);T../1  !o%aQPPt9>dx||X[[&AA7N$$77r   c                    |t        t              v rB| j                  +|| j                  vrd| d| j                   }t        |      t        |      S |t        t              v rB| j
                  +|| j
                  vrd| d| j
                   }t        |      t	        |      S d| dt        t              t        t              z    }t        |      )NzReceived disallowed comparator z. Allowed comparators are zReceived disallowed operator z. Allowed operators are zReceived unrecognized function z. Valid functions are )setr   r-   rE   r   r.   list)r5   r;   rJ   s      r   rC   z!QueryTransformer._match_func_name   s    J'((4T%=%== 6i[ A''+'?'?&@B  !o%i((H%&&2T%;%;; 4I; ? 2235  !o%I&&-i[8NH~Z 0013 	 or   c                     |S )zaTransforms items into a tuple.

        Args:
            items: The items to transform.
        r   r9   s     r   r0   zQueryTransformer.args   s	     r   c                      y)zReturns false.Fr   r5   s    r   falsezQueryTransformer.false   s    r   c                      y)zReturns true.Tr   rQ   s    r   truezQueryTransformer.true   s    r   itemc                      |g S t        |      S )z`Transforms an item into a list.

        Args:
            item: The item to transform.
        )rN   r5   rU   s     r   rN   zQueryTransformer.list   s     <IDzr   c                     t        |      S )z`Transforms an item into an int.

        Args:
            item: The item to transform.
        )intrW   s     r   rY   zQueryTransformer.int   s     4yr   c                     t        |      S )zaTransforms an item into a float.

        Args:
            item: The item to transform.
        )floatrW   s     r   r[   zQueryTransformer.float   s     T{r   c                     t        |      j                  d      }	 t        j                  j                  |d       |ddS # t        $ r t        j                  dd       Y 'w xY w)zTransforms an item into a ISO8601Date object.

        Args:
            item: The item to transform.

        Raises:
            ValueError: If the item is not in ISO 8601 date format.
        "'z%Y-%m-%dzGDates are expected to be provided in ISO 8601 date format (YYYY-MM-DD).   )
stacklevelr   )r   r   )r$   stripr(   strptimerE   warningswarnrW   s     r   r   zQueryTransformer.date   sc     4yu%	&&tZ8 f--  	MM 	s    A  A$#A$c                 "   t        |      j                  d      }	 t        j                  j                  |d       |ddS # t        $ rD 	 t        j                  j                  |d       n# t        $ r}d}t	        |      |d}~ww xY wY Qw xY w)zTransforms an item into a ISO8601DateTime object.

        Args:
            item: The item to transform.

        Raises:
            ValueError: If the item is not in ISO 8601 datetime format.
        r]   z%Y-%m-%dT%H:%M:%S%zz%Y-%m-%dT%H:%M:%Sz6Datetime values are expected to be in ISO 8601 format.Nr(   )r(   r   )r$   r`   r(   ra   rE   )r5   rU   erJ   s       r   r(   zQueryTransformer.datetime   s     4yu%	-&&t-BC !*55  	--!!**41DE -N o1,- F	-s5    A 	B A,+B,	B5BBBBc                 6    t        |      j                  d      S )zTransforms an item into a string.

        Removes escaped quotes.

        Args:
            item: The item to transform.
        r]   )r$   r`   rW   s     r   stringzQueryTransformer.string   s     4yu%%r   )r    r!   r"   r#   r   r   r   r   r$   r4   tupler:   rN   r   rK   rC   r0   boolrR   rT   rY   r[   r   r   r'   r(   rg   __classcell__)r6   s   @r   r+   r+   M   sF   G
 <@7;3755 &j1D85 $H-4	5
 %SMD05 5,c e 83 8d 8 86# (Z2G :3 5 t d     # % . . .(6S 6_ 6*&3 &3 &r   r+   r-   r.   r/   c                 j    t         sd}t        |      t        | ||      }t        t        d|d      S )a  Return a parser for the query language.

    Args:
        allowed_comparators: The allowed comparators.
        allowed_operators: The allowed operators.
        allowed_attributes: The allowed attributes.

    Returns:
        Lark parser for the query language.
    z>Cannot import lark, please install it with 'pip install lark'.r,   lalrr:   )parsertransformerstart)	_HAS_LARKImportErrorr+   r
   GRAMMAR)r-   r.   r/   rJ   rn   s        r   
get_parserrs     s=     N#"/+-K
 KyQQr   )NNN)r(   rb   collections.abcr   typingr   r   langchain_core.utilsr   typing_extensionsr   r   r
   r   r   rp   rq   objectlangchain_core.structured_queryr   r   r   r   r   rr   r   r'   r+   r$   rs   r   r   r   <module>rz      s     $  6 '&g6..I >) i  tp&{ p& p&h 8<37/3R!*-4R)D0R !,R 
	Rg  3 c c  KDIs   B B21B2