
    ~fW                       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jB                  r3ejD                  ejF                  ejH                  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jV                  dd      Z, G d de*      Z-y)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y)ConfigurableErrorN__name__
__module____qualname__     X/var/www/cvtools/html/venv/lib/python3.12/site-packages/traitlets/config/configurable.pyr   r   (       r    r   c                      e Zd Zy)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dd
              ZddZeddd       Ze	 	 d	 	 	 	 	 	 	 dd       Zeddd       Ze	 	 	 	 	 	 dd       Zeddd       Zedd       Z xZS ) Configurabler   z*traitlets.config.configurable.ConfigurableT)
allow_nonec                   j                  dd      }|(j                  dd      |j                  d<   || _        j                  dd      }t	        |   di  t               dfd}| j                  |       ||| _        n| j                  | j                         | j                  |       D ]  }t        | ||           y)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configc                Z    | j                   v rj                  | j                          yy)zRecord traits set by both config and kwargs.

            They will need to be overridden again after loading config.
            N)nameadd)changeconfig_override_nameskwargss    r!   notice_config_overridez5Configurable.__init__.<locals>.notice_config_overridea   s(    
 {{f$%))&++6 %r    r   r.   r   returnNone)popgetr*   r)   super__init__setr   _load_config	unobservesetattr)selfr0   r)   r*   r1   r,   r/   	__class__s    `    @r!   r8   zConfigurable.__init__9   s    2 Hd+zz(D)1#)==x  DKHd+ 	"6" !$	7 	+,  !DK dkk*-.) 	.DD$t-	.r    c                    t        | j                        D cg c]*  }t        |t              rt        | |      r|j                  , c}S c c}w )zreturn section names as a list)reversed__mro__
issubclassr&   r   )clscs     r!   section_nameszConfigurable.section_names   sD    
 ckk*
!\*z#q/A JJ
 	
 
s   /A
c                   |g}| j                   r*|j                  | j                   j                  |             t               }|D ]=  }| j	                         D ](  }|j                  |      s|j                  ||          * ? |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   rE   _has_sectionmerge)r=   cfgcfgs	my_configrD   snames         r!   rH   zConfigurable._find_my_config   s     u;;KK33C89H	 	.A++- . >>%(OOAeH-	.	. r    c           
        || j                  d      }|| j                         }| j                  |      }| j                         5  |j	                         D ]S  \  }}||v rit        |t              rt        | |      }|j                  |      }n$t        |t              r|j                  ||         }t        | |t        |             tt        |      rt        |t              rd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  }n;t+        |
      d
k\  r-|dj-                  dj/                  t1        |
                  z  } |	|       V 	 ddd       y# 1 sw Y   yxY w)z load traits from a Config objectNTr*   r   )get_close_matchesc                :    t        j                  | t        d      S )N	   
stacklevel)r   warnUserWarning)msgs    r!   rV   z'Configurable._load_config.<locals>.warn   s    #+==ka#PPr    zConfig option `z` not recognized by `z`.r   z  Did you mean `z`?z#  Did you mean one of: `{matches}`?, )matches)rX   t.Anyr3   r4   )traitsrE   rH   hold_trait_notificationsitems
isinstancer   getattr	get_valuer   r<   r   r   r   difflibrQ   LoggingConfigurablelogwarningr>   r   lenformatjoinsorted)r=   rK   rE   r\   rM   r,   config_valueinitialrQ   rV   rZ   rX   s               r!   r:   zConfigurable._load_config   s    >[[[-F  ..0M((-	 **, #	&/oo&7 ""l6>!,@ #*$"5'3'='=g'F#L.A'3'='=fTl'K D$(>?(.z,PV7W9!$(;<#xx333#xx//Q 0f=G+D61Ft~~G^G^F__abC7|q(!1'!*R@@W*DKK$(IIfWo$>  L    IE"#	 #	 #	s   BF:F:*CF::Gr*   c                    | j                  d      }| j                         }| j                  |j                  ||       y)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.
        TrP   )r\   rE   N)r\   rE   r:   new)r=   r.   r\   rE   s       r!   _config_changedzConfigurable._config_changed   s=     D)
 **,&**V=Qr    c                    t        | j                        | _        | j                  |       | j                  j                  |       y)z%Update config and load the new valuesN)r   r*   r:   rJ   )r=   r*   s     r!   update_configzConfigurable.update_config   s4     t{{+&!&!r    c                   |t        ||       sJ g }dj                  d | j                  D              }|j                  | j                   d| d       |j                  t        |d         dz         t        | j                  d      j                               D ](  \  }}| j                  ||      }|j                  |       * d	j                  |      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.
        rY   c              3  4   K   | ]  }|j                     y wN)r   .0ps     r!   	<genexpr>z.Configurable.class_get_help.<locals>.<genexpr>  s      C Cs   (z	) optionsr   -TrP   
)
r_   rh   	__bases__rG   r   rf   ri   class_traitsr^   class_get_trait_help)rC   inst
final_helpbase_classes_vhelps          r!   class_get_helpzConfigurable.class_get_help   s     |z$444
yy CS]] CCS\\N!L>CD#jm,s233++4+8>>@A 	$DAq++At4Dd#	$ yy$$r    c           
        |t        ||       sJ g }d| j                   d|j                   }t        |t        t        f      rm|j
                  j                  dd      }t        |t              rd}n'd|j                  j                  j                         z  }|dk(  r	| d| d	}n%| d
| d	}n| d|j                  j                   d}|j                  |       ||j                  }|dk7  r5dj                  t        |d            }|j                  t        |             d|j                  j                  v r+|j                  t        d|j                         z               |6|j                  t        dt        ||j                  xs d                   nF	 |j!                         }|3t%        |      dkD  r|dd d	z   }|j                  t        d|z               dj                  |      S # t"        $ r d}Y Sw xY w)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--.multiplicityrG   z<key-1>=<value-1>z<%s-item-1>=... z=<> rz   L   EnumzChoices: %sz	Current: @   =   zDefault: %s)r_   r   r,   r   r   metadatar6   r>   lowerrG   r   rh   r   r   infor`   default_value_repr	Exceptionrf   )	rC   traitr~   helptextlinesheaderr   sample_valuedvrs	            r!   r}   z!Configurable.class_get_trait_help  s    |z$444cll^1UZZL1ei./ >>--nhGL%&2,u/G/G/M/M/OOx'"81\N#6"81\N#6xr%//":":!;1=FVzzHr>yy2!>?HLL)*U__---LL

 <=>LL)GD%**:J,K+N OPQ..0 s8b=cr(U*CVMC$789yy  s   #G9 9HHc                8    t        | j                  |             y)z4Get the help string for a single trait and print it.N)printr   )rC   r~   s     r!   class_print_helpzConfigurable.class_print_helpD  s     	c  &'r    c                    | }|j                   J | j                         D ]I  }t        |t              s||v s|j	                  d      j                  |j                   d      |u sH|}K |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`.
        NTrP   )r,   mrorB   r&   class_own_traitsr6   )rC   r   classesdefining_clsr)   s        r!   _defining_classzConfigurable._defining_classI  su    ( zz%%%ggi 	&F6<0g%++4+8<<UZZNRWW%	& r    c           	     N   dd}d}dj                  d | j                  D              }d| j                   d| d}|||g}| j                         j	                  d      }|r|j
                  }|st        | d	d
      }|r(|j                   ||             |j                  d
       t        | j                  d      j                               D ]B  \  }}	|	j                         }
|r| j                  |	|      }n| }|| u r{|	j                  r!|j                   ||	j                               dt        |	      j                  v r"|j                  d|	j                         z         |j                  d|
z         na|	j                  r4|j                   ||	j                  j                  dd      d                |j                  d|j                   d|        |j                  d| j                   d| d|
        |j                  d
       E dj                  |      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.
        c                b    dj                  t        | d            } d| j                  dd      z   S )z"return a commented, wrapped block.z

N   z## rz   z
#  )rh   r   replace)ss    r!   rD   z,Configurable.class_config_section.<locals>.cs  s-    OAr23A199T7333r    zO#------------------------------------------------------------------------------rY   c              3  V   K   | ]!  }t        |t              s|j                   # y wrs   )rB   r&   r   rt   s     r!   rw   z4Configurable.class_config_section.<locals>.<genexpr>{  s     "d!
STVbHc1::"ds   ))z# rx   z) configurationdescription__doc__r   TrP   r   z#  Choices: %sz#  Default: %srz   r   r   z#  See also: r   z# c.z = )r   strr3   r   )rh   r{   r   r|   r6   default_valuer`   rG   ri   r^   r   r   r   typer   split)rC   r   rD   breakerparent_classesr   r   descr,   r   default_reprdefining_classs               r!   class_config_sectionz!Configurable.class_config_sectionh  s   	4 !"ds}}"dda/?!W%!%%m4%%D3	2.DLL4!LL!#"2"2$"2"?"E"E"GH 	KD% 335L!$!4!4UG!D!$$::LL5::/T%[111LL!1EJJL!@A-<= ::LL5::#3#3D!#<Q#?!@A}^-D-D,EQtfMNLL4~QtfC~FGLL/	0 yyr    c                   g }| j                   }t        | j                  d      j                               D ]  \  }}|j                  j                   }|j
                  s*|dz   |j
                  z   }d|v r|d|j                         z   z  }n|d|z   z  }|j                  |       	 |j                         }|Vt        |      dkD  r|dd d	z   }|j                  d
d      }|j                  t        d|z               |j                  d       |j                  xs d}|j                  t        t        |                   |j                  d        dj                  |      S # t        $ r d}Y w xY w)zwGenerate rST documentation for this class' config options.

        Excludes traits defined on parent classes.
        TrP   r   r   z : Nr   r   r   z\nz\\nzDefault: ``%s``r   zNo descriptionrz   )r   ri   r|   r^   r>   r,   info_rstrG   r   r   rf   r   r   r   r   rh   )	rC   r   	classnamer   r   ttypetermliner   r   s	            r!   class_config_rst_docz!Configurable.class_config_rst_doc  sh    LL	s//t/<BBDE  	HAuOO,,E:: 33H EENN$444EEM)LL"..0 s8b=cr(U*Ckk%1V$5$;<=R ::1!1DLLt-. LLA 	D yy!  s   )E))E76E7)r0   r[   r3   r4   )r3   z	list[str])rK   r   r3   r[   )NN)rK   r   rE   zlist[str] | Noner\   z)dict[str, TraitType[t.Any, t.Any]] | Noner3   r4   r2   )r*   r   r3   r4   rs   )r~   HasTraits | Noner3   r   )r   TraitType[t.Any, t.Any]r~   r   r   z
str | Noner3   r   )r~   r   r3   r4   )r   r   r   zt.Sequence[type[HasTraits]]r3   ztype[Configurable])r   z"t.Sequence[type[HasTraits]] | Noner3   r   )r3   r   )r   r   r   r
   r   r*   r)   r8   classmethodrE   rH   r:   r   r   rn   rp   r   r}   r   r   r   r   __classcell__)r>   s   @r!   r&   r&   5   sZ   fb"%FBtTFB.P 
 
8 +/<@	33 (3 :	3
 
3j XR  R "  % %   "&#	6 &6  6  	6 
 
6  6 p ( ( +6Q	 < 9  9 v )  ) r    r&   c                  h    e Zd ZdZ edd      Z ed      d
d       Z ed      dd       Z	ddZ
y	)rc   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'   rd   c                2   t        |j                  t        j                  t        j                  f      s@t        j                  | j                  j                   d|j                   dt        d       t        j                  t        |j                        S )Nz..log should be a Logger or LoggerAdapter, got r      rT   )r_   valueloggingLoggerLoggerAdapterr   rV   r>   r   rW   tcast
LoggerType)r=   proposals     r!   _validate_logz!LoggingConfigurable._validate_log  sn    (..7>>7;P;P*QRMM>>**+ , 'q*	 vvj(..11r    c                    t        | j                  t              rF| j                  J t        j                  t
        j                  | j                  j                        S ddlm} |j                         S )Nr   )rd   )
r_   r)   rc   r   r   r   r   rd   	traitlets
get_logger)r=   rd   s     r!   _log_defaultz LoggingConfigurable._log_default  sN    dkk#67;;***66'..$++//::!~~r    c                    | j                   syt        | j                   t        j                        r| j                   n| j                   j                  }t        |dd      sy|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   )rd   r_   r   r   loggerr`   r   )r=   r   s     r!   _get_log_handlerz$LoggingConfigurable._get_log_handler  sU     xx"488W^^<DHH$((// 	 vz40q!!r    N)r   r   r3   r   )r3   r   )r3   zlogging.Handler | None)r   r   r   r   r   rd   r   r   r   r   r   r   r    r!   rc   rc     sI     5%
HCe_	2 	2 U^   "r    rc   CTSingletonConfigurable)boundc                  \    e Zd ZdZdZedd       Zedd       Zed	d       Zed
d       Z	y)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.
    Nc              #     K   | j                         D ].  }t        | |      st        |t              s!|t        k7  s+| 0 yw)zfWalk the cls.mro() for parent classes that are also singletons

        For use in instance()
        N)r   rB   r   rC   subclasss     r!   	_walk_mrozSingletonConfigurable._walk_mro  s@      	 	H3)x)>? 55	s    AA	AAc                    | j                         sy| j                         D ]   }t        |j                  |       sd|_        " y)z5unset _instance for this class and singleton parents.N)initializedr   r_   	_instancer   s     r!   clear_instancez$SingletonConfigurable.clear_instance  s@       	*H(,,c2 &*"		*r    c                   | j                   $ | |i |}| j                         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
        zAn incompatible sibling of 'z(' is already instantiated as singleton: )r   r   r_   r$   r   r   )rC   argsr0   r~   r   s        r!   instancezSingletonConfigurable.instance)  s    : == ''D  MMO *%)"* cmmS)== '.s||n =""&s}}"5">">!?A r    c                :    t        | d      xr | j                  duS )zHas an instance been created?r   N)hasattrr   )rC   s    r!   r   z!SingletonConfigurable.initializedU  s     sK(FS]]$-FFr    )r3   z4t.Generator[type[SingletonConfigurable], None, None])r3   r4   )rC   ztype[CT]r   r[   r0   r[   r3   r   )r3   bool)
r   r   r   r   r   r   r   r   r   r   r   r    r!   r   r     s_     I  * * ) )V 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&   rc   TypeVarr   r   r   r    r!   <module>r      s    5 #       % ' 8 L L ??)>)>quu)EEFJJ		 		- 	Y 9 Y x.", ."b QYYt23SG/ SGr    