o
    si                     @   s\   d dl Z d dlZd dlmZ dddZdd Zdd Zd	d
 Zdd Z	dd Z
dddZdS )    N)parsec                 C   sr   dd }|du rt  }|  D ]&}||}| |  D ]}|| | | }|jd| | | | |d qq|S )a  Prepare an argparser from a dictionary.

    Args:
        dic (dict): Two-level config dictionary with unique bottom-level keys.
        parser (argparse.ArgumentParser, optional): If a parser already
            exists, add the keys from the dictionary on the top of it.

    Returns:
        argparse.ArgumentParser:
            Parser instance with groups corresponding to the first level keys
            and arguments corresponding to the second level keys with default
            values given by the values.
    c                 S   s&   | du rt S tt| trtS t| S )zIf the default value is None, replace NoneType by str_int_float.
        If the default value is boolean, look for boolean strings.N)str_int_float
isinstancestr2boolboolstr2bool_argtypevalue r   O/home/ubuntu/.local/lib/python3.10/site-packages/asteroid/utils/parser_utils.pystandardized_entry_type   s
   z9prepare_parser_from_dict.<locals>.standardized_entry_typeNz--)defaultr   )argparseArgumentParserkeysadd_argument_groupadd_argument)dicparserr   kgroupkk
entry_typer   r   r   prepare_parser_from_dict   s   	
r   c                 C   s2   t | rt| S t| rt| S t| tr| S dS )zType to convert strings to int, float (in this order) if possible.

    Args:
        value (str): Value to convert.

    Returns:
        int, float, str: Converted value.
    N)isintintisfloatfloatr   strr	   r   r   r   r   (   s   	
r   c                 C   s2   t | ts| S |  dv rdS |  dv rdS | S )zAType to convert strings to Boolean (returns input if not boolean))yestruey1T)nofalsen0F)r   r   lowerr	   r   r   r   r   9   s   
r   c                 C   s    t | } t| tr| S td)z+Argparse type to convert strings to BooleanzBoolean value expected.)r   r   r   r   ArgumentTypeErrorr	   r   r   r   r   E   s   

r   c                 C   $   zt |  W dS  ty   Y dS w )zComputes whether `value` can be cast to a float.

    Args:
        value (str): Value to check.

    Returns:
        bool: Whether `value` can be cast to a float.

    TF)r   
ValueErrorr	   r   r   r   r   M      
r   c                 C   r*   )zComputes whether `value` can be cast to an int

    Args:
        value (str): Value to check.

    Returns:
        bool: Whether `value` can be cast to an int.

    TF)r   r+   r	   r   r   r   r   ^   r,   r   Fc                    sx   | j  d i }| jD ]} fdd|jD }|||j< qtt tdk r)dnd}|| |d< ||= |r:| fS |S )a4  Get a dict of dicts out of process `parser.parse_args()`

    Top-level keys corresponding to groups and bottom-level keys corresponding
    to arguments. Under `'main_args'`, the arguments which don't belong to a
    argparse group (i.e main arguments defined before parsing from a dict) can
    be found.

    Args:
        parser (argparse.ArgumentParser): ArgumentParser instance containing
            groups. Output of `prepare_parser_from_dict`.
        return_plain_args (bool): Whether to return the output or
            `parser.parse_args()`.
        args (list): List of arguments as read from the command line.
            Used for unit testing.

    Returns:
        dict:
            Dictionary of dictionaries containing the arguments. Optionally the
            direct output `parser.parse_args()`.
    argsc                    s   i | ]}|j t |j d qS N)destgetattr).0ar-   r   r   
<dictcomp>   s    z&parse_args_as_dict.<locals>.<dictcomp>z3.10zoptional argumentsoptions	main_args)
parse_args_action_groups_group_actionstitleparse_versionplatformpython_version)r   return_plain_argsr.   args_dicr   
group_dictopt_args_keyr   r-   r   parse_args_as_dicto   s   
rB   r/   )FN)r   r<   packaging.versionr   r;   r   r   r   r   r   r   rB   r   r   r   r   <module>   s    
"