U
    uel                     @   s   d dl Z d dlZd dlZd dlmZ d dlmZmZmZm	Z	m
Z
mZ ddlmZ ddlmZmZmZ ddlmZmZ ddlmZ dd	lmZmZmZmZ e je je je je j e j!e j"hZ#d
d Z$G dd de%Z&G dd de&Z'dS )    N)Mapping)CallableDictListOptionalSequenceUnion   )io)ChoicesCompleterFilesCompleterSuppressCompleter)debugmute_stderr)
split_line)IntrospectiveArgumentParseraction_is_greedyaction_is_openaction_is_satisfiedc                 C   s
   |  |S N
startswith)
completionprefix r   2lib/python3.8/site-packages/argcomplete/finders.pydefault_validator   s    r   c                	   @   s   e Zd ZdZddddde dfddZdejddddde fej	e
eef eeee  ee eee ddd	Zd
d Zdd Zdd Zdd Zdd Zedd Zdd Zeej	 ejeee dddZdd Zee ee dddZee eee ee d d!d"Zd#d$ Z d%d& Z!dS )'CompletionFindera   
    Inherit from this class if you wish to override any of the stages below. Otherwise, use
    ``argcomplete.autocomplete()`` directly (it's a convenience instance of this class). It has the same signature as
    :meth:`CompletionFinder.__call__()`.
    NTFc                 C   s^   || _ || _|| _|d krt}|| _|| _d| _i | _|| _|d krTt	j
ddk}|| _d S )NFZ_ARGCOMPLETE_SUPPRESS_SPACE1)_parseralways_complete_optionsexcluder   	validatorprint_suppressed
completing_display_completionsdefault_completerosenvirongetappend_space)selfargument_parserr    r!   r"   r#   r&   r*   r   r   r   __init__(   s    
zCompletionFinder.__init__)r,   r    exit_methodr!   r"   r#   r*   c
              	      s  j |||||||	d dtjkr&dS ztddt_W n tk
rT   tjt_Y nX t	  |dkrtj
d}
|
dk	rt	d|
 t|
d}|dkrztdd}W n$ tk
r   t	d	 |d
 Y nX tj
ddtd
krt	djd |d
 tj
d  r:t d
kr:t	dj d |d
 tjd }ttjd }t||\}}}}}ttjd d
 }||d }|r|d jjkrd|kr||dd
d  t	d|d|d|d|d|d| ||||} r0fddj D  fdd|D }tj
dd krTfd!d|D }t	d"| || |  tj  |d dS )#a  
        :param argument_parser: The argument parser to autocomplete on
        :param always_complete_options:
            Controls the autocompletion of option strings if an option string opening character (normally ``-``) has not
            been entered. If ``True`` (default), both short (``-x``) and long (``--x``) option strings will be
            suggested. If ``False``, no option strings will be suggested. If ``long``, long options and short options
            with no long variant will be suggested. If ``short``, short options and long options with no short variant
            will be suggested.
        :param exit_method:
            Method used to stop the program after printing completions. Defaults to :meth:`os._exit`. If you want to
            perform a normal exit that calls exit handlers, use :meth:`sys.exit`.
        :param exclude: List of strings representing options to be omitted from autocompletion
        :param validator:
            Function to filter all completions through before returning (called with two string arguments, completion
            and prefix; return value is evaluated as a boolean)
        :param print_suppressed:
            Whether or not to autocomplete options that have the ``help=argparse.SUPPRESS`` keyword argument set.
        :param append_space:
            Whether to append a space to unique matches. The default is ``True``.

        .. note::
            If you are not subclassing CompletionFinder to override its behaviors,
            use :meth:`argcomplete.autocomplete()` directly. It has the same signature as this method.

        Produces tab completions for ``argument_parser``. See module docs for more info.

        Argcomplete only executes actions if their class is known not to have side effects. Custom action classes can be
        added to argcomplete.safe_actions, if their values are wanted in the ``parsed_args`` completer argument, or
        their execution is otherwise desirable.
        )r    r!   r"   r#   r*   r&   Z_ARGCOMPLETEN	   wZ_ARGCOMPLETE_STDOUT_FILENAMEzUsing output file {}   z)Unable to open fd 8 for writing, quittingr	   Z_ARGCOMPLETE_IFSz%Invalid value for IFS, quitting [{v}])vZ_ARGCOMPLETE_DFSz%Invalid value for DFS, quitting [{v}]Z	COMP_LINEZ
COMP_POINTr   =z
LINE: {!r}z
POINT: {!r}z
PREQUOTE: {!r}z
PREFIX: {!r}z
SUFFIX: {!r}z
WORDS:c                    s&   i | ]\}}||r|  d ndqS )  replace).0keyvalue)ifsr   r   
<dictcomp>   s     z-CompletionFinder.__call__.<locals>.<dictcomp>c                    s$   g | ]}  ||pd fqS )r6   )joinr)   )r9   r:   )dfsdisplay_completionsr   r   
<listcomp>   s     z-CompletionFinder.__call__.<locals>.<listcomp>_ARGCOMPLETE_SHELLzshc                    s"   g | ]}| d  j | qS ):)r%   r)   r9   cr+   r   r   rA      s     z
Returning completions:)r-   r'   r(   fdopen_ioZdebug_stream	Exceptionsysstderrr   r)   formatopenlenintr   r   prefix_charsappendsplit_get_completionsr%   itemswriter>   flush)r+   r,   r    r.   Zoutput_streamr!   r"   r#   r*   r&   filenameZ	comp_lineZ
comp_pointcword_prequotecword_prefixcword_suffix
comp_wordslast_wordbreak_posstartcompletionsr   )r?   r@   r<   r+   r   __call__@   s    *



"



zCompletionFinder.__call__c           
   
   C   s   |   }t }d| _zJtd|dd   t  | jj|dd  |d}W 5 Q R X td| W n8 tk
r } ztdt	|t
|d W 5 d }~X Y nX d| _d	|krd| _| |||}	| |	}	| |	||}	|	S )
NTzinvoking parser withr	   )	namespacezparsed args:z

exceptionzwhile parsing argsFz--)_patch_argument_parserargparse	Namespacer$   r   r   r   parse_known_argsBaseExceptiontypestrr    collect_completionsfilter_completionsquote_completions)
r+   r\   rZ   rY   r]   active_parsersparsed_argsaer_   r   r   r   rT      s"    "(
z!CompletionFinder._get_completionsc                    sF   g | _ g | _|   fdd| j td| j  td| j | j S )a  
        Since argparse doesn't support much introspection, we monkey-patch it to replace the parse_known_args method and
        all actions with hooks that tell us which action was last taken or about to be taken, and let us have the parser
        figure out which subparsers need to be activated (then recursively monkey-patch those).
        We save all active ArgumentParsers to extract all their possible option names later.
        c                    s    j |   j|  t| tr&d S d}t|t| jfi | _| jD ]>}t|drTqDG  fddd|j}|j|_	|j
|_||_qDd S )NZ(MonkeyPatchedIntrospectiveArgumentParser_orig_classc                       s   e Zd Zd fdd	ZdS )zPCompletionFinder._patch_argument_parser.<locals>.patch.<locals>.IntrospectActionNc                    s   t d|  t d|||| t d| j t d| j  jsL| j||||d njt| jtjrt d | j|d   | j||||d n.| jtkr| j	s j
|  | j||||d d S )NzAction stub called onz	args:z	orig class:z	orig callable:)option_stringz:orig class is a subparsers action: patching and running itr   )r   rp   _orig_callabler$   
issubclassrc   _SubParsersAction_name_parser_mapsafe_actionsoption_stringsvisited_positionalsrR   )r+   parserra   valuesrq   	completerpatchr   r   r`      s    

zYCompletionFinder._patch_argument_parser.<locals>.patch.<locals>.IntrospectAction.__call__)N)__name__
__module____qualname__r`   r   r{   r   r   IntrospectAction   s   r   )rx   rR   rl   
isinstancer   rg   	__class___actionshasattrrp   r`   rr   )ry   Z	classnameactionr   r{   r   r   r}      s    


z6CompletionFinder._patch_argument_parser.<locals>.patchzActive parsers:zVisited positionals:)rl   rx   r   r   rG   r   r{   r   rb      s    '
z'CompletionFinder._patch_argument_parserc           	         s   i }|j  D ] }|j | }||g | q| D ]4}||j |j  D ]}| rL|jpbd| j|< qLq8 fdd|j  D }|S )Nr6   c                    s   g | ]}|  r|qS r   r   )r9   ZsubcmdrZ   r   r   rA     s     
 z?CompletionFinder._get_subparser_completions.<locals>.<listcomp>)	choiceskeys
setdefaultrR   _get_subactionsdestr   helpr%   )	r+   ry   rZ   Zaliases_by_parserr:   pr   aliasr_   r   r   r   _get_subparser_completions  s    

z+CompletionFinder._get_subparser_completionsc                    sz   t  dks| jdkr* fdd|jD S dd |jD }dd |jD }| jdkr`|r\|S |S | jdkrv|rr|S |S g S )	Nr   Tc                    s   g | ]}|  r|qS r   r   r9   optr   r   r   rA   #  s     
 z5CompletionFinder._include_options.<locals>.<listcomp>c                 S   s   g | ]}t |d kr|qS    rO   r   r   r   r   rA   $  s      c                 S   s   g | ]}t |d kr|qS r   r   r   r   r   r   rA   %  s      ZlongZshort)rO   r    rw   )r+   r   rZ   Z	long_optsZ
short_optsr   r   r   _include_options!  s    

z!CompletionFinder._include_optionsc                 C   s   |j D ]0}|jr|jD ]}||r|jp,d| j|< qqg }|j D ]b}| jszt|dd }t|trl|	 rlqB|jt
jkrzqB| ||sqBt|t
jsB|| ||7 }qB|S )Nr6   r|   )r   rw   r   r   r%   r#   getattrr   r   suppressrc   SUPPRESS_action_allowedrt   r   )r+   ry   rZ   r   rq   Zoption_completionsr|   r   r   r   _get_option_completions,  s$    



z(CompletionFinder._get_option_completionsc                 C   s(   |j | g D ]}||jkr dS qdS NFT)Z_action_conflictsr)   _seen_non_default_actions)r   ry   conflict_actionr   r   r   r   A  s    
z CompletionFinder._action_allowedc                    s  t djt|j|jd |o*|d |jk d fdd|jD }|rxt|dks\tdt d	|d d
 i | _g }n" rd|kr|d\}}n|S d}|p|jD ]}	|	jst	|	rd}|st
|	rt	|	st d|	 qt d|	|	j t|	dd }
|
d kr>|	jd k	r*t|	tjs*t|	j}
nt|	tjs>| j}
|
rt|
trZ|
 rZqt|
r|
||	||d}t|tr| D ],\}}| ||r|| || j|< qnN|D ]H}| ||r|| t|
tr|	jpd| j|< n
d| j|< qnVt d tdD ]D}|
||}|d kr8 q^| ||rd| j|< || qrvfdd|D }t d| q|S )NzActive actions (L={l}): {a})lrn   r   r6   c                    s   g | ]}t | r|qS r   )r   )r9   x)
isoptionalr   r   rA   O  s     
 z<CompletionFinder._complete_active_option.<locals>.<listcomp>r	   zexpect at most 1 greedy actionzResetting completions becausezmust consume the next argumentr4   FTZSkippingzActivating completion forr|   )r   r   ry   rm   zICompleter is not callable, trying the readline completer protocol insteadi'  c                    s   g | ]} d  | qS )r4   r   )r9   r   )optional_prefixr   r   rA     s     zCompletions:)r   rM   rO   Zactive_actionsrQ   AssertionErrorr%   	partitionrw   r   r   rp   r   r   r   rc   rt   r   r&   r   r   callabler   rU   r"   rR   r   rangeZcomplete)r+   ry   next_positionalrZ   rm   r_   Zgreedy_actions_Zcomplete_remaining_positionalsZactive_actionr|   Zcompleter_outputr   descriptioniZnext_completionr   )r   r   r   _complete_active_optionJ  s|    


   



z(CompletionFinder._complete_active_option)rl   rm   rZ   returnc                 C   s   g }t d| |d }t d| | js@t|dkrP|d |jkrP|| ||7 }t d| |  }t d| t|tjr|| 	||7 }| 
|||||}t d| t d| j |S )	a0  
        Visits the active parsers and their actions, executes their completers or introspects them to collect their
        option strings. Returns the resulting completions as a list of strings.

        This method is exposed for overriding in subclasses; there is no need to use it directly.
        zall active parsers:zactive_parser:r   zoptional options:znext_positional:zactive options:zdisplay completions:)r   r    rO   rQ   r   _get_next_positionalr   rc   rt   r   r   r%   )r+   rl   rm   rZ   r_   active_parserr   r   r   r   ri     s*    	

 

    
z$CompletionFinder.collect_completionsc                 C   sz   | j d }| jd }| }|s$dS ||kr4|d S d}tt|D ]}|| |krD qZqD|d t|k rv||d  S dS )z>
        Get the next positional action if it exists.
        r   Nr   r	   )rl   rx   _get_positional_actionsr   rO   )r+   r   Zlast_positionalZall_positionalsr   r   r   r   r     s    

z%CompletionFinder._get_next_positional)r_   r   c                 C   s:   g }|D ],}| j dk	r"|| j kr"q||kr|| q|S )z
        De-duplicates completions and excludes those specified by ``exclude``.
        Returns the filtered completions as a list.

        This method is exposed for overriding in subclasses; there is no need to use it directly.
        N)r!   rR   )r+   r_   Zfiltered_completionsr   r   r   r   rj     s    

z#CompletionFinder.filter_completions)r_   rY   r]   r   c                    sD  d}|dkr, r" fdd|D }|d7 }n|dkr<|d7 }t jdd	krRd}n|d
krld}dd |D }t jddkrd}|dd}nd}t jddkr|d7 }g }|D ]F}|}|D ]}	||	||	 }q|| || jkr| j| | j|< q| jr@d}
t|dkr@|d d |
kr@|dkr@|d  d7  < |S )a  
        If the word under the cursor started with a quote (as indicated by a nonempty ``cword_prequote``), escapes
        occurrences of that quote character in the completions, and adds the quote to the beginning of each completion.
        Otherwise, escapes all characters that bash splits words on (``COMP_WORDBREAKS``), and removes portions of
        completions before the first colon if (``COMP_WORDBREAKS``) contains a colon.

        If there is only one completion, and it doesn't end with a **continuation character** (``/``, ``:``, or ``=``),
        adds a space after the completion.

        This method is exposed for overriding in subclasses; there is no need to use it directly.
        \r6   c                    s   g | ]}| d  d qS )r	   Nr   rE   r]   r   r   rA     s     z6CompletionFinder.quote_completions.<locals>.<listcomp>z();<>|&!`$* 	
"'"z"`$!rB   )ZtcshZfish'c                 S   s   g | ]}| d dqS )r   z'\''r7   rE   r   r   r   rA     s     Z
powershell`rC   rD   z=/:r	   r   r   r5   )r'   r(   r)   r8   rR   r%   r*   rO   )r+   r_   rY   r]   Zspecial_charsZescape_charZescaped_completionsr   Zescaped_completioncharZcontinuation_charsr   r   r   rk     s@    


 
z"CompletionFinder.quote_completionsc                    sr   |dkrRt \} }}}|dtjd  | | ||} fdd|D | _|t| jk rj| j| S dS dS )ap  
        Alternate entry point for using the argcomplete completer in a readline-based REPL. See also
        `rlcompleter <https://docs.python.org/3/library/rlcompleter.html#completer-objects>`_.
        Usage:

        .. code-block:: python

            import argcomplete, argparse, readline
            parser = argparse.ArgumentParser()
            ...
            completer = argcomplete.CompletionFinder(parser)
            readline.set_completer_delims("")
            readline.set_completer(completer.rl_complete)
            readline.parse_and_bind("tab: complete")
            result = input("prompt> ")
        r   c                    s    g | ]}|t  d   qS r   r   )r9   matchrZ   textr   r   rA   =  s     z0CompletionFinder.rl_complete.<locals>.<listcomp>N)r   insertrK   argvrT   Z_rl_matchesrO   )r+   r   staterY   r[   r\   Zfirst_colon_posmatchesr   r   r   rl_complete(  s    
zCompletionFinder.rl_completec                 C   s   | j S )zr
        This function returns a mapping of completions to their help strings for displaying to the user.
        )r%   rG   r   r   r   get_display_completionsD  s    z(CompletionFinder.get_display_completions)"r~   r   r   __doc__r   r-   r'   _exitrc   ArgumentParserr   boolrh   r   r   r   r`   rT   rb   r   r   r   staticmethodr   r   r   rd   ri   r   rj   rP   rk   r   r   r   r   r   r   r   !   sb   


 :
U     Br   c                   @   s   e Zd Zedd ZdS )ExclusiveCompletionFinderc                 C   s<   t | |sdS tjtjf}| j|kr*dS | |jkr8dS dS r   )r   r   rc   _AppendAction_AppendConstActionrp   r   )r   ry   Zappend_classesr   r   r   r   L  s    

z)ExclusiveCompletionFinder._action_allowedN)r~   r   r   r   r   r   r   r   r   r   K  s   r   )(rc   r'   rK   Zcollections.abcr   typingr   r   r   r   r   r   r6   r
   rI   Z
completersr   r   r   r   r   Zlexersr   Zpackages._argparser   r   r   r   _StoreAction_StoreConstAction_StoreTrueAction_StoreFalseActionr   r   _CountActionrv   r   objectr   r   r   r   r   r   <module>   s0        .