o
    .&ßa,  ã                   @   sZ   d dl Z d dlmZ d dlmZ d dlmZ e  d¡ZG dd„ de	ƒZ
G dd	„ d	e
ƒZdS )
é    N)ÚOrderedDict)ÚDocStringParser)Ú	ReSTStyleÚbcdocsc                   @   sn   e Zd Zd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dd„ Zdd„ Zdd„ ZdS )ÚReSTDocumentÚmanc                 C   sB   t | ƒ| _|| _t| ƒ| _d| _d| _i | _i | _g | _	d | _
d S )NTF)r   ÚstyleÚtargetr   ÚparserÚ	keep_dataÚdo_translationÚtranslation_mapÚhrefsÚ_writesÚ_last_doc_string)Úselfr	   © r   ú6/usr/lib/python3/dist-packages/awscli/bcdoc/restdoc.pyÚ__init__   s   


zReSTDocument.__init__c                 C   s&   | j r|d ur| j |¡ d S d S d S ©N)r   r   Úappend©r   Úsr   r   r   Ú_write#   s   ÿzReSTDocument._writec                 C   s   |   |¡ dS )z2
        Write content into the document.
        N)r   ©r   Úcontentr   r   r   Úwrite'   s   zReSTDocument.writec                 C   s   |   d| j ¡ |f ¡ dS )z-
        Write content on a newline.
        z%s%s
N)r   r   Úspacesr   r   r   r   Úwriteln-   s   zReSTDocument.writelnc                 C   s
   | j d S )zn
        Returns the last content written to the document without
        removing it from the stack.
        éÿÿÿÿ©r   ©r   r   r   r   Ú
peek_write3   s   
zReSTDocument.peek_writec                 C   s
   | j  ¡ S )zL
        Removes and returns the last content written to the stack.
        )r   Úpopr!   r   r   r   Ú	pop_write:   s   
zReSTDocument.pop_writec                 C   s   | j  |¡ dS )z2
        Places new content on the stack.
        N)r   r   r   r   r   r   Ú
push_write@   s   zReSTDocument.push_writec                 C   sD   | j r| j ¡  | j  ¡ D ]\}}| j ||¡ qd | j¡ d¡S )zJ
        Returns the current content of the document as a string.
        Ú úutf-8)r   r   Únew_paragraphÚitemsÚlink_target_definitionÚjoinr   Úencode)r   ÚrefnameÚlinkr   r   r   ÚgetvalueF   s
   
zReSTDocument.getvaluec                    s   ‡ fdd„|D ƒS )Nc                    s   g | ]	}ˆ j  ||¡‘qS r   )r   Úget)Ú.0Úwr!   r   r   Ú
<listcomp>Q   s    z0ReSTDocument.translate_words.<locals>.<listcomp>r   )r   Úwordsr   r!   r   Útranslate_wordsP   ó   zReSTDocument.translate_wordsc                 C   s    |r| j r|  |¡ d S d S d S r   )r   r   )r   Údatar   r   r   Úhandle_dataS   s   
ÿzReSTDocument.handle_datac                 C   sp   |r6zt | jƒ}| j |¡ | j ¡  t | jƒ}||f| _W d S  ty5   tjddd� t |¡ Y d S w d S )NzError parsing doc stringT)Úexc_info)	Úlenr   r
   ÚfeedÚcloser   Ú	ExceptionÚLOGÚdebug)r   Ú
doc_stringÚstartÚendr   r   r   Úinclude_doc_stringW   s   


þùzReSTDocument.include_doc_stringc                 C   s(   | j d ur| j \}}| j||…= d S d S r   )r   r   )r   rA   rB   r   r   r   Úremove_last_doc_stringc   s   

þz#ReSTDocument.remove_last_doc_stringN)r   )Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r"   r$   r%   r/   r5   r8   rC   rD   r   r   r   r   r      s    

r   c                       sž   e Zd Zd‡ fdd„	Zedd„ ƒZedd„ ƒZejd	d„ ƒZed
d„ ƒZedd„ ƒZ	dd„ Z
ddd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Z‡  ZS ) ÚDocumentStructureNr   c                    sX   t t| ƒj|d� || _tƒ | _| jg| _i | _|dur|| _|dur*|  |¡ dS dS )a5  Provides a Hierarichial structure to a ReSTDocument

        You can write to it similiar to as you can to a ReSTDocument but
        has an innate structure for more orginaztion and abstraction.

        :param name: The name of the document
        :param section_names: A list of sections to be included
            in the document.
        :param target: The target documentation of the Document structure
        :param context: A dictionary of data to store with the strucuture. These
            are only stored per section not the entire structure.
        )r	   N)	ÚsuperrH   r   Ú_namer   Ú
_structureÚ_pathÚ_contextÚ_generate_structure)r   ÚnameÚsection_namesr	   Úcontext©Ú	__class__r   r   r   k   s   
ÿzDocumentStructure.__init__c                 C   ó   | j S )z"The name of the document structure)rJ   r!   r   r   r   rO   ‚   s   zDocumentStructure.namec                 C   rT   )zv
        A list of where to find a particular document structure in the
        overlying document structure.
        ©rL   r!   r   r   r   Úpath‡   s   zDocumentStructure.pathc                 C   s
   || _ d S r   rU   )r   Úvaluer   r   r   rV   �   ó   
c                 C   s
   t | jƒS r   )ÚlistrK   r!   r   r   r   Úavailable_sections“   rX   z$DocumentStructure.available_sectionsc                 C   rT   r   )rM   r!   r   r   r   rQ   —   s   zDocumentStructure.contextc                 C   s   |D ]}|   |¡ qd S r   )Úadd_new_section)r   rP   Úsection_namer   r   r   rN   ›   s   ÿz%DocumentStructure._generate_structurec                 C   sJ   | j || j|d�}| j|g |_| jj|j_| j|_| j|_|| j|< |S )a�  Adds a new section to the current document structure

        This document structure will be considered a section to the
        current document structure but will in itself be an entirely
        new document structure that can be written to and have sections
        as well

        :param name: The name of the section.
        :param context: A dictionary of data to store with the strucuture. These
            are only stored per section not the entire structure.
        :rtype: DocumentStructure
        :returns: A new document structure to add to but lives as a section
            to the document structure it was instantiated from.
        )rO   r	   rQ   )rS   r	   rV   r   Úindentationr   r   rK   )r   rO   rQ   Úsectionr   r   r   r[   Ÿ   s   
ÿ
z!DocumentStructure.add_new_sectionc                 C   s
   | j | S )zRetrieve a section©rK   ©r   rO   r   r   r   Úget_section¹   rX   zDocumentStructure.get_sectionc                 C   s   | j |= dS )zDelete a sectionNr_   r`   r   r   r   Údelete_section½   s   z DocumentStructure.delete_sectionc                 C   sl   t | jƒdkr | jr | j ¡  | j ¡ D ]\}}| j ||¡ q|  ¡ }| j ¡ D ]
\}}|| 	¡ 7 }q)|S )zÍFlushes a doc structure to a ReSTructed string

        The document is flushed out in a DFS style where sections and their
        subsections' values are added to the string as they are visited.
        é   )
r:   rV   r   r   r(   r)   r*   r/   rK   Úflush_structure)r   r-   r.   rW   rO   r^   r   r   r   rd   Á   s   
z!DocumentStructure.flush_structurec                 C   s   d  | j¡ d¡S )Nr&   r'   )r+   r   r,   r!   r   r   r   r/   Ó   r6   zDocumentStructure.getvaluec                 C   s   t ƒ | _d S r   )r   rK   r!   r   r   r   Úremove_all_sectionsÖ   s   z%DocumentStructure.remove_all_sectionsc                 C   s
   g | _ d S r   r    r!   r   r   r   Ú
clear_textÙ   s   
zDocumentStructure.clear_text)Nr   Nr   )rE   rF   rG   r   ÚpropertyrO   rV   ÚsetterrZ   rQ   rN   r[   ra   rb   rd   r/   re   rf   Ú__classcell__r   r   rR   r   rH   j   s(    





rH   )ÚloggingÚbotocore.compatr   Úawscli.bcdoc.docstringparserr   Úawscli.bcdoc.styler   Ú	getLoggerr>   Úobjectr   rH   r   r   r   r   Ú<module>   s   
T