3
d}                 @   s   d Z ddlm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	 ddl
mZ ddlmZ ddlmZ ddlmZ ejeZd	d
 ZG dd de	Zdd Zdd Zdd ZdS )zP
    A Trait Documenter
    (Subclassed from the autodoc ClassLevelDocumenter)

    )import_moduleN)ClassLevelDocumenter)logging)MetaHasTraits)	TraitType)generic_traitc             C   s"   t |to | |jko |j|  tk	S )zJ Check if the name is in the list of class defined traits of ``cls``.
    )
isinstancer   Z__class_traits__r   )namecls r   >/tmp/pip-build-7vycvbft/traits/traits/util/trait_documenter.py_is_class_trait"   s    

r   c               @   sF   e Zd ZdZdZdZdZdZedd Z	dd	d
Z
dd Zdd ZdS )TraitDocumentera    Specialized Documenter subclass for trait attributes.

    The class defines a new documenter that recovers the trait definition
    signature of module level and class level traits.

    To use the documenter, append the module path in the extension
    attribute of the `conf.py`.
    Ztraitattribute	attribute<      c             C   s"   |rt t|tpt||j}|S )z? Check that the documented member is a trait instance.
        )
issubclasstyper   r   object)r
   memberZ
membernameZisattrparentcheckr   r   r   can_document_member?   s    z#TraitDocumenter.can_document_memberFc             C   s   dS )z" Trait attributes have no members Nr   )selfZall_membersr   r   r   document_membersJ   s    z TraitDocumenter.document_membersc             C   s   yTt | j }| _x"| jdd D ]}| j||}q"W | jd }|| _d| _|| _dS  tk
r } zZ| j	j
r| j	j
j r| j	j
jtj j  d}| jj|j| jt| j| | j	j  dS d}~X nX dS )zy Get the Trait object.

        Notes
        -----
        Code adapted from autodoc.Documenter.import_object.

        N   Tzeautodoc can't import/find {0} {r1}, it reported error: "{2}", please check your spelling and sys.pathFr   )r   modnamemoduleZobjpathZget_attrobject_namer   r   	Exceptionenvappquietinfo	traceback
format_excrstripZ	directivewarnformatobjtypestrfullnameZnote_reread)r   currentpartr	   errmsgr   r   r   import_objectN   s"    

zTraitDocumenter.import_objectc             C   s   t j| | yt| j| jd}W n. tk
rN   tjdj| j| jdd dS X d|krj|j	dd d }| j
d	j|d
 dS )zo Add the directive header 'attribute' with the annotation
        option set to the trait definition.

        )r
   
trait_namez>No definition for the trait {!r} could be found in class {!r}.T)exc_infoN
r   u    …z   :annotation: = {0}z	<autodoc>)r   add_directive_headertrait_definitionr   r   
ValueErrorloggerwarningr)   	partitionZadd_line)r   sig
definitionr   r   r   r5   o   s    
z$TraitDocumenter.add_directive_headerN)F)__name__
__module____qualname____doc__r*   ZdirectivetypeZmember_orderpriorityclassmethodr   r   r1   r5   r   r   r   r   r   ,   s   
!r   c       
      C   s   t j| }tj|}tj|j}d}d}xV|s~t|d}|dkrBP |rb|dd tj	dfkrbd}q*|dd tj
|fkr*d}q*W |stdj|| t|}tj|j }	|	S )a   Retrieve the portion of the source defining a Trait attribute.

    For example, given a class::

        class MyModel(HasStrictTraits)
            foo = List(Int, [1, 2, 3])

    ``trait_definition(cls=MyModel, trait_name="foo")`` returns
    ``"List(Int, [1, 2, 3])"``.

    Parameters
    ----------
    cls : MetaHasTraits
        Class being documented.
    trait_name : str
        Name of the trait being documented.

    Returns
    -------
    str
        The portion of the source containing the trait definition. For
        example, for a class trait defined as ``"my_trait = Float(3.5)"``,
        the returned string will contain ``"Float(3.5)"``.

    Raises
    ------
    ValueError
        If *trait_name* doesn't appear as a class-level variable in the
        source.
    FN   =Tz*No trait definition for {!r} found in {!r})inspect	getsourceioStringIOtokenizegenerate_tokensreadlinenexttokenOPNAMEr7   r)   _get_definition_tokens
untokenizestrip)
r
   r2   sourceZ	string_iotokensZtrait_foundZ
name_founditemdefinition_tokensr<   r   r   r   r6      s*     



r6   c       	      C   s   g }d}xr| D ]j\}}}}}|dkr,|d }|t jkr8P |||d | d |d f|d | d |d f|f}|j| qW |S )z Given the tokens, extracts the definition tokens.

    Parameters
    ----------
    tokens : iterator
        An iterator producing tokens.

    Returns
    -------
    A list of tokens for the definition.
    Nr   r   )rM   NEWLINEappend)	rT   rV   
first_liner   r	   startstopZ	line_textrU   r   r   r   rP      s    
rP   c             C   s   | j t dS )zA Add the TraitDocumenter in the current sphinx autodoc instance. N)Zadd_autodocumenterr   )r"   r   r   r   setup   s    r\   )r@   	importlibr   rE   rG   rM   rI   r%   Zsphinx.ext.autodocr   Zsphinx.utilr   Ztraits.has_traitsr   Ztraits.trait_typer   Ztraits.traitsr   	getLoggerr=   r8   r   r   r6   rP   r\   r   r   r   r   <module>   s"   

_=$