o
    )TiR                     @   s`  U 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 d dlmZ d dl	m
Z
mZmZmZmZmZmZ ddlmZ ddlmZmZmZmZmZmZmZmZmZmZmZmZ ddl m!Z!m"Z"m#Z# d	gZ$ed
Z%edZ&e' Z(e&rte()e& ee&de( G dd deZ*e+e,e-e.f e*f Z/edddZ0eee"  e1d< edddZ2ee. e1d< edddZ3eee  e1d< edddZ4eee.e5f  e1d< edddZ6ee. e1d< edddZ7ee. e1d< edddZ8ee. e1d< edddZ9eee5  e1d< edddZ:eee/  e1d< edg dZ;ee<e+  e1d< edddZ=eee'  e1d< edddZ>ee. e1d< e0e2e3e4e6e7e8e9e:e;e=e>dZ?edd  Z@dddddd!ZAd"e5d#e.fd$d%ZBdddddddddddd&d'ee. d(ee. d)ee. d*ed+ d,ee. d-ee. d.ee. d/ee. d0ee. d1ee<ee-e
e-ge.f f   d2ee<ee-e5f   d#dfd3d	ZCd"e5fd4d5ZDd6e!d7e jEfd8d9ZFdpd:d;ZGd<d= ZHd#e.fd>d?ZId#e.fd@dAZJd#e.fdBdCZKd#e.fdDdEZLd#e.fdFdGZMdHdI ZNdJdK ZOdLdM ZPd#e.fdNdOZQe' ZRe'e- e1dP< e' ZSe'e- e1dQ< eQeeeKdRZTe+e5e
e-gee.eUf f f e1dS< d#e.fdTdUZV		dqd1ee<ee-e
e-ge.f f   d2ee<ee-e5f   d#dfdVdWZWdXe-e% d#e%fdYdZZXG d[d\ d\ZYd#e*fd]d^ZZh d_Z[e\d`Z]e]^e_  de]_`dadb Zadcee.e5e+ejbf fdddeZcG dfdg dgZdd#e.fdhdiZeee rdjejfdk< G dldm dmede jEZEG dndo doeEZgdS )r    N)contextmanager)
ContextVar)CallableGenericOptionalProtocolTypeVarUnion_GenericAlias   )	Namespace)_set_config_read_mode_set_docstring_parse_options capture_typing_extension_shadowsget_alias_targetget_annotated_base_typeimport_reconploggeris_alias_typeis_annotatedis_attrs_classis_pydantic_modelreconplogger_supporttyping_extensions_import)ActionsContainerArgumentParserdocstring_parserset_parsing_settings	ClassType_UnpackAlias_UnpackGenericAliasc                   @   s"   e Zd Zdee defddZdS )InstantiatorCallable
class_typereturnc                 O   s   d S N )selfr!   argskwargsr$   r$   H/home/ubuntu/.local/lib/python3.10/site-packages/jsonargparse/_common.py__call__2   s   zInstantiatorCallable.__call__N)__name__
__module____qualname__typer   r)   r$   r$   r$   r(   r    1   s    r    parent_parser)defaultparser_captureFdefaults_cachelenient_checkparsing_defaultssingle_subcommandTvalidating_defaultsload_value_modeclass_instantiatorsnested_linksapplied_instantiation_linkspath_dump_preserve_relative)r.   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   c               
   k   sx    g }|   D ]\}}t| }||}|||f qzd V  W |D ]	\}}|| q#d S |D ]	\}}|| q1w r#   )itemsparser_context_varssetappendreset)r'   context_var_tokensnamevaluecontext_vartokenr$   r$   r(   parser_contextW   s   
rE   )validate_defaultsparse_optionals_as_positionalsadd_print_completion_argumentstubs_resolver_allow_py_files$omegaconf_absolute_to_relative_pathsrA   r"   c                 C   s8   t | d}| }|dvrtd|  d| |dkS )N >   rK   truefalsez/Invalid boolean value for environment variable z: rL   )osgetenvlower
ValueError)rA   	raw_valuerB   r$   r$   r(   get_env_var_booln   s
   rS   rF   config_read_mode_urls_enabledconfig_read_mode_fsspec_enableddocstring_parse_style$docstring_parse_attribute_docstringsrG   rH   rI   rJ   subclasses_disabledsubclasses_enabledrF   rU   rV   rW   zdocstring_parser.DocstringStylerX   rG   rH   rI   rJ   rY   rZ   c                 C   s@  t | tr
| td< n| durtd|  d|durt|d |dur(t|d |dur1t|d |dur:t|d t |trD|td	< n|durPtd
| dt |trZ|td< n|durftd| dt |trp|td< n|dur|td| dt |tr|td< n|durtd| d|	s|
rt|	|
d dS dS )a3
  
    Modify global parser settings that affect parser creation and parsing behavior.

    Args:
        validate_defaults: Whether default values must be valid according to the
            argument type. The default is ``False``, meaning no default
            validation, like in argparse.
        config_read_mode_urls_enabled: Whether to read config files from URLs
            using requests package. Default is ``False``.
        config_read_mode_fsspec_enabled: Whether to read config files from
            fsspec supported file systems. Default is ``False``.
        docstring_parse_style: The docstring style to expect. Default is
            ``DocstringStyle.AUTO``.
        docstring_parse_attribute_docstrings: Whether to parse attribute
            docstrings (slower). Default is ``False``.
        parse_optionals_as_positionals: If ``True``, the parser will take extra
            positional command line arguments as values for optional arguments.
            This means that optional arguments can be given by name
            ``--key=value`` as usual, but also as positional. The extra
            positionals are applied to optionals in the order that they were
            added to the parser. By default, this is ``False``.
        add_print_completion_argument: If ``True``, top-level parsers
            automatically include ``--print_completion`` argument when
            ``shtab`` is installed.
        stubs_resolver_allow_py_files: Whether the stubs resolver should search
            in ``.py`` files in addition to ``.pyi`` files.
        omegaconf_absolute_to_relative_paths: If ``True``, when loading configs
            with ``omegaconf+`` parser mode, absolute interpolation paths are
            converted to relative. This is only intended for backward
            compatibility with ``omegaconf`` parser mode.
        subclasses_disabled: List of types or functions, so that when parsing
            only the exact type hints (not their subclasses) are accepted.
            Descendants of the configured types are also disabled. Functions
            should return ``True`` for types to disable.
        subclasses_enabled: List of types or disable function names, so that
            subclasses are accepted. Types given here have precedence over those
            in ``subclasses_disabled``. Giving a function name removes the
            corresponding function from ``subclasses_disabled``. By default, the
            following disable functions are registered: ``is_pure_dataclass``,
            ``is_pydantic_model``, ``is_attrs_class`` and ``is_final_class``.
    rF   Nz-validate_defaults must be a boolean, but got .)urls_enabled)fsspec_enabled)style)attribute_docstringsrG   z:parse_optionals_as_positionals must be a boolean, but got rH   z9add_print_completion_argument must be a boolean, but got rI   z9stubs_resolver_allow_py_files must be a boolean, but got rJ   z@omegaconf_absolute_to_relative_paths must be a boolean, but got )rY   rZ   )
isinstanceboolparsing_settingsrQ   r   r   subclass_type_behaviorrT   r$   r$   r(   r   v   sH   
8














c                 C   s>   | t vrtd|  d| dkrd}|tjv rt|S t |  S )NzUnknown parsing setting r[   rH   *JSONARGPARSE_ADD_PRINT_COMPLETION_ARGUMENT)rb   rQ   rN   environrS   )rA   var_namer$   r$   r(   get_parsing_setting   s   
rg   	containeractionc              
   C   s   |j d u stdrt|dsd S z5ddlm} t| |r| j} t| dd |j }d |_ |||_ W d    W d S 1 s>w   Y  W d S  t	yY } zt
d| |d }~ww )NrF   _check_typer   )ArgumentGroupT)r.   r5   zDefault value is not valid: )r/   rg   hasattr_corerk   r`   parserrE   _check_type_	ExceptionrQ   )rh   ri   rk   r/   exr$   r$   r(   validate_default   s   
&rr   c           
      C   s   ddl m}m}m}m} ddlm} ddlm} g }|| j	D ]'}	t
|	||||fr+q|j|	ddr3q|	jdvr9q|sA|	jg krAq||	 q|S )Nr   )ActionConfigFile
ActionFail_ActionConfigLoadfilter_non_parsing_actions)PrintCompletionAction)ActionTypeHintF)all_subtypes>   Nr   )jsonargparse._actionsrs   rt   ru   rv   jsonargparse._completionsrw   jsonargparse._typehintsrx   _actionsr`   is_subclass_typehintnargsoption_stringsr>   )
rn   include_positionalsrs   rt   ru   rv   rw   rx   actionsri   r$   r$   r(   $get_optionals_as_positionals_actions   s   
r   c                 C   s   t do| j ot| dd S )NrG   _inner_parserF)rg   _subcommands_actiongetattr)rn   r$   r$   r(   !supports_optionals_as_positionals  s
   r   c                 C   sP   zt |}t| rt| |W S t| rt| j|W S W dS  ty'   Y dS w )zGExtension of issubclass that supports non-class arguments and generics.F)get_generic_originsinspectisclass
issubclassis_generic_class
__origin__	TypeError)clsclass_or_tupler$   r$   r(   is_subclass  s   
r   c                 C   s   t |}t| |S )z/Extension of isinstance that supports generics.)r   r`   )objr   r$   r$   r(   is_instance  s   
r   c                 C   s   t | ddS )zFChecks whether a class is final, i.e. decorated with ``typing.final``.	__final__F)r   r   r$   r$   r(   is_final_class#  s   r   c                 C   s   t | tot| dddkS )Nr+   rK   typing)r`   r
   r   r   r$   r$   r(   r   (  s   r   c                    s   t  fddtD S )Nc                 3       | ]}t  |V  qd S r#   )r`   ).0unpack_typer   r$   r(   	<genexpr>-      z%is_unpack_typehint.<locals>.<genexpr>)anyunpack_meta_typesr   r$   r   r(   is_unpack_typehint,  s   r   c                 C   s   t | r| jS | S r#   )r   r   r   r$   r$   r(   get_generic_origin0  s   r   c                 C   s$   t | trtdd | D S t| S )Nc                 s   s    | ]}t |V  qd S r#   )r   )r   r   r$   r$   r(   r   6      z&get_generic_origins.<locals>.<genexpr>)r`   tupler   )r   r$   r$   r(   r   4  s   
r   c                 C   s:   | }	 |}t |rt|}t|rt|}||kr	 |S qr#   )r   r   r   r   )r   new_clscur_clsr$   r$   r(   get_unaliased_type:  s   r   c                 C   s&   dd t | D }tdd |D S )Nc                 S   s   g | ]
}|t thvr|qS r$   )objectr   r   cr$   r$   r(   
<listcomp>H  s    z%is_pure_dataclass.<locals>.<listcomp>c                 s   s    | ]}t |V  qd S r#   )dataclassesis_dataclassr   r$   r$   r(   r   I  r   z$is_pure_dataclass.<locals>.<genexpr>)r   getmroall)r   classesr$   r$   r(   is_pure_dataclassG  s   r   subclasses_enabled_typessubclasses_disabled_types)r   r   r   r   subclasses_disabled_selectorsc                    st   t  r	t jS t sdS t fddt D }|s*t fddtD }|r8t fddt	D  }|S )NFc                 3   s    | ]}| V  qd S r#   r$   )r   selectorr   r$   r(   r   [  r   z)is_subclasses_disabled.<locals>.<genexpr>c                 3   r   r#   r   )r   disable_typer   r$   r(   r   ]  r   c                 3   r   r#   r   )r   enable_typer   r$   r(   r   _  r   )
r   is_subclasses_disabledr   r   r   r   r   valuesr   r   )r   subclass_disabledr$   r   r(   r   V  s   

r   c                 C   s   |pg D ]*}t |tr|tvrtd| dt| qt|r(t| qtd|| p2g D ]}t|r@t	| q3t
|rK|t|j< q3td|dS )z8Configures whether class types accept or not subclasses.zThere is no function 'z#' registered in subclasses_disabledzIExpected 'subclasses_enabled' list items to be types or strings, but got zLExpected 'subclasses_disabled' list items to be types or functions, but got N)r`   strr   rQ   popr   r   r   addr   
isfunctionr*   )rY   rZ   enable_itemdisable_itemr$   r$   r(   rc   c  s&   



rc   r!   c                 O   s   | |i |S r#   r$   )r!   r&   r'   r$   r$   r(   default_class_instantiator  s   r   c                   @   s4   e Zd ZdeddfddZdee defddZdS )	ClassInstantiatorinstantiatorsr"   Nc                 C   s
   || _ d S r#   r   )r%   r   r$   r$   r(   __init__  s   
zClassInstantiator.__init__r!   c           	      O   s   | j  D ]:\\}}}||u s|r?t||r?tt|j}d|v r2t p(t }dd |D |d< ||g|R i |  S qt	|g|R i |S )Nr9   c                 S   s   i | ]	}|j d  |jqS )r   )targetapplied_value)r   ri   r$   r$   r(   
<dictcomp>  s    z.ClassInstantiator.__call__.<locals>.<dictcomp>)
r   r;   r   r=   r   	signature
parametersr9   getr   )	r%   r!   r&   r'   r   
subclassesinstantiatorparam_namesapplied_linksr$   r$   r(   r)     s   
zClassInstantiator.__call__)r*   r+   r,   InstantiatorsDictTyper   r-   r   r)   r$   r$   r$   r(   r     s    r   c                  C   s   t  } | stS t| S r#   )r7   r   r   r   r   r$   r$   r(   get_class_instantiator  s   r   >   INFODEBUGERRORWARNINGCRITICALjsonargparse_null_loggerc                 C   s   |}t | tr
| }nt | trd| v r| d }t|}d |_t|jdkr7t }|	t
d || tt|}|jD ]}|| q?|S )NrA   r   z4%(asctime)s - %(name)s - %(levelname)s - %(message)s)r`   r   dictlogging	getLoggerparentlenhandlersStreamHandlersetFormatter	Formatter
addHandlerr   setLevel)datalevelcallerrA   loggerhandlerr$   r$   r(   setup_default_logger  s   




r   r   c                 C   s  t | ttttjfstd|  dt | tr4tt| ddh dkr4dd | 	 D }td| d| d	u r:t
S d
}t | trId| v rI| d }|tvrXtd|dt d| du set | tr{d| vr{tr{t rodddni }tdjdi |} t | tjst| ||} | S )NzPExpected logger to be an instance of (bool, str, dict, logging.Logger), but got r[   rA   r   r   c                 S   s   i | ]\}}|d vr||qS )>   rA   r   r$   r   kvr$   r$   r(   r     s    z parse_logger.<locals>.<dictcomp>z%Unexpected data to configure logger: Fr   zGot logger level z but must be one of Tr   )r   reloadparse_loggerr$   )r`   ra   r   r   r   LoggerrQ   r   r=   r;   null_loggerlogging_levelsr   debug_mode_activer   logger_setupr   )r   r   rB   r   r'   r$   r$   r(   r     s$   "r   c                       sp   e Zd ZdZdddeeeeej	f f fddZ
edej	fdd	Zejdeeeeej	f fd
d	Z  ZS )LoggerPropertyzIClass designed to be inherited by other classes to add a logger property.F)r   r   c                   s   || _ t j|i | d S r#   )r   superr   )r%   r   r&   r'   	__class__r$   r(   r     s   zLoggerProperty.__init__r"   c                 C   s   | j S )as  The logger property for the class.

        :getter: Returns the current logger.
        :setter: Sets the given logging.Logger as logger or sets the default logger
                 if given True/str(logger name)/dict(name, level), or disables logging
                 if given False.

        Raises:
            ValueError: If an invalid logger value is given.
        )_loggerr%   r$   r$   r(   r     s   zLoggerProperty.loggerc                 C   sX   |d u rddl m}m} |tjd f|dd d}|s!t r!ddi}t|t| j| _	d S )Nr   )deprecation_warninglogger_property_none_message   )
stacklevelFr   r   )
_deprecatedr   r   r   r   r   r   r-   r*   r   )r%   r   r   r   r$   r$   r(   r     s   
)r*   r+   r,   __doc__r	   ra   r   r   r   r   r   propertyr   setter__classcell__r$   r$   r   r(   r     s    &&r   c                   C   s   t dS )NJSONARGPARSE_DEBUG)rS   r$   r$   r$   r(   r     s   r   r   LOGGER_LEVELc                   @   s   e Zd ZdZdd ZdS )Actionz%Base for jsonargparse Action classes.c                    sF   t  dstt jj _ fdd| D } j|fi |S )N_check_type_kwargsc                    s    i | ]\}}| j v r||qS r$   )r  r   r   r$   r(   r     s     z'Action._check_type_.<locals>.<dictcomp>)rl   r=   r   r   rj   r   r  r;   )r%   rB   r'   r$   r   r(   ro     s   
zAction._check_type_N)r*   r+   r,   r   ro   r$   r$   r$   r(   r    s    r  c                   @   s   e Zd ZdZdS )NonParsingActionz-Base for jsonargparse utility Action classes.N)r*   r+   r,   r   r$   r$   r$   r(   r     s    r  )F)NN)hargparser   r   r   rN   
contextlibr   contextvarsr   r   r   r   r   r   r   r	   r
   
_namespacer   
_optionalsr   r   r   r   r   r   r   r   r   r   r   r   _type_checkingr   r   r   __all__r   r   r=   r   r   r    r   r   r-   ra   r   r.   __annotations__r0   r1   r2   r   r3   r4   r5   r6   r7   r8   listr9   r:   r<   rE   rb   rS   r   rg   r  rr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   intr   rc   r   r   r   r   r   r   r   NullHandlerr   r   r   r   r   r   re   r  r$   r$   r$   r(   <module>   s  
 $
8
 
	
	

d

(
	
!

