o
    wi                     @  s  d dl mZ d dlmZ d dlmZmZmZ d dlm	Z	m
Z
mZ d dlmZ d dlmZ ddlmZmZmZ dd	lmZ ed
Zd#ddZee
e e	eeef Z	 G dd deeZd#ddZd#ddZd#ddZd#ddZd#ddZd$ddZ d$d d!Z!d"S )%    )annotations)Enum)	AnnotatedAnyTypeVar)BeforeValidatorJsonPlainSerializerPydanticUseDefault)to_json   )AndMongoLikeFilterOr)simplify_exprTvr   returnc                 C  s   t | ttfr	| S t| S )zIn case the incoming value isn't serialized JSON, reserialize it.

    This lets us use `Json[...]` fields with values that are already deserialized.
    )
isinstancestrbytesr   r    r   Q/home/ubuntu/.local/lib/python3.10/site-packages/wandb/automations/_validators.pyensure_json   s   r   c                   @  s(   e Zd ZdZdddZedd	d
ZdS )LenientStrEnumzA string enum allowing for case-insensitive lookups by value.

    May include other internal customizations if needed.

    Note: This is a bespoke, internal implementation and NOT intended as a
    backport of `enum.StrEnum` from Python 3.11+.
    r   r   c                 C  s   | j S N)name)selfr   r   r   __repr__-   s   zLenientStrEnum.__repr__valueobjectr   c                   s.   t |tr|  t fdd| D d S d S )Nc                 3  s"    | ]}|j   kr|V  qd S r   )r!   lower).0er   r   r   	<genexpr>5   s     z+LenientStrEnum._missing_.<locals>.<genexpr>)r   r   r#   next)clsr!   r   r   r   	_missing_0   s   
zLenientStrEnum._missing_N)r   r   )r!   r"   r   r   )__name__
__module____qualname____doc__r    classmethodr)   r   r   r   r   r   $   s
    
r   c                 C  s   | du rt | S )zA "before"-mode field validator that coerces `None` to the field default.

    See: https://docs.pydantic.dev/2.11/api/pydantic_core/#pydantic_core.PydanticUseDefault
    Nr
   r   r   r   r   default_if_none9   s   r/   c                 C  s   t | tr|   S | S r   )r   r   stripupperr   r   r   r   upper_if_strC   s   r2   c                 C  s`   ddl m}m} ddlm}m}m} t| |r|| S t| |r.| 	 r'|n|}|| S | S )zHConvert eligible objects (including wandb types) to an automation scope.r   )ArtifactCollectionProjectr   )ProjectScope_ArtifactPortfolioScope_ArtifactSequenceScope)
wandb.apis.publicr3   r4   scopesr5   r6   r7   r   model_validateis_sequence)r   r3   r4   r5   r6   r7   typr   r   r   parse_scopeH   s   



r=   c                 C  s   ddl m}m}m}m}m}m} t| |r#|ddd| jii| 	 S t| |r6|ddd| jii| 	 S t| |rD|di | 	 S | S )zBIf necessary (and possible), convert the object to a saved action.r   	DoNothingSavedNoOpActionSavedNotificationActionSavedWebhookActionSendNotificationSendWebhookintegrationidNr   )
actionsr?   r@   rA   rB   rC   rD   r   integration_id
model_dumpr   r?   r@   rA   rB   rC   rD   r   r   r   parse_saved_actionV   s     
	

rK   c                 C  s   ddl m}m}m}m}m}m} t| |r"|dd| jj	i| 
 S t| |r4|dd| jj	i| 
 S t| |rB|di | 
 S | S )zCIf necessary (and possible), convert the object to an input action.r   r>   rH   Nr   )rG   r?   r@   rA   rB   rC   rD   r   rE   rF   rI   rJ   r   r   r   parse_input_actionn   s    
	

rL   fr   c                 C  s   t t| S )zWrap a run filter in an `And` operator if it's not already.

    This is a necessary constraint imposed elsewhere by backend/frontend code.
    )r   wrapr   rM   r   r   r   wrap_run_event_run_filter   s   rP   c                 C  s   t tt| S )zWrap filters as `{"$or": [{"$and": [<original_filter>]}]}`.

    This awkward format is necessary because the frontend expects it.
    )r   rN   r   r   rO   r   r   r   wrap_mutation_event_filter   s   rQ   N)r   r   r   r   )rM   r   r   r   )"
__future__r   enumr   typingr   r   r   pydanticr   r   r	   pydantic_corer   wandb._pydanticr   _filtersr   r   r   _filters.filterutilsr   r   r   JsonEncodedr   r   r/   r2   r=   rK   rL   rP   rQ   r   r   r   r   <module>   s(    








