a
    !fR                     @   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 )/ParserzParser 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)   Z/var/www/html/python-backend/venv/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pathsZ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   rn   r+   r/   r:   r   r>   r   r   argparse	NamespacerQ   rM   r   rf   rh   r   rg   rp   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-   )ry   rc   	option_id)r(   ry   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 : )rz   ry   r(   r)   r)   r*   __str__   s    zArgumentError.__str__)rq   rr   rs   rt   rc   r   r+   r}   r)   r)   r)   r*   rx      s   rx   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   zClass 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rt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
IndexErrorrx   )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)splitro   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st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)lenrx   r   ro   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__)rq   rr   rs   rt   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   ro   )r(   r,   r   r   r)   r)   r*   r     s    

zOptionGroup._addoption_instance)r0   N)F)rq   rr   rs   rt   rc   r
   r   rn   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 re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   ry   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   ro   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rt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_tuplesry   rW   option_tupler)   r)   r*   _parse_optional  s>    







zMyOptionParser._parse_optional)NN)NN)rq   rr   rs   r   r
   r   rc   r   r+   r	   r   r   rv   rw   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rq|dstd	| ||dd  }|dd
}	|	|vst||	 t|k r|||	< q|D ]\}t|dks|d dkr"|	| |dd  |
|dd
kr|	|ddd qd|}
|
|_|
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   rx   r   ro   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)rq   rr   rs   rt   r   r+   rv   r   rc   r   r   r   r)   r)   r   r*   r     s   &r   ))rv   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   	Exceptionrx   r   r    ArgumentParserrR   HelpFormatterr   r)   r)   r)   r*   <module>   sD    4z5V