
    MhW                       d Z ddlmZ ddlZddlZddlmZ ddlm	Z	 ddl
mZmZmZmZmZmZmZmZmZmZ ddlmZ ddlmZ dd	lmZmZ d
dlmZmZmZm Z  ej!        r%ej"        ej#        ej$        ej                 f         Z%nej        Z% G d de&          Z' G d de'          Z( G d de          Z) G d de)          Z* ej+        dd          Z, G d de*          Z-dS )z/A base class for objects that are configurable.    )annotationsN)deepcopy)dedent)
Any	ContainerDict	HasTraitsInstance	TraitTypedefaultobserveobserve_compatvalidate)warnings)Bunch)indentwrap_paragraphs   )ConfigDeferredConfigLazyConfigValue_is_section_keyc                      e Zd ZdS )ConfigurableErrorN__name__
__module____qualname__     ]/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/traitlets/config/configurable.pyr   r   (           Dr    r   c                      e Zd ZdS )MultipleInstanceErrorNr   r   r    r!   r$   r$   ,   r"   r    r$   c                  `    e Zd Z eedi           Z edd          Zd+ fd	Zed,d            Z	d-dZ
	 	 d.d/dZ ed          ed0d                        Zd1dZed2d3d            Ze	 	 d.d4d"            Zed2d5d#            Zed6d'            Zed2d7d)            Zed8d*            Z xZS )9Configurabler   z*traitlets.config.configurable.ConfigurableT)
allow_nonekwargst.AnyreturnNonec                                        dd          }|'                    dd          
|j        d<   || _                             dd          } t	                      j        d	i  t                      d
fd}|                     |           ||| _        n|                     | j                   | 	                    |           D ]}t          | ||                    dS )a  Create a configurable given a config config.

        Parameters
        ----------
        config : Config
            If this is empty, default values are used. If config is a
            :class:`Config` instance, it will be used to configure the
            instance.
        parent : Configurable instance, optional
            The parent Configurable instance of this object.

        Notes
        -----
        Subclasses of Configurable must call the :meth:`__init__` method of
        :class:`Configurable` *before* doing anything else and using
        :func:`super`::

            class MyConfigurable(Configurable):
                def __init__(self, config=None):
                    super(MyConfigurable, self).__init__(config=config)
                    # Then any other code you need to finish initialization.

        This ensures that instances will be configured properly.
        parentNconfigchanger   r*   r+   c                R    | j         v r                    | j                    dS dS )zRecord traits set by both config and kwargs.

            They will need to be overridden again after loading config.
            N)nameadd)r/   config_override_namesr(   s    r!   notice_config_overridez5Configurable.__init__.<locals>.notice_config_overridea   s6    
 {f$$%))&+66666 %$r    r   r/   r   r*   r+   )popgetr.   r-   super__init__setr   _load_config	unobservesetattr)selfr(   r-   r.   r4   r1   r3   	__class__s    `    @r!   r9   zConfigurable.__init__9   s-   2 Hd++zz(D))1#)=x  DKHd++ 	""6""" !$	7 	7 	7 	7 	7 	7 	7 	+,,,  !DKK dk***-...) 	. 	.DD$t----	. 	.r    	list[str]c                D      fdt           j                  D             S )zreturn section names as a listc                h    g | ].}t          |t                    t          |          '|j        /S r   
issubclassr&   r   ).0cclss     r!   
<listcomp>z.Configurable.section_names.<locals>.<listcomp>   sO     
 
 
!\**
 0:#q/A/A
J
 
 
r    )reversed__mro__rG   s   `r!   section_nameszConfigurable.section_names   s8    
 
 
 
ck**
 
 
 	
r    cfgr   c                (   |g}| j         r-|                    | j                             |                     t                      }|D ]I}|                                 D ]2}|                    |          r|                    ||                    3J|S )a  extract my config from a global Config object

        will construct a Config object of only the config values that apply to me
        based on my mro(), as well as those of my parent(s) if they exist.

        If I am Bar and my parent is Foo, and their parent is Tim,
        this will return merge following config sections, in this order::

            [Bar, Foo.Bar, Tim.Foo.Bar]

        With the last item being the highest priority.
        )r-   append_find_my_configr   rL   _has_sectionmerge)r>   rM   cfgs	my_configrF   snames         r!   rP   zConfigurable._find_my_config   s     u; 	:KK33C88999HH	 	. 	.A++-- . . >>%(( .OOAeH---	.
 r    NrL   list[str] | Nonetraits)dict[str, TraitType[t.Any, t.Any]] | Nonec           
         ||                      d          }||                                 }|                     |          }|                                 5  |                                D ]\  }}||v rt          |t                    r&t          | |          }|                    |          }n0t          |t                    r|                    ||                   }t          | |t          |                     t          |          st          |t                    sddlm} t          | t                     r| j        J | j        j        }	ndd
}	 |||          }
d| d| j        j         d}t+          |
          dk    r|d|
d          dz  }nLt+          |
          dk    r9|d                    d                    t1          |
                              z  } |	|           	 ddd           dS # 1 swxY w Y   dS )z load traits from a Config objectNTr.   r   )get_close_matchesmsgr)   r*   r+   c                :    t          j        | t          d          S )N	   
stacklevel)r   warnUserWarning)r\   s    r!   ra   z'Configurable._load_config.<locals>.warn   s    #+=ka#P#P#PPr    zConfig option `z` not recognized by `z`.r   z  Did you mean `z`?z#  Did you mean one of: `{matches}`?, )matches)r\   r)   r*   r+   )rW   rL   rP   hold_trait_notificationsitems
isinstancer   getattr	get_valuer   r=   r   r   r   difflibr[   LoggingConfigurablelogwarningr?   r   lenformatjoinsorted)r>   rM   rL   rW   rT   r1   config_valueinitialr[   ra   rd   r\   s               r!   r;   zConfigurable._load_config   s    >[[[--F  ..00M((--	 **,, #	 #	&/oo&7&7 " ""l6>>!,@@ L #*$"5"5'3'='=g'F'F#L.AA L'3'='=fTl'K'K D$(>(>????(.. z,PV7W7W 999999!$(;<< Q#x333#x/Q Q Q Q 0/f==GbDbbt~G^bbbC7||q((@'!*@@@@W**DKK$(IIfWoo$>$>  L      DIIIE"#	 #	 #	 #	 #	 #	 #	 #	 #	 #	 #	 #	 #	 #	 #	 #	 #	 #	s   FHH
Hr.   r/   r   c                    |                      d          }|                                 }|                     |j        ||           dS )zUpdate all the class traits having ``config=True`` in metadata.

        For any class trait with a ``config`` metadata attribute that is
        ``True``, we update the trait with the value of the corresponding
        config entry.
        TrZ   )rW   rL   N)rW   rL   r;   new)r>   r/   rW   rL   s       r!   _config_changedzConfigurable._config_changed   sM     D))
 **,,&*V=QQQQQr    c                    t          | j                  | _        |                     |           | j                            |           dS )z%Update config and load the new valuesN)r   r.   r;   rR   )r>   r.   s     r!   update_configzConfigurable.update_config   sE     t{++&!!!&!!!!!r    instHasTraits | Nonestrc                   |t          ||           sJ g }d                    d | j        D                       }|                    | j         d| d           |                    t          |d                   dz             t          |                     d	                                                    D ]0\  }}| 	                    ||          }|                    |           1d
                    |          S )zGet the help string for this class in ReST format.

        If `inst` is given, its current trait values will be used in place of
        class defaults.
        Nrc   c              3  $   K   | ]}|j         V  d S N)r   rE   ps     r!   	<genexpr>z.Configurable.class_get_help.<locals>.<genexpr>  s$       C C C C C C C Cr    (z	) optionsr   -TrZ   
)
rg   rp   	__bases__rO   r   rn   rq   class_traitsrf   class_get_trait_help)rG   ry   
final_helpbase_classes_vhelps          r!   class_get_helpzConfigurable.class_get_help   s    |z$44|||
yy C CS] C C CCCS\CCLCCCDDD#jm,,s23333++4+88>>@@AA 	$ 	$DAq++At44Dd####yy$$$r    traitTraitType[t.Any, t.Any]helptext
str | Nonec           	     d   |t          ||           sJ g }d| j         d|j         }t          |t          t          f          rl|j                            dd          }t          |t                    rd}n!d|j        j                                        z  }|dk    r	| d| d	}n| d
| d	}n| d|j        j         d}|	                    |           ||j
        }|dk    rEd                    t          |d                    }|	                    t          |                     d|j        j        v r7|	                    t          d|                                z                       |;|	                    t          dt          ||j        pd                               no	 |                                }n# t"          $ r d}Y nw xY w|Et%          |          dk    r|dd         d	z   }|	                    t          d|z                       d                    |          S )a  Get the helptext string for a single trait.

        :param inst:
            If given, its current trait values will be used in place of
            the class default.
        :param helptext:
            If not given, uses the `help` attribute of the current trait.
        Nz--.multiplicityrO   z<key-1>=<value-1>z<%s-item-1>=... z=<> r   L   EnumzChoices: %sz	Current: @   =   zDefault: %s)rg   r   r1   r   r   metadatar7   r?   lowerrO   r   rp   r   r   inforh   default_value_repr	Exceptionrn   )	rG   r   ry   r   linesheaderr   sample_valuedvrs	            r!   r   z!Configurable.class_get_trait_help  sV    |z$44|||1cl11UZ11ei.// 	> >--nhGGL%&& P2,u/G/M/M/O/OOx''"66\666"66\666==%/":===FVzHr>>yy2!>!>??HLL))***U_---LL

 <==>>>LL OGD%*:J,K,K O OPPQQQQ..00   s88b==crc(U*CVMC$788999yys   /G GGc                J    t          |                     |                     dS )z4Get the help string for a single trait and print it.N)printr   )rG   ry   s     r!   class_print_helpzConfigurable.class_print_helpD  s&     	c  &&'''''r    classest.Sequence[type[HasTraits]]type[Configurable]c                    | }|j         J |                                 D ]N}t          |t                    r7||v r3|                    d                              |j         d          |u r|}O|S )a7  Get the class that defines a trait

        For reducing redundant help output in config files.
        Returns the current class if:
        - the trait is defined on this class, or
        - the class where it is defined would not be in the config file

        Parameters
        ----------
        trait : Trait
            The trait to look for
        classes : list
            The list of other classes to consider for redundancy.
            Will return `cls` even if it is not defined on `cls`
            if the defining class is not in `classes`.
        NTrZ   )r1   mrorD   r&   class_own_traitsr7   )rG   r   r   defining_clsr-   s        r!   _defining_classzConfigurable._defining_classI  s    ( z%%%ggii 	& 	&F6<00&g%%++4+88<<UZNNRWWW%r    "t.Sequence[type[HasTraits]] | Nonec           	        dd}d}d                     d | j        D                       }d| j         d	| d
}|||g}|                                                     d          }|r|j        }|st          | dd          }|r3|                     ||                     |                    d           t          |                     d          	                                          D ]X\  }}	|	
                                }
|r|                     |	|          }n| }|| u r|	j        r#|                     ||	j                             dt          |	          j        v r*|                    d|	                                z             |                    d|
z             nd|	j        r=|                     ||	j                            dd          d                              |                    d|j         d|            |                    d| j         d| d|
            |                    d           Zd                     |          S )zGet the config section for this class.

        Parameters
        ----------
        classes : list, optional
            The list of other classes in the config file.
            Used to reduce redundant information.
        sr{   r*   c                z    d                     t          | d                    } d|                     dd          z   S )z"return a commented, wrapped block.z

N   z## r   z
#  )rp   r   replace)r   s    r!   rF   z,Configurable.class_config_section.<locals>.cs  s6    OAr2233A199T73333r    zO#------------------------------------------------------------------------------rc   c              3  N   K   | ] }t          |t                    |j        V  !d S r~   rC   r   s     r!   r   z4Configurable.class_config_section.<locals>.<genexpr>{  s5      "d"d!
STVbHcHc"d1:"d"d"d"d"d"dr    z# r   z) configurationdescription__doc__r   TrZ   r   z#  Choices: %sz#  Default: %sr   r   r   z#  See also: r   z# c.z = )r   r{   r*   r{   )rp   r   r   r   r7   default_valuerh   rO   rq   rf   r   r   r   typer   split)rG   r   rF   breakerparent_classesr   r   descr1   r   default_reprdefining_classs               r!   class_config_sectionz!Configurable.class_config_sectionh  s   	4 	4 	4 	4 !"d"ds}"d"d"ddd??????!W%!!%%m44 	&%D 	/3	2..D 	LL4!!!LL!#"2"2$"2"?"?"E"E"G"GHH 	 	KD% 3355L %!$!4!4UG!D!D!$$$: 0LL5:///T%[[111LL!1EJJLL!@AAA-<==== : BLL5:#3#3D!#<#<Q#?!@!@AAAM^-DMMtMMNNNLLFFFtFFFFGGGLLyyr    c                @   g }| j         }t          |                     d                                                    D ]J\  }}|j        j         }|j        s|dz   |j        z   }d|v r|d|                                z   z  }n|d|z   z  }|                    |           	 |                                }n# t          $ r d}Y nw xY w|pt          |          dk    r|dd         d	z   }|                    d
d          }|                    t          d|z                       |                    d           |j        pd}|                    t          t          |                               |                    d           Ld                    |          S )zwGenerate rST documentation for this class' config options.

        Excludes traits defined on parent classes.
        TrZ   r   r   z : Nr   r   r   z\nz\\nzDefault: ``%s``r   zNo descriptionr   )r   rq   r   rf   r?   r1   info_rstrO   r   r   rn   r   r   r   r   rp   )	rG   r   	classnamer   r   ttypetermliner   r   s	            r!   class_config_rst_docz!Configurable.class_config_rst_doc  s    L	s//t/<<BBDDEE  	  	HAuO,E:  33H EENN$4$444EEM)LL"""..00   s88b==crc(U*Ckk%11V$5$;<<===R   :1!1DLLt--... LLyys   #B88CC)r(   r)   r*   r+   )r*   r@   )rM   r   r*   r)   )NN)rM   r   rL   rV   rW   rX   r*   r+   r5   )r.   r   r*   r+   r~   )ry   rz   r*   r{   )r   r   ry   rz   r   r   r*   r{   )ry   rz   r*   r+   )r   r   r   r   r*   r   )r   r   r*   r{   )r*   r{   )r   r   r   r
   r   r.   r-   r9   classmethodrL   rP   r;   r   r   rv   rx   r   r   r   r   r   r   __classcell__)r?   s   @r!   r&   r&   5   s       Xfb"%%FXBtTTTFB. B. B. B. B. B.P 
 
 
 [
   8 +/<@	3 3 3 3 3j WXR R R ^ R " " " "  % % % % [%   "&#	6  6  6  6  [6 p ( ( ( ( [(    [< 9  9  9  9  [9 v )  )  )  [)  )  )  )  ) r    r&   c                      e Zd ZdZ edd          Z ed          dd
            Z ed          dd            Z	ddZ
dS )rk   zA parent class for Configurables that log.

    Subclasses have a log trait, and the default behavior
    is to get the logger from the currently running Application.
    z Logger or LoggerAdapter instanceF)r   r'   rl   proposalr   r*   
LoggerTypec                    t          |j        t          j        t          j        f          s1t          j        | j        j         d|j         dt          d           t          j        t          |j                  S )Nz..log should be a Logger or LoggerAdapter, got r      r_   )rg   valueloggingLoggerLoggerAdapterr   ra   r?   r   rb   tcastr   )r>   r   s     r!   _validate_logz!LoggingConfigurable._validate_log  s{    (.7>7;P*QRR 	M>* * * * * *	    vj(.111r    c                    t          | j        t                    r2| j        J t          j        t
          j        | j        j                  S ddlm} |	                                S )Nr   )rl   )
rg   r-   rk   r   r   r   r   rl   	traitlets
get_logger)r>   rl   s     r!   _log_defaultz LoggingConfigurable._log_default  s_    dk#677 	;;***6'.$+/:::!!!!!!~~r    logging.Handler | Nonec                    | j         sdS t          | j         t          j                  r| j         n| j         j        }t          |dd          sdS |j        d         S )zReturn the default Handler

        Returns None if none can be found

        Deprecated, this now returns the first log handler which may or may
        not be the default one.
        Nhandlersr   )rl   rg   r   r   loggerrh   r   )r>   r   s     r!   _get_log_handlerz$LoggingConfigurable._get_log_handler  s`     x 	4"48W^<<QDHH$(/ 	 vz400 	4q!!r    N)r   r   r*   r   )r*   r   )r*   r   )r   r   r   r   r   rl   r   r   r   r   r   r   r    r!   rk   rk     s          #5%
H
H
HCXe__	2 	2 	2 _	2 WU^^      ^ " " " " " "r    rk   CTSingletonConfigurable)boundc                  v    e Zd ZdZdZedd            Zedd            Zedd            Zedd            Z	dS )r   zA configurable that only allows one instance.

    This class is for classes that should only have one instance of itself
    or *any* subclass. To create and retrieve such a class use the
    :meth:`SingletonConfigurable.instance` method.
    Nr*   4t.Generator[type[SingletonConfigurable], None, None]c              #     K   |                                  D ]6}t          | |          r$t          |t                    r|t          k    r|V  7dS )zfWalk the cls.mro() for parent classes that are also singletons

        For use in instance()
        N)r   rD   r   rG   subclasss     r!   	_walk_mrozSingletonConfigurable._walk_mro  sf       		 	 	H3))x)>??  555	 	r    r+   c                    |                                  sdS |                                 D ]}t          |j        |           rd|_        dS )z5unset _instance for this class and singleton parents.N)initializedr   rg   	_instancer   s     r!   clear_instancez$SingletonConfigurable.clear_instance  s\        	F 	* 	*H(,c22 * &*"		* 	*r    rG   type[CT]argsr)   r(   r   c                    | j         & | |i |}|                                 D ]	}||_         
t          | j         |           r| j         S t          d| j         dt          | j                   j                   )a  Returns a global instance of this class.

        This method create a new instance if none have previously been created
        and returns a previously created instance is one already exists.

        The arguments and keyword arguments passed to this method are passed
        on to the :meth:`__init__` method of the class upon instantiation.

        Examples
        --------
        Create a singleton class using instance, and retrieve it::

            >>> from traitlets.config.configurable import SingletonConfigurable
            >>> class Foo(SingletonConfigurable): pass
            >>> foo = Foo.instance()
            >>> foo == Foo.instance()
            True

        Create a subclass that is retrieved using the base class instance::

            >>> class Bar(SingletonConfigurable): pass
            >>> class Bam(Bar): pass
            >>> bam = Bam.instance()
            >>> bam == Bar.instance()
            True
        NzAn incompatible sibling of 'z(' is already instantiated as singleton: )r   r   rg   r$   r   r   )rG   r   r(   ry   r   s        r!   instancezSingletonConfigurable.instance)  s    : = 3'''D  MMOO * *%)""cmS)) 	= 'As| A A"&s}"5"5">A A  r    boolc                4    t          | d          o| j        duS )zHas an instance been created?r   N)hasattrr   rK   s    r!   r   z!SingletonConfigurable.initializedU  s      sK((FS]$-FFr    )r*   r   )r*   r+   )rG   r   r   r)   r(   r)   r*   r   )r*   r   )
r   r   r   r   r   r   r   r   r   r   r   r    r!   r   r     s          I   [ * * * [* ) ) ) [)V G G G [G G Gr    ).r   
__future__r   r   typingr   copyr   textwrapr   traitlets.traitletsr   r   r   r	   r
   r   r   r   r   r   traitlets.utilsr   traitlets.utils.bunchr   traitlets.utils.textr   r   loaderr   r   r   r   TYPE_CHECKINGUnionr   r   r   r   r   r$   r&   rk   TypeVarr   r   r   r    r!   <module>r     sy   5 5 # " " " " "                                         % $ $ $ $ $ ' ' ' ' ' ' 8 8 8 8 8 8 8 8 L L L L L L L L L L L L ? )>qu)EEFJJJ	 	 	 	 		 	 	 		 	 	 	 	- 	 	 	Y  Y  Y  Y  Y 9 Y  Y  Y x." ." ." ." .", ." ." ."b QYt2333SG SG SG SG SG/ SG SG SG SG SGr    