B
    lßU\þ  ã               @   sJ   d Z ddlmZ ddlmZ ddlmZmZ G dd„ deƒZdd	d
„Z	dS )z½
Output various aspects of topic tree to string or file.

:copyright: Copyright since 2006 by Oliver Schoenborn, all rights reserved.
:license: BSD, see LICENSE_BSD_Simple.txt for details.
é    )ÚTextIO)ÚTextWrapperé   )ÚITopicTreeVisitorÚTopicTreeTraverserc               @   sŠ   e Zd ZdZedƒZdZd$eeeeee	d
œdd„Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd%dd„Zdd„ Zd&dd„Zd d!„ Zd"d#„ ZdS )'ÚTopicTreePrintera}  
    Example topic tree visitor that prints a prettified representation
    of topic tree by doing a depth-first traversal of topic tree and
    print information at each (topic) node of tree. Extra info to be
    printed is specified via the 'extra' kwarg. Its value must be a
    list of characters, the order determines output order:
    - D: print description of topic
    - a: print kwarg names only
    - A: print topic kwargs and their description
    - L: print listeners currently subscribed to topic

    E.g. TopicTreePrinter(extra='LaDA') would print, for each topic,
    the list of subscribed listeners, the topic's list of kwargs, the
    topic description, and the description for each kwarg,

        >>> Topic "delTopic"
           >> Listeners:
              > listener1_2880 (from yourModule)
              > listener2_3450 (from yourModule)
           >> Names of Message arguments:
              > arg1
              > arg2
           >> Description: whenever a topic is deleted
           >> Descriptions of Message arguments:
              > arg1: (required) its description
              > arg2: some other description

    ZDAaLZ
ALL_TOPICSNéF   é   ú\--ú|==ú-)ÚwidthÚ
indentStepÚbulletTopicÚbulletTopicItemÚbulletTopicArgÚfileObjc             C   s²   t | j| j| j| jd| _| jt| j ¡ ƒks2t	‚ddl
}|pB|j| _g | _|pRd| _t| jƒ| j }	|	r€dd |	¡ }
t|
ƒ‚|| _t|ƒ| _d| _|| _|| _|| _|| _dS )a…  
        Topic tree printer will print listeners for each topic only
        if printListeners is True. The width will be used to limit
        the width of text output, while indentStep is the number of
        spaces added each time the text is indented further. The
        three bullet parameters define the strings used for each
        item (topic, topic items, and kwargs).
        )ÚDÚAÚaÚLr   NÚ zThese extra chars not known: %sú,)ÚdictÚ(_TopicTreePrinter__printTopicDescriptionÚ$_TopicTreePrinter__printTopicArgsAllÚ%_TopicTreePrinter__printTopicArgNamesÚ&_TopicTreePrinter__printTopicListenersÚ_TopicTreePrinter__contentMethÚallowedExtrasÚsetÚkeysÚAssertionErrorÚsysÚstdoutÚ_TopicTreePrinter__destinationÚ_TopicTreePrinter__outputÚ_TopicTreePrinter__contentÚjoinÚ
ValueErrorÚ_TopicTreePrinter__widthr   Ú_TopicTreePrinter__wrapperÚ_TopicTreePrinter__indentÚ_TopicTreePrinter__indentStepÚ_TopicTreePrinter__topicsBulletÚ#_TopicTreePrinter__topicItemsBulletÚ"_TopicTreePrinter__topicArgsBullet)ÚselfZextrar   r   r   r   r   r   r#   Z
unknownSelÚmsg© r3   ú<lib/python3.7/site-packages/pubsub/utils/topictreeprinter.pyÚ__init__/   s*    

zTopicTreePrinter.__init__c             C   s   d  | j¡S )NÚ
)r(   r&   )r1   r3   r3   r4   Ú	getOutputR   s    zTopicTreePrinter.getOutputc             C   s   | j d k	r| j  |  ¡ ¡ d S )N)r%   Úwriter7   )r1   r3   r3   r4   Ú_doneTraversalU   s    
zTopicTreePrinter._doneTraversalc             C   s|   | j | j_| j}| ¡ r | j}n| ¡ }d| j|f }| j 	|  
||¡¡ || j7 }x"| jD ]}| j| }|||ƒ q\W dS )z@This gets called for each topic. Print as per specified content.z%s Topic "%s"N)r*   r+   r   r,   ZisAllÚALL_TOPICS_NAMEZgetNodeNamer.   r&   ÚappendÚ_TopicTreePrinter__formatDefnr-   r'   r   )r1   ÚtopicObjÚindentZ	topicNameÚheadÚitemZfunctionr3   r3   r4   Ú_onTopicY   s    


zTopicTreePrinter._onTopicc             C   s   |  j | j7  _ dS )zIncrease the indentN)r,   r-   )r1   r3   r3   r4   Ú_startChildrenl   s    zTopicTreePrinter._startChildrenc             C   s   |  j | j8  _ dS )zDecrease the indentN)r,   r-   )r1   r3   r3   r4   Ú_endChildrenp   s    zTopicTreePrinter._endChildrenr   ú: c             C   sP   |r<dd| ||f }|| j _d|| j  | j _| j  |¡S dd| |f S dS )z 
        Print a definition: a block of text at a certain indent,
        has item name, and an optional definition separated from
        item by sep.
        z%s%s%sú z%s%sN)r+   Úinitial_indentr-   Úsubsequent_indentÚfill)r1   r>   r@   ÚdefnÚsepÚprefixr3   r3   r4   Z__formatDefnt   s    zTopicTreePrinter.__formatDefnc             C   s(   d| j  }| j |  ||| ¡ ¡¡ d S )Nz%s Description)r/   r&   r;   r<   ZgetDescription)r1   r>   r=   rI   r3   r3   r4   Z__printTopicDescription‚   s    
z(TopicTreePrinter.__printTopicDescriptionTc             C   s¨   |  ¡ }|r¤d}|rd}d| j|f }| j |  ||¡¡ || j }| ¡ d }xR| ¡ D ]F\}	}
|sld}
n|	|kr|d|
 }
d| j|	f }| j |  |||
¡¡ qZW d S )NzNames of Message arguments:z"Descriptions of message arguments:z%s %sr   r   z(required) %s)	ZgetArgDescriptionsr/   r&   r;   r<   r-   ZgetArgsÚitemsr0   )r1   r>   r=   ZdescÚargsZheadNamer?   Ú	tmpIndentÚrequiredÚkeyÚargr2   r3   r3   r4   Z__printTopicArgsAllˆ   s     
z$TopicTreePrinter.__printTopicArgsAllc             C   s   |   ||d¡ d S )NF)r   )r1   r>   r=   r3   r3   r4   Z__printTopicArgNamesœ   s    z%TopicTreePrinter.__printTopicArgNamesc             C   sr   |  ¡ rnd| j }| j |  ||¡¡ || j }x<| ¡ D ]0}d| j| ¡ | 	¡ f }| j |  ||¡¡ q:W d S )Nz%s Listeners:z%s %s (from %s))
ZhasListenersr/   r&   r;   r<   r-   ZgetListenersIterr0   ÚnameÚmodule)r1   r>   r=   r@   rN   Zlistenerr3   r3   r4   Z__printTopicListenersŸ   s    

z&TopicTreePrinter.__printTopicListeners)Nr   r	   r
   r   r   N)r   rD   )T)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú	frozensetr   r:   ÚintÚstrr   r5   r7   r9   rA   rB   rC   r<   r   r   r   r   r3   r3   r3   r4   r      s      !

r   Nc             K   sL   | dkr,|dkr$ddl m} | ¡ }| ¡ } tf |Ž}t|ƒ}| | ¡ dS )a”  
    Print out the topic tree to a file (or file-like object like a
    StringIO), starting at rootTopic. If root topic should be root of
    whole tree, get it from pub.getDefaultTopicTreeRoot().
    The treeVisitor is an instance of pub.TopicTreeTraverser.

    Printing the tree docs would normally involve this::

        from pubsub import pub
        from pubsub.utils.topictreeprinter import TopicTreePrinter
        traverser = pub.TopicTreeTraverser( TopicTreePrinter(**kwargs) )
        traverser.traverse( pub.getDefaultTopicTreeRoot() )

    With printTreeDocs, it looks like this::

        from pubsub import pub
        from pubsub.utils import printTreeDocs
        printTreeDocs()

    The kwargs are the same as for TopicTreePrinter constructor:
    extra(None), width(70), indentStep(4), bulletTopic, bulletTopicItem,
    bulletTopicArg, fileObj(stdout). If fileObj not given, stdout is used.
    Nr   )Úpub)r   r[   ZgetDefaultTopicMgrZgetRootAllTopicsr   r   Ztraverse)Z	rootTopicZtopicMgrÚkwargsr[   ZprinterZ	traverserr3   r3   r4   ÚprintTreeDocs©   s    
r]   )NN)
rW   Útypingr   Útextwrapr   Zcore.topictreetraverserr   r   r   r]   r3   r3   r3   r4   Ú<module>   s    