a
    lc{                     @   s  d Z ddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
mZmZmZmZmZmZ ddlmZmZ G dd deZG dd	 d	eZG d
d deZG dd deZG dd dZe ZG dd dejZdd ZG dd deZdd ZG dd deZ G dd dZ!G dd de"e!Z#G dd de$e!Z%G d d! d!Z&G d"d# d#e&Z'G d$d% d%e'Z(G d&d' d'e'Z)G d(d) d)e&Z*e+d*Z,d+Z-d,Z.G d-d. d.ej/Z0G d/d0 d0eZ1G d1d2 d2ejZ2G d3d4 d4e*Z3G d5d6 d6ej/Z4G d7d8 d8e3Z5G d9d: d:e5Z6d;d< Z7dS )=zA simple configuration system.    N)Any	ContainerDict	HasTraitsList	Undefined   )cast_unicodefilefindc                   @   s   e Zd ZdS )ConfigErrorN__name__
__module____qualname__ r   r   S/var/www/html/django/DPS/env/lib/python3.9/site-packages/traitlets/config/loader.pyr      s   r   c                   @   s   e Zd ZdS )ConfigLoaderErrorNr   r   r   r   r   r      s   r   c                   @   s   e Zd ZdS )ConfigFileNotFoundNr   r   r   r   r   r       s   r   c                   @   s   e Zd ZdS )ArgumentErrorNr   r   r   r   r   r   $   s   r   c                   @   s   e Zd Zdd Zdd ZdS )	_Sentinelc                 C   s   dS )Nz<Sentinel deprecated>r   selfr   r   r   __repr__4   s    z_Sentinel.__repr__c                 C   s   dS )Nz<deprecated>r   r   r   r   r   __str__7   s    z_Sentinel.__str__N)r   r   r   r   r   r   r   r   r   r   3   s   r   c                       s.   e Zd ZdZd fdd	Zejjje_  ZS )ArgumentParserz?Simple argparse subclass that prints help to stdout by default.Nc                    s   |d u rt j}t |S N)sysstdoutsuper
print_help)r   file	__class__r   r   r   A   s    zArgumentParser.print_help)N)r   r   r   __doc__r   argparser   __classcell__r   r   r!   r   r   >   s   r   c                 C   sF   t | d(}tt| | d|| W d    n1 s80    Y  d S )Nrbexec)openr'   compileread)fnameglobfr   r   r   execfileN   s    r.   c                   @   s|   e Zd ZdZdZe Ze Ze Zdd Z	dd Z
dd Zd	d
 Zdd Ze Zdd Zdd Zdd Zdd Zdd ZdS )LazyConfigValuea  Proxy object for exposing methods on configurable containers

    These methods allow appending/extending/updating
    to add to non-empty defaults instead of clobbering them.

    Exposes:

    - append, extend, insert on lists
    - update on dicts
    - update, add on sets
    Nc                 C   s   | j | dS )zAppend an item to a ListN)_extendappendr   objr   r   r   r1   g   s    zLazyConfigValue.appendc                 C   s   | j | dS )zExtend a listN)r0   extendr   otherr   r   r   r4   k   s    zLazyConfigValue.extendc                 C   s   || j dd< dS )z#like list.extend, but for the frontNr   )_prependr5   r   r   r   prependo   s    zLazyConfigValue.prependc                 C   sp   t |trb|j| j |j| _| j|j |j| j |j| _| jr^|| j |j| _| S | |S dS )a  
        Merge with another earlier LazyConfigValue or an earlier container.
        This is useful when having global system-wide configuration files.

        Self is expected to have higher precedence.

        Parameters
        ----------
        other : LazyConfigValue or container

        Returns
        -------
        LazyConfigValue
            if ``other`` is also lazy, a reified container otherwise.
        N)	
isinstancer/   r0   r4   r7   _inserts_updateupdate	get_valuer5   r   r   r   
merge_intos   s    
zLazyConfigValue.merge_intoc                 C   s&   t |tstd| j||f d S )NzAn integer is required)r9   int	TypeErrorr:   r1   )r   indexr6   r   r   r   insert   s    
zLazyConfigValue.insertc                 C   s4   | j du r$t|tri | _ nt | _ | j | dS )zUpdate either a set or dictN)r;   r9   dictsetr<   r5   r   r   r   r<      s
    

zLazyConfigValue.updatec                 C   s   |  |h dS )zAdd an item to a setN)r<   r2   r   r   r   add   s    zLazyConfigValue.addc                 C   s   | j dur| j S t|}t|tr\| jD ]\}}||| q*| j|dd< || j	 n:t|t
rz| jr|| j nt|tr| jr|| j || _ |S )znconstruct the value from the initial one

        after applying any insert / extend / update changes
        Nr   )_valuecopydeepcopyr9   listr:   rB   r7   r4   r0   rC   r;   r<   rD   )r   initialvalueidxr3   r   r   r   r=      s     




zLazyConfigValue.get_valuec                 C   sJ   i }| j r| j |d< | jr$| j|d< | jr6| j|d< n| jrF| j|d< |S )zreturn JSONable dict form of my data

        Currently update as dict or set, extend, prepend as lists, and inserts as list of tuples.
        r<   r4   r8   inserts)r;   r0   r7   r:   )r   dr   r   r   to_dict   s    


zLazyConfigValue.to_dictc                 C   s@   | j d ur"d| jj d| j dS d| jj d|  dS d S )N<z value=> )rF   r"   r   rO   r   r   r   r   r      s    
zLazyConfigValue.__repr__)r   r   r   r#   rF   r   r0   r7   r:   r1   r4   r8   r>   rB   r   r;   r<   rE   r=   rO   r   r   r   r   r   r/   S   s    !
r/   c                 C   s.   | r&| d   | d kr&| ds&dS dS dS )z>Is a Config key a section name (does it start with a capital)?r   _TFN)upper
startswith)keyr   r   r   _is_section_key   s    "rW   c                       s   e Zd ZdZdd Zdd Zdd Zdd	 Zd ej	e
ejf d
ddZ fddZeZdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Z  ZS )!Configa  An attribute-based dict that can do smart merges.

    Accessing a field on a config object for the first time populates the key
    with either a nested Config object for keys starting with capitals
    or :class:`.LazyConfigValue` for lowercase keys,
    allowing quick assignments such as::

        c = Config()
        c.Class.int_trait = 5
        c.Class.list_trait.append("x")

    c                 O   s$   t j| g|R i | |   d S r   )rC   __init___ensure_subconfig)r   argskwdsr   r   r   rY      s    zConfig.__init__c                 C   sB   | D ]8}| | }t |rt|trt|tst| |t| qdS )zensure that sub-dicts that should be Config objects are

        casts dicts that are under section keys to Config objects,
        which is necessary for constructing Config objects from dict literals.
        N)rW   r9   rC   rX   setattr)r   rV   r3   r   r   r   rZ      s    zConfig._ensure_subconfigc                 C   s   |  | dS )z$deprecated alias, use Config.merge()N)merger5   r   r   r   _merge   s    zConfig._mergec                 C   s   i }|  D ]h\}}|| vr&|||< qt|trNt| | trN| | | qt|trl|| | | |< q|||< q| | dS )z)merge another config object into this oneN)itemsr9   rX   r^   r/   r>   r<   )r   r6   Z	to_updatekvr   r   r   r^     s    


zConfig.merge)r6   returnc                 C   sx   i }| D ]j}||vrq| | }|| }|D ]F}||v r*|| || kr*| |i  || d|| || |< q*q|S )zCheck for collisions between two config objects.

        Returns a dict of the form {"Class": {"trait": "collision message"}}`,
        indicating which values have been ignored.

        An empty dict indicates no collisions.
        z ignored, using )
setdefault)r   r6   
collisionssectionZmineZtheirsrV   r   r   r   re     s    "zConfig.collisionsc                    s<   d|v r0| dd\}}|| vr$dS || | v S t |S )N.   F)splitr   __contains__)r   rV   first	remainderr!   r   r   rj   (  s    zConfig.__contains__c                 C   s   t |o|| v S r   )rW   r   rV   r   r   r   _has_section5  s    zConfig._has_sectionc                 C   s   t | t| S r   )typerC   rG   r   r   r   r   rG   8  s    zConfig.copyc                 C   s   |   S r   )rG   r   r   r   r   __copy__;  s    zConfig.__copy__c                 C   sb   t |  }|  D ]J\}}t|ttfr6t||}nt |ttt	t
hv rTt|}|||< q|S r   )ro   r`   r9   rX   r/   rG   rH   rC   rI   rD   tuple)r   memoZ
new_configrV   rK   r   r   r   __deepcopy__>  s    


zConfig.__deepcopy__c                 C   st   zt | |W S  tyn   t|r@t }t | || | Y S |dsft }t | || | Y S tY n0 d S )NrS   )rC   __getitem__KeyErrorrW   rX   __setitem__rU   r/   )r   rV   crb   r   r   r   rt   J  s    
zConfig.__getitem__c                 C   s4   t |r"t|ts"td||f t| || d S )NzOvalues whose keys begin with an uppercase char must be Config instances: %r, %r)rW   r9   rX   
ValueErrorrC   rv   )r   rV   rK   r   r   r   rv   Z  s    
zConfig.__setitem__c              
   C   sT   | drt| |S z| |W S  tyN } zt|W Y d }~n
d }~0 0 d S N__)rU   rC   __getattr__rt   ru   AttributeErrorr   rV   er   r   r   r{   c  s    
zConfig.__getattr__c              
   C   sZ   | drt| ||S z| || W n, tyT } zt|W Y d }~n
d }~0 0 d S ry   )rU   rC   __setattr__rv   ru   r|   )r   rV   rK   r~   r   r   r   r   k  s    
zConfig.__setattr__c              
   C   sX   | drt| |S zt| | W n, tyR } zt|W Y d }~n
d }~0 0 d S ry   )rU   rC   __delattr____delitem__ru   r|   r}   r   r   r   r   s  s    
zConfig.__delattr__)r   r   r   r#   rY   rZ   r_   r^   tr   strr   re   rj   Zhas_keyrn   rG   rp   rs   rt   rv   r{   r   r   r%   r   r   r!   r   rX      s"   	rX   c                       s(   e Zd ZdZdd Z fddZ  ZS )DeferredConfigz0Class for deferred-evaluation of config from CLIc                 C   s   t dd S )NzImplement in subclassesNotImplementedError)r   traitr   r   r   r=     s    zDeferredConfig.get_valuec                    s   t | j|  S r   )r   r"   r   r   r!   r   r   _super_repr  s    zDeferredConfig._super_repr)r   r   r   r#   r=   r   r%   r   r   r!   r   r   |  s   r   c                   @   s    e Zd ZdZdd Zdd ZdS )DeferredConfigStringa  Config value for loading config from a string

    Interpretation is deferred until it is loaded into the trait.

    Subclass of str for backward compatibility.

    This class is only used for values that are not listed
    in the configurable classes.

    When config is loaded, `trait.from_string` will be used.

    If an error is raised in `.from_string`,
    the original string is returned.

    .. versionadded:: 5.0
    c                 C   s0   t | }z||W S  ty*   | Y S 0 dS )#Get the value stored in this stringN)r   from_string	Exception)r   r   sr   r   r   r=     s
    zDeferredConfigString.get_valuec                 C   s   | j j d|   dS N()r"   r   r   r   r   r   r   r     s    zDeferredConfigString.__repr__Nr   r   r   r#   r=   r   r   r   r   r   r     s   r   c                   @   s    e Zd ZdZdd Zdd ZdS )DeferredConfigLista  Config value for loading config from a list of strings

    Interpretation is deferred until it is loaded into the trait.

    This class is only used for values that are not listed
    in the configurable classes.

    When config is loaded, `trait.from_string_list` will be used.

    If an error is raised in `.from_string_list`,
    the original string list is returned.

    .. versionadded:: 5.0
    c                 C   s|   t |drt| }|j}n<t| dkrHt|j dt|  dt|  | d }|j}z
||W S  tyv   | Y S 0 dS )r   from_string_listrh   z only accepts one value, got z: r   N)hasattrrI   r   lenrx   namer   r   )r   r   srccastr   r   r   r=     s    

zDeferredConfigList.get_valuec                 C   s   | j j d|   dS r   r   r   r   r   r   r     s    zDeferredConfigList.__repr__Nr   r   r   r   r   r     s   r   c                   @   s2   e Zd ZdZdd ZdddZdd Zd	d
 ZdS )ConfigLoadera1  A object for loading configurations from just about anywhere.

    The resulting configuration is packaged as a :class:`Config`.

    Notes
    -----
    A :class:`ConfigLoader` does one thing: load a config from a source
    (file, command line arguments) and returns the data as a :class:`Config` object.
    There are lots of things that :class:`ConfigLoader` does not do.  It does
    not implement complex logic for finding config files.  It does not handle
    default values or merge multiple configs.  These things need to be
    handled elsewhere.
    c                 C   s   ddl m} | S )Nr   )
get_logger)Ztraitlets.logr   )r   r   r   r   r   _log_default  s    zConfigLoader._log_defaultNc                 C   s2   |    |du r(|  | _| jd n|| _dS )ae  A base class for config loaders.

        log : instance of :class:`logging.Logger` to use.
              By default logger of :meth:`traitlets.config.application.Application.instance()`
              will be used

        Examples
        --------
        >>> cl = ConfigLoader()
        >>> config = cl.load_config()
        >>> config
        {}
        NzUsing default logger)clearr   logdebug)r   r   r   r   r   rY     s
    
zConfigLoader.__init__c                 C   s   t  | _d S r   )rX   configr   r   r   r   r     s    zConfigLoader.clearc                 C   s   |    | jS )a  Load a config from somewhere, return a :class:`Config` instance.

        Usually, this will cause self.config to be set and then returned.
        However, in most cases, :meth:`ConfigLoader.clear` should be called
        to erase any previous state.
        )r   r   r   r   r   r   load_config  s    zConfigLoader.load_config)N)r   r   r   r#   r   rY   r   r   r   r   r   r   r     s
   
r   c                       s*   e Zd ZdZd fdd	Zdd Z  ZS )FileConfigLoaderzA base class for file based configurations.

    As we add more file based config loaders, the common logic should go
    here.
    Nc                    s(   t  jf i | || _|| _d| _dS )a.  Build a config loader for a filename and path.

        Parameters
        ----------
        filename : str
            The file name of the config file.
        path : str, list, tuple
            The path to search for the config file on, or a sequence of
            paths to try in order.
         N)r   rY   filenamepathfull_filename)r   r   r   kwr!   r   r   rY     s    zFileConfigLoader.__init__c                 C   s   t | j| j| _dS )z,Try to find the file by searching the paths.N)r
   r   r   r   r   r   r   r   
_find_file'  s    zFileConfigLoader._find_file)N)r   r   r   r#   rY   r   r%   r   r   r!   r   r     s   r   c                   @   s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )JSONFileConfigLoadera  A JSON file loader for config

    Can also act as a context manager that rewrite the configuration file to disk on exit.

    Example::

        with JSONFileConfigLoader('myapp.json','/home/jupyter/configurations/') as c:
            c.MyNewConfigurable.new_value = 'Updated'

    c              
   C   s`   |    z|   W n0 tyD } ztt|W Y d}~n
d}~0 0 |  }| || _| jS z=Load the config from a file and return it as a Config object.N)r   r   OSErrorr   r   _read_file_as_dict_convert_to_configr   )r   r~   dctr   r   r   r   8  s    "z JSONFileConfigLoader.load_configc                 C   s8   t | j}t|W  d    S 1 s*0    Y  d S r   )r(   r   jsonload)r   r-   r   r   r   r   C  s    z'JSONFileConfigLoader._read_file_as_dictc                 C   s:   d|v r| d}nd}|dkr(t|S td| d S )Nversionrh   z%Unknown version of JSON config file: )poprX   rx   )r   
dictionaryr   r   r   r   r   G  s    z'JSONFileConfigLoader._convert_to_configc                 C   s   |    | jS r   )r   r   r   r   r   r   	__enter__R  s    zJSONFileConfigLoader.__enter__c                 C   sR   d| j _tj| j dd}t| jd}|| W d   n1 sD0    Y  dS )z
        Exit the context manager but do not handle any errors.

        In case of any error, we do not want to write the potentially broken
        configuration to disk.
        rh   r   )indentwN)r   r   r   dumpsr(   r   write)r   exc_type	exc_value	tracebackZjson_configr-   r   r   r   __exit__V  s    zJSONFileConfigLoader.__exit__N)	r   r   r   r#   r   r   r   r   r   r   r   r   r   r   ,  s   r   c                   @   s*   e Zd ZdZdd Zd	ddZdd ZdS )
PyFileConfigLoaderzA config loader for pure python files.

    This is responsible for locating a Python config file by filename and
    path, then executing it to construct a Config object.
    c              
   C   sT   |    z|   W n0 tyD } ztt|W Y d}~n
d}~0 0 |   | jS r   )r   r   r   r   r   r   r   )r   r~   r   r   r   r   j  s    "zPyFileConfigLoader.load_configNc                 C   sJ   |du r| j }| ||}z| }W n ty8   Y n0 | j| dS )z5Injected into config file namespace as load_subconfigN)r   r"   r   r   r   r^   )r   r+   r   loaderZ
sub_configr   r   r   load_subconfigt  s    z!PyFileConfigLoader.load_subconfigc                    sn    fdd}t  j j| jd} j}t|d(}tt| |d|| W d   n1 s`0    Y  dS )z>Load the config file into self.config, with recursive loading.c                      s    j S )zKUnnecessary now, but a deprecation warning is more trouble than it's worth.r   r   r   r   r   
get_config  s    z9PyFileConfigLoader._read_file_as_dict.<locals>.get_config)rw   r   r   __file__r&   r'   N)rC   r   r   r   r(   r'   r)   r*   )r   r   	namespaceZconf_filenamer-   r   r   r   r     s    z%PyFileConfigLoader._read_file_as_dict)N)r   r   r   r#   r   r   r   r   r   r   r   r   c  s   

r   c                   @   s"   e Zd ZdZdddZdd ZdS )CommandLineConfigLoaderzA config loader for command line arguments.

    As we add more command line based loaders, the common logic should go
    here.
    Nc           	      C   s   |}t |trJ|r||}qbt |trbt|dkrbttj|d }n|rZ|	|}nt|}|
d^ }}| j}|D ]}|| }q||||< dS )zexecute self.config.<lhs> = <rhs>

        * expands ~ with expanduser
        * interprets value with trait if available
        rh   r   rg   N)r9   r   r=   r   r   r   osr   
expanduserr   ri   r   )	r   lhsrhsr   rK   r   rV   rf   partr   r   r   _exec_config_str  s    

z(CommandLineConfigLoader._exec_config_strc                 C   sB   t |ttfr2| D ]\}}| j| | qntd| dS )z=update self.config from a flag, which can be a dict or ConfigzInvalid flag: %rN)r9   rC   rX   r`   r   r<   r@   )r   cfgsecrw   r   r   r   
_load_flag  s    z"CommandLineConfigLoader._load_flag)N)r   r   r   r#   r   r   r   r   r   r   r     s   
r   z^\-?\-[A-Za-z][\w]*(\.[\w]+)*$Z__DOT__Z__DASH__c                   @   s   e Zd ZdZdddZdS )	_KVActionzDCustom argparse action for handling --Class.trait=x

    Always
    Nc                 C   s`   t |tr|g}dd |D }t|| jd }|d u r<t }nt|}|| t|| j| d S )Nc                 S   s   g | ]}|t u rd n|qS -_DASH_REPLACEMENT).0rb   r   r   r   
<listcomp>      z&_KVAction.__call__.<locals>.<listcomp>)r9   r   getattrdestr   r4   r]   )r   parserr   valuesoption_stringr`   r   r   r   __call__  s    

z_KVAction.__call__)N)r   r   r   r#   r   r   r   r   r   r     s   r   c                       s>   e Zd ZdZdd Z fddZ fddZdd	d
Z  ZS )_DefaultOptionDictz_Like the default options dict

    but acts as if all --Class.trait options are predefined
    c                 C   s,   t |g|ddt|dd| |< d S )Nr   rg   )option_stringsr   metavar)r   lstripreplace_DOT_REPLACEMENTrm   r   r   r   _add_kv_action  s
    z!_DefaultOptionDict._add_kv_actionc                    sB   d|v rdS t  |rdS |dr>t|r>| | dS dS )N=FTr   )r   rj   rU   class_trait_opt_patternmatchr   rm   r!   r   r   rj     s    
z_DefaultOptionDict.__contains__c                    s    || v rt  |S t|d S r   )r   rt   ru   rm   r!   r   r   rt     s    z_DefaultOptionDict.__getitem__Nc                 C   s&   z
| | W S  t y    | Y S 0 d S r   )ru   )r   rV   defaultr   r   r   get   s    
z_DefaultOptionDict.get)N)	r   r   r   r#   r   rj   rt   r   r%   r   r   r!   r   r     s
   r   c                       s"   e Zd ZdZd fdd	Z  ZS )_KVArgParserzOsubclass of ArgumentParser where any --Class.trait option is implicitly definedNc                    s*   | | j fD ]}t|j|_q
t ||S r   )
_optionalsr   _option_string_actionsr   parse_known_args)r   r[   r   	containerr!   r   r   r   
  s    z_KVArgParser.parse_known_args)NN)r   r   r   r#   r   r%   r   r   r!   r   r     s   r   c                
       s   e Zd ZdZeZejeej	edf f Z
dejeje  ejeje
ef  ejeje
ef  ejejejejej   ejejdd fddZddedfddZd	d
 Zdd Zdd Zdd Zdd Z  ZS )ArgParseConfigLoaderzEA loader that uses the argparse module to load from the command line..N)argvaliasesflagsr   classesparser_args	parser_kwrc   c           	         s   |pg }t t| j|d |   |du r8tjdd }|| _|pDi | _|pNi | _|| _|| _	|
dd| _ttjd}|| || _dS )a>  Create a config loader for use with argparse.

        Parameters
        ----------
        classes : optional, list
            The classes to scan for *container* config-traits and decide
            for their "multiplicity" when adding them as *argparse* arguments.
        argv : optional, list
            If given, used to read command-line arguments from, otherwise
            sys.argv[1:] is used.
        *parser_args : tuple
            A tuple of positional arguments that will be passed to the
            constructor of :class:`argparse.ArgumentParser`.
        **parser_kw : dict
            A tuple of keyword arguments that will be passed to the
            constructor of :class:`argparse.ArgumentParser`.
        aliases : dict of str to str
            Dict of aliases to full traitlets names for CLI parsing
        flags : dict of str to str
            Dict of flags to full traitlets names for CLI parsing
        log
            Passed to `ConfigLoader`

        Returns
        -------
        config : Config
            The resulting Config object.
        )r   Nrh   r   )argument_default)r   r   rY   r   r   r   r   r   r   r   r   r   rC   r$   SUPPRESSr<   r   )	r   r   r   r   r   r   r   r   kwargsr!   r   r   rY     s    &


zArgParseConfigLoader.__init__c                 C   sv   |t ur$tjdt|  dtdd |   |du r:| j}|durH|| _|durV|| _| 	  | 
| |   | jS )a  Parse command line arguments and return as a Config object.

        Parameters
        ----------
        argv : optional, list
            If given, a list with the structure of sys.argv[1:] to parse
            arguments from. If not given, the instance's self.argv attribute
            (given at construction time) is used.
        flags
            Deprecated in traitlets 5.0, instanciate the config loader with the flags.

        zjThe `flag` argument to load_config is deprecated since Traitlets 5.0 and will be ignored, pass flags the `z` constructor.r   
stacklevelN)_deprecatedwarningswarnro   DeprecationWarningr   r   r   r   _create_parser_parse_argsr   r   )r   r   r   r   r   r   r   r   r   N  s&    
z ArgParseConfigLoader.load_configc                 C   s   t | dr| jS g S d S )N
extra_args)r   r   r   r   r   r   get_extra_argsp  s    
z#ArgParseConfigLoader.get_extra_argsc                 C   s.   | j | ji | j| _| | j| j| j d S r   )parser_classr   r   r   _add_argumentsr   r   r   r   r   r   r   r   v  s    z#ArgParseConfigLoader._create_parserc                 C   s   t dd S )Nz(subclasses must implement _add_argumentsr   )r   r   r   r   r   r   r   r  |  s    z#ArgParseConfigLoader._add_argumentsc           	         s   dd |D }i | j ri | j  D ]Z\}}|| jv r:q&t|tsJ|f}|D ]0}t|dkrnd| d| < d| d| < qNq&fdd d|v r|d}||d d }|d| }ng }|} fd	d|D }| j|| _	|| _
dS )
zself.parser->self.parsed_datac                 S   s   g | ]}t |qS r   )r	   r   ar   r   r   r     r   z4ArgParseConfigLoader._parse_args.<locals>.<listcomp>rh   --r   c                    s`   | dkrt S   D ]F\}}| |kr,|  S | |d r|d | t|d d     S q| S )Nr   r   rh   )r   r`   rU   r   )argra   rb   )unpacked_aliasesr   r   _replace  s    "z2ArgParseConfigLoader._parse_args.<locals>._replaceNc                    s   g | ]} |qS r   r   r  )r  r   r   r     r   )r   r`   r   r9   rq   r   rA   r   
parse_argsparsed_datar   )	r   r[   ZuargsaliasZalias_targetalrL   r   Zto_parser   )r  r  r   r     s.    



z ArgParseConfigLoader._parse_argsc                 C   sP   t | j D ]<\}}|d^ }}| j}|D ]}|| }q0t||| qdS )zself.parsed_data->self.configrg   N)varsr
  r`   ri   r   r]   )r   ra   rb   r   rV   rf   pr   r   r   r     s    
z'ArgParseConfigLoader._convert_to_config)NNNNN)r   r   r   r#   r   r  r   Unionr   TupleFlagsOptionalr   r   r   TyperY   r   r   r   r   r  r   r   r%   r   r   r!   r   r     s0        6"'r   c                       s*   e Zd ZdZ fddZdddZ  ZS )_FlagActionz ArgParse action to handle a flagc                    sF   | d| _| dd | _t|d< | js0d|d< t j|i | d S )Nflagr  constr   nargs)r   r  r  r   r   rY   r   r[   r   r!   r   r   rY     s    z_FlagAction.__init__Nc                 C   sD   | j dks|tu r2t|ds"g |_|j| j nt|| j| d S )Nr   _flags)r  r   r   r  r1   r  r]   r  )r   r   r   r   r   r   r   r   r     s
    
z_FlagAction.__call__)N)r   r   r   r#   rY   r   r%   r   r   r!   r   r    s   r  c                   @   s$   e Zd ZdZeZdd Zdd ZdS )KVArgParseConfigLoaderznA config loader that loads aliases and flags with argparse,

    as well as arbitrary --Class.trait value
    c                 C   s  i }| j j}| j jg d |ddd i  | _}|D ]x}|jdd D ]b\}}	|j d| }
dti}t|	t	t
fr|	jd	d
}|d
kr||d< n||d< |	|f||
< qHq4| D ]t\}\}}t|ts|f}|D ]R}||v r|||| < qt|dkrd| d| fnd| f}||t|d qq| D ]\}}t|tsN|f}|D ]}t|dt|d}||v r||| d  d|v r||v rtd||f ||v r|dd t|d< || |d< ||d< t|dkrd| d| fnd| f}||i | qRq4d S )N)r  r   *)r  Tr   rg   ro   multiplicityr1   actionr  rh   r   r  )r  r  )ro   r   r   zRThe alias `%s` for the 'append' sequence config-trait `%s` cannot be also a flag!'?r  r  )r   add_argumentset_defaultsargparse_traitsZclass_traitsr`   r   r   r9   r   r   metadatar   rq   r   r  r   r   r<   r   rd   )r   r   r   r   Zalias_flagsZpaar!  clsZ	traitnamer   argnameZargparse_kwdsr  keysrK   rS   rV   r   r   r   r    s^    


(


(z%KVArgParseConfigLoader._add_argumentsc                 C   s0  | j }t| j D ]\}}|dkr<dd |D | | _ qn
|dkrFq|td}d|vrl| jd| d}t|t	rt
|}nt|trt|}| j|}|r|d }z| ||| W q ty } z8t|tr| }td	| d
| d| W Y d}~qd}~0 0 q| jjD ]}| | qdS )zJself.parsed_data->self.config, parse unrecognized extra args via KVLoader.r   c                 S   s   g | ]}|t krd n|qS r   r   r  r   r   r   r     r   z=KVArgParseConfigLoader._convert_to_config.<locals>.<listcomp>r  rg   z1Unrecognized alias: '%s', it will have no effect.Nr   zError loading argument r   z, )r   r  r
  r`   r   r   r   warningr9   rI   r   r   r   r!  r   r   r   r   r   r   r  r   )r   r   r   r   r   r~   Zsubcr   r   r   r     s4    



2z)KVArgParseConfigLoader._convert_to_configN)r   r   r   r#   r   r  r  r   r   r   r   r   r    s   Br  c                       s    e Zd ZdZ fddZ  ZS )KeyValueConfigLoaderz@Deprecated in traitlets 5.0

    Use KVArgParseConfigLoader
    c                    s&   t jdtdd t j|i | d S )Nz[KeyValueConfigLoader is deprecated since Traitlets 5.0. Use KVArgParseConfigLoader instead.r   r   )r   r   r   r   rY   r  r!   r   r   rY   =  s    zKeyValueConfigLoader.__init__)r   r   r   r#   rY   r%   r   r   r!   r   r'  7  s   r'  c              	   C   s\   t  }| D ]L}t||d}z| }W n$ ty8   Y q
 tyJ    Y q
0 || q
|S )a  Load multiple Python config files, merging each of them in turn.

    Parameters
    ----------
    config_files : list of str
        List of config files names to load and merge into the config.
    path : unicode
        The full path to the location of the config files.
    )r   )rX   r   r   r   r   r^   )config_filesr   r   cfr   Znext_configr   r   r   load_pyconfig_filesG  s    
r*  )8r#   r$   rG   r   r   rer   typingr   r   Ztraitlets.traitletsr   r   r   r   r   r   utilsr	   r
   r   r   r   r   r   r   r   r   r.   r/   rW   rC   rX   r   r   r   rI   r   r   r   r   r   r   r)   r   r   r   Actionr   r   r   r   r  r  r'  r*  r   r   r   r   <module>   sV      !/7723
& s