o
    !d                     @   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)	ReSTStyleZbcdocsc                   @   sv   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d ZdS )ReSTDocumentmanc                 C   sB   t | | _|| _t| | _d| _d| _i | _i | _g | _	d | _
d S )NTF)r   styletargetr   parser	keep_dataZdo_translationtranslation_maphrefs_writes_last_doc_string)selfr    r   8usr/lib/python3.10/site-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   Z
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   r>   r?   r   r   r   remove_last_doc_stringc   s   

z#ReSTDocument.remove_last_doc_stringc                 C   sL   t |d}| D ]	}| |  q
W d    d S 1 sw   Y  d S )Nr)open	readlinesr   strip)r   filenamefliner   r   r   write_from_filei   s
   "zReSTDocument.write_from_fileN)r   )__name__
__module____qualname__r   r   r   r   r    r"   r#   r-   r3   r6   r@   rA   rI   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)	superrM   r   _namer   
_structure_path_context_generate_structure)r   namesection_namesr   context	__class__r   r   r   p   s   
zDocumentStructure.__init__c                 C      | j S )z"The name of the document structure)rO   r   r   r   r   rT      s   zDocumentStructure.namec                 C   rY   )zv
        A list of where to find a particular document structure in the
        overlying document structure.
        rQ   r   r   r   r   path   s   zDocumentStructure.pathc                 C   s
   || _ d S r   rZ   )r   valuer   r   r   r[         
c                 C   s
   t | jS r   )listrP   r   r   r   r   available_sections   r]   z$DocumentStructure.available_sectionsc                 C   rY   r   )rR   r   r   r   r   rV      s   zDocumentStructure.contextc                 C   s   |D ]}|  | qd S r   )add_new_section)r   rU   section_namer   r   r   rS      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.
        )rT   r   rV   )rX   r   r[   r   Zindentationr   r   rP   )r   rT   rV   sectionr   r   r   r`      s   

z!DocumentStructure.add_new_sectionc                 C   s
   | j | S )zRetrieve a sectionrP   r   rT   r   r   r   get_section   r]   zDocumentStructure.get_sectionc                 C   s   | j |= dS )zDelete a sectionNrc   rd   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 )zFlushes 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.
           )
r8   r[   r   r   r&   r'   r(   r-   rP   flush_structure)r   r+   r,   r\   rT   rb   r   r   r   rh      s   
z!DocumentStructure.flush_structurec                 C   s   d | jdS )Nr$   r%   )r)   r   r*   r   r   r   r   r-      r4   zDocumentStructure.getvaluec                 C   s   t  | _d S r   )r   rP   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   )rJ   rK   rL   r   propertyrT   r[   setterr_   rV   rS   r`   re   rf   rh   r-   ri   rj   __classcell__r   r   rW   r   rM   o   s(    





rM   )loggingZbotocore.compatr   Zawscli.bcdoc.docstringparserr   Zawscli.bcdoc.styler   	getLoggerr<   objectr   rM   r   r   r   r   <module>   s   
Y