a
    ˆÏ?eéR  ã                   @   sˆ  d dl Z d dlZd dl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 d dlm	Z	 d dlm
Z
 d dlmZ d d	lmZ d d
lmZ d dlmZ d dlmZ d dlmZ d dlm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 d dlmZ d dlmZ e�rd dlmZ dZeG dd„ dƒƒZ G dd„ de!ƒZ"G dd„ dƒZ#G dd„ dƒZ$G dd „ d e j%ƒZ&G d!d"„ d"e j'ƒZ(dS )#é    N)Úgettext)ÚAny)ÚCallable)Úcast)ÚDict)ÚList)ÚMapping)ÚNoReturn)ÚOptional)ÚSequence)ÚTuple)ÚTYPE_CHECKING)ÚUnion)Úfinal)Ú
UsageError)ÚARGUMENT_PERCENT_DEFAULT)ÚARGUMENT_TYPE_STR)ÚARGUMENT_TYPE_STR_CHOICE)Úcheck_ispytest)ÚLiteralZfile_or_dirc                   @   sh  e Zd ZU dZdZee ed< d(ddœee eedgdf  e	ddœdd	„Z
ddd
œdd„Zd)eeee ddœdd„Zeeddœdd„Zd*eeedf  eej ejdœdd„Zddœdd„Zd+eeedf  ejeej ee dœdd„Zd,eeedf  eej ejdœd d!„Zd-eeedf  eej eejee f dœd"d#„Zd.eeed$ edd%œd&d'„ZdS )/ÚParserzÄParser for command line arguments and ini-file values.

    :ivar extra_info: Dict of generic param -> value to display in case
        there's an error processing the command line arguments.
    NÚprogF©Ú	_ispytestÚArgument)ÚusageÚ
processoptr   Úreturnc                C   s@   t |ƒ td| dd�| _g | _|| _|| _i | _g | _i | _d S )NzCustom optionsT©Úparserr   )	r   ÚOptionGroupÚ
_anonymousÚ_groupsÚ_processoptÚ_usageÚ_inidictÚ	_ininamesÚ
extra_info)Úselfr   r   r   © r)   ú\/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/_pytest/config/argparsing.pyÚ__init__+   s    zParser.__init__)Úoptionr   c                 C   s   | j r|jr|   |¡ d S ©N)r#   Údest)r(   r,   r)   r)   r*   Úprocessoption;   s    zParser.processoptionÚ r    )ÚnameÚdescriptionÚafterr   c                 C   sj   | j D ]}|j|kr|  S qt||| dd�}d}t| j ƒD ]\}}|j|kr< qTq<| j  |d |¡ |S )a  Get (or create) a named option Group.

        :param name: Name of the option group.
        :param description: Long description for --help output.
        :param after: Name of another group, used for ordering --help output.
        :returns: The option group.

        The returned group object has an ``addoption`` method with the same
        signature as :func:`parser.addoption <pytest.Parser.addoption>` but
        will be shown in the respective group in the output of
        ``pytest --help``.
        Tr   r   é   )r"   r1   r    Ú	enumerateÚinsert)r(   r1   r2   r3   ÚgroupÚiÚgrpr)   r)   r*   Úgetgroup@   s    



zParser.getgroup©ÚoptsÚattrsr   c                 O   s   | j j|i |¤Ž dS )a!  Register a command line option.

        :param opts:
            Option names, can be short or long options.
        :param attrs:
            Same attributes as the argparse library's :py:func:`add_argument()
            <argparse.ArgumentParser.add_argument>` function accepts.

        After command line parsing, options are available on the pytest config
        object via ``config.option.NAME`` where ``NAME`` is usually set
        by passing a ``dest`` attribute, for example
        ``addoption("--long", dest="NAME", ...)``.
        N)r!   Ú	addoption)r(   r<   r=   r)   r)   r*   r>   Z   s    zParser.addoptionzos.PathLike[str]©ÚargsÚ	namespacer   c                 C   s>   ddl m} |  ¡ | _|| jƒ dd„ |D ƒ}| jj||d�S )Nr   )Útry_argcompletec                 S   s   g | ]}t  |¡‘qS r)   ©ÚosÚfspath©Ú.0Úxr)   r)   r*   Ú
<listcomp>s   ó    z Parser.parse.<locals>.<listcomp>©rA   )Ú_pytest._argcompleterB   Ú
_getparserÚ	optparserÚ
parse_args)r(   r@   rA   rB   Ústrargsr)   r)   r*   Úparsej   s
    

zParser.parseÚMyOptionParser©r   c                 C   s–   ddl m} t| | j| jd�}| j| jg }|D ]L}|jr0|jpD|j	}| 
|¡}|jD ]$}| ¡ }| ¡ }	|j|i |	¤Ž qVq0|jtdd�}
||
_|S )Nr   )Úfilescompleter)r   Ú*)Únargs)rL   rT   rR   r'   r   r"   r!   Úoptionsr2   r1   Úadd_argument_groupÚnamesr=   Úadd_argumentÚFILE_OR_DIRZ	completer)r(   rT   rN   Úgroupsr7   ÚdescZarggroupr,   ÚnÚaZfile_or_dir_argr)   r)   r*   rM   v   s    

zParser._getparser)r@   r,   rA   r   c                 C   sB   | j ||d�}|j ¡ D ]\}}t|||ƒ qttt t|tƒƒS )NrK   )	rQ   Ú__dict__ÚitemsÚsetattrr   r   ÚstrÚgetattrr[   )r(   r@   r,   rA   Zparsedoptionr1   Úvaluer)   r)   r*   Úparse_setoption‰   s    zParser.parse_setoptionc                 C   s   | j ||d�d S )zbParse the known arguments at this point.

        :returns: An argparse namespace object.
        rK   r   )Úparse_known_and_unknown_args)r(   r@   rA   r)   r)   r*   Úparse_known_args”   s    	zParser.parse_known_argsc                 C   s$   |   ¡ }dd„ |D ƒ}|j||d�S )a  Parse the known arguments at this point, and also return the
        remaining unknown arguments.

        :returns:
            A tuple containing an argparse namespace object for the known
            arguments, and a list of the unknown arguments.
        c                 S   s   g | ]}t  |¡‘qS r)   rC   rF   r)   r)   r*   rI   ¬   rJ   z7Parser.parse_known_and_unknown_args.<locals>.<listcomp>rK   )rM   rh   )r(   r@   rA   rN   rP   r)   r)   r*   rg   Ÿ   s    z#Parser.parse_known_and_unknown_argszBLiteral['string', 'paths', 'pathlist', 'args', 'linelist', 'bool'])r1   ÚhelpÚtypeÚdefaultr   c                 C   s,   |dv sJ ‚|||f| j |< | j |¡ dS )a–  Register an ini-file option.

        :param name:
            Name of the ini-variable.
        :param type:
            Type of the variable. Can be:

                * ``string``: a string
                * ``bool``: a boolean
                * ``args``: a list of strings, separated as in a shell
                * ``linelist``: a list of strings, separated by line breaks
                * ``paths``: a list of :class:`pathlib.Path`, separated as in a shell
                * ``pathlist``: a list of ``py.path``, separated as in a shell

            .. versionadded:: 7.0
                The ``paths`` variable type.

            Defaults to ``string`` if ``None`` or not passed.
        :param default:
            Default value if no ini-file option exists but is queried.

        The value of ini-variables can be retrieved via a call to
        :py:func:`config.getini(name) <pytest.Config.getini>`.
        )NÚstringÚpathsÚpathlistr@   ZlinelistÚboolN)r%   r&   Úappend)r(   r1   ri   rj   rk   r)   r)   r*   Úaddini¯   s    !zParser.addini)NN)r0   N)N)N)N)N)NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r
   rc   Ú__annotations__r   ro   r+   r/   r:   r   r>   r   r   ÚargparseÚ	NamespacerQ   rM   r   rf   rh   r   rg   rq   r)   r)   r)   r*   r   !   sp   
  ýûú ÿ
þ ýü üû ýü ýü  ùÿør   c                   @   s8   e Zd ZdZeedef ddœdd„Zedœdd	„ZdS )
ÚArgumentErrorzURaised if an Argument instance is created with invalid or
    inconsistent arguments.r   N)Úmsgr,   r   c                 C   s   || _ t|ƒ| _d S r-   )rz   rc   Ú	option_id)r(   rz   r,   r)   r)   r*   r+   Ù   s    zArgumentError.__init__rS   c                 C   s$   | j rd| j › d| j› �S | jS d S )Nzoption ú: )r{   rz   ©r(   r)   r)   r*   Ú__str__Ý   s    zArgumentError.__str__)rr   rs   rt   ru   rc   r   r+   r~   r)   r)   r)   r*   ry   Õ   s   ry   c                   @   sz   e Zd ZdZeeeedœZee	ddœdd„Z
ee dœdd	„Zeee	f dœd
d„Zee ddœdd„Zedœdd„ZdS )r   z÷Class that mimics the necessary behaviour of optparse.Option.

    It's currently a least effort implementation and ignoring choices
    and integer prefixes.

    https://docs.python.org/3/library/optparse.html#optparse-standard-option-types
    )Úintrl   ÚfloatÚcomplexN)rY   r=   r   c              
   O   sŠ  || _ g | _g | _d| d¡p dv r2tjtdd� z|d }W n tyP   Y nz0 t|t	ƒrÄ|dkr’tjt
j||d�d	d� t|d
 d ƒ|d< n&tjtj||d�d	d� tj| |d< |d | _n|| _z|d | _W n tyê   Y n0 |  |¡ | d¡}|�r|| _nx| j�r4| jd dd…  dd¡| _nRz| jd dd… | _W n8 t�y„ } zd| _td| ƒ|‚W Y d}~n
d}~0 0 dS )z5Store params in private vars for use in add_argument.ú%defaultri   r0   é   )Ú
stacklevelrj   Úchoice)ÚtyprY   é   Úchoicesr   rk   r.   é   Nú-Ú_r4   z???zneed a long or short option)Ú_attrsÚ_short_optsÚ
_long_optsÚgetÚwarningsÚwarnr   ÚKeyErrorÚ
isinstancerc   r   Úformatrj   r   r   Ú_typ_maprk   Ú_set_opt_stringsr.   ÚreplaceÚ
IndexErrorry   )r(   rY   r=   r†   r.   Úer)   r)   r*   r+   ï   sJ    
þÿ

zArgument.__init__rS   c                 C   s   | j | j S r-   )r�   rŽ   r}   r)   r)   r*   rY      s    zArgument.namesc              	   C   sx   d  ¡ }| | j¡ |D ],}zt| |ƒ| j|< W q tyB   Y q0 q| j d¡rr| jd }| dd¡}|| jd< | jS )Nzdefault dest helpri   r‚   z%(default)s)Úsplitrp   r.   rd   rŒ   ÚAttributeErrorr�   r—   )r(   r=   Úattrr_   r)   r)   r*   r=   #  s    

zArgument.attrs)r<   r   c                 C   s    |D ]–}t |ƒdk r$td| | ƒ‚qt |ƒdkrd|d dkrH|d dksVtd| | ƒ‚| j |¡ q|dd… dkr€|d dksŽtd| | ƒ‚| j |¡ qd	S )
zhDirectly from optparse.

        Might not be necessary as this is passed to argparse later on.
        r‰   z>invalid option string %r: must be at least two characters longr   rŠ   r4   zMinvalid short option string %r: must be of the form -x, (x any non-dash char)ú--zGinvalid long option string %r: must start with --, followed by non-dashN)Úlenry   r�   rp   rŽ   )r(   r<   Úoptr)   r)   r*   r–   3  s2    ÿýÿýÿýzArgument._set_opt_stringsc                 C   s˜   g }| j r|dt| j ƒ g7 }| jr8|dt| jƒ g7 }|dt| jƒ g7 }t| dƒrj|dt| jƒ g7 }t| dƒrˆ|dt| jƒ g7 }d d	 |¡¡S )
Nz_short_opts: z_long_opts: zdest: rj   ztype: rk   z	default: zArgument({})ú, )	r�   ÚreprrŽ   r.   Úhasattrrj   rk   r”   Újoin)r(   r@   r)   r)   r*   Ú__repr__P  s    

zArgument.__repr__)rr   rs   rt   ru   r   rc   r€   r�   r•   r   r+   r   rY   r   r=   r   r–   r¤   r)   r)   r)   r*   r   ä   s   1r   c                   @   sj   e Zd ZdZdddœeeee eddœdd„Zee	dd	œd
d„Z
ee	dd	œdd„Zddeddœdd„ZdS )r    z,A group of options shown in its own section.r0   NFr   )r1   r2   r   r   r   c                C   s$   t |ƒ || _|| _g | _|| _d S r-   )r   r1   r2   rW   r   )r(   r1   r2   r   r   r)   r)   r*   r+   a  s
    zOptionGroup.__init__r;   c                 O   sJ   t |ƒ dd„ | jD ƒ¡}|r*td| ƒ‚t|i |¤Ž}| j|dd� dS )aM  Add an option to this group.

        If a shortened version of a long option is specified, it will
        be suppressed in the help. ``addoption('--twowords', '--two-words')``
        results in help showing ``--two-words`` only, but ``--twowords`` gets
        accepted **and** the automatic destination is in ``args.twowords``.

        :param opts:
            Option names, can be short or long options.
        :param attrs:
            Same attributes as the argparse library's :py:func:`add_argument()
            <argparse.ArgumentParser.add_argument>` function accepts.
        c                 s   s    | ]}|  ¡ D ]
}|V  qqd S r-   )rY   )rG   rŸ   r1   r)   r)   r*   Ú	<genexpr>}  s   z(OptionGroup.addoption.<locals>.<genexpr>zoption names %s already addedF©Ú
shortupperN)ÚsetÚintersectionrW   Ú
ValueErrorr   Ú_addoption_instance)r(   r<   r=   Úconflictr,   r)   r)   r*   r>   o  s    ÿzOptionGroup.addoptionc                 O   s    t |i |¤Ž}| j|dd� d S )NTr¦   )r   r«   )r(   r<   r=   r,   r)   r)   r*   Ú
_addoption…  s    zOptionGroup._addoptionr   )r,   r§   r   c                 C   sR   |s0|j D ]$}|d dkr
|d  ¡ r
tdƒ‚q
| jrB| j |¡ | j |¡ d S )Nr   rŠ   r4   zlowercase shortoptions reserved)r�   Úislowerrª   r   r/   rW   rp   )r(   r,   r§   rŸ   r)   r)   r*   r«   ‰  s    

zOptionGroup._addoption_instance)r0   N)F)rr   rs   rt   ru   rc   r
   r   ro   r+   r   r>   r­   r«   r)   r)   r)   r*   r    ^  s     üúùr    c                       s¨   e Zd Zdeeeeef  ee ddœ‡ fdd„Zee	dœdd„Z
deee  eej ejdœd	d
„Zejdd… dk r eeeeej eee f  dœdd„Z‡  ZS )rR   N)r   r'   r   r   c                    s0   || _ tƒ j||jdtdd� |r&|ni | _d S )NF)r   r   Úadd_helpÚformatter_classÚallow_abbrev)Ú_parserÚsuperr+   r$   ÚDropShorterLongHelpFormatterr'   )r(   r   r'   r   ©Ú	__class__r)   r*   r+   ”  s    û	zMyOptionParser.__init__)Úmessager   c                 C   sD   | j › d|› �}t| jdƒr0|› d| jj› d�}t|  ¡ | ƒ‚dS )z1Transform argparse error message into UsageError.z	: error: Ú_config_source_hintz (ú)N)r   r¢   r²   r¸   r   Úformat_usage)r(   r·   rz   r)   r)   r*   Úerror¦  s    zMyOptionParser.errorr?   c           	      C   s�   |   ||¡\}}|rŒ|D ]b}|r|d dkrdd |¡ g}t| j ¡ ƒD ]\}}| d|› d|› �¡ qJ|  d |¡¡ qt|tƒ 	|¡ |S )z(Allow splitting of positional arguments.r   rŠ   zunrecognized arguments: %sú z  r|   Ú
)
rh   r£   Úsortedr'   ra   rp   r»   rd   r[   Úextend)	r(   r@   rA   ÚparsedZunrecognizedÚargÚlinesÚkÚvr)   r)   r*   rO   ±  s    zMyOptionParser.parse_argsr‰   )rƒ   é	   )Ú
arg_stringr   c           	      C   s  |sd S |d | j vrd S || jv r8| j| }||d fS t|ƒdkrHd S d|v r~| dd¡\}}|| jv r~| j| }|||fS | jsŽ| d¡sì|  |¡}t|ƒdkrÖtdƒ}d dd„ |D ƒ¡}|  	|||d	œ ¡ nt|ƒdkrì|\}|S | j
 |¡�r| j�sd S d
|v �rd S d |d fS )Nr   r4   ú=r�   z4ambiguous option: %(option)s could match %(matches)sr    c                 s   s   | ]\}}}|V  qd S r-   r)   )rG   r‹   r,   r)   r)   r*   r¥   Ü  rJ   z1MyOptionParser._parse_optional.<locals>.<genexpr>)r,   Úmatchesr¼   )Úprefix_charsÚ_option_string_actionsrž   rš   r±   Ú
startswithÚ_get_option_tuplesr   r£   r»   Ú_negative_number_matcherÚmatchÚ_has_negative_number_optionals)	r(   rÆ   ÚactionÚoption_stringÚexplicit_argÚoption_tuplesrz   rW   Úoption_tupler)   r)   r*   Ú_parse_optionalÅ  s>    






ÿ
zMyOptionParser._parse_optional)NN)NN)rr   rs   rt   r   r
   r   rc   r   r+   r	   r»   r   rw   rx   rO   ÚsysÚversion_infor   ÚActionrÕ   Ú__classcell__r)   r)   rµ   r*   rR   “  s(     üû  ý
üþrR   c                       sH   e Zd ZdZeeddœ‡ fdd„Zejedœ‡ fdd„Z	d	d
„ Z
‡  ZS )r´   a+  Shorten help for long options that differ only in extra hyphens.

    - Collapse **long** options that are the same except for extra hyphens.
    - Shortcut if there are only two options and one of them is a short one.
    - Cache result on the action object as this is called at least 2 times.
    N)r@   Úkwargsr   c                    s,   d|vrt j ¡ |d< tƒ j|i |¤Ž d S )NÚwidth)Ú_pytestÚ_ioZget_terminal_widthr³   r+   )r(   r@   rÚ   rµ   r)   r*   r+   ñ  s    z%DropShorterLongHelpFormatter.__init__)rÐ   r   c                    sj  t ƒ  |¡}|r |d dkr |S t|dd ƒ}|r4|S | d¡}t|ƒdkrtt|d ƒdksjt|d ƒdkrt||_|S g }i }|D ]r}t|ƒdks€|d dkržq€| d¡s¶td	| |ƒ‚|dd … }| dd
¡}	|	|vsêt||	 ƒt|ƒk r€|||	< q€|D ]\}t|ƒdk�s|d dk�r"| 	|¡ |dd … | 
| dd
¡¡krø| 	| ddd¡¡ qød |¡}
|
|_|
S )Nr   rŠ   Ú_formatted_action_invocationr    r‰   r4   r¼   r�   z)long optional argument without "--": [%s]r0   rÇ   )r³   Ú_format_action_invocationrd   rš   rž   rÞ   rË   ry   r—   rp   r�   r£   )r(   rÐ   ZorgstrÚresrW   Zreturn_listZ
short_longr,   ZxxoptionZ	shortenedZformatted_action_invocationrµ   r)   r*   rß   ÷  sB    
,
ÿÿ


z6DropShorterLongHelpFormatter._format_action_invocationc                 C   s4   ddl }g }| ¡ D ]}| | | ¡ |¡¡ q|S )z}Wrap lines after splitting on original newlines.

        This allows to have explicit line breaks in the help text.
        r   N)ÚtextwrapÚ
splitlinesr¿   ÚwrapÚstrip)r(   ÚtextrÛ   rá   rÂ   Úliner)   r)   r*   Ú_split_lines  s
    z)DropShorterLongHelpFormatter._split_lines)rr   rs   rt   ru   r   r+   rw   rØ   rc   rß   rç   rÙ   r)   r)   rµ   r*   r´   é  s   &r´   ))rw   rD   rÖ   r�   r   Útypingr   r   r   r   r   r   r	   r
   r   r   r   r   Z_pytest._iorÜ   Z_pytest.compatr   Z_pytest.config.exceptionsr   Z_pytest.deprecatedr   r   r   r   Ztyping_extensionsr   r[   r   Ú	Exceptionry   r   r    ÚArgumentParserrR   ÚHelpFormatterr´   r)   r)   r)   r*   Ú<module>   sD    4z5V