a
    •˜ëjª!  ã                   @   s2  d dl Z d dlZd dlmZ d dlmZmZmZmZ d dl	m
Z
 d dlmZ z(e
ddd� d d	lmZmZmZ d
ZW n0 ey¤   eeedœdd„ZeZeZdZY n0 d dlmZmZmZmZmZ dZG dd„ deƒZG dd„ deƒZed
d�G dd„ deƒƒZdeee  eee  eee  edœdd„ZdS )é    N)ÚSequence)ÚAnyÚLiteralÚOptionalÚUnion)Úcheck_package_version)Ú	TypedDictÚlarkz1.1.5)Zgte_version)ÚLarkÚTransformerÚv_argsT)Ú_Ú__Úreturnc                  O   s   dd„ S )z/Dummy decorator for when lark is not installed.c                 S   s   d S )N© )r   r   r   úh/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/langchain/chains/query_constructor/parser.pyÚ<lambda>   ó    zv_args.<locals>.<lambda>r   )r   r   r   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                   @   s&   e Zd ZU dZeed< ed ed< dS )Ú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   
r   c                   @   s&   e Zd ZU dZeed< ed ed< dS )ÚISO8601DateTimez4A datetime in ISO 8601 format (YYYY-MM-DDTHH:MM:SS).Údatetimer   Nr   r   r   r   r   r#   F   s   
r#   )Úinlinec                       s  e Zd ZdZddddœeeee  eee  eee	  edœ‡ fdd„Z
eedœdd	„Zeeed
œdd„Ze	eeef dœdd„Zeedœdd„Zedœdd„Zedœdd„Zeedœdd„Zeedœdd„Zeedœdd„Zeedœdd„Zeedœd d!„Zee	dœd"d#„Z‡  ZS )$ÚQueryTransformerz=Transform a query string into an intermediate representation.N©Úallowed_comparatorsÚallowed_operatorsÚallowed_attributes)Úargsr(   r)   r*   Úkwargsc                   s(   t ƒ j|i |¤Ž || _|| _|| _dS )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*   r+   r,   ©Ú	__class__r   r   r.   Q   s    zQueryTransformer.__init__)Úitemsr   c                 G   s   |S )z!Transform the items into a tuple.r   ©r/   r2   r   r   r   Úprogramg   s    zQueryTransformer.program)Ú	func_namer+   r   c                 C   s�   |   t|ƒ¡}t|tƒr`| jrJ|d | jvrJd|d › d| j› �}t|ƒ‚t||d |d d�S t|ƒdkr„|tj	tj
fv r„|d S t||d�S )a‰  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:
            FilterDirective: 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 é   )Z
comparatorÚ	attributeÚvalue)ÚoperatorÚ	arguments)Ú_match_func_namer!   Ú
isinstancer   r*   Ú
ValueErrorr   Úlenr   ÚANDÚORr   )r/   r5   r+   ÚfuncÚmsgr   r   r   Ú	func_callk   s    
ÿÿzQueryTransformer.func_call)r5   r   c                 C   s¬   |t tƒv rB| jd ur:|| jvr:d|› d| j› �}t|ƒ‚t|ƒS |t tƒv r„| jd ur||| jvr|d|› d| j› �}t|ƒ‚t|ƒS d|› dttƒttƒ › �}t|ƒ‚d S )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(   r=   r   r)   Úlist)r/   r5   rB   r   r   r   r;   †   s6    ÿþÿÿÿþÿÿÿÿz!QueryTransformer._match_func_namec                 G   s   |S )zaTransforms items into a tuple.

        Args:
            items: The items to transform.
        r   r3   r   r   r   r+   £   s    zQueryTransformer.args)r   c                 C   s   dS )zReturns false.Fr   ©r/   r   r   r   Úfalse«   s    zQueryTransformer.falsec                 C   s   dS )zReturns true.Tr   rF   r   r   r   Útrue¯   s    zQueryTransformer.true)Úitemr   c                 C   s   |du rg S t |ƒS )z`Transforms an item into a list.

        Args:
            item: The item to transform.
        N)rE   ©r/   rI   r   r   r   rE   ³   s    zQueryTransformer.listc                 C   s   t |ƒS )z`Transforms an item into an int.

        Args:
            item: The item to transform.
        )ÚintrJ   r   r   r   rK   ½   s    zQueryTransformer.intc                 C   s   t |ƒS )zaTransforms an item into a float.

        Args:
            item: The item to transform.
        )ÚfloatrJ   r   r   r   rL   Å   s    zQueryTransformer.floatc                 C   sL   t |ƒ d¡}ztj |d¡ W n  ty@   tjddd� Y n0 |ddœS )zÃTransforms 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$   Ústrptimer=   ÚwarningsÚwarnrJ   r   r   r   r   Í   s    	ýzQueryTransformer.datec                 C   s„   t |ƒ d¡}ztj |d¡ W nX tyx   ztj |d¡ W n2 tyr } zd}t|ƒ|‚W Y d}~n
d}~0 0 Y n0 |ddœS )zËTransforms an item into a ISO8601DateTime object.

        Args:
            item: The item to transform.

        Raises:
            ValueError: If the item is not in ISO 8601 datetime format.
        rM   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!   rP   r$   rQ   r=   )r/   rI   ÚerB   r   r   r   r$   á   s    	&zQueryTransformer.datetimec                 C   s   t |ƒ d¡S )zƒTransforms an item into a string.

        Removes escaped quotes.

        Args:
            item: The item to transform.
        rM   )r!   rP   rJ   r   r   r   Ústringö   s    zQueryTransformer.string)r   r   r   r    r   r   r   r   r   r!   r.   Útupler4   rE   r   rC   r   r;   r+   ÚboolrG   rH   rK   rL   r   r   r#   r$   rU   Ú__classcell__r   r   r0   r   r&   M   s.   û


ú
r&   )r(   r)   r*   r   c                 C   s.   t sd}t|ƒ‚t| ||d�}ttd|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'   Zlalrr4   )ÚparserÚtransformerÚstart)Ú	_HAS_LARKÚImportErrorr&   r
   ÚGRAMMAR)r(   r)   r*   rB   rZ   r   r   r   Ú
get_parser  s    ýr_   )NNN) r$   rR   Úcollections.abcr   Útypingr   r   r   r   Zlangchain_core.utilsr   Ztyping_extensionsr   r	   r
   r   r   r\   r]   ÚobjectZlangchain_core.structured_queryr   r   r   r   r   r^   r   r#   r&   r!   r_   r   r   r   r   Ú<module>   s<   
 5   ý


ü