o
    )Ti5L                     @   s  U d Z ddlZddlZddlZddlZddlZddlZddlmZm	Z	m
Z
mZ ejdkr3ddlmZ neZddlmZmZ ddlmZmZ dd	lmZmZ dd
lmZmZmZ g dZejdejdej dej!dej"dej#diZ$dd e$% D Z&i Z'e(e)ef e*d< i Z+e(edf e*d< i Z,e(e-e	f e*d< 			dde-dede	de
e- de
e( de
e) defdd Z.	!	dde
e- ded"ee)e/e) f d#e-de
e- defd$d%Z0	dde-d&ee-ej1f de
e- defd'd(Z2d)d* Z3d+efd,d-Z4dd.e-de
e- defd/d0Z5G d1d dZ6e-de7e8e9fd2d3 d4dfd5ed6e	d7e
e	 d8eee: e)ee: d9f f d:e	d;e;d<e
e) ddfd=d>Z<d?e-fd@dAZ=de
e6 fdBdCZ>dd5ed<e
e) d:e
e	 fdDdEZ?dFZ@e0dGeAdHdIdJZBe0dKeAdLdMdJZCe0dNeDdHdOdJZEe0dPeDdLdQdJZFe0dReDdLdSgdTdJZGe0dUeDdHdVgdWdJZHe2dXdYdZdJZIe2d[d\d]dJZJe5d^d_dJZKe5d`dadJZLe5dbdcdJZMe5dddedJZNe5dfdgdJZOe<ejPe-e- e<eQ e=dheD e=di ejejRejSfD ]ZTe<eTe-eTeUdj qdkdl ZVe=dmeVdn doeeWeXf de-fdpdqZYdoe-deWfdrdsZZdoe-deXfdtduZ[e=dveYeZdw e=dxeYe[dw dydz Z\e]d{Z^e]d|Z_e]d}Z`d~d Zae<ebe\eadw G dd dZce<ec e=d dd Zddd Zeh dZfdd Zgdd Zh[@dS )z(Collection of types and type generators.    N)AnyCallableOptionalUnion)   
   )	TypeAlias   )is_final_classpath_dump_preserve_relative)finalpydantic_support)Pathchange_to_path_dir)get_import_pathget_private_kwargsimport_object)r   r
   register_typeextend_base_typerestricted_number_typerestricted_string_type	path_typePositiveIntNonNegativeIntPositiveFloatNonNegativeFloatClosedUnitIntervalOpenUnitInterval	SecretStrNotEmptyStrEmailr   Path_frPath_fcPath_dwPath_dcPath_drw>>=<<=z==z!=c                 C   s   i | ]\}}||qS  r*   ).0kvr*   r*   G/home/ubuntu/.local/lib/python3.10/site-packages/jsonargparse/typing.py
<dictcomp>7   s    r/   registered_typesRegisteredTyperegistered_type_handlersregistration_pendingname	base_typevalidation_fn	docstringextra_attrsregister_keyreturnc                    s   |t v rt | }|j| krtd|j d|S G  fddd}|dur5| D ]
\}}	t|||	 q*t| | fd|i}
t|
| |
S )a  Creates and registers an extension of base type.

    Args:
        name: How the new type will be called.
        base_type: The type from which the created type is extended.
        validation_fn: Function that validates the value on instantiation/casting. Gets two arguments: ``type_class``
            and ``value``.
        docstring: The ``__doc__`` attribute value for the created type.
        extra_attrs: Attributes set to the type class that the ``validation_fn`` can access.
        register_key: Used to determine the uniqueness of registered types.

    Raises:
        ValueError: If the type has already been registered with a different name.
    z4Same type already registered with a different name: .c                       s$   e Zd ZZZ fddZ  ZS )z"extend_base_type.<locals>.TypeCorec                    s    |  | | t | | |S N)_validation_fnsuper__new___typeclsr-   	__class__r*   r.   r?   ^   s   z*extend_base_type.<locals>.TypeCore.__new__)__name__
__module____qualname__r=   r@   r?   __classcell__r*   r5   r6   rC   r.   TypeCoreZ   s    rJ   N__doc__)r0   rE   
ValueErroritemssetattrtypeadd_type)r4   r5   r6   r7   r8   r9   registered_typerJ   keyvaluecreated_typer*   rI   r.   r   >   s   

r   andrestrictionsjoinc                    s<   t thvr
td|dvrtdt|tr|gn|}t|tr5tdd |D r5t fdd|D s>td j dtt| |f}d	d
 |D }d| d 	dd
 |D }| du r j} t
|D ]!\}\}}	| |dkrxd| d nd7 } | |jt|	dd 7 } qg||| d}
dd }t|  ||||
dS )a  Creates or returns an already registered restricted number type class.

    Args:
        name: Name for the type or ``None`` for an automatic name.
        base_type: One of ``{int, float}``.
        restrictions: Tuples of pairs (comparison, reference), e.g. ``('>', 0)``.
        join: How to combine multiple comparisons, one of ``{'or', 'and'}``.
        docstring: Docstring for the type class.

    Returns:
        The created or retrieved type class.
    z-Expected base_type to be one of {int, float}.>   orrU   z)Expected join to be one of {'or', 'and'}.c                 s   s&    | ]}t |tot|d kV  qdS )   N)
isinstancetuplelenr+   xr*   r*   r.   	<genexpr>   s   $ z)restricted_number_type.<locals>.<genexpr>c                 3   s0    | ]}|d  t v o|d  |d kV  qdS )r   r	   N_operators2r]   r5   r*   r.   r_      s   . z}Expected restrictions to be a list of tuples each with a comparison operator (> >= < <= == !=) and a reference value of type r;   c                 S   s    g | ]}t |d   |d fqS )r   r	   r`   r]   r*   r*   r.   
<listcomp>   s     z*restricted_number_type.<locals>.<listcomp> c                 S   s$   g | ]\}}d t |  t| qS )r-   )_operators1str)r+   oprefr*   r*   r.   rc      s   $ Nr   _ )_restrictions_expression_joinr@   c                    s   t |trt| d| jtkr"t |tr"t|s"t| d| |  fdd| jD }| jdkr:t	|rC| jdkrMt
|sOt| d| j d S d S )Nz not a numberz not an integerc                    s   g | ]	\}}| |qS r*   r*   )r+   
comparisonrh   vvr*   r.   rc          zArestricted_number_type.<locals>.validation_fn.<locals>.<listcomp>rU   rX   z! does not conform to restriction )rZ   boolrL   r@   intfloat
is_integerrk   rm   allanyrl   )rB   r-   checkr*   ro   r.   r6      s   

$z-restricted_number_type.<locals>.validation_fnr4   r5   r6   r9   r7   r8   )rs   rt   rL   rZ   r[   listrv   rE   sortedrW   	enumeraterf   replacer   )r4   r5   rV   rW   r7   r9   
expressionnumrn   rh   r8   r6   r*   rb   r.   r   l   sL   
r   regexc                 C   sJ   t |tr
t|}d|j }||td}dd }t| t||tf||dS )aC  Creates or returns an already registered restricted string type class.

    Args:
        name: Name for the type or ``None`` for an automatic name.
        regex: Regular expression that the string must match.
        docstring: Docstring for the type class.

    Returns:
        The created or retrieved type class.
    z	matching )_regexrl   r@   c                 S   s&   | j |st| d| j j d S )Nz# does not match regular expression )r   matchrL   patternrA   r*   r*   r.   r6      s   z-restricted_string_type.<locals>.validation_fnry   )rZ   rf   recompiler   r   )r4   r   r7   r~   r8   r6   r*   r*   r.   r      s    


r   c                 C   s
   t | tS r<   )rZ   r   )rS   
type_classr*   r*   r.   _is_path_type      
r   pathc                 C   s<   t | ts	tdt r| j| jkr| j| jdS t	| S )NzExpected a Path instance.)relativecwd)
rZ   r   rL   r   getr   absolute	_relative_cwdrf   )r   r*   r*   r.   _serialize_path   s   
r   modec                    s   t   d  ddt  }t|ddr-ddlm} |dd	 d
7 |d7 }|tf}|tv r9t| S G  fdddt }t	|fd|i}t
||td |S )a  Creates or returns an already registered path type class.

    Args:
        mode: The required type and access permissions among ``[fdrwxcuFDRWX]``.
        docstring: Docstring for the type class.

    Returns:
        The created or retrieved type class.
    Path_zpath rj   F)
skip_checkr	   )path_skip_check_deprecation   )
stacklevel_skip_checkz skip_checkc                       s,   e Zd ZZZZeZ fddZ  Z	S )zpath_type.<locals>.PathTypec                    s   t |tr7t|ddhkr7t|d  t j|d f| j| jd| W d    d S 1 s0w   Y  d S t j|f| j| jd| d S )Nr   r   )r   r   )rZ   dictsetr   r>   __init___moder   )selfr-   r,   rC   r*   r.   r     s
   $""z$path_type.<locals>.PathType.__init__)
rE   rF   rG   rl   r   r   r   r@   r   rH   r*   r   r4   r   rC   r.   PathType  s    r   rK   
type_check)r   _check_moderW   r{   r   _deprecatedr   rf   r0   rO   rP   r   )r   r7   kwargskey_namer   r9   r   restricted_typer*   r   r.   r      s    


r   c                   @   s^   e Zd Zdededee deee e	ee df f def
ddZ
d	d
 Zdd Zdd ZdS )r1   r   
serializerdeserializerdeserializer_exceptions.r   c                 C   s.   || _ || _|d u r|n|| _|| _|| _d S r<   )r   r   base_deserializerr   r   )r   r   r   r   r   r   r*   r*   r.   r     s
   
zRegisteredType.__init__c                    s   t  fdddD S )Nc                 3   s$    | ]}t |t  |kV  qd S r<   )getattr)r+   r,   otherr   r*   r.   r_   (  s   " z(RegisteredType.__eq__.<locals>.<genexpr>)r   r   r   )rv   r   r   r*   r   r.   __eq__'     zRegisteredType.__eq__c                 C   s   |  || jS r<   )r   r   r   rS   r*   r*   r.   is_value_of_type*  s   zRegisteredType.is_value_of_typec              
   C   s\   z|  |W S  | jy- } zt| jdt| j}td| d| }||_||d }~ww )NrE   zNot of type z: )r   r   r   r   rf   rL   parent)r   rS   extype_class_nameex2r*   r*   r.   r   -  s   zRegisteredType.deserializerN)rE   rF   rG   r   r   r   r   rO   	Exceptionr[   r   r   r   r   r*   r*   r*   r.   r1     s    
c                 C   s
   | j |kS r<   rC   )r-   tr*   r*   r.   <lambda>@  s   
 r   Tr   r   r   r   .r   fail_already_registereduniqueness_keyc                 C   s   t | std| t| ||||}t d|}|s3|r3t| r3|t|  kr+dS td|  d|t| < |durA| t|< dS dS )a  Registers a new type for use in jsonargparse parsers.

    Args:
        type_class: The class to be registered.
        serializer: Function that converts an instance of the class to a basic type.
        deserializer: Function that converts a basic type to an instance of the
            class. Default instantiates ``type_class``.
        deserializer_exceptions: Exceptions that deserializer raises when it fails.
        type_check: Function to check if a value is of ``type_class``. Gets as arguments the value and ``type_class``.
        fail_already_registered: Whether to fail if type has already been registered.
        uniqueness_key: Key to determine uniqueness of type.
    z'Expected type_class to be a class, got _fail_already_registeredNzType "zC" already registered with different serializer and/or deserializer.)	inspectisclassrL   r1   globalsr   get_registered_typer2   r0   )r   r   r   r   r   r   r   type_handlerr*   r*   r.   r   7  s   
r   import_pathc                    s    fddt < d S )Nc                      s   t tg R i S r<   )r   r   r*   argsr   r   r*   r.   r   ^  s    z,register_type_on_first_use.<locals>.<lambda>)r3   )r   r   r   r*   r   r.   register_type_on_first_use]  r   r   c                 C   sd   | t vr-ddlm} |tt t| }|tv rt|  W d    n1 s(w   Y  t | S )Nr   )suppress)	r2   
contextlibr   AttributeErrorrL   r   r3   popr   )r   r   r   r*   r*   r.   r   e  s   
r   c                 C   sf   |t vsJ | jt v rtd| j d| t | j< d|i}|d ur'||d< t| | jfi | d S )NzType name "z;" clashes with name already defined in jsonargparse.typing.r   r   )r0   rE   r   rL   r   r@   )r   r   r   r   r*   r*   r.   rP   p  s   rP   Fr   )r&   r   zint restricted to be >0)r7   r   )r'   r   u   int restricted to be ≥0r   zfloat restricted to be >0r   u   float restricted to be ≥0r   )r)   r	   u$   float restricted to be ≥0 and ≤1r   )r(   r	   z float restricted to be >0 and <1r   z
^.*[^ ].*$z2str restricted to not-empty pattern ``^.*[^ ].*$``r    z^[^@ ]+@[^@ ]+\.[^@ ]+$z?str restricted to the email pattern ``^[^@ ]+@[^@ ]+\.[^@ ]+$``frz*path to a file that exists and is readablefcz7path to a file that can be created if it does not existdwz0path to a directory that exists and is writeabledcz<path to a directory that can be created if it does not existdrwz=path to a directory that exists and is readable and writeablezdecimal.Decimalz	uuid.UUIDr   c                    sv    fdd}t  ts|  d}d v rd| }t| }|s#|  dd |  D }dd	lm} |d
i |S )Nc                      s   t d  d)Nz@Expected a string with form "h:m:s" or "d days, h:m:s" but got "")rL   r*   rS   r*   r.   raise_error  s   z+timedelta_deserializer.<locals>.raise_errorz7(?P<hours>\d+):(?P<minutes>\d+):(?P<seconds>\d[\.\d+]*)dayz(?P<days>[-\d]+) day[s]*, c                 S   s   i | ]	\}}|t |qS r*   )rt   )r+   rR   valr*   r*   r.   r/     rq   z*timedelta_deserializer.<locals>.<dictcomp>r   )	timedeltar*   )rZ   rf   r   r   	groupdictrM   datetimer   )rS   r   r   r   r   r   r*   r   r.   timedelta_deserializer  s   
r   zdatetime.timedelta)r   rS   c                 C   s   ddl m} ||  S )Nr   )	b64encode)base64r   decode)rS   r   r*   r*   r.   bytes_serializer     r   c                 C   s   ddl m} || S Nr   )	b64decode)r   r   rS   r   r*   r*   r.   bytes_deserializer  s   r   c                 C   s   ddl m} t|| S r   )r   r   	bytearrayr   r*   r*   r.   bytearray_deserializer  r   r   zbuiltins.bytes)r   r   zbuiltins.bytearrayc                 C   sV   | j dkr| jdkrd| j dS d| j d| j dS d| j d| j d| j  dS )Nr	   r   range()z, )stepstartstopr   r*   r*   r.   range_serializer  s
   

r   z	^(-?\d+)$z^(-?\d+),(-?\d+)$z^(-?\d+),(-?\d+),(-?\d+)$c                 C   s   |   } | drT| drT| dd dd} t| }|r'tt|d S t| }|r;tt|d t|d S t	| }|rTtt|d t|d t|d	 S t
d
)Nr   r      rd   rj   r	   rY   r   zXExpected 'range(<stop>)' or 'range(<start>, <stop>)' or 'range(<start>, <stop>, <step>)')strip
startswithendswithr}   re_range_stopr   rangers   re_range_start_stopre_range_start_stop_steprL   )rS   r   r*   r*   r.   range_deserializer  s   


$r   c                   @   sh   e Zd ZdZdefddZdefddZdefdd	Zd
e	de
fddZdefddZdefddZdS )r   z8Holds a secret string that serializes to ``**********``.rS   c                 C   s
   || _ d S r<   _valuer   r*   r*   r.   r     r   zSecretStr.__init__r:   c                 C   s   dS )Nz
**********r*   r   r*   r*   r.   __str__  s   zSecretStr.__str__c                 C   
   t | jS r<   )r\   r   r   r*   r*   r.   __len__  r   zSecretStr.__len__r   c                 C   s   t || jo| j|jkS r<   )rZ   rD   r   r   r*   r*   r.   r     r   zSecretStr.__eq__c                 C   r   r<   )hashr   r   r*   r*   r.   __hash__  r   zSecretStr.__hash__c                 C   s   | j S )z Returns the actual secret value.r   r   r*   r*   r.   get_secret_value  s   zSecretStr.get_secret_valueN)rE   rF   rG   rK   rf   r   r   rs   r   r   rr   r   r   r   r*   r*   r*   r.   r     s    r   zpydantic.SecretStrc                    s,   ddl m} |d| dfd  fdd}|S )Nr   )create_modelpydantic_model.pydantic_fieldc                    s    | dj S )Nr   r   r   r   r*   r.   deserialize	  s   z*pydantic_deserializer.<locals>.deserialize)pydanticr   )r   r   r   r*   r   r.   pydantic_deserializer  s   r  c                 C   sP   t }tttttttffD ]}t|ts||f}t	| |d r%|d } |S q|S )Nr   r	   )
rf   rs   rt   rr   rz   r   r   rZ   r[   
issubclass)r   r   baser*   r*   r.   pydantic_serializer  s   
r  >   pydantic.typespydantic.networkspydantic_core._pydantic_corepydantic_extra_typesc                 C   s&   t ot| otdd t| D S )Nc                 s   s     | ]}t |d dtv V  qdS )rF   rj   N)r   pydantic_type_modules)r+   r   r*   r*   r.   r_   &  s    z#is_pydantic_type.<locals>.<genexpr>)r   r   r   rw   getmro)r   r*   r*   r.   is_pydantic_type"  s
   r  c                 C   s^   ddl m} || r| j} t| sd S t| s-ddlm} t| t| t	| |t
fd d S d S )Nr	   )is_annotatedr   )ValidationError)r   r   r   r   )
_optionalsr  
__origin__r  r   r  r  r   r  r  	TypeError)r   r  r  r*   r*   r.   register_pydantic_type*  s   
r  )NNN)rU   Nr<   )irK   r   operatorospathlibr   systypingr   r   r   r   version_infor   
_TypeAliasrO   _commonr
   r   r  r   r   _pathsr   r   _utilr   r   r   __all__gtgeltleeqnere   rM   ra   r0   r   r[   __annotations__r2   r3   rf   r   rz   r   Patternr   r   r   r   r1   rL   r  r   r   rr   r   r   r   rP   r   rs   r   r   rt   r   r   r   r   r   r    r!   r"   r#   r$   r%   PathLikecomplex	PosixPathWindowsPath_pathrZ   r   bytesr   r   r   r   r   r   r   r   r   r   r   r   r  r  r
  r  r  r*   r*   r*   r.   <module>   s2   

2
L
'-!	

& 



