a
    IDg^                     @   s  d Z ddlZddlZddlZddlZddlZ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Ze	jdkrddlmZ nddlmZ ejejejejejgZe	jdkreej dd Znd	d ZeeZi Zd8d
dZd9ddZG dd dejejej Z!G dd de"Z#G dd de$Z%G dd de#Z&G dd de#Z'G dd de#Z(e)dZ*ej)dej+dZ,ej-dddd:d d!Z.d;d"d#Z/d$d% Z0d&d' Z1G d(d) d)e#Z2G d*d+ d+e#Z3G d,d- d-e#Z4d.Z5d/Z6d0Z7d1Z8G d2d3 d3ej9Z9d4d5 Z:d6d7 Z;ej<j:ej<_=e:ej<_:ej>Z>ejZej?Z?ej@Z@ejAZAejBZBejCZCejDZDej Z ejZejEZEejFZFejGZGejHZHejIZIejJZJeZKeZLeZMeZNeZOeZPe9ZQe9ZRej<j:ej<_Sej<j:ej<_Te9jUe9_Ve9jWe9_Xe ZYeZZe!Z[dS )<z
A drop-in replacement for `argparse` that allows options to also be set via config files and/or environment variables.

:see: `configargparse.ArgumentParser`, `configargparse.add_argument`
    N)OrderedDict)   r   )StringIO)r   	   c                 C   s   t | tjS N)
isinstanceargparseBooleanOptionalActionaction r   _/mounts/lovelace/software/anaconda3/envs/paleomix/lib/python3.9/site-packages/configargparse.py<lambda>       r   c                 C   s   dS )NFr   r
   r   r   r   r   !   r   c                 K   sX   | du rd} | t v r(td| t |  f |dtj |dd tf i |t | < dS )zCreates a global ArgumentParser instance with the given name,
    passing any args other than "name" to the ArgumentParser constructor.
    This instance can then be retrieved using get_argument_parser(..)
    Ndefaultzbkwargs besides 'name' can only be passed in the first time. '%s' ArgumentParser already exists: %sformatter_classconflict_handlerresolve)_parsers
ValueError
setdefaultr   ArgumentDefaultsHelpFormatterArgumentParsernamekwargsr   r   r   init_argument_parser)   s    r   c                 K   s8   | du rd} t |dks | tvr0t| fi | t|  S )a  Returns the global ArgumentParser instance with the given name. The 1st
    time this function is called, a new ArgumentParser instance will be created
    for the given name, and any args other than "name" will be passed on to the
    ArgumentParser constructor.
    Nr   r   )lenr   r   r   r   r   r   get_argument_parser<   s
    r   c                   @   s   e Zd ZdZdS ) ArgumentDefaultsRawHelpFormatterzCHelpFormatter that adds default values AND doesn't do line-wrappingN__name__
__module____qualname____doc__r   r   r   r   r   K   s   r   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	ConfigFileParserzRThis abstract class can be extended to add support for new config file
    formatsc                 C   s   t ddS )z3Returns a string describing the config file syntax.z*get_syntax_description(..) not implementedNNotImplementedErrorselfr   r   r   get_syntax_descriptionW   s    z'ConfigFileParser.get_syntax_descriptionc                 C   s   t ddS )aU  Parses the keys and values from a config file.

        NOTE: For keys that were specified to configargparse as
        action="store_true" or "store_false", the config file value must be
        one of: "yes", "no", "on", "off", "true", "false". Otherwise an error will be raised.

        Args:
            stream (IO): A config file input stream (such as an open file object).

        Returns:
            OrderedDict: Items where the keys are strings and the
            values are either strings or lists (eg. to support config file
            formats like YAML which allow lists).
        zparse(..) not implementedNr&   )r)   streamr   r   r   parse[   s    zConfigFileParser.parsec                 C   s   t ddS )a  Does the inverse of config parsing by taking parsed values and
        converting them back to a string representing config file contents.

        Args:
            items: an OrderedDict of items to be converted to the config file
                format. Keys should be strings, and values should be either strings
                or lists.

        Returns:
            Contents of config file as a string
        zserialize(..) not implementedNr&   )r)   itemsr   r   r   	serializel   s    zConfigFileParser.serializeNr!   r"   r#   r$   r*   r,   r.   r   r   r   r   r%   S   s   r%   c                   @   s   e Zd ZdZdS )ConfigFileParserExceptionz'Raised when config file parsing failed.Nr    r   r   r   r   r0   {   s   r0   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	DefaultConfigFileParsera  
    Based on a simplified subset of INI and YAML formats. Here is the
    supported syntax

    .. code::

        # this is a comment
        ; this is also a comment (.ini style)
        ---            # lines that start with --- are ignored (yaml style)
        -------------------
        [section]      # .ini-style section names are treated as comments

        # how to specify a key-value pair (all of these are equivalent):
        name value     # key is case sensitive: "Name" isn't "name"
        name = value   # (.ini style)  (white space is ignored, so name = value same as name=value)
        name: value    # (yaml style)
        --name value   # (argparse style)

        # how to set a flag arg (eg. arg which has action="store_true")
        --name
        name
        name = True    # "True" and "true" are the same

        # how to specify a list arg (eg. arg which has action="append")
        fruit = [apple, orange, lemon]
        indexes = [1, 12, 35 , 40]

    c                 C   s   d}|S )NzrConfig file syntax allows: key=value, flag=true, stuff=[a,b,c] (for details, see syntax at https://goo.gl/R74nmi).r   r)   msgr   r   r   r*      s    z.DefaultConfigFileParser.get_syntax_descriptionc                 C   sN  t  }t|D ]8\}}| }|r|d dv s|dr<qtd|}|r.|d}|d}|d}|d}	|d u r|d ur|d	krd
}n|d u rd}|dr
|dr
zt	|}W nB t
y }
 z(dd |dd dD }W Y d }
~
n
d }
~
0 0 |	r$|	 dd   }	|||< qtd|t|dd|q|S )Nr   )#;[z---zn^(?P<key>[^:=;#\s]+)\s*(?:(?P<equal>[:=\s])\s*([\'"]?)(?P<value>.+?)?\3)?\s*(?:\s[;#]\s*(?P<comment>.*?)\s*)?$keyequalvaluecomment  truer6   ]c                 S   s   g | ]}|  qS r   strip.0elemr   r   r   
<listcomp>   r   z1DefaultConfigFileParser.parse.<locals>.<listcomp>   ,zUnexpected line {} in {}: {}r   r+   )r   	enumerater@   
startswithrematchgroupendswithjsonloads	Exceptionsplitr0   formatgetattr)r)   r+   r-   ilinerK   r7   r8   r9   r:   er   r   r   r,      s:    



2
zDefaultConfigFileParser.parsec                 C   sT   t  }| D ]<\}}t|tr8ddtt| d }|d|| q|	 S )Nr6   , r>   z{} = {}
)
r   r-   r   listjoinmapstrwriterR   getvalue)r)   r-   rr7   r9   r   r   r   r.      s    
z!DefaultConfigFileParser.serializeNr/   r   r   r   r   r1      s   $r1   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	ConfigparserConfigFileParserz,parses INI files using pythons configparser.c                 C   s   d}|S )Na  Uses configparser module to parse an INI file which allows multi-line
        values.

        Allowed syntax is that for a ConfigParser with the following options:

            allow_no_value = False,
            inline_comment_prefixes = ("#",)
            strict = True
            empty_lines_in_values = False

        See https://docs.python.org/3/library/configparser.html for details.

        Note: INI file sections names are still treated as comments.
        r   r2   r   r   r   r*      s    z3ConfigparserConfigFileParser.get_syntax_descriptionc              
   C   s   dd l }ddlm} |jddddddd}z||  W n0 tyl } ztd| W Y d }~n
d }~0 0 t }|	 D ]t}|| 
 D ]b\}}	|	d	d
dd
}
d|
v rd|
v r|
dd }d|vr||
||< q|
||< q|
||< qq||S )Nr   )literal_eval)=:F)r4   r5   T)
delimitersallow_no_valuecomment_prefixesinline_comment_prefixesstrictempty_lines_in_valuesCouldn't parse config file: %s
r;   r6   r>   {)configparserastr`   ConfigParserread_stringreadrP   r0   r   sectionsr-   replacerQ   )r)   r+   rm   r`   configrV   resultsectionkvZmultiLine2SingleLineZprelist_stringr   r   r   r,      s2    "
z"ConfigparserConfigFileParser.parsec                 C   sX   dd l }dd l}|jddddd}d|i}|| | }|| |d | S )Nr   F)r4   T)rd   rf   rg   rh   DEFAULT)rm   ioro   	read_dictr   r\   seekrq   )r)   r-   rm   rz   rt   r+   r   r   r   r.     s    


z&ConfigparserConfigFileParser.serializeNr/   r   r   r   r   r_      s   #r_   c                   @   s2   e Zd ZdZdd Zdd Zdd Zdd	d
ZdS )YAMLConfigFileParserzdParses YAML config files. Depends on the PyYAML module.
    https://pypi.python.org/pypi/PyYAML
    c                 C   s   d}|S )NzThe config file uses YAML syntax and must represent a YAML 'mapping' (for details, see http://learn.getgrav.org/advanced/yaml).r   r2   r   r   r   r*     s    z+YAMLConfigFileParser.get_syntax_descriptionc                 C   sz   zddl }W n ty&   tdY n0 zddl m} ddl m} W n* tyn   ddl m} ddl m} Y n0 |||fS )zjlazy-import PyYAML so that configargparse doesn't have to depend
        on it unless this parser is used.r   NzJCould not import yaml. It can be installed by running 'pip install PyYAML')CSafeLoader)CDumper)
SafeLoader)Dumper)yamlImportErrorr0   r~   r   r   r   )r)   r   r   r   r   r   r   
_load_yaml$  s    zYAMLConfigFileParser._load_yamlc           
   
   C   s   |   \}}}z|j||d}W n0 tyP } ztd| W Y d }~n
d }~0 0 t|tsztdt|ddt|jf t	 }|
 D ]2\}}	t|	tr|	||< q|	d u rqt|	||< q|S )N)Loaderri   zThe config file doesn't appear to contain 'key: value' pairs (aka. a YAML mapping). yaml.load('%s') returned type '%s' instead of 'dict'.r   r+   )r   loadrP   r0   r   dictrS   typer!   r   r-   rX   r[   )
r)   r+   r   r   _Z
parsed_objrV   ru   r7   r9   r   r   r   r,   6  s$    "


zYAMLConfigFileParser.parseFc                 C   s&   |   \}}}t|}|j|||dS )N)default_flow_styler   )r   r   dump)r)   r-   r   r   r   r   r   r   r   r.   P  s    zYAMLConfigFileParser.serializeN)F)r!   r"   r#   r$   r*   r   r,   r.   r   r   r   r   r}     s
   r}   z1(^\"(?:\\.|[^\"\\])*\"$)|(^\'(?:\\.|[^\'\\])*\'$)z(^\"\"\"(\s+)?(([^\"]|\"([^\"]|\"[^\"]))*(\"\"?)?)?(\s+)?(?:\\.|[^\"\\])?\"\"\"$)|(^\'\'\'(\s+)?(([^\']|\'([^\']|\'[^\']))*(\'\'?)?)?(\s+)?(?:\\.|[^\'\\])?\'\'\'$))flags   T)maxsizetypedc                 C   s    t t| p|ot t| S )zu
    Detect whether a string is a quoted representation. 

    :param triple: Also match tripple quoted strings.
    )bool_QUOTED_STR_REGEXrK   _TRIPLE_QUOTED_STR_REGEX)texttripler   r   r   	is_quoted}  s    r   c              
   C   sl   t | |drhzt| }t|ts&J W n: tyb } z"td|  d| |W Y d}~n
d}~0 0 |S | S )a  
    Unquote a maybe quoted string representation. 
    If the string is not detected as being a quoted representation, it returns the same string as passed.
    It supports all kinds of python quotes: ``"""``, ``'''``, ``"`` and ``'``.

    :param triple: Also unquote tripple quoted strings.
    @raises ValueError: If the string is detected as beeing quoted but literal_eval() fails to evaluate it as string.
        This would be a bug in the regex. 
    r   z+Error trying to unquote the quoted string: : N)r   rn   r`   r   r[   rP   r   )r   r   srV   r   r   r   unquote_str  s    

,r   c                 C   sB   g }t j| gddD ]$}|D ]}|t| dd qqt|S )u  
    Parse a TOML section name to a sequence of strings.

    The following names are all valid: 

    .. python::

        "a.b.c"            # this is best practice -> returns ("a", "b", "c")
        " d.e.f "          # same as [d.e.f] -> returns ("d", "e", "f")
        " g .  h  . i "    # same as [g.h.i] -> returns ("g", "h", "i")
        ' j . "ʞ" . "l" '  # same as [j."ʞ"."l"], double or simple quotes here are supported. -> returns ("j", "ʞ", "l")
    .)	delimiterFr   )csvreaderappendr   r@   tuple)section_namerv   rowar   r   r   parse_toml_section_name  s
    r   c                 C   s\   t |trt|n|}| |d }|s,dS |dd }|rFt||S t |tsTdS |S dS )z
    Given some TOML data (as loaded with `toml.load()`), returns the requested section of the data.
    Returns ``None`` if the section is not found.
    r   NrE   )r   r[   r   getget_toml_sectionr   )datarv   rr   Zitemdatar   r   r   r     s    

r   c                       s8   e Zd ZdZ fddZdd Zdd Zdd	 Z  ZS )
TomlConfigParsera(  
    Create a TOML parser bounded to the list of provided sections.

    Example::
        # this is a comment
        [tool.my-software] # TOML section table.
        # how to specify a key-value pair
        format-string = "restructuredtext" # strings must be quoted
        # how to set an arg which has action="store_true"
        warnings-as-errors = true
        # how to set an arg which has action="count" or type=int
        verbosity = 1
        # how to specify a list arg (eg. arg which has action="append")
        repeatable-option = ["https://docs.python.org/3/objects.inv",
                       "https://twistedmatrix.com/documents/current/api/objects.inv"]
        # how to specify a multiline text:
        multi-line-text = '''
            Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
            Vivamus tortor odio, dignissim non ornare non, laoreet quis nunc. 
            Maecenas quis dapibus leo, a pellentesque leo. 
            '''

    Note that the config file fragment above is also valid for the `IniConfigParser` class and would be parsed the same manner. 
    Thought, any valid TOML config file will not be necessarly parsable with `IniConfigParser` (INI files must be rigorously indented whereas TOML files).
    
    See the `TOML specification <>`_ for details. 
    c                    s   t    || _dS )zO
        :param sections: The section names bounded to the new parser.
        N)super__init__rr   )r)   rr   	__class__r   r   r     s    
zTomlConfigParser.__init__c                 C   s   | S r   r   r(   r   r   r   __call__  s    zTomlConfigParser.__call__c           
   
   C   s   ddl }z||}W n0 tyF } ztd| W Y d}~n
d}~0 0 t }| jD ]R}t||}|rT| D ]2\}}	t|	t	r|	||< qn|	du rqnt
|	||< qn qqT|S )z3Parses the keys and values from a TOML config file.r   NzCouldn't parse TOML file: %s)tomlr   rP   r0   r   rr   r   r-   r   rX   r[   )
r)   r+   r   rt   rV   ru   rv   r   r7   r9   r   r   r   r,     s"    "



zTomlConfigParser.parsec                 C   s   dS )NzConfig file syntax is Tom's Obvious, Minimal Language. See https://github.com/toml-lang/toml/blob/v0.5.0/README.md for details.r   r(   r   r   r   r*     s    z'TomlConfigParser.get_syntax_description	r!   r"   r#   r$   r   r   r,   r*   __classcell__r   r   r   r   r     s
   r   c                       s8   e Zd ZdZ fddZdd Zdd Zdd	 Z  ZS )
IniConfigParsera  
    Create a INI parser bounded to the list of provided sections.
    Optionaly convert multiline strings to list.

    Example (if split_ml_text_to_list=False)::

        # this is a comment
        ; also a comment
        [my-software]
        # how to specify a key-value pair
        format-string: restructuredtext 
        # white space are ignored, so name = value same as name=value
        # this is why you can quote strings 
        quoted-string = '	hello	mom...  '
        # how to set an arg which has action="store_true"
        warnings-as-errors = true
        # how to set an arg which has action="count" or type=int
        verbosity = 1
        # how to specify a list arg (eg. arg which has action="append")
        repeatable-option = ["https://docs.python.org/3/objects.inv",
                       "https://twistedmatrix.com/documents/current/api/objects.inv"]
        # how to specify a multiline text:
        multi-line-text = 
            Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
            Vivamus tortor odio, dignissim non ornare non, laoreet quis nunc. 
            Maecenas quis dapibus leo, a pellentesque leo. 
    
    Example (if split_ml_text_to_list=True)::

        # the same rules are applicable with the following changes:
        [my-software]
        # how to specify a list arg (eg. arg which has action="append")
        repeatable-option = # Just enter one value per line (the list literal format can also be used)
            https://docs.python.org/3/objects.inv
            https://twistedmatrix.com/documents/current/api/objects.inv
        # how to specify a multiline text (you have to quote it):
        multi-line-text = '''
            Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
            Vivamus tortor odio, dignissim non ornare non, laoreet quis nunc. 
            Maecenas quis dapibus leo, a pellentesque leo. 
            '''
    c                    s   t    || _|| _dS )z
        :param sections: The section names bounded to the new parser.
        :split_ml_text_to_list: Wether to convert multiline strings to list
        N)r   r   rr   split_ml_text_to_list)r)   rr   r   r   r   r   r   2  s    
zIniConfigParser.__init__c                 C   s   | S r   r   r(   r   r   r   r   ;  s    zIniConfigParser.__call__c           
      C   s  ddl }| }z||  W n0 tyR } ztd| W Y d}~n
d}~0 0 t }| |jg D ]f}|| jvr|qj|| 	 D ]F\}}|
 }	|	sq|	dr
|	dr
zt|	||< W n< ty } z"tdt| d |W Y d}~n
d}~0 0 qt|	r\zt|	||< W n4 tyX } ztt||W Y d}~n
d}~0 0 q| jrd|dv rzd	d
 |	dD ||< W n4 ty } ztt||W Y d}~n
d}~0 0 q|||< qqj|S )z3Parses the keys and values from an INI config file.r   NzCouldn't parse INI file: %sr6   r>   zError evaluating list: z;. Put quotes around your text if it's meant to be a string.rj   c                 S   s   g | ]}|rt |qS r   )r   )rB   rT   r   r   r   rD   d  r   z)IniConfigParser.parse.<locals>.<listcomp>)rm   ro   rp   rq   rP   r0   r   rr   DEFAULTSECTr-   r@   rI   rM   rn   r`   r   r[   r   r   r   rstriprQ   )
r)   r+   rm   rt   rV   ru   rv   rw   rx   Zstrip_vr   r   r   r,   >  s>    "
.
&&zIniConfigParser.parsec                 C   s   d}| j r|d7 }|S )NzUses configparser module to parse an INI file which allows multi-line values. See https://docs.python.org/3/library/configparser.html for details. This parser includes support for quoting strings literal as well as python list syntax evaluation. z{Alternatively lists can be constructed with a plain multiline string, each non-empty line will be converted to a list item.)r   r2   r   r   r   r*   l  s    z&IniConfigParser.get_syntax_descriptionr   r   r   r   r   r     s
   +	.r   c                       s8   e Zd ZdZ fddZdd Zdd Zdd	 Z  ZS )
CompositeConfigParserz
    Createa a config parser composed by others `ConfigFileParser`s.  

    The composite parser will successively try to parse the file with each parser, 
    until it succeeds, else raise execption with all encountered errors. 
    c                    s   t    dd |D | _d S )Nc                 S   s   g | ]
}| qS r   r   )rB   pr   r   r   rD     r   z2CompositeConfigParser.__init__.<locals>.<listcomp>)r   r   parsers)r)   Zconfig_parser_typesr   r   r   r   }  s    
zCompositeConfigParser.__init__c                 C   s   | S r   r   r(   r   r   r   r     s    zCompositeConfigParser.__call__c                 C   s|   g }| j D ]N}z||W   S  tyV } z |d || W Y d }~q
d }~0 0 q
tdddd |D  d S )Nr   zError parsing config: rW   c                 s   s   | ]}t t|V  qd S r   )reprr[   )rB   rV   r   r   r   	<genexpr>  r   z.CompositeConfigParser.parse.<locals>.<genexpr>)r   r,   rP   r|   r   r0   rY   )r)   r+   errorsr   rV   r   r   r   r,     s    

"zCompositeConfigParser.parsec              	   C   sP   dd }d}t | jD ]4\}}|d|d  d||jj d|  d7 }q|S )	Nc                 S   s6   |   ddddddd}|r2| S dS )Nr   parserr<   rt   filez??)lowerr@   rs   upper)	classnamer@   r   r   r   guess_format_name  s    zGCompositeConfigParser.get_syntax_description.<locals>.guess_format_namez2Uses multiple config parser settings (in order): 
r6   rE   z] r   z 
)rH   r   r   r!   r*   )r)   r   r3   rT   r   r   r   r   r*     s
    .z,CompositeConfigParser.get_syntax_descriptionr   r   r   r   r   r   u  s
   r   Zcommand_lineZenvironment_variablesconfig_filedefaultsc                   @   s   e Zd ZdZdd ZdddejfddZdddejdfdd	Zd
d Z	dddZ
dd Zdd Zdd Zdd Zdd Zdd ZejfddZdd ZdS )r   zDrop-in replacement for `argparse.ArgumentParser` that adds support for
    environment variables and ``.ini`` or ``.yaml-style`` config files.
    c                 O   s  | dd}| dd}| dd}| dg }| dd}| d	t}| d
g }	| dd}
| dd}| dg }| dd}| dt| _|| _|| _|| _tjj	| g|R i | |du rt | _
n| | _
|| _|| _|	r| j|	d|
|dd |r| j|dd|dd dS )aR  Supports args of the `argparse.ArgumentParser` constructor
        as \*\*kwargs, as well as the following additional args.

        Arguments:
            add_config_file_help: Whether to add a description of config file
                syntax to the help message.
            add_env_var_help: Whether to add something to the help message for
                args that can be set through environment variables.
            auto_env_var_prefix: If set to a string instead of None, all config-
                file-settable options will become also settable via environment
                variables whose names are this prefix followed by the config
                file key, all in upper case. (eg. setting this to ``foo_`` will
                allow an arg like ``--my-arg`` to also be set via the FOO_MY_ARG
                environment variable)
            default_config_files: When specified, this list of config files will
                be parsed in order, with the values from each config file
                taking precedence over previous ones. This allows an application
                to look for config files in multiple standard locations such as
                the install directory, home directory, and current directory.
                Also, shell \* syntax can be used to specify all conf files in a
                directory. For example::

                    ["/etc/conf/app_config.ini",
                    "/etc/conf/conf-enabled/*.ini",
                    "~/.my_app_config.ini",
                    "./app_config.txt"]

            ignore_unknown_config_file_keys: If true, settings that are found
                in a config file but don't correspond to any defined
                configargparse args will be ignored. If false, they will be
                processed and appended to the commandline like other args, and
                can be retrieved using parse_known_args() instead of parse_args()
            config_file_open_func: function used to open a config file for reading
                or writing. Needs to return a file-like object.
            config_file_parser_class: configargparse.ConfigFileParser subclass
                which determines the config file format. configargparse comes
                with DefaultConfigFileParser and YAMLConfigFileParser.
            args_for_setting_config_path: A list of one or more command line
                args to be used for specifying the config file path
                (eg. ["-c", "--config-file"]). Default: []
            config_arg_is_required: When args_for_setting_config_path is set,
                set this to True to always require users to provide a config path.
            config_arg_help_message: the help message to use for the
                args listed in args_for_setting_config_path.
            args_for_writing_out_config_file: A list of one or more command line
                args to use for specifying a config file output path. If
                provided, these args cause configargparse to write out a config
                file with settings based on the other provided commandline args,
                environment variants and defaults, and then to exit.
                (eg. ["-w", "--write-out-config-file"]). Default: []
            write_out_config_file_arg_help_message: The help message to use for
                the args in args_for_writing_out_config_file.
        add_config_file_helpTadd_env_var_helpauto_env_var_prefixNdefault_config_filesignore_unknown_config_file_keysFconfig_file_parser_classargs_for_setting_config_pathconfig_arg_is_requiredconfig_arg_help_messagezconfig file path args_for_writing_out_config_file&write_out_config_file_arg_help_messagezftakes the current command line args and writes them out to a config file at the given path, then exitsZconfig_file_open_funcr   )destrequiredhelpis_config_file_argZ"write_out_config_file_to_this_pathZCONFIG_OUTPUT_PATH)r   metavarr   is_write_out_config_file_arg)popr1   open_config_file_open_func_add_config_file_help_add_env_var_help_auto_env_var_prefixr   r   r   _config_file_parser_default_config_files _ignore_unknown_config_file_keysadd_argument)r)   argsr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r     sZ    :
zArgumentParser.__init__Nc                 C   s4   | j ||||dd\}}|r0| dd|  |S )a  Supports all the same args as the `argparse.ArgumentParser.parse_args()`,
        as well as the following additional args.

        Arguments:
            args: a list of args as in argparse, or a string (eg. "-x -y bla")
            config_file_contents: String. Used for testing.
            env_vars: Dictionary. Used for testing.

        Returns:
            argparse.Namespace: namespace
        F)r   	namespaceconfig_file_contentsenv_varsignore_help_argszunrecognized arguments: %sr;   )parse_known_argserrorrY   )r)   r   r   r   r   argvr   r   r   
parse_args  s    
zArgumentParser.parse_argsFc                    s   du rt jdd  nt tr,   nt  jD ]}|j |_q:|r\dd  D  t	 _
 rdt f}d|ij
t< jdurjD ]j}|}|r|js|js|js|jst|tjst|tjs|d j}	j|	 dd |_qg }
d	} fd
djD }|D ]}|j}| }|jsJt|tjrd}|dr|drzt|}W n0 ty   dd |dd dD }Y n0 |
|||7 }
q"|r̈ |
  n|
   |
rt	fdd|D j
t < t!dd jD }|o d v p d v }fddjD }g }|durZt"|}d|_#|g}n|sj$ }t%|D ]f}z^zj&'|}W n4 t(y } z)t| W Y d}~n
d}~0 0 W t*|dr|+  nt*|dr|+  0 g }d	}|, D ]\}}||v r*|| }t- |jj}n"d}j.pJt- /|gj}|s||||7 }dt0|j#f }|j
vrt	 j
|< ||fj
| |< |r|jst|tjrd}q|rЈ |  n|   qrt	 }jD ]}|j p|jt1t2fv }t- |jjs|r|j3du s|j3t4kst|t5rFqn,|jrZ|jd }n|j6}|t|j3f||< q|r|j
t7< tj8j9 d\}fddjD }dd |D }j:|dd |fS )a  Supports all the same args as the `argparse.ArgumentParser.parse_args()`,
        as well as the following additional args.

        Arguments:
            args: a list of args as in argparse, or a string (eg. "-x -y bla")
            config_file_contents (str). Used for testing.
            env_vars (dict). Used for testing.
            ignore_help_args (bool): This flag determines behavior when user specifies ``--help`` or ``-h``. If False,
                it will have the default behavior - printing help and exiting. If True, it won't do either.

        Returns:
            tuple[argparse.Namespace, list[str]]: tuple namescpace, unknown_args
        NrE   c                 S   s   g | ]}|d vr|qS ))-h--helpr   )rB   argr   r   r   rD   H  r   z3ArgumentParser.parse_known_args.<locals>.<listcomp>r<   r   -r   Fc                    s6   g | ].}|j s|jr|jv rt |jjs|qS r   )is_positional_argenv_varalready_on_command_lineoption_stringsprefix_charsrB   r   )r   r   r)   r   r   rD   c  s   Tr6   r>   c                 S   s   g | ]}|  qS r   r?   rA   r   r   r   rD   r  r   rF   rG   c                    s    g | ]}|j | |j  ffqS r   )r   r   )r   r   r   rD   }  s   c                 s   s   | ]}t |tjr|V  qd S r   )r   r   _HelpActionr   r   r   r   r     s   z2ArgumentParser.parse_known_args.<locals>.<genexpr>r   r   c                    s"   i | ]}  |D ]
}||qqS r   )get_possible_config_keys)rB   r   Z
config_keyr(   r   r   
<dictcomp>  s   z3ArgumentParser.parse_known_args.<locals>.<dictcomp>z
method argclosez%s|%s)r   r   c                    s&   g | ]}t |d drt  |jdqS )r   FN)rS   r   r   )r   r   r   rD     s   c                 S   s   g | ]}|d ur|qS r   r   r   r   r   r   rD     r   )
exit_after);sysr   r   r[   rQ   rX   _actionsr   r   r   _source_to_settings_COMMAND_LINE_SOURCE_KEYr   r   r   r   r   r   _VersionActionr   r@   r   rs   r   nargs_AppendActionrI   rM   rN   rO   rP    convert_item_to_command_line_arg_ENV_VAR_SOURCE_KEYanyr   r   _open_config_filesreversedr   r,   r0   r   hasattrr   r-   r   r   4get_command_line_key_for_unknown_config_file_setting_CONFIG_FILE_SOURCE_KEYOPTIONALZERO_OR_MOREr   SUPPRESS#ACTION_TYPES_THAT_DONT_NEED_A_VALUEr   _DEFAULTS_SOURCE_KEYr   r   write_config_file)r)   r   r   r   r   r   r   Za_v_pairconfig_file_keysZstripped_config_file_keyZenv_var_argsr   Zactions_with_env_var_valuesr   r7   r9   Zsupports_help_argZskip_config_file_parsingZknown_config_keysZconfig_streamsr+   config_itemsrV   Zconfig_argsZdiscard_this_keyZ
source_keyZdefault_settingsZcares_about_default_valueZunknown_argsoutput_file_pathsr   )r   r   r   r)   r   r   (  s   







"





&










zArgumentParser.parse_known_argsc                 C   s   | j S )ar  
        If called after `parse_args()` or `parse_known_args()`, returns a dict that contains up to 4 keys corresponding
        to where a given option's value is coming from:
        - "command_line"
        - "environment_variables"
        - "config_file"
        - "defaults"
        Each such key, will be mapped to another dictionary containing the options set via that method. Here the key
        will be the option name, and the value will be a 2-tuple of the form (`argparse.Action` obj, `str` value).

        Returns:
            dict[str, dict[str, tuple[argparse.Action, str]]]: source to settings dict
        )r   r(   r   r   r   get_source_to_settings_dict  s    z*ArgumentParser.get_source_to_settings_dictc           	      C   s   |D ]j}z0|  |d}W d   n1 s,0    Y  W q tyl } ztd||W Y d}~qd}~0 0 q|r| | j|}| j|}|D ]:}|  |d}|| W d   q1 s0    Y  qt	dd
|  |r| d dS )a4  Write the given settings to output files.

        Args:
            parsed_namespace: namespace object created within parse_known_args()
            output_file_paths: any number of file paths to write the config to
            exit_after: whether to exit the program after writing the config files
        wNz Couldn't open {} for writing: {}zWrote config file to rW   r   )r   IOErrorr   rR    get_items_for_config_file_outputr   r   r.   r\   printrY   exit)	r)   parsed_namespacer  r   Zoutput_file_pathZoutput_filerV   r  file_contentsr   r   r   r	    s&    "*z ArgumentParser.write_config_filec                 C   s"   | | j}| jd d | }|S )aM  Compute a commandline arg key to be used for a config file setting
        that doesn't correspond to any defined configargparse arg (and so
        doesn't have a user-specified commandline arg key).

        Args:
            key: The config file key that was being set.

        Returns:
            str: command line key
        r      )r@   r   )r)   r7   Zkey_without_prefix_charscommand_line_keyr   r   r   r    s    zCArgumentParser.get_command_line_key_for_unknown_config_file_settingc                 C   sl  t  }| D ]V\}}|tkr|d \}}| jD ]`}| |}	|	r2|js2t||j| jr2t	||j
d}
|
dur2t|
trt|
 }
|
||	d < q2q|tkr| D ]<\}\}}
| |}	|	rt	||j
d}
|
dur|
||	d < qq|tr| D ]\}\}}
|
||< qq|tkr| D ]B\}\}}
| |}	|	r"t	||j
d}
|
dur"|
||	d < q"q|S )a  Converts the given settings back to a dictionary that can be passed
        to ConfigFormatParser.serialize(..).

        Args:
            source_to_settings: the dictionary described in parse_known_args()
            parsed_namespace: namespace object created within parse_known_args()
        Returns:
            OrderedDict: where keys are strings and values are either strings
            or lists
        r<   Nr   )r   r-   r   r   r   r   r   r   r   rS   r   r   r   r[   r   r   rI   r  r  )r)   Zsource_to_settingsr  Zconfig_file_itemssourcesettingsr   Zexisting_command_line_argsr   r
  r9   r7   r   r   r   r    sF    






z/ArgumentParser.get_items_for_config_file_outputc           	         s\  g }|du r|  |}nt|s*|jd }|durt|trt|tsRJ d| dv rt|sr|| n||jd  n| dv rt|sn||jd  n^t|tj	r|D ]  t
 fdd	|jD rd}q||jd gt| 7 }n| d
||f  nJt|tr$|duon|jduont|tjsLt|tjon|jdv pnt|jton|jdk}|du st|tjr|D ]T}|rt|tr|| |D ]}|t| qn|d|t|f  qn>|r|| |D ]}|t| qn| d||f  n4t|trD|d||f  ntdt|||S )a  Converts a config file or env var key + value to a list of
        commandline args to append to the commandline.

        Args:
            action: The argparse Action object for this setting, or None if this
                config file setting doesn't correspond to any defined
                configargparse arg.
            key: string (config file key or env var name)
            value: parsed value of type string or list

        Returns:
            list[str]: args
        NrF   zCconfig parser should convert anything that is not a list to string.)r=   yeson1r   )falsenooff0rE   c                    s   g | ]}  |qS r   rI   )rB   r   r   r   r   rD   t  r   zCArgumentParser.convert_item_to_command_line_arg.<locals>.<listcomp>z^Unexpected value for %s: '%s'. Expecting 'true', 'false', 'yes', 'no', 'on', 'off', '1' or '0')+*z%s=%szp%s can't be set to a list '%s' unless its action type is changed to 'append' or nargs is set to '*', '+', or > 1z&Unexpected value type {} for value: {})r  is_boolean_optional_actionr   r   r  r[   r   r   r   _CountActionr   intr   rX   r   _StoreActionr   r   rR   r   )	r)   r   r7   r9   r   r  Zaccepts_list_and_has_nargsZ	list_elemZsub_elemr   r!  r   r   K  sd    

"

z/ArgumentParser.convert_item_to_command_line_argc                    sP   g }t |ddr|S |jD ]0 t fdd| jD r| dd  g7 }q|S )a  This method decides which actions can be set in a config file and
        what their keys will be. It returns a list of 0 or more config keys that
        can be used to set the given action's value in a config file.

        Returns:
            list[str]: keys
        r   Nc                 3   s   | ]}  d | V  qdS )r  Nr   )rB   cr!  r   r   r     r   z:ArgumentParser.get_possible_config_keys.<locals>.<genexpr>r  )rS   r   r   r   )r)   r   keysr   r!  r   r     s    
z'ArgumentParser.get_possible_config_keysc                 C   sr  g }t tjt tjj| jD ]}|D ]}|| | q$qdd | jD }|sT|S |D ]}t	j
| jdd}|| dd }t|||_|j|d}	|	sqX|	\}
}t|
|jd}|sqXtj|}z| |}W n ty` } zpt|jd	kr|j\}}nt|}|D ](}z|  W n ty4   Y n0 q| d
||f  W Y d}~n
d}~0 0 ||g7 }qX|S )a  Tries to parse config file path(s) from within command_line_args.
        Returns a list of opened config files, including files specified on the
        commandline as well as any default_config_files specified in the
        constructor that are present on disk.

        Args:
            command_line_args: List of all args
        
        Returns:
            list[IO]: open config files
        c                 S   s   g | ]}t |d dr|qS r   FrS   r   r   r   r   rD     s   z5ArgumentParser._open_config_files.<locals>.<listcomp>F)r   add_helpc                 S   s   d S r   r   )r)   messager   r   r   error_method  s    z7ArgumentParser._open_config_files.<locals>.error_method)r   Nr  z)Unable to open config file: %s. Error: %s)rZ   globospath
expanduserr   r   r   r   r   r   r   _add_actiontypes
MethodTyper   r   rS   r   rP   r   r   r[   r   )r)   Zcommand_line_argsconfig_filesfilesfZuser_config_file_arg_actionsr   Z
arg_parserr.  Z
parsed_argr   r   Zuser_config_filer+   rV   errnor3   r   r   r   r   r     sR    


z!ArgumentParser._open_config_filesc                 C   s   t dtdtdtdi}t }| j D ]\}}|d}||d  t|dd  }|	| | D ]`\}\}}|r|	d	
|d
 | qdt|tr|	d|  qdt|trd|	dd|  qdq$| S )zReturns a string with all args and settings and where they came from
        (eg. commandline, config file, environment variable or default)

        Returns:
            str: source to settings string
        zCommand Line Args: zEnvironment Variables:
zConfig File (%s):
z
Defaults:
|r   rE   Nz  {:<19}{}
rb   z  %s
r;   )r   r   r  r  r   r   r-   rQ   r   r\   rR   r   r[   rX   rY   r]   )r)   Zsource_key_to_display_value_mapr^   r  r  r7   r   r9   r   r   r   format_values  s$    



zArgumentParser.format_valuesc                 C   s   | |   dS )zBPrints the format_values() string (to sys.stdout or another file).N)r\   r;  )r)   r   r   r   r   print_values  s    zArgumentParser.print_valuesc              	      s  d}d}d} j r̈ j}d jd  } fdd jD }dd  jD }|r|sV|rd _ d}|d	| 7 }d
dd |D }|rd| }|s|r|dd
t|ttd |g  7 }|d j  7 } j	r6dd  jD }	|	D ]L\}
}|j
tkrqd|
 }|j
sd|_
||j
vr| j
|7  _
d}d _	q|sB|rzdg}|rXdg| }|rhdg| }|dd| 7 }t  jd}t||}tj |dkrd|nd S )Nr<   Fr  r   c                    s@   g | ]8}|j D ],} |r|jd ks|js|js||fqqS )r   )r   r   r   r   r   )rB   r   r   r(   r   r   rD      s   
z.ArgumentParser.format_help.<locals>.<listcomp>c                 S   s   g | ]}t |d dr|qS r*  r+  r   r   r   r   rD   $  s   Tz:Args that start with '%s' can also be set in a config filez or c                 s   s   | ]}|j r|j d  V  qdS )r   N)r   r   r   r   r   r   .  s   z-ArgumentParser.format_help.<locals>.<genexpr>zspecified via z (%s).r;   c                 S   s"   g | ]}t |d dr|j|fqS )r   N)rS   r   r   r   r   r   rD   8  s   z   [env var: %s]r   zconfig file valueszenvironment variablesz- In general, command-line values override %s.z which override    z
{}
)r   r   r   r   rY   r   filterr   r*   r   r   r  max_get_formatter_widthtextwrapfillr   r   format_helprR   )r)   r3   Zadded_config_file_helpZadded_env_var_helpr   ccZconfig_settable_argsZconfig_path_actionsZconfig_arg_stringZenv_var_actionsr   r   Zenv_var_help_stringZvalue_sources
text_widthr   r(   r   rD    sl    






zArgumentParser.format_help)F)r!   r"   r#   r$   r   r0  environr   r   r  r	  r  r  r   r   r   r;  r   stdoutr<  rD  r   r   r   r   r     s,   j

 6
/MKr   c                 O   s   | dd}| ddp"| dd}| dd}| j|i |}|j |_||_||_||_|jrn|rntd|jrt|t	j
std|jrd}t|t	j
st|d	 |rt|d
 |S )aH  
    This method supports the same args as ArgumentParser.add_argument(..)
    as well as the additional args below.

    Arguments:
        env_var: If set, the value of this environment variable will override
            any config file or default values for this arg (but can itself
            be overridden on the commandline). Also, if auto_env_var_prefix is
            set in the constructor, this env var name will be used instead of
            the automatic name.
        is_config_file_arg: If True, this arg is treated as a config file path
            This provides an alternative way to specify config files in place of
            the ArgumentParser(fromfile_prefix_chars=..) mechanism.
            Default: False
        is_write_out_config_file_arg: If True, this arg will be treated as a
            config file path, and, when it is specified, will cause
            configargparse to write all current commandline args to this file
            as config options and then exit.
            Default: False
    
    Returns:
        argparse.Action: the new argparse action
    r   Nr   Zis_config_filer   z*env_var can't be set for a positional arg.z9arg with is_config_file_arg=True must have action='store'z+arg with is_write_out_config_file_arg=True zmust have action='store'z'can't also have is_config_file_arg=True)r   original_add_argument_methodr   r   r   r   r   r   r   r   r'  )r)   r   r   r   r   r   r   Zerror_prefixr   r   r   r   U  s6    

r   c                    sb   g  | D ]B}|r@|d |v r@d|v r@| dd\}} | q | qt fdd|D S )zUtility method for checking if any of the potential_command_line_args is
    already present in existing_args.

    Returns:
        bool: already on command line?
    r   ra   rE   c                 3   s   | ]}| v V  qd S r   r   )rB   Zpotential_arg	arg_namesr   r   r     s   z*already_on_command_line.<locals>.<genexpr>)rQ   r   r   )Zexisting_args_listZpotential_command_line_argsr   
arg_stringoption_stringexplicit_argr   rJ  r   r     s    r   )N)N)T)T)\r$   r   rn   r   	functoolsrN   r/  r0  rJ   r   r4  collectionsr   rB  version_inforz   r   _StoreTrueAction_StoreFalseActionr%  _StoreConstAction_AppendConstActionr  r   r	   r$  r   r   r   r   r   RawTextHelpFormatterRawDescriptionHelpFormatterr   objectr%   rP   r0   r1   r_   r}   compiler   DOTALLr   	lru_cacher   r   r   r   r   r   r   r   r   r  r  r   r   r   _ActionsContainerrI  ActionArgumentErrorArgumentTypeErrorFileTypeHelpFormatterMetavarTypeHelpFormatter	NamespaceONE_OR_MOREr  PARSER	REMAINDERr  r  ZinitArgumentParserZgetArgumentParserZgetArgParserZ	getParserZget_arg_parserZ
get_parserZ	ArgParserParserZadd_argaddr   r,   r   Zparse_knownZRawFormatterZDefaultsFormatterZDefaultsRawFormatterr   r   r   r   <module>   s   





(RIZ
	
Ho&     99