
    B/gin                     t   S SK r S SKrS SKrS SKJr  S SKJrJrJrJ	r	J
r
JrJr  SSKJr  SSKJrJrJrJr  SSKJrJr  SSKJr  SS	KJrJrJrJr  \ R<                  \ R>                  \ R@                  \ RB                  \ RD                  \ RF                  \ RH                  1r%S
 r& " S S\'5      r( " S S\(5      r)g)    N)Mapping)CallableDictListOptionalSequenceTextIOUnion   )io)BaseCompleterChoicesCompleterFilesCompleterSuppressCompleter)debugmute_stderr)
split_line)IntrospectiveArgumentParseraction_is_greedyaction_is_openaction_is_satisfiedc                 $    U R                  U5      $ N)
startswith)
completionprefixs     5/usr/lib/python3/dist-packages/argcomplete/finders.pydefault_validatorr      s      ((    c                      \ rS rSrSrSSSSS\" 5       S4S jrS\R                  SSSSS\" 5       4S\	R                  S\\\4   S	\S
\\   S\\\      S\\   S\S\\   S\SS4S jjrS rS rS rS rS rS r\S 5       rS rS\\	R                     S\	R<                  S\S\\   4S jrS r S\\   S\\   4S  jr!S\\   S!\S"\\"   S\\   4S# jr#S$ r$S% r%S&r&g)'CompletionFinder!   z
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                     Xl         X l        X0l        Uc  [        nX@l        XPl        SU l        0 U l        X`l        Uc"  [        R                  R                  S5      S:g  nXpl        g )NF_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-   r1   s           r   __init__CompletionFinder.__init__(   sc     ''>$)I" 046!!2::>>*GHCOL(r   r3   r'   exit_methodoutput_streamr(   r)   r*   r1   r-   returnc
           
         U R                  UUUUUUU	S9  S[        R                  ;  a  gU R                  5         UcH  [        R                  R	                  S5      n
U
b&  [        SR                  U
5      5        [        U
S5      nUc   [        R                  " SS5      nUc   e[        R                  R	                  S
S5      n[        U5      S	:w  a   [        SR                  US95        U" S	5        [        R                  R	                  S5      nU(       a/  [        U5      S	:w  a   [        SR                  US95        U" S	5        [        R                  S   n[        [        R                  S   5      n[        X5      u  nnnnn[        [        R                  S   5      S	-
  nUUS nU(       aG  US   U R                  R                  ;   a*  SU;   a$  UR                  UR!                  SS	5      S   5        [        SR                  U5      SR                  U5      SR                  U5      SR                  U5      SR                  U5      SU5        U R#                  UUUU5      nU(       a  U R$                  R'                  5        VVs0 s H"  u  nnUU(       a  UR)                  US5      OS_M$     nnnU Vs/ s H.  nUR+                  UUR	                  U5      =(       d    S45      PM0     nn[        R                  R	                  S5      S:X  a0  U Vs/ s H#  nU SU R$                  R	                  U5       3PM%     nn[        SU5        UR-                  UR+                  U5      5        UR/                  5         [0        R2                  R/                  5         U" S5        g! [         a    [        S5        U" S	5         GN,f = fs  snnf s  snf s  snf ) 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*   r1   r-   _ARGCOMPLETEN_ARGCOMPLETE_STDOUT_FILENAMEzUsing output file {}w   z)Unable to open fd 8 for writing, quittingr   _ARGCOMPLETE_IFSz%Invalid value for IFS, quitting [{v}])v_ARGCOMPLETE_DFSz%Invalid value for DFS, quitting [{v}]	COMP_LINE
COMP_POINTr   =z
LINE: {!r}z
POINT: {!r}z
PREQUOTE: {!r}z
PREFIX: {!r}z
SUFFIX: {!r}z
WORDS:  _ARGCOMPLETE_SHELLzsh:z
Returning completions:)r4   r.   r/   _init_debug_streamr0   r   formatopenfdopen	Exceptionlenintr   r&   prefix_charsappendsplit_get_completionsr,   itemsreplacejoinwriteflush_iodebug_stream)r2   r3   r'   r6   r7   r(   r)   r*   r1   r-   filenameifsdfs	comp_line
comp_pointcword_prequotecword_prefixcword_suffix
comp_wordslast_wordbreak_posstartcompletionskeyvaluedisplay_completionscs                             r   __call__CompletionFinder.__call__@   sy   T 	$;-%/ 	 	
 +! zz~~&DEH#,33H=> $Xs 3  "		!S 1
 (((jjnn/8s8q=9@@3@GHNjjnn/03s8q=9@@3@GHNJJ{+	L12
U_`iUvRlJ@R BJJ~./!3'
LOt||/H/HHST`M`l00a8;<!!),"":.%%n5##L1##L1	
 ++JnVhiPTPiPiPoPoPq#Pq*#uU]]3,2=Pq   # [ffZeSV388S*=*A*A#*F*L"$MNZeKf::>>./58NYZkaS$";";"?"?"B!CDkKZ(+6CHH[12 Au  ABA\# g [s$   
N- )O 5O*O-OOc                      [         R                  " SS5      [        l        [        5         g! [         a    [
        R                  [        l         N0f = f)zInitialize debug output stream

By default, writes to file descriptor 9, or stderr if that fails.
This can be overridden by derived classes, for example to avoid
clashes with file descriptors being used elsewhere (such as in pytest).
	   r<   N)r.   rM   rZ   r[   rN   sysstderrr   r2   s    r   rJ   #CompletionFinder._init_debug_stream   s>    	*!yyC0C 	  	*"zzC	*s    - #AAc                    U R                  5       n[        R                  " 5       nSU l         [	        SUSS  5        [        5          U R                  R                  USS  US9nS S S 5        [	        SW5        SU l        S	U;   a  SU l        U R                  XVU5      n	U R                  U	5      n	U R                  XU5      n	U	$ ! , (       d  f       Ne= f! [         a*  n[	        S[        U5      [        U5      S5         S nANS nAff = f)
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)
r2   rd   rb   ra   re   active_parsersparsed_argsaerg   s
             r   rT   !CompletionFinder._get_completions   s    446((*	H(*QR.9LL11*QR.K1X .!$  :+0D(..~LY--k:,,[J\]   	H-a#a&2FGG	Hs/   C C%C 
CC 
D	 DD	c                    ^^ / U l         / U l        U mUU4S jmT" U R                  5        [        SU R                   5        [        SU R                  5        U R                   $ )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                   > TR                   R                  U 5        TR                  R                  U 5        [        U [        5      (       a  g Sn[        U[        U R                  40 5      U l        U R                   HW  n[        US5      (       a  M   " UU4S jSUR                  5      nUR                  Ul	        UR                  Ul        X2l        MY     g )N(MonkeyPatchedIntrospectiveArgumentParser_orig_classc                   (   > \ rS rSrSU U4S jjrSrg)PCompletionFinder._patch_argument_parser.<locals>.patch.<locals>.IntrospectAction   Nc                 0  > [        SU 5        [        SXX45        [        SU R                  5        [        SU R                  5        TR                  (       d  U R                  XX4S9  g [	        U R                  [
        R                  5      (       a4  [        S5        T" U R                  US      5        U R                  XX4S9  g U R                  [        ;   a=  U R                  (       d  TR                  R                  U 5        U R                  XX4S9  g g )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   r   _orig_callabler+   
issubclassrw   _SubParsersAction_name_parser_mapsafe_actionsoption_stringsvisited_positionalsrR   )r2   parserru   valuesr   	completerpatchs        r   rl   YCompletionFinder._patch_argument_parser.<locals>.patch.<locals>.IntrospectAction.__call__  s    5t<iFRot/?/?@0$2E2EF(33 //6/g'(8(8(:T:TUU!"^_!$"7"7q	"BC //6/g!--=#'#6#6 ) = = D DT J //6/g	 >r    r   )__name__
__module____qualname____firstlineno__rl   __static_attributes__)r   r   s   r   IntrospectActionr      s    h hr   r   )r   rR   r   
isinstancer   r{   	__class___actionshasattrr   rl   r   )r   	classnameactionr   r   r   s       r   r   6CompletionFinder._patch_argument_parser.<locals>.patch   s    ))008$$++F3&"=>>BI#I0KVM]M]/^`bcF //6=11h hv'7'7 h& &,%5%5"(.%#3 5 *r   zActive parsers:zVisited positionals:)r   r   r&   r   )r2   r   r   s    @@r   rv   'CompletionFinder._patch_argument_parser   s]     >@:< 	%	4N 	dll!4!45$d&>&>?"""r   c                    0 nUR                   R                  5        H3  nUR                   U   nUR                  U/ 5      R                  U5        M5     UR	                  5        H_  nX1R                   UR
                         H=  nUR                  U5      (       d  M  UR                  =(       d    SU R                  U'   M?     Ma     UR                   R                  5        Vs/ s H  oR                  U5      (       d  M  UPM     n	nU	$ s  snf )NrF   )	choiceskeys
setdefaultrR   _get_subactionsdestr   helpr,   )
r2   r   rb   aliases_by_parserrh   pr   aliassubcmdrg   s
             r   _get_subparser_completions+CompletionFinder._get_subparser_completions  s    FH>>&&(Cs#A((B/66s; ) ,,.F*>>&+++FG##L117={{7HbD--e4 H /
 -3NN,?,?,Ae,A&EVEVWcEdv,Ae fs   #D
 D
c                    [        U5      S:  d  U R                  SL a4  UR                   Vs/ s H  o3R                  U5      (       d  M  UPM     sn$ UR                   Vs/ s H  n[        U5      S:  d  M  UPM     nnUR                   Vs/ s H  n[        U5      S::  d  M  UPM     nnU R                  S:X  a  U(       a  U$ U$ U R                  S:X  a  U(       a  U$ U$ / $ s  snf s  snf s  snf )Nr   T   longshort)rO   r'   r   r   )r2   r   rb   opt	long_opts
short_optss         r   _include_options!CompletionFinder._include_options,  s    |q D$@$@D$H#)#8#8Y#8CNN<<XC#8YY$*$9$9J$9SSX\S$9	J%+%:%:L%:cc#h!mc%:
L''61 )99z9))W4!+:::	 ZJLs#   C%
C%!C*8C*C/%C/c                    UR                    Hc  nUR                  (       d  M  UR                   H=  nUR                  U5      (       d  M  UR                  =(       d    SU R                  U'   M?     Me     / nUR                    H  nU R
                  (       dY  [        USS 5      n[        U[        5      (       a  UR                  5       (       a  MM  UR                  [        R                  :X  a  Mm  U R                  X15      (       d  M  [        U[        R                  5      (       a  M  XPR                  X25      -  nM     U$ )NrF   r   )r   r   r   r   r,   r*   getattrr   r   suppressrw   SUPPRESS_action_allowedr   r   )r2   r   rb   r   r   option_completionsr   s          r   _get_option_completions(CompletionFinder._get_option_completions7  s    ooF$$$%+%:%:M$//==CI;;CTRT11-@ &; &  ooF((#FK>	i):;;	@R@R@T@T;;("3"33''77fh&@&@AA"&;&;F&QQ" & "!r   c                 l    UR                   R                  U / 5       H  nX!R                  ;   d  M    g   gNFT)_action_conflictsr0   _seen_non_default_actions)r   r   conflict_actions      r   r    CompletionFinder._action_allowedL  s6      &77;;FBGO"B"BB  H r   c                 v   [        SR                  [        UR                  5      UR                  S95        U=(       a    US   UR                  ;   nSnUR                   Vs/ s H  n[        X5      (       d  M  UPM     n	nU	(       a0  [        U	5      S:X  d   S5       e[        SU	S   S5        0 U l        / nO$U(       a  S	U;   a  UR                  S	5      u  pznOU$ S
nU	=(       d    UR                   GH  nUR                  (       dG  [        U5      (       a  SnU(       d.  [        U5      (       a  [        U5      (       d  [        SU5        M\  [        SXR                  5        [        USS 5      nUcm  UR                  b5  [        U[        R                   5      (       d  [#        UR                  5      nO+[        U[        R                   5      (       d  U R$                  nU(       d  M  [        U[&        5      (       a  UR)                  5       (       a  GM%  [+        U5      (       a  U" X<XS9n[        U[,        5      (       aS  UR/                  5        H>  u  nnU R1                  X5      (       d  M  UR3                  U5        UU R                  U'   M@     OU Ht  nU R1                  X5      (       d  M  UR3                  U5        [        U["        5      (       a$  UR4                  =(       d    SU R                  U'   Me  SU R                  U'   Mv     Om[        S5        [7        S5       HS  nUR9                  UU5      nUc    O=U R1                  UU5      (       d  M3  SU R                  U'   UR3                  U5        MU     U(       a  U Vs/ s H
  oS	-   U-   PM     nn[        SU5        GM     U$ s  snf s  snf )NzActive actions (L={l}): {a})lr   r   rF   r   zexpect at most 1 greedy actionzResetting completions becausezmust consume the next argumentrD   FTSkippingzActivating completion forr   )r   r   r   r   zICompleter is not callable, trying the readline completer protocol insteadi'  zCompletions:)r   rK   rO   active_actionsrQ   r   r,   	partitionr   r   r   r   r   r   r   rw   r   r   r-   r   r   callabler   rU   r)   rR   r   rangecomplete)r2   r   next_positionalrb   r   rg   
isoptionaloptional_prefixxgreedy_actions_complete_remaining_positionalsactive_actionr   completer_outputr   descriptioninext_completions                      r   _complete_active_option(CompletionFinder._complete_active_optionU  sK   +22S9N9N5OSYShSh2ij!Ll1o9L9L&L
%+%:%:^%:>Nq>]!%:^~&!+M-MM+ 1>!3DFfg(*D%Kl" 4@3I3I#3N0L #").&+Dv/D/DDM //!-00 6:25*=99.Q^B_B_j-8 -}>W>WX{DAI  ((4ZW_WqWq=r=r 01F1F GI#M83M3MNN $ 6 6Iyi):;;	@R@R@T@TI&&'0+&($ ""2G<<7G7M7M7O3J#~~jGG + 2 2: >HS 9 9* E 8P
 +;J#~~jGG + 2 2: >#-i9I#J#JLYL^L^LdbdD$=$=j$ILND$=$=j$I +; ef"4[*3*<*<\1*M*2!>>/<HHIKD55oF'..? ) #Xc"dXc*S#8:#EXcK"dnk2q Er [ _V #es   "N1:N1N6r   r   rb   c                    / n[        SU5        US   n[        SU5        U R                  (       d"  [        U5      S:  a&  US   UR                  ;   a  X@R	                  XS5      -  n[        SU5        U R                  5       n[        SU5        [        U[        R                  5      (       a  X@R                  Xc5      -  nU R                  XVX2U5      n[        SU5        [        SU R                  5        U$ )	a  
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   rw   r   r   r   r,   )r2   r   r   rb   rg   active_parserr   s          r   r}   $CompletionFinder.collect_completions  s     "$#^4&r*.''C,=,AlSToYfYsYsFs77TTK!;/335 /2ox'A'ABB::?YYK22L{
 	-$d&?&?@r   c                    U R                   S   nU R                  S   nUR                  5       nU(       d  gX:X  a  US   $ Sn[        [	        U5      5       H  nX4   U:X  d  M    O   US-   [	        U5      :  a  X4S-      $ g)z.
Get the next positional action if it exists.
r   Nr   r   )r   r   _get_positional_actionsr   rO   )r2   r   last_positionalall_positionalsr   s        r   r   %CompletionFinder._get_next_positional  s     ++B/2226'??A+"1%%s?+,A!_4 - q53''"q5))r   rg   c                     / nU H9  nU R                   b  X0R                   ;   a  M!  X2;  d  M(  UR                  U5        M;     U$ )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.
)r(   rR   )r2   rg   filtered_completionsr   s       r   r~   #CompletionFinder.filter_completions  sG      "%J||'-5$++J7 & $#r   ra   re   c                 4   SnUS:X  a   Ub  U Vs/ s H
  oUUS-   S PM     nnUS-  nOUS:X  a  US-  n[         R                  R                  S5      S	;   a  SnO)US
:X  a#  SnU Vs/ s H  oUR                  S
S5      PM     nn[         R                  R                  S5      S:X  a  SnUR                  SS5      nO*Sn[         R                  R                  S5      S:X  a  US-  n/ nU H`  nUn	U H  n
U	R                  XU
-   5      n	M     UR	                  U	5        XR
                  ;   d  MD  U R
                  U   U R
                  U	'   Mb     U R                  (       a0  Sn[        U5      S:X  a  US   S   U;  a  US:X  a  US==   S-  ss'   U$ s  snf s  snf )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.
\rF   Nr   z();<>|&!`$* 	
"'"z"`$!rG   )tcshfish'z'\''
powershell`rH   rI   z=/:r   r   rE   )r.   r/   r0   rV   rR   r,   r1   rO   )r2   rg   ra   re   special_charsrk   escape_charescaped_completionsr   escaped_completioncharcontinuation_charss               r   r   "CompletionFinder.quote_completions  s     R "-DOPKq!3a!7!9:KP22Ms"V#M::>>./3CCMs" M<GHKq99S'2KKH ::>>./<?K)11#r:MKzz~~23u<$ %J!+%%7%?%?TXFX%Y" &&&'9:666@D@Y@YZd@e))*<= &  "'&'1,1DQ1G1KSe1e!R''*c1*""U Q Is   F&Fc                 >   US:X  aj  [        U5      u  p4pVnUR                  S[        R                  S   5        U R	                  XdX75      nU V	s/ s H  oU	[        U5      S -   PM     sn	U l        U[        U R                  5      :  a  U R                  U   $ gs  sn	f )a  
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   N)r   insertrp   argvrT   rO   _rl_matches)
r2   textstatera   rb   rc   rd   first_colon_posmatchesmatchs
             r   rl_completeCompletionFinder.rl_complete3  s    " A:V`aeVfSN,Oa!-++JnfGOVWweuS->-@'A AwWD3t''((##E**  Xs   Bc                     U R                   $ )zb
This function returns a mapping of completions to their help strings for displaying to the user.
)r,   rr   s    r   get_display_completions(CompletionFinder.get_display_completionsO  s     (((r   )r,   r&   r  r   r'   r1   r+   r-   r(   r*   r)   r   )'r   r   r   r   __doc__r   r4   r.   _exitrw   ArgumentParserr
   boolr|   r   r   r	   r   r   rl   rJ   rT   rv   r   r   r   staticmethodr   r   r   rx   r}   r   r~   rP   r   r  r  r   r   r   r   r!   r!   !   s     $(*)6 59 "*.+/(,!&'++9+;}!00} "'tSy!1} 	}
  '} (3-(} H%} } tn} )} 
}~08#t	"*  Rh"8#:#:;JRJ\J\lo	c@0$d3i $DI $ @#9@#69@#OWX[}@#	c@#D8)r   r!   c                   $    \ rS rSr\S 5       rSrg)ExclusiveCompletionFinderiV  c                     [         R                  X5      (       d  g[        R                  [        R                  4nU R
                  U;   a  gXR                  ;  a  ggr   )r!   r   rw   _AppendAction_AppendConstActionr   r   )r   r   append_classess      r   r   )ExclusiveCompletionFinder._action_allowedW  sO    //??"00(2M2MN/999r   r   N)r   r   r   r   r  r   r   r   r   r   r  r  V  s     r   r  )*rw   r.   rp   collections.abcr   typingr   r   r   r   r   r	   r
   rF   r   rZ   
completersr   r   r   r   r   r   lexersr   packages._argparser   r   r   r   _StoreAction_StoreConstAction_StoreTrueAction_StoreFalseActionr  r  _CountActionr   r   objectr!   r  r   r   r   <module>r$     s     	 
 # J J J  Z Z "  r r )r)v r)j 0 r   