B
    `aP                 @   sV  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Zd dlZd dlmZ d dlmZ erd dlmZ 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)Optional)Sequence)Tuple)TYPE_CHECKING)Union)final)
UsageError)NoReturn)LiteralZfile_or_dirc               @   sn  e Zd ZU dZdZee ed< d%ee eedgdf  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ejjf  eej ejdddZddddZd(eeeejjf  ejeej ee dddZd)eeeejjf  eej ejdddZd*eeeejjf  eej eejee f ddd Zd+eeed! 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progArgument)usage
processoptreturnc             C   s6   t d| d| _g | _|| _|| _i | _g | _i | _d S )Nzcustom options)parser)OptionGroup
_anonymous_groups_processopt_usage_inidict	_ininames
extra_info)selfr   r    r"   a/home/kop/projects/devel/pgwui/test_venv/lib/python3.7/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#   processoption5   s    zParser.processoption r   )namedescriptionafterr   c             C   sj   x| j D ]}|j|kr|S qW t||| d}d}x"t| j D ]\}}|j|kr<P q<W | j |d | |S )a  Get (or create) a named option Group.

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

        The returned group object has an ``addoption`` method with the same
        signature as :py:func:`parser.addoption
        <_pytest.config.argparsing.Parser.addoption>` but will be shown in the
        respective group in the output of ``pytest. --help``.
        )r   r      )r   r)   r   	enumerateinsert)r!   r)   r*   r+   groupigrpr"   r"   r#   getgroup:   s    

zParser.getgroup)optsattrsr   c             O   s   | j j|| dS )a  Register a command line option.

        :opts: Option names, can be short or long options.
        :attrs: Same attributes which the ``add_argument()`` function of the
           `argparse library <https://docs.python.org/library/argparse.html>`_
           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!   r3   r4   r"   r"   r#   r5   S   s    zParser.addoption)args	namespacer   c             C   s>   ddl m} |  | _|| j dd |D }| jj||dS )Nr   )try_argcompletec             S   s&   g | ]}t |tjjrt|n|qS r"   )
isinstancepypathlocalstr).0xr"   r"   r#   
<listcomp>k   s    z Parser.parse.<locals>.<listcomp>)r7   )_pytest._argcompleter8   
_getparser	optparser
parse_args)r!   r6   r7   r8   strargsr"   r"   r#   parseb   s
    

zParser.parseMyOptionParser)r   c             C   s   ddl m} t| | j| jd}| j| jg }xT|D ]L}|jr2|jpF|j	}|
|}x*|jD ] }| }| }	|j||	 qZW q2W |jtdd}
||
_|S )Nr   )filescompleter)r   *)nargs)rA   rH   rG   r    r   r   r   optionsr*   r)   add_argument_groupnamesr4   add_argumentFILE_OR_DIRZ	completer)r!   rH   rC   groupsr/   descZarggroupr%   naZfile_or_dir_argr"   r"   r#   rB   n   s    

zParser._getparser)r6   r%   r7   r   c             C   sF   | j ||d}x"|j D ]\}}t||| qW ttt t|tS )N)r7   )	rF   __dict__itemssetattrr   r   r=   getattrrO   )r!   r6   r%   r7   Zparsedoptionr)   valuer"   r"   r#   parse_setoption   s    zParser.parse_setoptionc             C   s   | j ||dd S )zGParse and return a namespace object with known arguments at this point.)r7   r   )parse_known_and_unknown_args)r!   r6   r7   r"   r"   r#   parse_known_args   s    zParser.parse_known_argsc             C   s$   |   }dd |D }|j||dS )ztParse and return a namespace object with known arguments, and
        the remaining arguments unknown at this point.c             S   s&   g | ]}t |tjjrt|n|qS r"   )r9   r:   r;   r<   r=   )r>   r?   r"   r"   r#   r@      s    z7Parser.parse_known_and_unknown_args.<locals>.<listcomp>)r7   )rB   r[   )r!   r6   r7   rC   rE   r"   r"   r#   rZ      s    z#Parser.parse_known_and_unknown_argsz9Literal['string', 'pathlist', 'args', 'linelist', 'bool'])r)   helptyper   c             C   s,   |dkst |||f| j|< | j| dS )a  Register an ini-file option.

        :name: Name of the ini-variable.
        :type: Type of the variable, can be ``string``, ``pathlist``, ``args``,
               ``linelist`` or ``bool``.  Defaults to ``string`` if ``None`` or
               not passed.
        :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.Config.getini>`.
        )Nstringpathlistr6   ZlinelistboolN)AssertionErrorr   r   append)r!   r)   r\   r]   defaultr"   r"   r#   addini   s    zParser.addini)NN)r(   N)N)N)N)N)NN)__name__
__module____qualname____doc__r   r	   r=   __annotations__r   r$   r'   r2   r   r5   r
   r   r:   r;   r<   argparse	NamespacerF   rB   r   rY   r[   r   rZ   rd   r"   r"   r"   r#   r      s*   
 	 	 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 )N)rm   r=   	option_id)r!   rm   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 z: )rn   rm   )r!   r"   r"   r#   __str__   s    zArgumentError.__str__)re   rf   rg   rh   r=   r   r$   ro   r"   r"   r"   r#   rl      s   rl   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
    )intr^   floatcomplexN)rM   r4   r   c          
   O   s  || _ g | _g | _d|dp dkr4tjdtdd y|d }W n tk
rT   Y nzX t|t	r|dkrtjd	||f td
d t
|d d |d< n&tjd||f td
d tj| |d< |d | _
n|| _
y|d | _W n tk
r   Y nX | | |d}|r|| _nv| jr:| jd dd dd| _nPy| jd dd | _W n6 tk
r } zd| _td| |W dd}~X Y nX dS )z4Store parms in private vars for use in add_argument.z%defaultr\   r(   zHpytest now uses argparse. "%default" should be changed to "%(default)s"    )
stacklevelr]   choicez`type` argument to addoption() is the string %r. For choices this is optional and can be omitted,  but when supplied should be a type (for example `str` or `int`). (options: %s)   choicesr   z`type` argument to addoption() is the string %r,  but when supplied should be a type (for example `str` or `int`). (options: %s)rc   r&      N-_r,   z???zneed a long or short option)_attrs_short_opts
_long_optsgetwarningswarnDeprecationWarningKeyErrorr9   r=   r]   r   _typ_maprc   _set_opt_stringsr&   replace
IndexErrorrl   )r!   rM   r4   typr&   er"   r"   r#   r$      sV    


zArgument.__init__)r   c             C   s   | j | j S )N)r|   r}   )r!   r"   r"   r#   rM     s    zArgument.namesc          	   C   s~   d  }|| j x6|D ].}yt| || j|< W q tk
rF   Y qX qW | jdrx| jd }|dd}|| jd< | jS )Nzdefault dest helpr\   z%defaultz%(default)s)splitrb   r&   rW   r{   AttributeErrorr~   r   )r!   r4   attrrS   r"   r"   r#   r4     s    



zArgument.attrs)r3   r   c             C   s   x|D ]}t |dk r&td| | qt |dkrf|d dkrJ|d dksXtd| | | j| q|dd dkr|d dkstd| | | j| qW d	S )
zhDirectly from optparse.

        Might not be necessary as this is passed to argparse later on.
        rx   z>invalid option string %r: must be at least two characters longr   ry   r,   zMinvalid short option string %r: must be of the form -x, (x any non-dash char)z--zGinvalid long option string %r: must start with --, followed by non-dashN)lenrl   r|   rb   r}   )r!   r3   optr"   r"   r#   r   #  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: r]   ztype: rc   z	default: zArgument({})z, )	r|   reprr}   r&   hasattrr]   rc   formatjoin)r!   r6   r"   r"   r#   __repr__@  s    

zArgument.__repr__)re   rf   rg   rh   rp   r=   rq   rr   r   r   r$   r   rM   r   r4   r
   r   r   r"   r"   r"   r#   r      s   >r   c               @   s^   e Zd Zd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   r(   N)r)   r*   r   r   c             C   s   || _ || _g | _|| _d S )N)r)   r*   rK   r   )r!   r)   r*   r   r"   r"   r#   r$   O  s    zOptionGroup.__init__)optnamesr4   r   c             O   sF   t |dd | jD }|r*td| t||}| j|dd dS )aF  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.
        c             s   s    | ]}|  D ]
}|V  qqd S )N)rM   )r>   r   r)   r"   r"   r#   	<genexpr>`  s    z(OptionGroup.addoption.<locals>.<genexpr>zoption names %s already addedF)
shortupperN)setintersectionrK   
ValueErrorr   _addoption_instance)r!   r   r4   conflictr%   r"   r"   r#   r5   W  s    
zOptionGroup.addoptionc             O   s   t ||}| j|dd d S )NT)r   )r   r   )r!   r   r4   r%   r"   r"   r#   
_addoptiong  s    
zOptionGroup._addoptionFr   )r%   r   r   c             C   sV   |s4x.|j D ]$}|d dkr|d  rtdqW | jrF| j| | j| d S )Nr   ry   r,   zlowercase shortoptions reserved)r|   islowerr   r   r'   rK   rb   )r!   r%   r   r   r"   r"   r#   r   k  s    zOptionGroup._addoption_instance)r(   N)F)re   rf   rg   r=   r	   r   r$   r   r5   r   r`   r   r"   r"   r"   r#   r   N  s
   r   c               @   s   e Zd Zdeeeeef  ee ddddZed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dS )rG   N)r   r    r   r   c             C   s2   || _ tjj| ||jdtdd |r(|ni | _d S )NF)r   r   add_helpformatter_classallow_abbrev)_parserrj   ArgumentParserr$   r   DropShorterLongHelpFormatterr    )r!   r   r    r   r"   r"   r#   r$   v  s    zMyOptionParser.__init__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   rm   r"   r"   r#   error  s    zMyOptionParser.error)r6   r7   r   c       	      C   s   |  ||\}}|rxn|D ]f}|r|d dkrdd| g}x0t| j D ]\}}|d| d|  qNW | d| qW t|t	| |S )z(Allow splitting of positional arguments.r   ry   zunrecognized arguments: %s z  z: 
)
r[   r   sortedr    rU   rb   r   rW   rO   extend)	r!   r6   r7   parsedZunrecognizedarglineskvr"   r"   r#   rD     s    
zMyOptionParser.parse_argsrx   )rs   	   )
arg_stringr   c       	      C   s  |sd S |d | j krd S || jkr8| j| }||d fS t|dkrHd S d|kr~|dd\}}|| jk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
|krd S d |d fS )Nr   r,   =z--z4ambiguous option: %(option)s could match %(matches)sz, c             s   s   | ]\}}}|V  qd S )Nr"   )r>   rz   r%   r"   r"   r#   r     s    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_tuplesrm   rK   option_tupler"   r"   r#   _parse_optional  s<    







zMyOptionParser._parse_optional)NN)NN)re   rf   rg   r   r	   r   r=   r   r$   r   r
   rj   rk   rD   sysversion_infor   Actionr   r"   r"   r"   r#   rG   u  s     rG   c                   sD   e Zd ZdZeedd fddZejed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)r6   kwargsr   c                s(   d|krt j |d< t j|| d S )Nwidth)_pytest_ioZget_terminal_widthsuperr$   )r!   r6   r   )	__class__r"   r#   r$     s    z%DropShorterLongHelpFormatter.__init__)r   r   c             C   sx  t j| |}|r"|d dkr"|S t|dd }|r6|S |d}t|dkrvt|d dkslt|d dkrv||_|S g }i }xz|D ]r}t|dks|d dkrq|dstd	| ||dd  }|	dd
}	|	|kst||	 t|k r|||	< qW xh|D ]`}t|dks |d dkr*|
| |dd  ||	dd
kr |
|	ddd q W d|}
|
|_|
S )Nr   ry   _formatted_action_invocationz, rx   r,   r   z--z)long optional argument without "--": [%s]r(   r   )rj   HelpFormatter_format_action_invocationrW   r   r   r   r   rl   r   rb   r~   r   )r!   r   ZorgstrresrK   Zreturn_listZ
short_longr%   ZxxoptionZ	shortenedZformatted_action_invocationr"   r"   r#   r     s>    
,



 
z6DropShorterLongHelpFormatter._format_action_invocationc             C   s8   ddl }g }x&| D ]}||| | qW |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)re   rf   rg   rh   r   r$   rj   r   r=   r   r   __classcell__r"   r"   )r   r#   r     s   &r   )$rj   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   r   Ztyping_extensionsr   rO   r   	Exceptionrl   r   r   r   rG   r   r   r"   r"   r"   r#   <module>   s<     'W