a
    ¦ˆc}  ã                   @   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 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v o |j|  tuS )zJ Check if the name is in the list of class defined traits of ``cls``.
    )Ú
isinstancer   Z__class_traits__r   )ÚnameÚcls© r   ú_/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/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Ò   zRt | jƒ }| _| jdd… D ]}|  ||¡}q | jd }|| _d| _|| _W dS  tyÌ } zb| j	j
rŠ| j	j
jsŠ| j	j
 t ¡  ¡ ¡ d}| j | | jt| jƒ|¡¡ | j	 ¡  W Y d}~dS d}~0 0 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.pathF)r   ÚmodnameÚmoduleZobjpathZget_attrÚobject_namer   r   Ú	ExceptionÚenvÚappÚquietÚinfoÚ	tracebackÚ
format_excÚrstripÚ	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  | |¡ zt| j| jd�}W n. tyN   tjd | j| j¡dd� Y dS 0 d|v rj| 	d¡d d }|  
d	 |¡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  | ¡}t |¡}t |j¡}d}d}|s|t|dƒ}|du r@q||r`|dd… tj	dfkr`d}q(|dd… tj
|fkr(d}q(|s�td || ¡ƒ‚t|ƒ}t |¡ ¡ }	|	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}| D ]l\}}}}}|du r*|d }|t jkr8 qz|||d | d |d f|d | d |d f|f}| |¡ q|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   é   )rM   ÚNEWLINEÚappend)	rT   rV   Ú
first_liner   r	   ÚstartÚstopZ	line_textrU   r   r   r   rP   È   s    
ûrP   c                 C   s   |   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"   

_=$