XBFjWdZddlmZddlZddlZddlmZddlm Z ddl m Z m Z m Z mZmZmZmZmZmZmZddlmZddlmZdd lmZmZd d lmZmZmZm Z ej!r%ej"ej#ej$ej fZ%nej Z%Gd d e&Z'Gdde'Z(GddeZ)Gdde)Z*ej+ddZ,Gdde*Z-dS)z/A base class for objects that are configurable.) annotationsN)deepcopy)dedent) Any ContainerDict HasTraitsInstance TraitTypedefaultobserveobserve_compatvalidate)warnings)Bunch)indentwrap_paragraphs)ConfigDeferredConfigLazyConfigValue_is_section_keyceZdZdS)ConfigurableErrorN__name__ __module__ __qualname__KC:\PYTHON\_runtimes\venv\Lib\site-packages\traitlets/config/configurable.pyrr(Dr rceZdZdS)MultipleInstanceErrorNrrr r!r$r$,r"r r$c`eZdZeediZeddZd+fd Zed,d Z d-dZ d.d/dZ e de d0dZd1dZed2d3dZe d.d4d"Zed2d5d#Zed6d'Zed2d7d)Zed8d*ZxZS)9 Configurablerz*traitlets.config.configurable.ConfigurableT) allow_nonekwargst.AnyreturnNonec dd}|'dd |jd<||_dd}t jd it d fd }|||||_n||j| |D]}t|||dS) aCreate 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. parentNconfigchangerr*r+cR|jvr|jdSdS)zRecord traits set by both config and kwargs. They will need to be overridden again after loading config. N)nameadd)r/config_override_namesr(s r!notice_config_overridez5Configurable.__init__..notice_config_overrideas6 {f$$%))&+66666%$r rr/rr*r+) popgetr.r-super__init__setr _load_config unobservesetattr)selfr(r-r.r4r1r3 __class__s ` @r!r9zConfigurable.__init__9s-2Hd++  zz(D))1#)=x  DKHd++ ""6"""!$ 7 7 7 7 7 7 7 +,,,  !DKK   dk * * * -...) . .D D$t - - - - . .r list[str]cDfdtjDS)zreturn section names as a listchg|].}t|tt|'|j/Sr issubclassr&r).0cclss r! z.Configurable.section_names..sO   !\** 0:#q/A/A J   r )reversed__mro__rGs`r! section_nameszConfigurable.section_namess8    ck**    r cfgrc(|g}|jr-||j|t}|D]I}|D]2}||r|||3J|S)aextract 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_configrrL _has_sectionmerge)r>rMcfgs my_configrFsnames r!rPzConfigurable._find_my_configsu ; : KK 33C88 9 9 9HH  . .A++-- . .>>%((.OOAeH---  . r NrLlist[str] | Nonetraits)dict[str, TraitType[t.Any, t.Any]] | Nonec  ||d}||}||}|5|D]\}}||vrt |t r&t||}||}n0t |tr|||}t||t|t|st |tsddlm}t |t r|jJ|jj} ndd } |||} d |d |jjd } t+| dkr| d| ddz } nLt+| dkr9| ddt1| z } | |  ddddS#1swxYwYdS)z load traits from a Config objectNTr.r)get_close_matchesmsgr)r*r+c:tj|tdS)N  stacklevel)rwarn UserWarning)r\s r!raz'Configurable._load_config..warns#+=ka#P#P#PPr zConfig option `z` not recognized by `z`.rz Did you mean `z`?z# Did you mean one of: `{matches}`?, )matches)r\r)r*r+)rWrLrPhold_trait_notificationsitems isinstancergetattr get_valuerr=rrrdifflibr[LoggingConfigurablelogwarningr?rlenformatjoinsorted) r>rMrLrWrTr1 config_valueinitialr[rardr\s r!r;zConfigurable._load_configs >[[[--F   ..00M((--  * * , ,# # &/oo&7&7" " "l6>>!,@@L#*$"5"5'3'='=g'F'F #L.AAL'3'='=fTl'K'K D$(>(>????(..z,PV7W7W999999!$(;<<Q#x333#x/QQQQ0/f==GbDbbt~G^bbbC7||q((@'!*@@@@W**DKK$(IIfWoo$>$> L  DIIIE" # # # # # # # # # # # # # # # # # # sFHH Hr.r/rc|d}|}||j||dS)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)rWrLN)rWrLr;new)r>r/rWrLs r!_config_changedzConfigurable._config_changedsMD)) **,,  &*V=QQQQQr ct|j|_|||j|dS)z%Update config and load the new valuesN)rr.r;rR)r>r.s r! update_configzConfigurable.update_configsEt{++  &!!! &!!!!!r instHasTraits | Nonestrc|t||sJg}dd|jD}||jd|d|t |ddzt |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. Nrcc3$K|] }|jV dSN)rrEps r! z.Configurable.class_get_help..s$ C C C C C C C Cr (z ) optionsr-TrZ ) rgrp __bases__rOrrnrq class_traitsrfclass_get_trait_help)rGry final_help base_classes_vhelps r!class_get_helpzConfigurable.class_get_helps|z$44||4 yy C CS] C C CCC S\CCLCCCDDD#jm,,s23333++4+88>>@@AA $ $DAq++At44D   d # # # #yy$$$r traitTraitType[t.Any, t.Any]helptext str | Nonec d|t||sJg}d|jd|j}t|ttfrl|jdd}t|trd}n!d|jjz}|dkr |d|d }n|d |d }n|d |jjd }| |||j }|d krEd t|d}| t|d|jjvr7| td|z|;| tdt||jpd no |}n#t"$rd}YnwxYw|Et%|dkr |ddd z}| td|zd |S)aGet 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--. multiplicityrOz=z <%s-item-1>=... z=<> LEnumz Choices: %sz Current: @=z Default: %sr)rgrr1rrmetadatar7r?lowerrOrrprrinforhdefault_value_repr Exceptionrn) rGrryrlinesheaderr sample_valuedvrs r!rz!Configurable.class_get_trait_help sW|z$44||41cl11UZ11 ei. / / > >--nhGGL%&& P2 ,u/G/M/M/O/OO x''"66\666"66\666==%/":===F V  zH r>>{{?8R#@#@AAH LL)) * * * U_- - - LL  <== > > >   LL OGD%*:J,K,K O OPP Q Q Q Q ..00    s88b==crc(U*C VMC$788999yys/G GGcJt||dS)z4Get the help string for a single trait and print it.N)printr)rGrys r!class_print_helpzConfigurable.class_print_helpDs& c  &&'''''r classest.Sequence[type[HasTraits]]type[Configurable]c|}|jJ|D]N}t|tr7||vr3|d|jd|ur|}O|S)a7Get 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)r1mrorDr&class_own_traitsr7)rGrr defining_clsr-s r!_defining_classzConfigurable._defining_classIs( z%%%ggii & &F6<00 &g%%++4+88<.css6 OAr2233A199T7333 3r zO#------------------------------------------------------------------------------rcc3NK|] }t|t|jV!dSr~rCrs r!rz4Configurable.class_config_section..{s5"d"d! STVbHcHc"d1:"d"d"d"d"d"dr z# rz) configuration description__doc__rTrZrz# Choices: %sz# Default: %srrrz # See also: rz# c.z = )rr{r*r{)rprrrr7 default_valuerhrOrqrfrrrtypersplit) rGrrFbreakerparent_classesrrdescr1r default_reprdefining_classs r!class_config_sectionz!Configurable.class_config_sectionhs 4 4 4 4!"d"ds}"d"d"ddd ? ? ? ? ? ?!W%!!%%m44  &%D /3 2..D   LL4 ! ! ! LL   !#"2"2$"2"?"?"E"E"G"GHH  KD% 3355L %!$!4!4UG!D!D!$$$:0LL5:///T%[[111LL!1EJJLL!@AAA - <====:BLL5:#3#3D!#<#rs r! _validate_logz!LoggingConfigurable._validate_logs{(.7>7;P*QRR  M>*** ***      vj(.111r ct|jtr2|jJtjt j|jjSddlm}| S)Nr)rl) rgr-rkrrrrrl traitlets get_logger)r>rls r! _log_defaultz LoggingConfigurable._log_defaults_ dk#6 7 7 ;;***6'.$+/:: :!!!!!!~~r logging.Handler | Nonec|jsdSt|jtjr|jn |jj}t |ddsdS|jdS)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. Nhandlersr)rlrgrrloggerrhr)r>rs r!_get_log_handlerz$LoggingConfigurable._get_log_handlers`x 4"48W^<< QDHH$(/ vz400 4q!!r N)rrr*r)r*r)r*r) rrrrrrlrrr rrrr r!rkrks #5% H H HC Xe__ 2 2 2_ 2 WU^^   ^ """"""r rkCTSingletonConfigurable)boundcveZdZdZdZeddZeddZeddZeddZ dS)rzA 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|tr|tkr|V7dS)zfWalk the cls.mro() for parent classes that are also singletons For use in instance() N)rrDrrGsubclasss r! _walk_mrozSingletonConfigurable._walk_mrosf   H3)) x)>??  555   r r+c|sdS|D]}t|j|rd|_dS)z5unset _instance for this class and singleton parents.N) initializedrrg _instancers r!clear_instancez$SingletonConfigurable.clear_instances\    F  * *H(,c22 *&*"  * *r rGtype[CT]argsr)r(rc|j&||i|}|D] }||_ t|j|r|jStd|jdt |jj)aReturns 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 NzAn incompatible sibling of 'z(' is already instantiated as singleton: )rrrgr$rr)rGrr(ryrs r!instancezSingletonConfigurable.instance)s: = 3'''D MMOO * *%)"" cmS ) ) = 'As|AA"&s}"5"5">AA r boolc4t|do|jduS)zHas an instance been created?rN)hasattrrrKs r!rz!SingletonConfigurable.initializedUs sK((FS]$-FFr )r*r)r*r+)rGrrr)r(r)r*r)r*r) rrrrrrrrrrrr r!rrsI   [ ***[*)))[)VGGG[GGGr ).r __future__rrtypingrcopyrtextwraprtraitlets.traitletsrrrr r r r r rrtraitlets.utilsrtraitlets.utils.bunchrtraitlets.utils.textrrloaderrrrr TYPE_CHECKINGUnionrrrrrr$r&rkTypeVarrrrr r!rsy55#"""""                        %$$$$$''''''88888888LLLLLLLLLLLL ?)>qu)EEFJJJ             -   Y Y Y Y Y 9Y Y Y x .".".".".",."."."bQYt2333SGSGSGSGSG/SGSGSGSGSGr