o
    )Ti+0                     @   s  U d Z ddlZddlmZ ddlmZ ddlmZ ddlm	Z	m
Z
mZmZ dgZh dZG d	d
 d
eZdedee fddZdedee fddZdedee fddZdedefddZdedef fddZd,ddZedd ZG dd dejZeeeZee e d< dZ!dedefdd Z"dedefd!d"Z#d#edefd$d%Z$d#eee	f defd&d'Z%dd(l&m'Z' e'd)rddl(Z(e(j)*ed*d+  dS dS )-z3Classes and functions related to namespace objects.    N)OrderedDict)Iterator)contextmanager)AnyCallableOptionalUnion	Namespace>   __orig____path____default_config__c                   @   s   e Zd Zdd ZdS )
NSKeyErrorc                 C   s   t | jd S )Nr   )strargsself r   K/home/ubuntu/.local/lib/python3.10/site-packages/jsonargparse/_namespace.py__str__   s   zNSKeyError.__str__N)__name__
__module____qualname__r   r   r   r   r   r      s    r   keyreturnc                 C   s
   |  dS )N.splitr   r   r   r   	split_key   s   
r   c                 C      |  ddS Nr      r   r   r   r   r   split_key_root      r"   c                 C   r   r    )rsplitr   r   r   r   split_key_leaf!   r#   r%   c                 C   s   t | d }|tv S )N)r%   	meta_keys)r   leaf_keyr   r   r   is_meta_key%   s   r)   cfgc                 C   s   | rt | td} | S )N	skip_keysrecreate_branchesr'   )r*   r   r   r   remove_meta*   s   r/   c                    s   | }t | ttfr1t | ts1t|  }t| d|  D ]\}} d u s'| vr.t| ||< q|S t | tr? fdd| D }|S )N__dict__c                    s   g | ]}t | qS r   )r.   .0vr+   r   r   
<listcomp>8   s    z%recreate_branches.<locals>.<listcomp>)	
isinstancer	   dictr   typegetattritemsr.   list)datar,   new_datar   valr   r+   r   r.   0   s   

r.   c                  c   s*    t j} tt _z	d V  W | t _d S | t _w N)argparser	   )namespace_classr   r   r   patch_namespace<   s   rA   c                
       s
  e Zd ZdZ fddZdedeeed  ef fddZdedeed ef fdd	Z	dedd fd
dZ
dededdf fddZdededdfddZdedefddZdeddfddZdedefddZdefddZdeeef fddZdejfddZd:d"ed#edeeeef  fd$d%Zd:d"ed#edee fd&d'Zd;d"edee fd(d)Zd!efd"ed*edee fd+d,Zd<d-edd fd.d/Z 	 d=de!d ef dee d0edd fd1d2Z"d>ded3edefd4d5Z#dedeed ef fd6d7Z$d>ded3edefd8d9Z%  Z&S )?r	   zJExtension of argparse's Namespace to support nesting and subscript access.c                    s   t |dkrt jdi | dS t |dks't |dks't|d tjtfs+tdt|d tr8|d  nt	|d  D ]\}}|| |< q@dS )a2  Initializer for Namespace instance.

        Instantiating a Namespace with initial values most commonly is done by
        providing keyword arguments, e.g. ``Namespace(name1=value1,
        name2=value2)``. Alternatively a single positional ``Namespace`` or
        ``dict`` object can be given.
        r   r!   zAExpected a single positional parameter of type Namespace or dict.Nr   )
lensuper__init__r5   r?   r	   r6   
ValueErrorr9   vars)r   r   kwargsr   r=   	__class__r   r   rD   I   s   ,2
zNamespace.__init__r   r   c                 C   s
  t |tstd|dd|v rtd| dt|}tdd |D r.td| dd	d
 |D }|d }| }d}t|dkrd|dd }|dd D ]-}t||sbt |try||v ry|| }|durxt |t	tfsx|d|f  S qR|d|f  S |||fS )ae  Parses a key for the nested namespace.

        Args:
            key: The key that is being parsed.

        Returns:
            Tuple with three elements corresponding to:
            - The leaf key.
            - The parent namespace object.
            - The parent namespace key.

        Raises:
            KeyError: When given invalid key.
        zKey must be a string, got: r    zSpaces not allowed in keys: "z".c                 s   s    | ]}|d kV  qdS ) Nr   r2   kr   r   r   	<genexpr>m   s    z'Namespace._parse_key.<locals>.<genexpr>zEmpty nested key: "c                 S   s   g | ]}t |qS r   )add_clash_markrL   r   r   r   r4   o       z(Namespace._parse_key.<locals>.<listcomp>r&   rK   r!   N)
r5   r   r   r   anyrB   joinhasattrr6   r	   )r   r   	key_splitr(   	parent_ns
parent_keysubkeyr   r   r   
_parse_keyY   s*   

zNamespace._parse_keyc                 C   s<   |  |\}}}|du st||std| d|||fS )z8Same as _parse_key but raises KeyError if key not found.NzKey "z" not found in namespace.)rX   rS   r   )r   r   r(   rU   rV   r   r   r   _parse_required_key~   s   
zNamespace._parse_required_keyc                 C   s@   | }t |D ]}tt||dtst||t  t||}q|S )zCreates a nested namespace object.

        Args:
            key: The key where the nested namespace is created.

        Returns:
            The created nested namespace.
        N)r   r5   r8   r	   setattr)r   r   rU   r   r   r   _create_nested_namespace   s   	z"Namespace._create_nested_namespacenamevalueNc                    s.   d|v r|  || dS t t|| dS )z1Sets an attribute to a possibly nested namespace.r   N)__setitem__rC   __setattr__rO   )r   r\   r]   rH   r   r   r_      s   zNamespace.__setattr__itemc                 C   sH   |  |\}}}|du r| |}t|tr|||< dS t||| dS )z,Sets an item to a possibly nested namespace.N)rX   r[   r5   r6   rZ   )r   r   r`   r(   rU   rV   r   r   r   r^      s   

zNamespace.__setitem__c                 C   s   |  |\}}}t||S )z.Gets an item from a possibly nested namespace.)rY   r8   r   r   r(   rU   _r   r   r   __getitem__   s   
zNamespace.__getitem__c                 C   s   |  |\}}}|j|= dS )z1Deletes an item from a possibly nested namespace.N)rX   r0   ra   r   r   r   __delitem__   s   zNamespace.__delitem__c                 C   sB   t |tsdS z
| |\}}}W n
 ty   Y dS w ||jv S )z8Checks if an item is set possibly in a nested namespace.F)r5   r   rY   KeyErrorr0   ra   r   r   r   __contains__   s   

zNamespace.__contains__c                 C   s
   t | jS )z4Returns False if namespace is empty, otherwise True.)boolr0   r   r   r   r   __bool__   s   
zNamespace.__bool__c                 C   s   i }t |  D ]K\}}t|tr| }n7t|tr4|i kr4tdd | D r4dd | D }nt|trM|g krMtdd |D rMdd |D }||t	|< q|S )z8Converts the nested namespaces into nested dictionaries.c                 s       | ]}t |tV  qd S r>   r5   r	   r1   r   r   r   rN          z$Namespace.as_dict.<locals>.<genexpr>c                 S   s   i | ]	\}}||  qS r   as_dict)r2   rM   r3   r   r   r   
<dictcomp>   s    z%Namespace.as_dict.<locals>.<dictcomp>c                 s   ri   r>   rj   r1   r   r   r   rN      rk   c                 S   s   g | ]}|  qS r   rl   r1   r   r   r   r4      rP   z%Namespace.as_dict.<locals>.<listcomp>)
rF   r9   r5   r	   rm   r6   allvaluesr:   del_clash_mark)r   dicr   r=   r   r   r   rm      s   

($zNamespace.as_dictc                 C   s*   t  }|  D ]
\}}t||| q|S )zEConverts the nested namespaces into a single argparse flat namespace.)r?   r	   r9   rZ   )r   flatr   r=   r   r   r   as_flat   s   zNamespace.as_flatFTbranchesnestedc                 c   sx    t |  D ]2\}}t|}t|tr4|r||fV  |sq||D ]\}}|d t| |fV  q#q||fV  qdS )zRReturns a generator of all leaf (key, value) items, optionally including branches.r   N)rF   r9   rq   r5   r	   )r   ru   rv   r   r=   rW   subvalr   r   r   r9      s   

zNamespace.itemsc                 c   s$    | j ||dD ]\}}|V  qdS )zDReturns a generator of all leaf keys, optionally including branches.)ru   rv   Nr9   )r   ru   rv   r   rb   r   r   r   keys   s   zNamespace.keysc                 c   s     |  |D ]\}}|V  qdS )zFReturns a generator of all leaf values, optionally including branches.Nrx   )r   ru   rb   r=   r   r   r   rp      s   zNamespace.values
key_filterc                    s    fdd|   D }|r:dd |D D ]%}t|}tt|d D ]}d|d|d  }||vr8|| q"q|jdd d	 |S )
zReturns a list of keys sorted by descending depth.

        Args:
            branches: Whether to include branch keys instead of only leaves.
            key_filter: Function that selects keys to exclude.
        c                    s   g | ]} |s|qS r   r   rL   rz   r   r   r4          z-Namespace.get_sorted_keys.<locals>.<listcomp>c                 S   s   g | ]}d |v r|qS )r   r   rL   r   r   r   r4      r|   r!   r   Nc                 S   s   t t|  S r>   )rB   r   )xr   r   r   <lambda>   s    z+Namespace.get_sorted_keys.<locals>.<lambda>r   )ry   r   rangerB   rR   appendsort)r   ru   rz   ry   r   rT   numrV   r   r{   r   get_sorted_keys   s   
zNamespace.get_sorted_keys	with_metac                 C   s   t | |rddS tdS )zCreates an new copy of the nested namespace.

        Args:
            with_meta: Whether to include metadata keys in the copy.
        Nr+   r-   )r   r   r   r   r   clone   s   zNamespace.clone
only_unsetc                 C   s   t |ts|std|r|| vr|| |< | S |r&t | |ts&t | |< |r,|d nd}| D ]\}}|r>|| | vrD|| || < q2| S )aY  Sets or replaces all items from the given nested namespace.

        Args:
            value: A namespace to update multiple values or other type to set in a single key.
            key: Branch key where to set the value. Required if value is not namespace.
            only_unset: Whether to only set the value if not set in namespace.
        z)Key is required if value not a Namespace.r   rK   )r5   r	   r   getr9   )r   r]   r   r   prefixrW   rw   r   r   r   update  s   


zNamespace.updatedefaultc              	   C   s&   z| | W S  t tfy   | Y S w r>   )re   	TypeError)r   r   r   r   r   r   r     s
   
zNamespace.getc                 C   s   |  |\}}}|| ||fS r>   )rY   ra   r   r   r   get_value_and_parent#  s   zNamespace.get_value_and_parentc                 C   s&   |  |\}}}|s|S |j||S r>   )rX   r0   pop)r   r   r   r(   rU   rb   r   r   r   r   '  s   zNamespace.pop)FT)F)T)NFr>   )'r   r   r   __doc__rD   r   tupler   rX   rY   r[   r   r_   r^   rc   rd   rg   rf   rh   r6   rm   r?   r	   rt   r   r9   ry   rp   r)   r   r:   r   r   r   r   r   r   r   __classcell__r   r   rH   r   r	   F   s@     %

$ 	

 clash_namesu   ​c                 C   s   | t v rt|  } | S r>   )r   
clash_markr   r   r   r   rO   2  s   rO   c                 C   s   | d t kr| dd  } | S )Nr   r!   )r   r   r   r   r   rq   8  s   rq   r;   c                 C   s   |   D ]=\}}t|trtdd |D rt|| |< qt|trAt|D ]\}}t|tr@tdd |D r@t|| | |< q&qtdi | S )Nc                 s   ri   r>   r5   r   rL   r   r   r   rN   @  rk   zexpand_dict.<locals>.<genexpr>c                 s   ri   r>   r   rL   r   r   r   rN   D  rk   r   )r9   r5   r6   ro   expand_dictr:   	enumerater	   )r;   rM   r3   nnvvr   r   r   r   >  s   
r   c                 C   s   t | } t| S r>   )r.   r   )r;   r   r   r   dict_to_namespaceI  s   r   )	find_specyamlc                 C   s   |  d| S )Nztag:yaml.org,2002:map)represent_mappingrm   )dr}   r   r   r   r~   T  s    r~   r>   )+r   r?   collectionsr   collections.abcr   
contextlibr   typingr   r   r   r   __all__r'   re   r   r   r:   r   r"   r%   rg   r)   r6   r/   r.   rA   r	   setdirr   __annotations__r   rO   rq   r   r   importlib.utilr   r   
SafeDumperadd_representerr   r   r   r   <module>   s<    

	 i