a
    *ñ»jžJ  ã                   @  s*  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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ZG dd„ dƒZeƒ ZeG dd„ dƒƒZdd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 )!é    )ÚannotationsN)ÚCallable)ÚMapping)ÚSequence)ÚAny)Úcast)Úfinal)ÚLiteral)ÚNoReturn)Ú
UsageError)Úcheck_ispytestZfile_or_dirc                   @  s   e Zd Zddœdd„ZdS )ÚNotSetÚstr©Úreturnc                 C  s   dS )Nz<notset>© ©Úselfr   r   ú?E:\soccer\ml_env\Lib\site-packages\_pytest/config/argparsing.pyÚ__repr__   s    zNotSet.__repr__N)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r      s   r   c                   @  sî   e Zd ZU dZdZded< d2ddœdddd	d
œdd„Zdd	dœdd„Zd3dddddœdd„Zddd	dœdd„Z	d4ddddœdd „Z
d!d"œd#d$„Zd5dddd%d&œd'd(„Zd6ddddœd)d*„Zd7ddd+dœd,d-„Zdefddd.dd	d/œd0d1„ZdS )8Ú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ú
str | NoneÚprogF©Ú	_ispytestz!Callable[[Argument], None] | NoneÚboolÚNone)ÚusageÚ
processoptr   r   c                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)r   r    r!   r   r   r   r   Ú__init__)   s    zParser.__init__ÚArgument)Úoptionr   c                 C  s   | j r|jr|   |¡ d S ©N)r'   Údest)r   r.   r   r   r   Úprocessoption9   s    zParser.processoptionÚ r   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&   r3   r$   Ú	enumerateÚinsert)r   r3   r4   r5   ÚgroupÚiÚgrpr   r   r   Úgetgroup>   s    



zParser.getgroupr   ©Ú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 :meth:`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@   X   s    zParser.addoptionz Sequence[str | os.PathLike[str]]úargparse.Namespace | Noneúargparse.Namespace©Ú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>q   ó    z Parser.parse.<locals>.<listcomp>©rE   )Ú_pytest._argcompleterF   Ú
_getparserÚ	optparserÚ
parse_args)r   rD   rE   rF   Ústrargsr   r   r   Úparseh   s
    

zParser.parseÚMyOptionParserr   c                 C  s˜   ddl m} t| | j| jd�}g | j¢| j‘}|D ]L}|jr2|jpF|j	}| 
|¡}|jD ]$}| ¡ }| ¡ }	|j|i |	¤Ž qXq2|jtdd�}
||
_|S )Nr   )Úfilescompleter)r   Ú*)Únargs)rP   rW   rV   r+   r   r&   r%   Úoptionsr4   r3   Úadd_argument_groupÚnamesr?   Úadd_argumentÚFILE_OR_DIRZ	completer)r   rW   rR   Úgroupsr9   ÚdescZarggroupr.   ÚnÚaZfile_or_dir_argr   r   r   rQ   t   s    

zParser._getparserú	list[str])rD   r.   rE   r   c                 C  sB   | j ||d�}|j ¡ D ]\}}t|||ƒ qttt t|tƒƒS )NrO   )	rU   Ú__dict__ÚitemsÚsetattrr   Úlistr   Úgetattrr^   )r   rD   r.   rE   Zparsedoptionr3   Ú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.
        rO   r   )Úparse_known_and_unknown_args)r   rD   rE   r   r   r   Úparse_known_args’   s    	zParser.parse_known_argsz$tuple[argparse.Namespace, list[str]]c                 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   rG   rJ   r   r   r   rM   ª   rN   z7Parser.parse_known_and_unknown_args.<locals>.<listcomp>rO   )rQ   rl   )r   rD   rE   rR   rT   r   r   r   rk   �   s    z#Parser.parse_known_and_unknown_argsúYLiteral['string', 'paths', 'pathlist', 'args', 'linelist', 'bool', 'int', 'float'] | None)r3   ÚhelpÚtypeÚdefaultr   c                 C  s<   |dv sJ ‚|t u rt|ƒ}|||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
                * ``int``: an integer
                * ``float``: a floating-point number

                .. versionadded:: 8.4

                    The ``float`` and ``int`` types.

            For ``paths`` and ``pathlist`` types, they are considered relative to the ini-file.
            In case the execution is happening without an ini-file defined,
            they will be considered relative to the current working directory (for example with ``--override-ini``).

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

            .. versionadded:: 8.1
                Use the current working directory to resolve ``paths`` and ``pathlist`` in the absence of an ini-file.

            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ÚpathlistrD   Úlinelistr   ÚintÚfloatN)ÚNOT_SETÚget_ini_default_for_typer)   r*   Úappend)r   r3   rn   ro   rp   r   r   r   Úaddini­   s
    /zParser.addini)NN)r2   N)N)N)N)N)r   r   r   Ú__doc__r   Ú__annotations__r,   r1   r<   r@   rU   rQ   rj   rl   rk   rw   rz   r   r   r   r   r      s.   
  ýû ÿ ý ü ý ýør   rm   r   )ro   r   c                 C  sD   | du rdS | dv rg S | dkr$dS | dkr0dS | dkr<d	S dS dS )
zp
    Used by addini to get the default value for a given ini-option type, when
    default is not supplied.
    Nr2   )rr   rs   rD   rt   r   Fru   r   rv   g        r   )ro   r   r   r   rx   î   s    
rx   c                   @  s0   e Zd ZdZddddœdd„Zddœd	d
„ZdS )ÚArgumentErrorzURaised if an Argument instance is created with invalid or
    inconsistent arguments.r   zArgument | strr   )Úmsgr.   r   c                 C  s   || _ t|ƒ| _d S r/   )r~   r   Ú	option_id)r   r~   r.   r   r   r   r,   
  s    zArgumentError.__init__r   c                 C  s$   | j rd| j › d| j› �S | jS d S )Nzoption ú: )r   r~   r   r   r   r   Ú__str__  s    zArgumentError.__str__N)r   r   r   r{   r,   r�   r   r   r   r   r}     s   r}   c                   @  s\   e Zd ZdZddddœdd„Zdd	œd
d„Zdd	œdd„Zdddœdd„Zd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
    r   r   r   )r\   r?   r   c              
   O  sî   || _ g | _g | _z|d | _W n ty2   Y n0 z|d | _W n tyT   Y n0 |  |¡ | d¡}|rv|| _nt| jrš| jd dd…  	dd¡| _nPz| jd d	d… | _W n6 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.ro   rp   r0   r   é   Nú-Ú_r6   z???zneed a long or short option)Ú_attrsÚ_short_optsÚ
_long_optsro   ÚKeyErrorrp   Ú_set_opt_stringsÚgetr0   ÚreplaceÚ
IndexErrorr}   )r   r\   r?   r0   Úer   r   r   r,     s,    

zArgument.__init__rc   r   c                 C  s   | j | j S r/   )r†   r‡   r   r   r   r   r\   9  s    zArgument.nameszMapping[str, Any]c              	   C  sL   d  ¡ }| | j¡ |D ],}zt| |ƒ| j|< W q tyB   Y q0 q| jS )Nzdefault dest help)Úsplitry   r0   rh   r…   ÚAttributeError)r   r?   Úattrr   r   r   r?   <  s    zArgument.attrszSequence[str])r>   r   c                 C  s¬   |D ]¢}t |ƒdk r(td|›d�| ƒ‚qt |ƒdkrl|d dkrL|d dks^td|›d�| ƒ‚| j |¡ q|dd… d	krˆ|d dksštd
|›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 z&: must be at least two characters longr   rƒ   r6   zinvalid short option string z/: must be of the form -x, (x any non-dash char)ú--zinvalid long option string z*: must start with --, followed by non-dashN)Úlenr}   r†   ry   r‡   )r   r>   Úoptr   r   r   r‰   G  s&    
ý
ý
ý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: ro   ztype: rp   z	default: zArgument({})ú, )	r†   Úreprr‡   r0   Úhasattrro   rp   ÚformatÚjoin)r   rD   r   r   r   r   d  s    

zArgument.__repr__N)	r   r   r   r{   r,   r\   r?   r‰   r   r   r   r   r   r-     s   r-   c                   @  sf   e Zd ZdZdddœddddd	d
œdd„Zddd	dœdd„Zddd	dœdd„Zdddd	dœdd„ZdS )r$   z,A group of options shown in its own section.r2   NFr   r   zParser | Noner   r   )r3   r4   r#   r   r   c                C  s$   t |ƒ || _|| _g | _|| _d S r/   )r   r3   r4   rZ   r#   )r   r3   r4   r#   r   r   r   r   r,   u  s
    zOptionGroup.__init__r   r=   c                 O  sN   t |ƒ dd„ | jD ƒ¡}|r.td|› d�ƒ‚t|i |¤Ž}| j|dd� dS )aJ  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 :meth:`add_argument()
            <argparse.ArgumentParser.add_argument>` function accepts.
        c                 s  s    | ]}|  ¡ D ]
}|V  qqd S r/   )r\   )rK   r“   r3   r   r   r   Ú	<genexpr>‘  s   z(OptionGroup.addoption.<locals>.<genexpr>zoption names z already addedF©Ú
shortupperN)ÚsetÚintersectionrZ   Ú
ValueErrorr-   Ú_addoption_instance)r   r>   r?   Úconflictr.   r   r   r   r@   ƒ  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ƒ   r6   zlowercase shortoptions reserved)r†   Úislowerrž   r#   r1   rZ   ry   )r   r.   r›   r“   r   r   r   rŸ   �  s    

zOptionGroup._addoption_instance)r2   N)F)r   r   r   r{   r,   r@   r¡   rŸ   r   r   r   r   r$   r  s     üúr$   c                      sN   e Zd Zddddddœ‡ fdd„Zd	d
dœdd„Zdddddœdd„Z‡  ZS )rV   Nr   zdict[str, Any] | Noner   r   )r#   r+   r   r   c                   s2   || _ tƒ j||jdtddd� |r(|ni | _d S )NFú@)r   r    Úadd_helpZformatter_classZallow_abbrevZfromfile_prefix_chars)Ú_parserÚsuperr,   r(   ÚDropShorterLongHelpFormatterr+   )r   r#   r+   r   ©Ú	__class__r   r   r,   ¨  s    ú
zMyOptionParser.__init__r   r
   )Ú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ª   r~   r   r   r   Úerror»  s    zMyOptionParser.errorzSequence[str] | NonerA   rB   rC   c           	      C  s’   |   ||¡\}}|rŽ|D ]d}|r|d dkrd d |¡¡g}t| j ¡ ƒD ]\}}| d|› d|› �¡ qL|  d |¡¡ qt|t	ƒ 
|¡ |S )z(Allow splitting of positional arguments.r   rƒ   zunrecognized arguments: {}ú z  r€   Ú
)rl   r—   r˜   Úsortedr+   re   ry   r®   rh   r^   Úextend)	r   rD   rE   ÚparsedZunrecognizedÚargÚlinesÚkÚvr   r   r   rS   Å  s    ÿzMyOptionParser.parse_args)NN)NN)r   r   r   r,   r®   rS   Ú__classcell__r   r   r¨   r   rV   §  s     ü  ýrV   c                      sF   e Zd ZdZddddœ‡ fdd„Zdd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.
    r   r   )rD   Úkwargsr   c                   s,   d|vrt j ¡ |d< tƒ j|i |¤Ž d S )NÚwidth)Ú_pytestÚ_ioZget_terminal_widthr¦   r,   )r   rD   r¹   r¨   r   r   r,   á  s    z%DropShorterLongHelpFormatter.__init__zargparse.Actionr   )Úactionr   c                   sn  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 ]v}t|ƒdks€|d dkržq€| d¡sºtd	|› 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‚   r6   r¯   r‘   z&long optional argument without "--": [ú]r2   ú=)r¦   Ú_format_action_invocationrh   rŽ   r’   r¾   Ú
startswithr}   r‹   ry   rŠ   r˜   )r   r½   ZorgstrÚresrZ   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)r   r   r   r{   r,   rÁ   rÊ   r¸   r   r   r¨   r   r§   Ù  s   &r§   )!Ú
__future__r   ÚargparseÚcollections.abcr   r   r   rH   Útypingr   r   r   r	   r
   Z_pytest._ior»   Z_pytest.config.exceptionsr   Z_pytest.deprecatedr   r^   r   rw   r   rx   Ú	Exceptionr}   r-   r$   ÚArgumentParserrV   ÚHelpFormatterr§   r   r   r   r   Ú<module>   s2    O]52