o
    )Ti                     @   s   d Z ddlmZmZmZmZ ddlmZ ddlm	Z	m
Z
 ddlmZ ddlmZmZ ddlmZmZ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gZG dd de	ZdS )zActions to support jsonnet.    )AnyDictOptionalUnion   )_is_action_value_list)Actionparser_context)ActionJsonSchema)get_loader_exceptions
load_value)_get_config_read_modeget_jsonschema_exceptionsimport_jsonnetimport_jsonschemapyyaml_available)find_action)ActionTypeHint)NoneTypePathargument_errorActionJsonnetc                	       s   e Zd ZdZ		ddee deeeef  f fddZdd Z	d	d
 Z
edd Zedeeeef  deeeef eeef f fddZ		ddeeef deeeef  dedefddZ  ZS )r   zGAction to parse a Jsonnet, optionally validating against a JSON Schema.Next_varsschemac                    s  d|vrqt d t|ttfstd|| _|durltdd }t|tr]tr)dnd}t|d% zt	|}W n t
|yM } ztd	| |d}~ww W d   n1 sXw   Y  || t||| _dS d| _dS |d
| _|d| _t jdi | dS )aa  Initializer for ActionJsonnet instance.

        Args:
            ext_vars: Key where to find the external variables required to parse the Jsonnet.
            schema: Schema to validate values against.

        Raises:
            ValueError: If a parameter is invalid.
            jsonschema.exceptions.SchemaError: If the schema is invalid.
        
_validatorr   z+ext_vars has to be either None or a string.Nr   yamljsonload_value_modezProblems parsing schema: 	_ext_vars )r   
isinstancestrr   
ValueErrorr   r   r   r	   r   r   check_schemar
   "_extend_jsonvalidator_with_defaultr   popsuper__init__)selfr   r   kwargsjsonvalidatormodeex	__class__r    I/home/ubuntu/.local/lib/python3.10/site-packages/jsonargparse/_jsonnet.pyr(      s0   


zActionJsonnet.__init__c                 O   s   t |dkr9| j|d< | j|d< d|v r2d|d v r2| jdur2ddl}|d |j| jjdd |d< tdi |S t|d	 | j| j	|d
 |d	 d dS )zParses an argument as Jsonnet using ``ext_vars`` if defined.

        Raises:
            TypeError: If the argument is not valid.
        r   r   r   helpz%sNT)	sort_keysr      )cfgr    )
lenr   r   r   dumpsr   r   setattrdest_check_type_)r)   argsr*   r   r    r    r0   __call__A   s   

$zActionJsonnet.__call__c           	      C   s   t | }i }|r|| ji }|s|g}t|D ]R\}}zt|tr+| j||dd}n| jd ur6| j| |||< W q t	t
ft  t  yj } z|sOdndt|d  }t	d| j d| d| |d }~ww |ro|S |d	 S )
NT)r   	with_meta z	 element r   zParser key ""z: r   )r   getr   	enumerater!   r"   parser   validate	TypeErrorRuntimeErrorr   r   r8   )	r)   valuer4   islistr   numvalr-   elemr    r    r0   _check_typeR   s&   

zActionJsonnet._check_typec                 C   s   t |trQ|jrSt| |j}|std|j dt |to |j}|tthvr2td|j d| |j	d u r:i |_	t |j	tsLtd|j d|j	 d|_
d S d S d S )Nz No argument found for ext_vars=''zType for ext_vars='z ' argument must be dict, given: z Default value for the ext_vars='z(' argument must be dict or None, given: T)r!   r   r   r   r#   r   	_typehintdictr   defaultjsonnet_ext_vars)parseractionext_vars_actionext_vars_typer    r    r0   _check_ext_vars_actione   s&   


z$ActionJsonnet._check_ext_vars_actionreturnc                    sD   | du ri } ddl   fdd|  D }dd |  D } | |fS )zSplits an ``ext_vars`` dict into the ``ext_codes`` and ``ext_vars`` required by Jsonnet.

        Args:
            ext_vars: External variables. Values can be strings or any other basic type.
        Nr   c                    s&   i | ]\}}t |ts| |qS r    )r!   r"   r6   .0kvr   r    r0   
<dictcomp>   s   & z0ActionJsonnet.split_ext_vars.<locals>.<dictcomp>c                 S   s    i | ]\}}t |tr||qS r    )r!   r"   rV   r    r    r0   r[      s     )r   itemsr   	ext_codesr    rZ   r0   split_ext_varsy   s   zActionJsonnet.split_ext_varsFjsonnetr<   c              
   C   s  t d}| |\}}d}d}|}z	t|t d}W n	 ty#   Y nw t|tr.|ddn|}| }z&ttr:dndd	 t	|j
||||d
}	W d   n1 sTw   Y  W n tyq }
 ztd| d|
 |
d}
~
ww | jdur}| j|	 |r|dur||	d< ||	d< |	S )a  Method that can be used to parse Jsonnet independent from an :class:`.ArgumentParser`.

        Args:
            jsonnet: Either a path to a Jsonnet file or the Jsonnet content.
            ext_vars: External variables. Values can be strings or any other basic type.
            with_meta: Whether to include metadata in config object.

        Returns:
            The parsed Jsonnet object.

        Raises:
            TypeError: If the input is neither a path to an existent file nor a Jsonnet.
        r   Nsnippet)r,   F)absoluter   r   r   r]   zProblems evaluating Jsonnet 'z': __path____orig__)r   r_   r   r   rC   r!   get_contentr	   r   r   evaluate_snippetrD   r   r   rB   )r)   r`   r   r<   _jsonnetr^   fpathfnamera   valuesr-   r    r    r0   rA      s8   
zActionJsonnet.parse)NN)NF)__name__
__module____qualname____doc__r   r"   r   rM   r(   r;   rJ   staticmethodrT   r   tupler_   r   boolrA   __classcell__r    r    r.   r0   r      s4    '
8
N) rn   typingr   r   r   r   _actionsr   _commonr   r	   _jsonschemar
   _loaders_dumpersr   r   
_optionalsr   r   r   r   r   _subcommandsr   
_typehintsr   _utilr   r   r   __all__r   r    r    r    r0   <module>   s    