
    hc                         S r 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	  SSK
JrJrJr  SSKJr  S r " S S	\R"                  5      r " S
 S5      r " S S\5      r " S S5      rS rS rg)a  
No public APIs are provided by this module. Internal use only.

This module implements dynamic tab-completion for any command that uses
twisted.python.usage. Currently, only zsh is supported. Bash support may
be added in the future.

Maintainer: Eric P. Mangold - twisted AT teratorn DOT org

In order for zsh completion to take place the shell must be able to find an
appropriate "stub" file ("completion function") that invokes this code and
displays the results to the user.

The stub used for Twisted commands is in the file C{twisted-completion.zsh},
which is also included in the official Zsh distribution at
C{Completion/Unix/Command/_twisted}. Use this file as a basis for completion
functions for your own commands. You should only need to change the first line
to something like C{#compdef mycommand}.

The main public documentation exists in the L{twisted.python.usage.Options}
docstring, the L{twisted.python.usage.Completions} docstring, and the
Options howto.
    N)
MethodType)DictListSet)reflectusageutil)ioTypec                 "   U(       a  [        U5      [        :X  a  UR                  nUS   R                  S5      u  pE[	        U5      nUS-  nX%   nUS:  a*  X%S-
     R                  S5      (       a  US-  nOOUS:  a  M*  USU n[        U SS5      nU(       a  Sn [        R                  " X R                  U R                  5      u  pU(       aS  U R                   HC  u  ppUS   U
:X  d  US   U:X  d  M  U" 5       nXl        [        XX5      nUR                  5           g   S	nUR                  S5      (       a  S
n[        XU5      nUR                  US9  g[        XU5      nUR                  5         g! [        R                   a     Nf = f)a  
Perform shell completion.

A completion function (shell script) is generated for the requested
shell and written to C{shellCompFile}, typically C{stdout}. The result
is then eval'd by the shell to produce the desired completions.

@type config: L{twisted.python.usage.Options}
@param config: The L{twisted.python.usage.Options} instance to generate
    completions for.

@type cmdName: C{str}
@param cmdName: The name of the command we're generating completions for.
    In the case of zsh, this is used to print an appropriate
    "#compdef $CMD" line at the top of the output. This is
    not necessary for the functionality of the system, but it
    helps in debugging, since the output we produce is properly
    formed and may be saved in a file and used as a stand-alone
    completion function.

@type words: C{list} of C{str}
@param words: The raw command-line words passed to use by the shell
    stub function. argv[0] has already been stripped off.

@type shellCompFile: C{file}
@param shellCompFile: The file to write completion data to.
:      -NsubCommandsr   TF)genSubs)r
   strbuffersplitint
startswithgetattrgetoptshortOptlongOpterrorr   parentZshSubcommandBuilderwrite
ZshBuilder)configcmdNamewordsshellCompFile	shellNamepositioncWordr   argsoptscmdshortparserdoc
subOptionsgenr   s                    `/root/1688_scrapy/alibaba-scraper/venv/lib/python3.13/site-packages/twisted/python/_shellcomp.pyshellCompleter1   '   s   > .#5%,,  )//#.I8}H MHOE a-A))#..MH	 a-
 )8E&-6K 	uoov~~NJD +1+=+='F7c>T!W%5!'J(.%&:"G'C IIK ,> C   G-8		'	"-8		7 || 		s   -E7 7FFc                       \ rS rSrS rSrg)SubcommandAction   c                 J    U[         R                  :X  a  g[        SU< 35      e)Nz*::subcmd:->subcmdzUnknown shellType )r   _ZSHNotImplementedError)selfoptName	shellTypes      r0   
_shellCodeSubcommandAction._shellCode   s$    

"'!$6ym"DEE     N)__name__
__module____qualname____firstlineno__r;   __static_attributes__r>   r=   r0   r3   r3      s    Fr=   r3   c                   (    \ rS rSrSrS rSS jrSrg)r       a  
Constructs zsh code that will complete options for a given usage.Options
instance, possibly including a list of subcommand names.

Completions for options to subcommands won't be generated because this
class will never be used if the user is completing options for a specific
subcommand. (See L{ZshSubcommandBuilder} below)

@type options: L{twisted.python.usage.Options}
@ivar options: The L{twisted.python.usage.Options} instance defined for this
    command.

@type cmdName: C{str}
@ivar cmdName: The name of the command we're generating completions for.

@type file: C{file}
@ivar file: The C{file} to write the completion function to.  The C{file}
    must have L{bytes} I/O semantics.
c                 (    Xl         X l        X0l        g N)optionsr"   file)r8   rH   r"   rI   s       r0   __init__ZshBuilder.__init__   s    	r=   c                    U(       Ga/  [        U R                  SS5      Gb  [        U R                  U R                  U R                  5      nUR
                  R                  S[        5       5        UR                  5         U R                  R                  S5        U R                  R                   HK  u  p4pVU R                  R                  SUR                  S5      -   S-   UR                  S5      -   S-   5        MM     U R                  R                  S	5        U R                  R                  S
5        g[        U R                  U R                  U R                  5      nUR                  5         g)aW  
Generate the completion function and write it to the output file
@return: L{None}

@type genSubs: C{bool}
@param genSubs: Flag indicating whether or not completions for the list
    of subcommand should be generated. Only has an effect
    if the C{subCommands} attribute has been defined on the
    L{twisted.python.usage.Options} instance.
r   Nr   s.   local _zsh_subcmds_array
_zsh_subcmds_array=(
   "utf-8   :s   "
s   )

s+   _describe "sub-command" _zsh_subcmds_array
)r   rH   ZshArgumentsGeneratorr"   rI   extraActionsinsertr3   r   r   encode)r8   r   r/   r*   r+   r,   descs          r0   r   ZshBuilder.write   s     wt||]DAM'dllDIINC##A'7'9:IIKIIOOOP,0LL,D,D(F		3::g..5G8LLvU -E IIOOH%IIOOKL'dllDIINCIIKr=   )r"   rI   rH   N)Tr?   r@   rA   rB   __doc__rJ   r   rC   r>   r=   r0   r    r       s    (
r=   r    c                   $    \ rS rSrSrS rS rSrg)r      am  
Constructs zsh code that will complete options for a given usage.Options
instance, and also for a single sub-command. This will only be used in
the case where the user is completing options for a specific subcommand.

@type subOptions: L{twisted.python.usage.Options}
@ivar subOptions: The L{twisted.python.usage.Options} instance defined for
    the sub command.
c                 >    Xl         [        R                  " U /UQ76   g rG   )r.   r    rJ   )r8   r.   r(   s      r0   rJ   ZshSubcommandBuilder.__init__   s    $D(4(r=   c                 8   [        U R                  U R                  U R                  5      nUR                  R                  S[        5       5        UR                  5         [        U R                  U R                  U R                  5      nUR                  5         g)zS
Generate the completion function and write it to the output file
@return: L{None}
r   N)	rP   rH   r"   rI   rQ   rR   r3   r   r.   )r8   r/   s     r0   r   ZshSubcommandBuilder.write   sd    
 $DLL$,,		J#3#56		#DOOT\\499M		r=   )r.   NrV   r>   r=   r0   r   r      s    )
r=   r   c                       \ rS rSrSrS rS rS rS rS r	S r
S	 rSS
 jrS\\\\   4   4S jrS rS rS rS rSS jrSrg)rP      a  
Generate a call to the zsh _arguments completion function
based on data in a usage.Options instance

The first three instance variables are populated based on constructor
arguments. The remaining non-constructor variables are populated by this
class with data gathered from the C{Options} instance passed in, and its
base classes.

@type options: L{twisted.python.usage.Options}
@ivar options: The L{twisted.python.usage.Options} instance to generate for

@type cmdName: C{str}
@ivar cmdName: The name of the command we're generating completions for.

@type file: C{file}
@ivar file: The C{file} to write the completion function to.  The C{file}
    must have L{bytes} I/O semantics.

@type descriptions: C{dict}
@ivar descriptions: A dict mapping long option names to alternate
    descriptions. When this variable is defined, the descriptions
    contained here will override those descriptions provided in the
    optFlags and optParameters variables.

@type multiUse: C{list}
@ivar multiUse: An iterable containing those long option names which may
    appear on the command line more than once. By default, options will
    only be completed one time.

@type mutuallyExclusive: C{list} of C{tuple}
@ivar mutuallyExclusive: A sequence of sequences, with each sub-sequence
    containing those long option names that are mutually exclusive. That is,
    those options that cannot appear on the command line together.

@type optActions: C{dict}
@ivar optActions: A dict mapping long option names to shell "actions".
    These actions define what may be completed as the argument to the
    given option, and should be given as instances of
    L{twisted.python.usage.Completer}.

    Callables may instead be given for the values in this dict. The
    callable should accept no arguments, and return a C{Completer}
    instance used as the action.

@type extraActions: C{list} of C{twisted.python.usage.Completer}
@ivar extraActions: Extra arguments are those arguments typically
    appearing at the end of the command-line, which are not associated
    with any particular named option. That is, the arguments that are
    given to the parseArgs() method of your usage.Options subclass.
c                 ,   Xl         X l        X0l        0 U l        [	        5       U l        / U l        0 U l        / U l        [        [        R                  " UR                  5      5       H  n[        USS 5      nU(       d  M  U R                  R                  UR                  5        U R                  R                  UR                  5        U R
                  R                  UR
                  5        U R                  R                  UR                  5        UR                  (       d  M  UR                  U l        M     [         R"                  n/ n/ nU" UR                  SU5        U" UR                  SU5        [%        U5       H/  u  p['        U
5      S:w  d  M  [(        R*                  " SU
5      Xy'   M1     [%        U5       H/  u  p['        U
5      S:w  d  M  [(        R*                  " SU
5      X'   M1     Xpl        Xl        0 nU H  n
U
SS  XS   '   M     Xl        0 nU H  n
U
SS  XS   '   M     Xl        0 nUR                  U5        UR                  U5        Xl        U R7                  5         U R9                  5         U R;                  5       U l        g )NcompDataoptFlagsoptParameters      r   r   )rH   r"   rI   descriptionssetmultiUsemutuallyExclusive
optActionsrQ   reversedinspectgetmro	__class__r   updateextendr   accumulateClassList	enumeratelenr	   padTorb   	optParamsparamNameToDefinitionflagNameToDefinitionallOptionsNameToDefinitionaddAdditionalOptionsverifyZshNamesmakeExcludesDictexcludes)r8   rH   r"   rI   clsdataaCLrb   ru   ioptListrv   rw   rx   s                 r0   rJ   ZshArgumentsGenerator.__init__  s3   	!#GNN7+<+<=>C3
D1Dt!!(():):;&&t7$$T]]3&&--d.D.DE
 $$$(,(9(9D% ? ))')(*	Gz84G	:#H-JA7|q "jjG4 . $I.JA7|q #zz!W5	 / !" " G07!!*- !%:"!G/6qr{ ,  $8!%'""))*?@"))*>?*D'!!# 	--/r=   c                     U R                  5         U R                  5         U R                  5         U R                  5         g)zN
Write the zsh completion code to the file given to __init__
@return: L{None}
N)writeHeaderwriteExtraswriteOptionswriteFooterr8   s    r0   r   ZshArgumentsGenerator.write\  s4    
 	r=   c                 x    U R                   R                  SU R                  R                  S5      -   S-   5        g)zF
This is the start of the code that calls _arguments
@return: L{None}
s	   #compdef rN   s   

_arguments -s -A "-*" \
N)rI   r   r"   rS   r   s    r0   r   !ZshArgumentsGenerator.writeHeaderf  s5    
 			4<<..w77 ;* *	
r=   c                     [        U R                  R                  5       5      nUR                  5         U H  nU R	                  U5        M     g)zE
Write out zsh code for each option in this command
@return: L{None}
N)listrx   keyssortwriteOpt)r8   optNameslongnames      r0   r   "ZshArgumentsGenerator.writeOptionsp  s<    
 77<<>? HMM(# !r=   c           	         [        U R                  5       H  u  pUR                  (       a'  U[        U R                  5      S-
  :w  a  [	        S5      eU R
                  R                  [        UR                  S[        R                  5      5      R                  S5      5        U R
                  R                  S5        M     g)ag  
Write out completion information for extra arguments appearing on the
command-line. These are extra positional arguments not associated
with a named option. That is, the stuff that gets passed to
Options.parseArgs().

@return: L{None}

@raise ValueError: If C{Completer} with C{repeat=True} is found and
    is not the last item in the C{extraActions} list.
r   zDCompleter with repeat=True must be last item in Options.extraActions rN       \
N)rr   rQ   _repeatrs   
ValueErrorrI   r   escaper;   r   r6   rS   )r8   r   actions      r0   r   !ZshArgumentsGenerator.writeExtrasz  s     #4#4#45IA~~!s4+<+<'='A"A 8  IIOOF6#4#4R#DELLWUVIIOOH% 6r=   c                 :    U R                   R                  S5        g)zR
Write the last bit of code that finishes the call to _arguments
@return: L{None}
s   && return 0
N)rI   r   r   s    r0   r   !ZshArgumentsGenerator.writeFooter  s    
 			()r=   c                 ,  ^  U 4S jn[         R                  " T R                  T R                  T R                  5       H  nUT R
                  ;  d  M  U" U5        M     T R                   H&  nU H  nUT R
                  ;  d  M  U" U5        M     M(     g)z
Ensure that none of the option names given in the metadata are typoed
@return: L{None}
@raise ValueError: If unknown option names have been found.
c                 >   > [        SU < STR                  < 35      e)NzUnknown option name "z1" found while
examining Completions instances on )r   rH   )namer8   s    r0   err1ZshArgumentsGenerator.verifyZshNames.<locals>.err  s    ;?O r=   N)	itertoolschainrf   rj   rh   rx   ri   )r8   r   r   seqs   `   r0   rz   $ZshArgumentsGenerator.verifyZshNames  sv    	 OOD$5$5tVD4:::D	 W ))Ct>>>I  *r=   c                    XR                   ;   a  U R                   U   R                  5       nO
[        5       nXR                  ;  a<  USL a&  U R	                  U5      nUb  UR                  U5        OUR                  U5        U(       d  g/ nU H<  n[        U5      S:X  a  UR                  SU-   5        M(  UR                  SU-   5        M>     UR                  5         SSR                  U5      -  $ )a[  
Generate an "exclusion string" for the given option

@type longname: C{str}
@param longname: The long option name (e.g. "verbose" instead of "v")

@type buildShort: C{bool}
@param buildShort: May be True to indicate we're building an excludes
    string for the short option that corresponds to the given long opt.

@return: The generated C{str}
Fr   r   r   z--z(%s) )
r|   copyrg   rh   getShortOptionaddrs   appendr   join)r8   r   
buildShort
exclusionsr+   stringsr9   s          r0   
excludeStr ZshArgumentsGenerator.excludeStr  s     }}$x0557JJ
 ==(U"++H5$NN5)x(!G7|q sW}-tg~. " 	)))r=   returnc                    0 n[         R                  " U R                  U R                  5       H  nUS   S:w  d  M  US   XS   '   M     0 nU R                   H~  n[        U5       Hl  u  pV[        USU XES-   S -   5      nUR                  5        H  nX;   d  M
  UR                  X   5        M     Xc;   a  X6   R                  U5      X6'   Mh  XsU'   Mn     M     U$ )z
@return: A C{dict} that maps each option name appearing in
    self.mutuallyExclusive to a set of those option names that is it
    mutually exclusive with (can't appear on the cmd line with).
r   Nr   )
r   r   ru   rb   ri   rr   rg   r   r   union)	r8   longToShortr   r|   lstr   r   tmpr   s	            r0   r{   &ZshArgumentsGenerator.makeExcludesDict  s      t~~t}}EGqzT!*1!*AJ' F )+))C(~#bq'CAL01HHJD* 12 ' ')1);)A)A#)FH&),X&  . * r=   c           	         XR                   ;   a  SU-  nOSU-  nU R                  U5      nUS:w  a  SU-   nOSnU R                  U5      nUR                  SS5      nUR                  SS	5      nS
U-  nU R	                  U5      nXR
                  ;   a  SnOSnU R                  U5      n	U(       am  U R                  USS9n
U R                  R                  [        U
< U< U< U< U< 35      R                  S5      5        U R                  R                  S5        U R                  R                  [        U	< U< U< U< U< 35      R                  S5      5        U R                  R                  S5        g)z
Write out the zsh code for the given argument. This is just part of the
one big call to _arguments

@type longname: C{str}
@param longname: The long option name (e.g. "verbose" instead of "v")

@return: L{None}
z--%sz--%s=Nr   r   [z\[]z\]z[%s]*T)r   rN   r   )rw   r   getDescriptionreplace	getActionrh   r   rI   r   r   rS   )r8   r   	longFieldr+   
shortFielddescrdescriptionFieldactionField
multiFieldlongExclusionsFieldshortExclusionsFields              r0   r   ZshArgumentsGenerator.writeOpt  sf    000)I(*I##H-D=uJJ##H- ==e4+33C?!$44nnX.}}$JJ"ooh7#'??8?#M IIOO -""(#	 &/ IIOOH%		 ($	 fWo	
 			!r=   c                    XR                   ;   aa  [        U R                   U   5      (       a  U R                   U   " 5       nOU R                   U   nUR                  U[        R                  5      $ XR
                  ;   a  SU S3$ g)zE
Return a zsh "action" string for the given argument
@return: C{str}
r   z:_filesr   )rj   callabler;   r   r6   rv   )r8   r   r   s      r0   r   ZshArgumentsGenerator.getAction4  sv    
 &122242$$Xuzz::111xj((r=   c                 d   XR                   ;   a  U R                   U   $  U R                  U   S   nUb  U$ UR	                  SS5      n[        U R                  SU-  S5      nUb  [        U5      nU(       a  U$ U$ ! [         a)     U R                  U   S   n Ni! [         a    Sn  Nxf = ff = f)zE
Return the description to be used for this argument
@return: C{str}
r   r   Nr   _zopt_%s)rf   rw   KeyErrorrv   r   r   rH   descrFromDoc)r8   r   r   longMangledobjs        r0   r   $ZshArgumentsGenerator.getDescriptionD  s     ((($$X..	--h7:E L &&sC0dllH{$:DA? %E '  	228<Q? 	s)   A< <
B/BB+'B/*B++B/c                 <    U R                   U   nUS   =(       d    S$ )zC
Return the short option letter or None
@return: C{str} or L{None}
r   N)rx   )r8   r   r   s      r0   r   $ZshArgumentsGenerator.getShortOptione  s#    
 11(;qz!T!r=   Nc                    0 n[         R                  " U R                  US5        0 nUR                  5        H  n[	        U5      S:X  d  M  X2X   '   X	 M     UR                  5        H  u  pEUR                  SS5      nX`R                  ;   a  M(  U R                  U5      nSnXR;   a  X%   nUR                  R                  R                  n	U	S:X  aD  U R                  R                  XhSU/5        USU/U R                  U'   USU/U R                  U'   M  U R                  R                  XhU/5        X/U R                   U'   USU/U R                  U'   M     g)z
Add additional options to the optFlags and optParams lists.
These will be defined by 'opt_foo' methods of the Options subclass
@return: L{None}
opt_r   r   r   Nr   )r   accumulateMethodsrH   r   rs   itemsr   rx   r   __func____code__co_argcountru   r   rv   rb   rw   )
r8   methodsDictmethodToShortr   
methodName	methodObjr   r   r+   reqArgss
             r0   ry   *ZshArgumentsGenerator.addAdditionalOptionsm  sX    .0!!$,,VD$$&D4yA~37k/0% '
 &1%6%6%8!J!))#s3H :::''1EE)%0((11==G!|%%xe&DE8=tU7K**84=BD%<P//9 $$hu%=>7<n))(3=BD%<P//9/ &9r=   )rx   r"   rf   r|   rQ   rI   rw   rh   ri   rj   rb   ru   rH   rv   )F)r   N)r?   r@   rA   rB   rW   rJ   r   r   r   r   r   rz   r   r   r   r   r{   r   r   r   r   ry   rC   r>   r=   r0   rP   rP      si    2hB0H
$&,*,(*T$sCH}"5 6@"D B"%Qr=   rP   c                 8   U R                   b  U R                   R                  5       (       a  gU R                   R                  S5       Vs/ s H2  o(       d  M  UR                  5       (       a  M"  UR                  5       PM4     nnSR	                  U5      $ s  snf )zH
Generate an appropriate description from docstring of the given object
N
r   )rW   isspacer   stripr   )r   xliness      r0   r   r     sp     {{ckk1133 # 1 1$ 7Q 711YQYY[YQWWY 7EQ88E? Rs   
BB0Bc                 `    SU ;  a  SU -   S-   $ SnU  H  nUS;   a  US-   nX-   nM     US-   nU$ )zk
Shell escape the given string

Implementation borrowed from now-deprecated commands.mkarg() in the stdlib
'"z\$"`\r>   )r   scs      r0   r   r     sO     !|Qw}A<DAE  	
CAHr=   )rW   r   rl   r   typesr   typingr   r   r   twisted.pythonr   r   r	   twisted.python.compatr
   r1   	Completerr3   r    r   rP   r   r   r>   r=   r0   <module>r      sp   
0     " " / / (aHFu F2 2j: 8oQ oQdr=   