
    Ibn6                         d dl Z d dlmZmZmZ 	 d dlmZ  G d de      Z G d d	e      Zy# e$ r d dl Z d dlZd dl	Z	d
dZddZ
Y 2w xY w)    N)ClassBasedTraitType	Undefinedwarn)describec                    t        | t              r| j                         } t        j                  |      st        |      j                  }n|j                  }|rt        |      |z   }| dk(  s| 8t        j                  |      s"|| d| }| t        |d|      S |S d}t        j                  |      r|j                  }nt        |t        j                        r|j                  }d}nt        |t        j                        r|j                  j                  }d}nUt        |      j                  t        j                  t
        j                  fv rdt        |      dd}d}nt!        |      }d}|rt        |      |z   }|r|j#                  d      }t%        | ||||	      S | d
v s| | |S t        |d|      S t'        d|       )a  Return string that describes a value
        Parameters
        ----------
        article : str or None
            A definite or indefinite article. If the article is
            indefinite (i.e. "a" or "an") the appropriate one
            will be infered. Thus, the arguments of ``describe``
            can themselves represent what the resulting string
            will actually look like. If None, then no article
            will be prepended to the result. For non-articled
            description, values that are instances are treated
            definitely, while classes are handled indefinitely.
        value : any
            The value which will be named.
        name : str or None (default: None)
            Only applies when ``article`` is "the" - this
            ``name`` is a definite reference to the value.
            By default one will be infered from the value's
            type and repr methods.
        verbose : bool (default: False)
            Whether the name should be concise or verbose. When
            possible, verbose names include the module, and/or
            class name where an object was defined.
        capital : bool (default: False)
            Whether the first letter of the article should
            be capitalized or not. By default it is not.
        Examples
        --------
        Indefinite description:
        >>> describe("a", object())
        'an object'
        >>> describe("a", object)
        'an object'
        >>> describe("a", type(object))
        'a type'
        Definite description:
        >>> describe("the", object())
        "the object at '0x10741f1b0'"
        >>> describe("the", object)
        "the type 'object'"
        >>> describe("the", type(object))
        "the type 'type'"
        Definitely named description:
        >>> describe("the", object(), "I made")
        'the object I made'
        >>> describe("the", object, "I will use")
        'the object I will use'
        the TFzat 'x'z'')nameverbosecapital)aanz?The 'article' argument should be 'the', 'a', 'an', or None not )
isinstancestrlowerinspectisclasstype__name___prefixadd_articletypesFunctionType
MethodType__func____repr__objectidreprjoinr   
ValueError)articlevaluer   r   r   typenameresult	tick_wraps           5/usr/lib/python3/dist-packages/notebook/traittypes.pyr   r      s   b gs#mmoGu%E{++H~~Hu~0He8N$:Qtf-&&vtW==!M!	??5) >>Du'9'9: >>D $Iu'7'78 >>22D $I%[))foot}}-MM!"U)Aa0D#G;D#G"5>D0D99T?DT#W6 6#wx88QRYQ\]     c                     |rd| z   }nEt        j                  d      j                  d|       }|dd j                         dv rd| z   }nd| z   }|r|d	   j	                         |dd z   S |S )
a  Returns the string with a prepended article.
        The input does not need to begin with a charater.
        Parameters
        ----------
        definite : bool (default: False)
            Whether the article is definite or not.
            Indefinite articles being 'a' and 'an',
            while 'the' is definite.
        capital : bool (default: False)
            Whether the added article should have
            its first letter capitalized or not.
        zthe z[\W_]+ N   aeiouzan za r   )recompilesubr   upper)r   definiter   r'   first_letterss        r)   r   r   s   sy     d]FJJy155b$?MRa &&(G3!9??$vabz11Mr*   c                   P     e Zd ZdZedf fd	Zd Zd Zd Z fdZ	d Z
d	 Z xZS )
TypeFromClasseszQA trait whose value must be a subclass of a class in a specified list of classes.Nc                 H   |t         u r|t        n|}n|}|||t         u rt        g}n|g}t        |t        t        t
        f      st        d      |D ]3  }t        j                  |      rt        |t              r*t        d       || _
        t        | 0  |fi | y)a0  Construct a Type trait
        A Type trait specifies that its values must be subclasses of
        a class in a list of possible classes.
        If only ``default_value`` is given, it is used for the ``klasses`` as
        well. If neither are given, both default to ``object``.
        Parameters
        ----------
        default_value : class, str or None
            The default value must be a subclass of klass.  If an str,
            the str must be a fully specified class name, like 'foo.bar.Bah'.
            The string is resolved into real class, when the parent
            :class:`HasTraits` class is instantiated.
        klasses : list of class, str [ default object ]
            Values of this trait must be a subclass of klass.  The klass
            may be specified in a string like: 'foo.bar.MyClass'.
            The string is resolved into real class, when the parent
            :class:`HasTraits` class is instantiated.
        allow_none : bool [ default False ]
            Indicates whether None is allowed as an assignable value.
        NzA`klasses` must be a list of class names (type is str) or classes.z1A OneOfType trait must specify a list of classes.)r   r   r   listtupleset
TraitErrorr   r   r   klassessuper__init__)selfdefault_valuer<   kwargsnew_default_valueklass	__class__s         r)   r>   zTypeFromClasses.__init__   s    * I%+2? -?%=I+E!((/ 'D%#56`aa 	VEOOE*j.D !TUU	V
 *5f5r*   c                 @    t        fd| j                  D              S )CCheck that a given class is a subclasses found in the klasses list.c              3   6   K   | ]  }t        |        y wN)
issubclass.0rC   r%   s     r)   	<genexpr>z8TypeFromClasses.subclass_from_klasses.<locals>.<genexpr>        Q:eU+Q   anyimportable_klassesr?   r%   s    `r)   subclass_from_klassesz%TypeFromClasses.subclass_from_klasses       Q9P9PQQQr*   c           
         t        |t              r	 | j                  |      }	 | j                  |      r|S 	 | j                  ||       y# t        $ r  t	        d| j
                   d| d|d      w xY w# t        $ r Y Jw xY w)z4Validates that the value is a valid object instance.zThe 'z' trait of z instance must be a type, but z could not be importedN)	r   r   _resolve_stringImportErrorr;   r   rS   	Exceptionerrorr?   objr%   s      r)   validatezTypeFromClasses.validate   s    eS!,,U3	))%0 1
 	

3   DII;k# 7 )#9;   		s   A A7 )A47	BBc                     d}| j                   D ]6  }t        |t              s|j                  dz   |j                  z   }|| dz  }8 |j                  d      }| j                  r|dz   S |S )z#Returns a description of the trait.za subclass of . or  or None)r<   r   r   
__module__r   strip
allow_noner?   r'   rC   s      r)   infozTypeFromClasses.info   st    !\\ 	%EeS)((3.?tn$F	%
 f%??J&&r*   c                 D    | j                          t        | 	  |       y rH   _resolve_classesr=   instance_initr?   r[   rD   s     r)   ri   zTypeFromClasses.instance_init       c"r*   c                    g | _         | j                  D ][  }t        |t              r.	 | j	                  |      }| j                   j                  |       A| j                   j                  |       ] t        | j                  t              r!| j	                  | j                        | _        y y #  t        | dt               Y xY wNz$ is not importable. Is it installed?)	rQ   r<   r   r   rV   appendr   ImportWarningr@   r?   rC   s     r)   rh   z TypeFromClasses._resolve_classes   s    "$\\ 	6E%%X 007E++2259 ''..u5	6 d((#.!%!5!5d6H6H!ID /XE7"FGWs   ,B..Cc                     | j                   }t        |t              rt        |      S t        |j                   d|j
                         S )Nr^   )r@   r   r   r!   ra   r   rR   s     r)   default_value_reprz"TypeFromClasses.default_value_repr   sC    ""eS!;5++,Aenn-=>??r*   )r   ra   __qualname____doc__r   r>   rS   r\   re   ri   rh   rr   __classcell__rD   s   @r)   r6   r6      s2    [%. +6ZR$#J @r*   r6   c                   X     e Zd ZdZd fd	Zd Zd Zd Z fdZd Z	d Z
d	 Zd
 Z xZS )InstanceFromClasseszA trait whose value must be an instance of a class in a specified list of classes.
    The value can also be an instance of a subclass of the specified classes.
    Subclasses can declare default classes by overriding the klass attribute
    c                     ||| _         n,t        d |D              r|| _         nt        dt              |t	        |t
              st        d      |t	        |t              st        d      || _        || _        t        | (  di | y)a  Construct an Instance trait.
        This trait allows values that are instances of a particular
        class or its subclasses.  Our implementation is quite different
        from that of enthough.traits as we don't allow instances to be used
        for klass and we handle the ``args`` and ``kw`` arguments differently.
        Parameters
        ----------
        klasses : list of classes or class_names (str)
            The class that forms the basis for the trait.  Class names
            can also be specified as strings, like 'foo.bar.Bar'.
        args : tuple
            Positional arguments for generating the default value.
        kw : dict
            Keyword arguments for generating the default value.
        allow_none : bool [ default False ]
            Indicates whether None is allowed as a value.
        Notes
        -----
        If both ``args`` and ``kw`` are None, then the default value is None.
        If ``args`` is a tuple and ``kw`` is a dict, then the default is
        created as ``klass(*args, **kw)``.  If exactly one of ``args`` or ``kw`` is
        None, the None is replaced by ``()`` or ``{}``, respectively.
        Nc              3   j   K   | ]+  }t        j                  |      xs t        |t               - y wrH   )r   r   r   r   )rK   ks     r)   rL   z/InstanceFromClasses.__init__.<locals>.<genexpr>  s(     Ka#9z!S'99Ks   13zDThe klasses attribute must be a list of class names or classes not: z)The 'kw' argument must be a dict or None.z,The 'args' argument must be a tuple or None. )r<   allr;   rC   r   dictr9   default_argsdefault_kwargsr=   r>   )r?   r<   argskwrA   rD   s        r)   r>   zInstanceFromClasses.__init__  s    2 ?"DLK7KK"DLVW\V_`  NJr4$8HIIju&=KLL  "6"r*   c                 @    t        fd| j                  D              S )rF   c              3   6   K   | ]  }t        |        y wrH   )r   rJ   s     r)   rL   zGInstanceFromClasses.instance_from_importable_klasses.<locals>.<genexpr>1  rM   rN   rO   rR   s    `r)    instance_from_importable_klassesz4InstanceFromClasses.instance_from_importable_klasses/  rT   r*   c                 N    | j                  |      r|S | j                  ||       y rH   )r   rY   rZ   s      r)   r\   zInstanceFromClasses.validate3  s#    007LJJsE"r*   c                     d}| j                   D ],  }t        |t              r||z  }n|t        d|      z  }|dz  }. |j	                  d      }| j
                  r|dz  }|S )Nzan instance of r   r_   r`   )r<   r   r   r   rb   rc   rd   s      r)   re   zInstanceFromClasses.info9  sn    "\\ 	E%%%(3..fF	 f%??j Fr*   c                 D    | j                          t        | 	  |       y rH   rg   rj   s     r)   ri   z!InstanceFromClasses.instance_initF  rk   r*   c                    g | _         | j                  D ][  }t        |t              r.	 | j	                  |      }| j                   j                  |       A| j                   j                  |       ] y #  t        | dt               Y vxY wrm   )rQ   r<   r   r   rV   rn   r   ro   rp   s     r)   rh   z$InstanceFromClasses._resolve_classesJ  s    "$\\ 	6E%%X 007E++2259 ''..u5	6
XE7"FGWs   ,A33B
c                     | j                   | j                  y  | j                  | j                   xs di | j                  xs i S )Nr|   )r   r   rC   r?   s    r)   make_dynamic_defaultz(InstanceFromClasses.make_dynamic_defaultW  sO    %D,?,?,GtzzD--3 9!006B9 	9r*   c                 4    t        | j                               S rH   )r!   r   r   s    r)   rr   z&InstanceFromClasses.default_value_repr]  s    D--/00r*   c                     t        |      S rH   )_safe_literal_eval)r?   ss     r)   from_stringzInstanceFromClasses.from_string`  s    !!$$r*   )NNN)r   ra   rs   rt   r>   r   r\   re   ri   rh   r   rr   r   ru   rv   s   @r)   rx   rx      s7    +#ZR##691%r*   rx   )NFF)FF)r   	traitletsr   r   r   traitlets.utils.descriptionsr   rW   r/   r   r   r6   rx   r|   r*   r)   <module>r      sc     : :A5Fl@) l@^d%- d%c  _DOs   - A	A	