
    KcR                        d 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 ddlmZmZ ddlmZmZmZmZ  G d	 d
e          Z G d de          Z G d de          Z G d de          Z G d de          ZdS )z/A base class for objects that are configurable.    N)deepcopy)dedent)	Any	ContainerDict	HasTraitsInstancedefaultobserveobserve_compatvalidate)indentwrap_paragraphs   )ConfigDeferredConfigLazyConfigValue_is_section_keyc                       e Zd ZdS )ConfigurableErrorN__name__
__module____qualname__     =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 fdZed             Z	d Z
dd	Z ed
          ed                         Zd Zedd            Zedd            Zedd            Zed             Zedd            Zed             Z xZS )Configurabler   z*traitlets.config.configurable.ConfigurableT)
allow_nonec                                         dd          }|'                    dd          
|j        d<   || _                             dd          } t	                      j        di  t                      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configc                 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)changeconfig_override_nameskwargss    r   notice_config_overridez5Configurable.__init__.<locals>.notice_config_override[   s:    
 {f$ 7%))&+666667 7r   r   )popgetr&   r%   super__init__setr   _load_config	unobservesetattr)selfr,   r%   r&   r-   r(   r+   	__class__s    `    @r   r1   zConfigurable.__init__3   s.   2 Hd++ 	!zz(D)) 1#)=x  DKHd++ 	""6""" !$	7 	7 	7 	7 	7 	7 	+,,,  	+ !DKK dk***-...) 	. 	.DD$t----	. 	.r   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__r>   s   `r   section_nameszConfigurable.section_names{   s8    
 
 
 
ck**
 
 
 	
r   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   rC   _has_sectionmerge)r6   cfgcfgs	my_configr=   snames         r   rF   zConfigurable._find_my_config   s     u; 	:KK33C88999HH	 	. 	.A++-- . . >>%(( .OOAeH---	.
 r   Nc           
      (   ||                      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        }	nd }	 |||          }
d                    || j        j                  }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_matchesc                 .    t          j        | d          S )N	   )
stacklevel)warningswarn)msgs    r   <lambda>z+Configurable._load_config.<locals>.<lambda>   s    8=+K+K+K r   z5Config option `{option}` not recognized by `{klass}`.)optionklassr   z  Did you mean `z`?z#  Did you mean one of: `{matches}`?, )matches)traitsrC   rF   hold_trait_notificationsitems
isinstancer   getattr	get_valuer   r5   r   r   r   difflibrO   LoggingConfigurablelogwarningformatr7   r   lenjoinsorted)r6   rI   rC   r[   rK   r(   config_valueinitialrO   rT   rZ   rU   s               r   r3   zConfigurable._load_config   s     	.[[[--F 	1 ..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!$(;<< L#x/KK//f==GQXX#4>+B Y  C 7||q( @'!*@@@@W* DKK$(IIfWoo$>$>  L      DIIIA !	 !	 !	 !	 !	 !	 !	 !	 !	 !	 !	 !	 !	 !	 !	 !	 !	 !	s   F!HHH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.
        TrN   )r[   rC   N)r[   rC   r3   new)r6   r*   r[   rC   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&   r3   rH   )r6   r&   s     r   update_configzConfigurable.update_config   sE     t{++&!!!&!!!!!r   c                    |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.
        NrY   c              3   $   K   | ]}|j         V  d S N)r   r<   ps     r   	<genexpr>z.Configurable.class_get_help.<locals>.<genexpr>   s$       C C C C C C C Cr   (z	) optionsr   -TrN   
)
r^   rg   	__bases__rE   r   rf   rh   class_traitsr]   class_get_trait_help)r>   inst
final_helpbase_classes_vhelps          r   class_get_helpzConfigurable.class_get_help   s    4z$444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   c           	      `   |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                       |9|	                    t          dt          ||j                                       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--.multiplicityrE   z<key-1>=<value-1>z<%s-item-1>=... z=<> rx   L   EnumzChoices: %sz	Current: @   =   zDefault: %s)r^   r   r(   r   r   metadatar/   r7   lowerrE   r   rg   r   r   infor_   default_value_repr	Exceptionrf   )	r>   traitr|   helptextlinesheaderr   sample_valuedvrs	            r   r{   z!Configurable.class_get_trait_help   sf    4z$444441cl11UZ11ei.// 	> >--nhGGL%&& P2,u/G/M/M/O/OOx' 7"66\666"66\666==%/":===FV 	"zHr> 	+yy2!>!>??HLL))***U_-- 	?LL

 <==>>> 
	:LL IGD%*,E,E I IJJKKKK..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   )r>   r|   s     r   class_print_helpzConfigurable.class_print_help2  s&     	c  &&'''''r   c                     | }|                                  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`.
        TrN   N)mror;   r"   class_own_traitsr/   r(   )r>   r   classesdefining_clsr%   s        r   _defining_classzConfigurable._defining_class7  s    $ ggii 	& 	&F6<00&g%& ++4+88<<UZNNRWW&
  &r   c           	         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.
        c                 z    d                     t          | d                    } d|                     dd          z   S )z"return a commented, wrapped block.z

N   z## rx   z
#  )rg   r   replace)ss    r   r=   z,Configurable.class_config_section.<locals>.c^  s6    OAr2233A199T73333r   zO#------------------------------------------------------------------------------rY   c              3   N   K   | ] }t          |t                    |j        V  !d S rr   r:   rs   s     r   ru   z4Configurable.class_config_section.<locals>.<genexpr>f  s5      "d"d!
STVbHcHc"d1:"d"d"d"d"d"dr   z# rv   z) configurationdescription__doc__r   TrN   r   z#  Choices: %sz#  Default: %srx   r   r   z#  See also: r   z# c.z = )rg   ry   r   rz   r/   default_valuer_   rE   rh   r]   r   r   r   typer   split)r>   r   r=   breakerparent_classesr   r   descr(   r   default_reprdefining_classs               r   class_config_sectionz!Configurable.class_config_sectionS  s   	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!$$ O: 0LL5:///T%[[11 B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                 0   g }| j         }t          |                     d                                                    D ]B\  }}|j        j         }|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           Dd                    |          S )zwGenerate rST documentation for this class' config options.

        Excludes traits defined on parent classes.
        TrN   r   r   z : Nr   r   r   z\nz\\nzDefault: ``%s``r   zNo descriptionrx   )r   rh   rz   r]   r7   r(   info_rstrE   r   r   rf   r   r   r   r   rg   )	r>   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   B00B?>B?)NNrr   )r   r   r   r	   r   r&   r%   r1   classmethodrC   rF   r3   r   r   rm   ro   r   r{   r   r   r   r   __classcell__)r7   s   @r   r"   r"   .   s       Xfb"%%FXBtTTTFB. B. B. B. B.P 
 
 [
  2, , , ,\ WXR R ^ R " " "  % % % [%  1  1  1  [1 f ( ( ( [(   [6 9  9  9  [9 v '  '  ['  '  '  '  ' r   r"   c                       e Zd ZdZ ed          Z ed          d             Z ed          d             Z	d Z
dS )	rb   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 instance)r   rc   c                     t          |j        t          j        t          j        f          s)t          j        | j        j         d|j         d           |j        S )Nz..log should be a Logger or LoggerAdapter, got r   )	r^   valueloggingLoggerLoggerAdapterrS   rT   r7   r   )r6   proposals     r   _validate_logz!LoggingConfigurable._validate_log  sd    (.7>7;P*QRR 	M>* * * * * *   ~r   c                     t          | j        t                    r| j        j        S ddlm} |                                S )Nr   )rc   )r^   r%   rb   rc   	traitlets
get_logger)r6   rc   s     r   _log_defaultz LoggingConfigurable._log_default  sC    dk#677 	#;?"!!!!!!~~r   c                     | j         }t          |t          j                  r|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.
        handlersNr   )rc   r^   r   r   loggerr_   r   )r6   r   s     r   _get_log_handlerz$LoggingConfigurable._get_log_handler  sM     fg344 	#]Fvz400 	4q!!r   N)r   r   r   r   r   rc   r   r   r
   r   r   r   r   r   rb   rb     s          #5
6
6
6CXe__  _ WU^^    ^ " " " " "r   rb   c                   n    e Zd ZdZdZed             Zed             Zed             Zed             Z	dS )SingletonConfigurablezA 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   |                                  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   r;   r   r>   subclasss     r   	_walk_mrozSingletonConfigurable._walk_mro  sj       		 	 	H3))x)>??  55
 	 	r   c                     |                                  sdS |                                 D ]}t          |j        |           rd|_        dS )z5unset _instance for this class and singleton parents.N)initializedr   r^   	_instancer   s     r   clear_instancez$SingletonConfigurable.clear_instance  s\        	F 	* 	*H(,c22 * &*"		* 	*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 retrived 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 instanciated as singleton: )r   r   r^   r    r   r   )r>   argsr,   r|   r   s        r   instancezSingletonConfigurable.instance
  s    : = 	*3'''D  MMOO * *%)""cmS)) 	= '''*|||T#-5H5H5Q5QS  r   c                 4    t          | d          o| j        duS )zHas an instance been created?r   N)hasattrr   rB   s    r   r   z!SingletonConfigurable.initialized6  s      sK((FS]$-FFr   )
r   r   r   r   r   r   r   r   r   r   r   r   r   r   r     s          I  [ * * [* ) ) [)V G G [G G Gr   r   )r   r   rS   copyr   textwrapr   traitlets.traitletsr   r   r   r   r	   r
   r   r   r   traitlets.utils.textr   r   loaderr   r   r   r   r   r   r    r"   rb   r   r   r   r   <module>r      s   5 5              
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 9 8 8 8 8 8 8 8 L L L L L L L L L L L L	 	 	 	 		 	 	 		 	 	 	 	- 	 	 	I  I  I  I  I 9 I  I  I X)" )" )" )" )", )" )" )"XSG SG SG SG SG/ SG SG SG SG SGr   