
    Mha(                        d Z ddlmZ ddlZddlZddlZ	 ddlZddlmZ n$# e	$ r  G d d          Z
 e
            ZeZY nw xY wdd	ZddZ G d de          ZdS )z;Helper utilities for integrating argcomplete with traitlets    )annotationsN)CompletionFinderc                      e Zd ZddZdS )
StubModuleattrstrreturnt.Anyc                p    |                     d          st          d          t          d| d          )N__zNo module named 'argcomplete'z*argcomplete stub module has no attribute '')
startswithModuleNotFoundErrorAttributeError)selfr   s     c/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/traitlets/config/argcomplete_config.py__getattr__zStubModule.__getattr__   s@    ??4(( K)*IJJJ !Ud!U!U!UVVV    N)r   r   r	   r
   )__name__
__module____qualname__r    r   r   r   r      s.        	W 	W 	W 	W 	W 	Wr   r   r	   t.Optional[t.List[str]]c                 <   dt           j        vrdS t           j        d         } t          t           j        d                   }	 t          j        | |          \  }}}}}n# t
          $ r Y dS w xY wt          t           j        d                   dz
  }||d         }|S )a  Get current words prior to completion point

    This is normally done in the `argcomplete.CompletionFinder` constructor,
    but is exposed here to allow `traitlets` to follow dynamic code-paths such
    as determining whether to evaluate a subcommand.
    _ARGCOMPLETEN	COMP_LINE
COMP_POINT   )osenvironintargcomplete
split_liner   )	comp_line
comp_pointcword_prequotecword_prefixcword_suffix
comp_wordslast_wordbreak_posstarts           r   get_argcomplete_cwordsr,      s     RZ''t
;'IRZ-..J	 "9j99	
   tt 
>*++a/EEFF#J s   A 
A-,A-Nonec                     	 t          t          t          j        d                   dz             t          j        d<   dS # t          $ rC 	 t          j        dt          j        d                    Y dS # t          t          f$ r Y Y dS w xY ww xY w)aU  Assumes ``$_ARGCOMPLETE`` is set and `argcomplete` is importable

    Increment the index pointed to by ``$_ARGCOMPLETE``, which is used to
    determine which word `argcomplete` should start evaluating the command-line.
    This may be useful to "inform" `argcomplete` that we have already evaluated
    the first word as a subcommand.
    r   r   z!Unable to increment $_ARGCOMPLETEN)	r   r!   r   r    	Exceptionr"   debugKeyErrorr   r   r   r   increment_argcomplete_indexr2   @   s    %(RZ-G)H)H1)L%M%M
>"""   	A2:nC]^^^^^^-. 	 	 	DDD	s)   <A   
B%A33B	BB		Bc                  h     e Zd ZU dZded<   g Zded<   g Zded<   ddZddZd fdZ	d fdZ
 xZS )ExtendedCompletionFindera  An extension of CompletionFinder which dynamically completes class-trait based options

    This finder adds a few functionalities:

    1. When completing options, it will add ``--Class.`` to the list of completions, for each
    class in `Application.classes` that could complete the current option.
    2. If it detects that we are currently trying to complete an option related to ``--Class.``,
    it will add the corresponding config traits of Class to the `ArgumentParser` instance,
    so that the traits' completers can be used.
    3. If there are any subcommands, they are added as completions for the first word

    Note that we are avoiding adding all config traits of all classes to the `ArgumentParser`,
    which would be easier but would add more runtime overhead and would also make completions
    appear more spammy.

    These changes do require using the internals of `argcomplete.CompletionFinder`.
    argparse.ArgumentParser_parserzt.List[t.Any]config_classest.List[str]subcommandsr'   r   r	   t.List[t.Tuple[t.Any, str]]c                    d | j         D             }|}dv r/d                    d          dz            fd|D             }n!t                    dk    rfd|D             }|S )zMatch the word to be completed against our Configurable classes

        Check if cword_prefix could potentially match against --{class}. for any class
        in Application.classes.
        c                (    g | ]}|d |j          dfS )--.)r   ).0clss     r   
<listcomp>zDExtendedCompletionFinder.match_class_completions.<locals>.<listcomp>n   s+    XXXSc#7#7#7#78XXXr   r>   Nr   c                *    g | ]\  }}|k    ||fS r   r   r?   r@   cr'   s      r   rA   zDExtendedCompletionFinder.match_class_completions.<locals>.<listcomp>r   s-    "c"c"caQRVbQbQbC8QbQbQbr   r   c                H    g | ]\  }}|                               ||fS r   r   rC   s      r   rA   zDExtendedCompletionFinder.match_class_completions.<locals>.<listcomp>t   sB     # # #%c1all<>X>X#a# # #r   )r7   indexlen)r   r'   class_completionsmatched_completionss    `  r   match_class_completionsz0ExtendedCompletionFinder.match_class_completionsh   s     YXDDWXXX/,'(E,*<*<S*A*AA*E(EFL"c"c"c"c<M"c"c"c""# # # #):# # # #"r   r@   r
   r-   c                   	 |                     d                                          D ]\  }}|j                            d          pt	          |dd          }|j                            d          }|| j                            d|j         d| t          |j	        |          _
        dS # t          $ r Y dS w xY w)	a-  Add dummy arguments to our ArgumentParser for the traits of this class

        The argparse-based loader currently does not actually add any class traits to
        the constructed ArgumentParser, only the flags & aliaes. In order to work nicely
        with argcomplete's completers functionality, this method adds dummy arguments
        of the form --Class.trait to the ArgumentParser instance.

        This method should be called selectively to reduce runtime overhead and to avoid
        spamming options across all of Application.classes.
        T)configargcompleterNmultiplicityr=   r>   )typehelpnargs)class_traitsitemsmetadatagetgetattrr6   add_argumentr   r   rQ   	completerr   )r   r@   	traitnametraitrY   rO   s         r   inject_class_to_parserz/ExtendedCompletionFinder.inject_class_to_parsery   s    	$'$4$4D$4$A$A$G$G$I$I ( ( 	5!N..~>> '>4C C	  %~11.AA ( ))333	33&	 *   )( (  	 	 	DD	s   B+B/ /
B=<B=r)   argsc                   | j         j        }t                    dk    o	d         |v }|rL|                               }t          |          dk    r#|d         d         }|                     |           nt          |          dk    rsd|d         v ri|sg|ddd         D ][}t          |          dk    rF|d         |v r<|                     |          }|r#|d         d         }|                     |            n\ t                      j        |g|R  }	| j        rNt          |          dk    r;t          j	        d           |	
                    fd| j        D                        |	S )a
  Overridden to dynamically append --Class.trait arguments if appropriate

        Warning:
            This does not (currently) support completions of the form
            --Class1.Class2.<...>.trait, although this is valid for traitlets.
            Part of the reason is that we don't currently have a way to identify
            which classes may be used with Class1 as a parent.

        Warning:
            This is an internal method in CompletionFinder and so the API might
            be subject to drift.
        r   r   r>   NzAdding subcommands forc              3  F   K   | ]}|                               |V  d S NrF   )r?   subcr'   s     r   	<genexpr>z<ExtendedCompletionFinder._get_completions.<locals>.<genexpr>   s5      bbDOOT`DaDabtbbbbbbr   )r6   prefix_charsrH   rK   r\   super_get_completionsr9   r"   r0   extend)r   r)   r'   r]   rd   	is_optionrJ   matched_cls	prev_wordcompletions	__class__s     `       r   rf   z)ExtendedCompletionFinder._get_completions   s   " |0%%)Ml1o.M	 	 #'">">|"L"L&''1,,1!4Q7++K888__q  SJrN%:%:9%: ("-  	y>>A%%)A,,*F*F*.*F*Fy*Q*Q'* A&9!&<Q&?33K@@@E /egg.z<O$OOO  	cJ1 4 46EEEbbbb0@bbbbbbr   parserc                   t                                          ||          }|                    d          r|S |                     |          }t	          |          dk    r|                    d |D                        |S )z7Overridden to add --Class. completions when appropriater>   r   c              3      K   | ]	\  }}|V  
d S ra   r   )r?   r@   opts      r   rc   zCExtendedCompletionFinder._get_option_completions.<locals>.<genexpr>   s&      GGxsCsGGGGGGr   )re   _get_option_completionsendswithrK   rH   rg   )r   rm   r'   rk   rJ   rl   s        r   rq   z0ExtendedCompletionFinder._get_option_completions   s    
 gg55flKK  %% 	"::<HH"##a''GG3FGGGGGG
 r   )r'   r   r	   r:   )r@   r
   r	   r-   )r)   r8   r'   r   r]   r
   r	   r8   )rm   r5   r'   r   r	   r8   )r   r   r   __doc____annotations__r7   r9   rK   r\   rf   rq   __classcell__)rl   s   @r   r4   r4   Q   s          $ %$$$$&N&&&&!K!!!!# # # #"   85 5 5 5 5 5n         r   r4   )r	   r   )r	   r-   )rs   
__future__r   argparser   typingtr"   r   ImportErrorr   objectr,   r2   r4   r   r   r   <module>r|      s/   A A # " " " " "  				    ,,,,,,, 
 
 
W W W W W W W W *,,K
" " " "J   "K K K K K/ K K K K Ks   
! AA