o
    rhRP                     @  sD  d dl mZ d dlZd dlm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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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)gettext)Any)Callable)cast)final)List)Literal)Mapping)NoReturn)Sequence)
UsageError)check_ispytestfile_or_dirc                   @  s   e Zd ZdddZdS )NotSetreturnstrc                 C  s   dS )Nz<notset> selfr   r   W/var/www/html/alpaca_bot/venv/lib/python3.10/site-packages/_pytest/config/argparsing.py__repr__   s   zNotSet.__repr__Nr   r   )__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< 		d9ddd:ddZd;ddZ	d<d=ddZd>dd Z		d?d@d&d'Z
dAd)d*Z	d?dBd,d-Z	d?d@d.d/Z	d?dCd1d2ZdefdDd7d8ZdS )E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
str | NoneprogF	_ispytestusage
processopt!Callable[[Argument], None] | Noner    boolr   Nonec                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__optionArgumentc                 C  s"   | j r|jr|  | d S d S d S N)r+   dest)r   r1   r   r   r   processoption<   s
   zParser.processoption namer   descriptionafterr(   c                 C  sj   | j D ]}|j|kr|  S qt||| dd}d}t| j D ]\}}|j|kr) nq| 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*   r7   r(   	enumerateinsert)r   r7   r8   r9   groupigrpr   r   r   getgroupA   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 :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   rA   rB   r   r   r   rC   [   s   zParser.addoptionargs Sequence[str | os.PathLike[str]]	namespaceargparse.Namespace | Noneargparse.Namespacec                 C  s>   ddl m} |  | _|| j dd |D }| jj||dS )Nr   )try_argcompletec                 S     g | ]}t |qS r   osfspath.0xr   r   r   
<listcomp>t       z Parser.parse.<locals>.<listcomp>rF   )_pytest._argcompleterI   
_getparser	optparser
parse_args)r   rD   rF   rI   strargsr   r   r   parsek   s
   

zParser.parseMyOptionParserc                 C  s   ddl m} t| | j| jd}g | j| j}|D ]&}|jr?|jp#|j	}|
|}|jD ]}| }| }	|j|i |	 q,q|jtdd}
||
_|S )Nr   )filescompleter)r   *)nargs)rT   r[   rZ   r/   r   r*   r)   optionsr8   r7   add_argument_groupnamesrB   add_argumentFILE_OR_DIR	completer)r   r[   rV   groupsr=   descarggroupr1   nafile_or_dir_argr   r   r   rU   w   s   

zParser._getparser	list[str]c                 C  sB   | j ||d}|j D ]
\}}t||| qttt t|tS )NrS   )	rY   __dict__itemssetattrr   r   r   getattrrb   )r   rD   r1   rF   parsedoptionr7   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.
        rS   r   )parse_known_and_unknown_args)r   rD   rF   r   r   r   parse_known_args   s   	zParser.parse_known_args$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  rJ   r   rK   rN   r   r   r   rQ      rR   z7Parser.parse_known_and_unknown_args.<locals>.<listcomp>rS   )rU   rs   )r   rD   rF   rV   rX   r   r   r   rr      s   z#Parser.parse_known_and_unknown_argshelptypeILiteral['string', 'paths', 'pathlist', 'args', 'linelist', 'bool'] | Nonedefaultc                 C  s<   |dv sJ |t u rt|}|||f| j|< | j| dS )aR  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

            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$   N)NOT_SETget_ini_default_for_typer-   r.   append)r   r7   ru   rv   rx   r   r   r   addini   s
   'zParser.addiniNN)r!   r   r"   r#   r    r$   r   r%   )r1   r2   r   r%   r6   N)r7   r   r8   r   r9   r   r   r(   rA   r   rB   r   r   r%   r3   )rD   rE   rF   rG   r   rH   )r   rZ   )rD   rE   r1   rH   rF   rG   r   rj   )rD   rE   rF   rG   r   rt   )
r7   r   ru   r   rv   rw   rx   r   r   r%   )r   r   r   __doc__r   __annotations__r0   r5   r@   rC   rY   rU   rq   rs   rr   r}   r   r   r   r   r   r   "   s0   
 


r   rv   rw   r   r   c                 C  s(   | du rdS | dv rg S | dkrdS dS )zp
    Used by addini to get the default value for a given ini-option type, when
    default is not supplied.
    Nr6   )rz   r{   rD   r|   r$   Fr   )rv   r   r   r   r~      s   r~   c                   @  s$   e Zd ZdZddd	Zdd
dZdS )ArgumentErrorzURaised if an Argument instance is created with invalid or
    inconsistent arguments.msgr   r1   Argument | strr   r%   c                 C  s   || _ t|| _d S r3   )r   r   	option_id)r   r   r1   r   r   r   r0      s   zArgumentError.__init__c                 C  s    | j rd| j  d| j S | jS )Nzoption : )r   r   r   r   r   r   __str__   s   zArgumentError.__str__N)r   r   r1   r   r   r%   r   )r   r   r   r   r0   r   r   r   r   r   r      s    
r   c                   @  sB   e Zd ZdZddd	ZdddZdddZdddZdddZdS )r2   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
    r`   r   rB   r   r   r%   c              
   O  s   || _ g | _g | _z|d | _W n	 ty   Y nw z|d | _W n	 ty*   Y nw | | |d}|r<|| _dS | jrO| jd dd 	dd| _dS z| jd d	d | _W dS  t
yq } z	d
| _td| |d}~ww )z5Store params in private vars for use in add_argument.rv   rx   r4   r      N-_r:   z???zneed a long or short option)_attrs_short_opts
_long_optsrv   KeyErrorrx   _set_opt_stringsgetr4   replace
IndexErrorr   )r   r`   rB   r4   er   r   r   r0     s4   


 zArgument.__init__rj   c                 C  s   | j | j S r3   )r   r   r   r   r   r   r`   #  s   zArgument.namesMapping[str, Any]c              	   C  sJ   d  }|| j |D ]}z
t| || j|< W q ty!   Y qw | jS )Nzdefault dest help)splitr   r4   rn   r   AttributeError)r   rB   attrr   r   r   rB   &  s   zArgument.attrsrA   Sequence[str]c                 C  s   |D ]P}t |dk rtd|d| t |dkr5|d dkr%|d dks.td|d| | j| q|dd d	krC|d dksLt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   r:   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   r   r   )r   rA   optr   r   r   r   1  s(   


zArgument._set_opt_stringsc                 C  s   g }| j r|dt| j  g7 }| jr|dt| j g7 }|dt| j g7 }t| dr5|dt| j g7 }t| drD|dt| j g7 }dd	|S )
Nz_short_opts: z_long_opts: zdest: rv   ztype: rx   z	default: zArgument({}), )	r   reprr   r4   hasattrrv   rx   formatjoin)r   rD   r   r   r   r   N  s   

zArgument.__repr__N)r`   r   rB   r   r   r%   )r   rj   )r   r   )rA   r   r   r%   r   )	r   r   r   r   r0   r`   rB   r   r   r   r   r   r   r2      s    



r2   c                   @  sF   e Zd ZdZ		ddddddZdddZdddZd d!ddZdS )"r(   z,A group of options shown in its own section.r6   NFr   r7   r   r8   r'   Parser | Noner    r$   r   r%   c                C  s$   t | || _|| _g | _|| _d S r3   )r   r7   r8   r^   r'   )r   r7   r8   r'   r    r   r   r   r0   _  s
   
zOptionGroup.__init__rA   rB   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 r3   )r`   )rO   r   r7   r   r   r   	<genexpr>{  s    z(OptionGroup.addoption.<locals>.<genexpr>zoption names z already addedF
shortupperN)setintersectionr^   
ValueErrorr2   _addoption_instance)r   rA   rB   conflictr1   r   r   r   rC   m  s   zOptionGroup.addoptionc                 O  s    t |i |}| j|dd d S )NTr   )r2   r   )r   rA   rB   r1   r   r   r   
_addoption  s   zOptionGroup._addoptionr1   r2   r   c                 C  sR   |s|j D ]}|d dkr|d  rtdq| jr!| j| | j| d S )Nr   r   r:   zlowercase shortoptions reserved)r   islowerr   r'   r5   r^   r   )r   r1   r   r   r   r   r   r     s   
zOptionGroup._addoption_instancer   )
r7   r   r8   r   r'   r   r    r$   r   r%   r   )F)r1   r2   r   r$   r   r%   )r   r   r   r   r0   rC   r   r   r   r   r   r   r(   \  s    

r(   c                      sZ   e Zd Z		dd fd
dZdddZ		dd ddZejdk r)d!ddZ  Z	S   Z	S )"rZ   Nr'   r   r/   dict[str, Any] | Noner   r   r   r%   c                   s8   || _ t j||jdtddd |r|| _d S i | _d S )NF@)r   r!   add_helpformatter_classallow_abbrevfromfile_prefix_chars)_parsersuperr0   r,   DropShorterLongHelpFormatterr/   )r   r'   r/   r   	__class__r   r   r0     s   
zMyOptionParser.__init__messager   r   c                 C  s@   | j  d| }t| jdr| d| jj d}t|  | )z1Transform argparse error message into UsageError.z	: error: _config_source_hintz ())r   r   r   r   r   format_usage)r   r   r   r   r   r   error  s   zMyOptionParser.errorrD   Sequence[str] | NonerF   rG   rH   c           	      C  s   |  ||\}}|rG|D ]2}|r>|d dkr>dd|g}t| j D ]\}}|d| d|  q&| d| qt|t	
| |S )z(Allow splitting of positional arguments.r   r   zunrecognized arguments: {} z  r   
)rs   r   r   sortedr/   rl   r   r   rn   rb   extend)	r   rD   rF   parsedunrecognizedarglineskvr   r   r   rW     s   zMyOptionParser.parse_args)   	   
arg_string5tuple[argparse.Action | None, str, str | None] | Nonec           	      C  s  |sd S |d | j vrd S || jv r| j| }||d fS t|dkr$d S d|v r?|dd\}}|| jv r?| j| }|||fS | jsG|dsv| |}t|dkrktd}ddd |D }| 	|||d	  nt|dkrv|\}|S | j
|r| jsd S d
|v rd S d |d fS )Nr   r:   =r   z4ambiguous option: %(option)s could match %(matches)sr   c                 s  s    | ]\}}}|V  qd S r3   r   )rO   r   r1   r   r   r   r     s    z1MyOptionParser._parse_optional.<locals>.<genexpr>)r1   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_tuplesr   r^   option_tupler   r   r   _parse_optional  s>   







zMyOptionParser._parse_optionalr   )r'   r   r/   r   r   r   r   r%   )r   r   r   r   )rD   r   rF   rG   r   rH   )r   r   r   r   )
r   r   r   r0   r   rW   sysversion_infor   __classcell__r   r   r   r   rZ     s    

rZ   c                      s8   e Zd ZdZd fddZ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.
    rD   r   kwargsr   r%   c                   s,   d|vrt j |d< t j|i | d S )Nwidth)_pytest_ioget_terminal_widthr   r0   )r   rD   r   r   r   r   r0     s   z%DropShorterLongHelpFormatter.__init__r   argparse.Actionr   c                   sj  t  |}|r|d dkr|S t|dd }|r|S |d}t|dkr:t|d dks5t|d dkr:||_|S g }i }|D ];}t|dksN|d dkrOq@|ds]td	| d
||dd  }|dd}	|	|vswt||	 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   r:   r   r   z&long optional argument without "--": []r6   r   )r   _format_action_invocationrn   r   r   r   r   r   r   r   r   r   )r   r   orgstrresr^   return_list
short_longr1   xxoption	shortenedformatted_action_invocationr   r   r   r     sF   
,


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)rD   r   r   r   r   r%   )r   r   r   r   )r   r   r   r   r0   r   r   r   r   r   r   r   r     s
    &r   )rv   rw   r   r   )#
__future__r   argparser   rL   r   typingr   r   r   r   r   r	   r
   r   r   _pytest._ior   _pytest.config.exceptionsr   _pytest.deprecatedr   rb   r   r}   r   r~   	Exceptionr   r2   r(   ArgumentParserrZ   HelpFormatterr   r   r   r   r   <module>   s:    
=]5X