U
    -e'                     @   s   d Z ddlZddlZddlZzddlZddlmZ W n, ek
r`   G dd dZe Ze	ZY nX ej
eje  dddZddd	d
ZG dd deZdS )z;Helper utilities for integrating argcomplete with traitlets    N)CompletionFinderc                   @   s   e Zd ZeejdddZdS )
StubModule)attrreturnc                 C   s&   | dstdtd| dd S )N__zNo module named 'argcomplete'z*argcomplete stub module has no attribute '')
startswithModuleNotFoundErrorAttributeError)selfr    r   d/var/www/html/Darija-Ai-Train/env/lib/python3.8/site-packages/traitlets/config/argcomplete_config.py__getattr__   s    
zStubModule.__getattr__N)__name__
__module____qualname__strtAnyr   r   r   r   r   r      s   r   )r   c                  C   sz   dt jkrdS t jd } tt jd }zt| |\}}}}}W n tk
rV   Y dS X tt jd d }||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.
    _ARGCOMPLETENZ	COMP_LINEZ
COMP_POINT   )osenvironintargcompleteZ
split_liner	   )Z	comp_lineZ
comp_pointZcword_prequotecword_prefixZcword_suffix
comp_wordsZlast_wordbreak_posstartr   r   r   get_argcomplete_cwords   s*    

 	r   c                   C   sj   z t ttjd d tjd< W nD tk
rd   ztdtjd  W n ttfk
r^   Y nX Y nX dS )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   r   increment_argcomplete_indexB   s      r"   c                       s   e Zd ZU dZejed< g Zej	ej
 ed< g Zej	e ed< eej	ejej
ef  dddZej
dd	d
dZej	e eej
ej	e d fddZejeej	e d fddZ  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`.
    _parserconfig_classessubcommands)r   r   c                    sh   dd | j D }|}d krF d dd    fdd|D }nt dkrd fd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                 S   s   g | ]}|d |j  dfqS )--.)r   ).0clsr   r   r   
<listcomp>r   s     zDExtendedCompletionFinder.match_class_completions.<locals>.<listcomp>r(   Nr   c                    s    g | ]\}}| kr||fqS r   r   r)   r*   cr   r   r   r+   v   s      r   c                    s"   g | ]\}}|  r||fqS r   r   r,   r.   r   r   r+   x   s    
 )r%   indexlen)r   r   Zclass_completionsmatched_completionsr   r.   r   match_class_completionsl   s    
z0ExtendedCompletionFinder.match_class_completionsN)r*   r   c                 C   s   zh|j dd D ]R\}}|jdp0t|dd}|jd}|| jjd|j d| t|j	|d_
qW n tk
r|   Y nX dS )	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)configZargcompleterNmultiplicityr'   r(   )typehelpnargs)Zclass_traitsitemsmetadatagetgetattrr$   add_argumentr   r   r7   	completerr
   )r   r*   Z	traitnameZtraitr>   r5   r   r   r   inject_class_to_parser}   s"      z/ExtendedCompletionFinder.inject_class_to_parser)r   r   argsr   c           
         s  | j j}t dko d |k}|rR|  }t|dkr|d d }| | npt|dkrd|d kr|s|ddd D ]D}t|dkr||d |kr|| |}|r|d d }| |  qq|t j| f| }	| jrt|dkrt	d  |	
 fdd| jD  |	S )	a	  Overriden 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   s   | ]}|  r|V  qd S Nr/   )r)   Zsubcr.   r   r   	<genexpr>   s     
 z<ExtendedCompletionFinder._get_completions.<locals>.<genexpr>)r$   prefix_charsr1   r3   r?   super_get_completionsr&   r   r    extend)
r   r   r   r@   rD   Z	is_optionr2   Zmatched_clsZ	prev_wordcompletions	__class__r.   r   rF      s(    


z)ExtendedCompletionFinder._get_completions)parserr   r   c                    sJ   t  ||}|dr|S | |}t|dkrF|dd |D  |S )z6Overriden to add --Class. completions when appropriater(   r   c                 s   s   | ]\}}|V  qd S rB   r   )r)   r*   optr   r   r   rC      s     zCExtendedCompletionFinder._get_option_completions.<locals>.<genexpr>)rE   _get_option_completionsendswithr3   r1   rG   )r   rK   r   rH   r2   rI   r   r   rM      s    

z0ExtendedCompletionFinder._get_option_completions)r   r   r   __doc__argparseArgumentParser__annotations__r%   r   Listr   r&   r   Tupler3   r?   rF   rM   __classcell__r   r   rI   r   r#   U   s   

"  8 r#   )rO   rP   r   typingr   r   r   ImportErrorr   objectOptionalrS   r   r   r"   r#   r   r   r   r   <module>   s   
'