o
    )Ti1                     @   s\  d Z ddl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 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mZmZmZ ddl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, ddl-m.Z.m/Z/ dgZ0G dd deZ1G dd dZ2G dd deeZ3	ddee+e3f dee de4fddZ5dS )zFormatter classes.    N)OPTIONALSUPPRESSZERO_OR_MOREActionHelpFormatter_HelpAction_SubParsersAction)Iterable)StringIO)Template)OptionalUnion   )ActionConfigFileActionYesNo_ActionConfigLoadfilter_non_parsing_actionsnon_parsing_actions)defaults_cache$get_optionals_as_positionals_actionsparent_parser!supports_optionals_as_positionals)HelpFormatterDeprecations)
ActionLink)	Namespace
NSKeyError)import_ruamel)ActionSubCommandsfind_action)ArgumentParserruamelCommentedMap)ActionTypeHinttype_to_strDefaultHelpFormatterc                   @   s   e Zd ZdZdZdS )PercentTemplate%z
    \%\((?:
    (?P<escaped>\%\%)|
    (?P<named>[_a-z][_a-z0-9]*)\)s|
    (?P<braced>[_a-z][_a-z0-9]*)\)s|
    (?P<invalid>)
    )
    N)__name__
__module____qualname__	delimiterpattern r+   r+   L/home/ubuntu/.local/lib/python3.10/site-packages/jsonargparse/_formatters.pyr$   *   s    r$   c                   @   sv   e Zd ZdZdefddZdedefddZd	edefd
dZ	d	ededede
fddZd	ededede
fddZdS )YAMLCommentFormatterz@Formatter class for adding YAML comments to configuration files.help_formatterc                 C   s
   || _ d S N)r.   )selfr.   r+   r+   r,   __init__9   s   
zYAMLCommentFormatter.__init__cfgreturnc                    s  t d}| }||}d fdd	 t  }|d< i | D ]8\}j|< |du r5dn|d }jD ]!}t|j	}dd |D }d	d
 |D }	|	D ]	}
|j
||
 < qSq<q&dfdd	jduruj| | t }||| | S )z Adds help text as yaml comments.add_yaml_comments c                    s\   i }| j d ur,| j jd j D ]\}}|r|d nd| }|||< | ||d q|S )Nr   .r5   )prefix)_subparsers_group_actionschoicesitemsupdate)parserr7   
subparserskey	subparserfull_key)get_subparsersr+   r,   rB   B   s   
z>YAMLCommentFormatter.add_yaml_comments.<locals>.get_subparsersNr6   c                 S   s    g | ]}t |tttfs|qS r+   )
isinstancer   r   r   .0ar+   r+   r,   
<listcomp>U   s
    z:YAMLCommentFormatter.add_yaml_comments.<locals>.<listcomp>c                 S   s&   h | ]}d |j v rtdd|j qS )r6   z	\.?[^.]+$r5   )destresubrD   r+   r+   r,   	<setcomp>X   s   & z9YAMLCommentFormatter.add_yaml_comments.<locals>.<setcomp>r   c                    s   |   D ]X}|r|d nd| }t|}d }| v r't| | tr' | }n|d ur6|jtkr6j|}t| | trR|rG|| || | | ||d  q|r\	|| || qd S )Nr6   r5   r   )
keysr   rC   dicthelpr   r.   _expand_helpset_yaml_group_commentset_yaml_argument_comment)r2   r7   depthr?   rA   actiontext)group_titlesr=   r0   set_commentsr+   r,   rV   \   s    

z<YAMLCommentFormatter.add_yaml_comments.<locals>.set_comments)r5   )r5   r   )r   YAMLloadr   getr;   description_action_groupsr   r9   titleset_yaml_start_commentr
   dumpgetvalue)r0   r2   ruyamlyamlparsers
parser_keyr7   groupactionsrL   r?   outr+   )rB   rU   r=   r0   rV   r,   r4   <   s8   
	


	
z&YAMLCommentFormatter.add_yaml_commentsrT   c                 C   s   | | dS )zSets the start comment to a ruamel.yaml object.

        Args:
            text: The content to use for the comment.
            cfg: The ruamel.yaml object.
        N)yaml_set_start_comment)r0   rT   r2   r+   r+   r,   r]   s   s   z+YAMLCommentFormatter.set_yaml_start_commentr?   rR   c                 C      |j |d| d| d dS )a  Sets the comment for a group to a ruamel.yaml object.

        Args:
            text: The content to use for the comment.
            cfg: The parent ruamel.yaml object.
            key: The key of the group.
            depth: The nested level of the group.
        
   beforeindentN!yaml_set_comment_before_after_keyr0   rT   r2   r?   rR   r+   r+   r,   rP         z+YAMLCommentFormatter.set_yaml_group_commentc                 C   rh   )a  Sets the comment for an argument to a ruamel.yaml object.

        Args:
            text: The content to use for the comment.
            cfg: The parent ruamel.yaml object.
            key: The key of the argument.
            depth: The nested level of the argument.
        ri   rj   rk   Nrn   rp   r+   r+   r,   rQ      rq   z.YAMLCommentFormatter.set_yaml_argument_commentN)r&   r'   r(   __doc__r   r1   strr4   r    r]   intrP   rQ   r+   r+   r+   r,   r-   6   s6    7

r-   c                       s   e Zd ZdZdedefddZdedef fddZdef fdd	Zdedef fd
dZ	dedefddZ
dedefddZdedee fddZdee dee ddf fddZ  ZS )r#   a  Help message formatter that includes types, default values and env var names.

    This class is an extension of `argparse.HelpFormatter
    <https://docs.python.org/3/library/argparse.html#argparse.HelpFormatter>`_.
    Default values are always included. Furthermore, if the parser is configured
    with ``default_env=True`` command line options are preceded by ``ARG:`` and
    the respective environment variable name is included preceded by ``ENV:``.
    rS   r3   c                 C   s&  t |ddr|jp
dS |jpd}t|tr|S t|tr4|d  |dd   }|d dkr2|d7 }|S d}t|do=|j}|rBd	}d
|vrW| |d urW||rRdndd 7 }d|vr{|j	t
kr{|j	d usg|s{|jsq|jtthv r{||rvdndd 7 }t|tr|| 7 }||rd| d  S d S )N_jsonargparse_preexpanded_helpFr5   r   r   r6   	_requiredrequiredz%(type), ztype: %(type)sz
%(default)zdefault: %(default)sz ())getattrrN   rC   r   r   upperhasattrrw   _get_type_strdefaultr   option_stringsnargsr   r   r!   
extra_help)r0   rS   action_helphelp_stris_requiredr+   r+   r,   _get_help_string   s4   





z%DefaultHelpFormatter._get_help_stringc                    sh   |j d u r.|j tkr.| |}|r.| r.||_ d|_zt |W |`d |_ S |`d |_ w t |S )NT)rN   r   rO   stripru   super_format_action)r0   rS   	help_text	__class__r+   r,   r      s   
z#DefaultHelpFormatter._format_actionc              	      sz  t |}t j||g|R i |}t }|s|S t|rt|}t|dkrd}t|D ]}|r:|j	 d| n|j	}d| d}q.|
 d}|d d | }	| j| j }
t|dkset|	|
krj|	|d< ntd	d
|d }|||  d}d|d| d }|S |jD ].}z||}W n ty   d }Y nw |d u rd| d|v rtjd| dd
|dd}q|S )Nr   r5    []ri   rv   r   z^( +)[^ ].*$z\1zInote: extra positionals are parsed as optionals in the order shown above.z

z[--z\[(--z	 [^\]]+)])count)r   r   _format_usager   rY   r   r   lenreversedrH   rstripsplit_width_current_indentrI   rJ   appendjoinrequired_argsget_defaultr   )r0   usagere   argskwargsr=   extra_positionalsrS   usage_lines	last_line
text_widthrm   noter?   r   r   r+   r,   r      s@   

z"DefaultHelpFormatter._format_usagec                    s   t  }|d us
J t|tr!d}|jrdt| | d| }|S |js*t |S t|tj	r6t |S d}t|t
sF|dt| | 7 }dt | | S )NzAvailable subcommands:zENV:   z

  r5   z

  ENV:   zARG:   )r   rY   rC   r   default_envget_env_varr   _format_action_invocationr   _ChoicesPseudoActionr   )r0   rS   r=   valueextrr   r+   r,   r      s   

z.DefaultHelpFormatter._format_action_invocationc                 C   s   |j dd  S )Nr6   rv   )rH   rsplitr|   )r0   rS   r+   r+   r,   !_get_default_metavar_for_optional  s   z6DefaultHelpFormatter._get_default_metavar_for_optionalc                 C   s   t t|| jd}|dd ur ddd |d D }||d< | |}|d ur-||d< |j}|dtkr;|d= n1d|v rlt }|d urR||j	 |d< |_|d d u r]d|d< nt
|d trl|d  |d< t| ||}||_|S )	N)progr:   ry   c                 S   s   g | ]}t |qS r+   )rs   )rE   cr+   r+   r,   rG     s    z5DefaultHelpFormatter._expand_help.<locals>.<listcomp>typer   null)rM   vars_progrY   r   r~   r   r   r   rH   rC   r   as_dictr$   r   safe_substitute)r0   rS   paramschoices_strtype_strorig_defaultdefaultsr   r+   r+   r,   rO     s*   

z!DefaultHelpFormatter._expand_helpc                 C   sF   d }t |trd}|S |jd urt|j}|S t |tr!t|j}|S )Nbool)rC   r   r   r"   r!   	_typehint)r0   rS   r   r+   r+   r,   r~   )  s   




z"DefaultHelpFormatter._get_type_strr   re   Nc                    s.   dd |D }t  j||g|R i | d S )Nc                 S   s   g | ]	}t |ts|qS r+   )rC   r   rD   r+   r+   r,   rG   4  s    z2DefaultHelpFormatter.add_usage.<locals>.<listcomp>)r   	add_usage)r0   r   re   r   r   r   r+   r,   r   3  s    zDefaultHelpFormatter.add_usage)r&   r'   r(   rr   r   rs   r   r   r   r   r   rO   r   r~   r	   r   __classcell__r+   r+   r   r,   r#      s    	'*
parser_or_formatterrS   r3   c                 C   sh   t | tr
t }n| }|dusJ d}t |jtr#|jddd }|r*||j7 }|dd }|S )zQReturns the environment variable name for a given parser or formatter and action.Nr5   -_r6   __)	rC   r#   r   rY   
env_prefixrs   replacerH   r|   )r   rS   r=   env_varr+   r+   r,   r   8  s   


r   r/   )6rr   rI   argparser   r   r   r   r   r   r   collections.abcr	   ior
   stringr   typingr   r   _actionsr   r   r   r   r   _commonr   r   r   r   _deprecatedr   _link_argumentsr   
_namespacer   r   
_optionalsr   _subcommandsr   r   _type_checkingr   r    
_typehintsr!   r"   __all__r$   r-   r#   rs   r   r+   r+   r+   r,   <module>   s:    $	m 
