o
    2&ßa5  ã                   @   s–  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m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 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 d dlm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#dd„ Z$G dd„ de%ƒZ&G dd„ de&ƒZ'G dd„ de&ƒZ(G dd„ de%ƒZ)G dd„ de)ƒZ*G d d!„ d!e)ƒZ+G d"d#„ d#e)ƒZ,G d$d%„ d%e)ƒZ-G d&d'„ d'e)ƒZ.dS )(é    N)ÚPopenÚPIPE)Úpublish_string)Úmanpage)ÚProviderDocumentEventHandler)ÚServiceDocumentEventHandler)ÚOperationDocumentEventHandler)ÚTopicListerDocumentEventHandler)ÚTopicDocumentEventHandler)Ú	docevents)ÚReSTDocument)Ú
TextWriter)ÚParamShorthandParser)ÚArgTableArgParser)Ú
TopicTagDB)Úignore_ctrl_czawscli.helpc                       s   e Zd Z‡ fdd„Z‡  ZS )ÚExecutableNotFoundErrorc                    s   t t| ƒ d| ¡ d S )Nz$Could not find executable named "%s")Úsuperr   Ú__init__)ÚselfÚexecutable_name©Ú	__class__© ú-/usr/lib/python3/dist-packages/awscli/help.pyr   )   s   
ÿz ExecutableNotFoundError.__init__)Ú__name__Ú
__module__Ú__qualname__r   Ú__classcell__r   r   r   r   r   (   s    r   c                   C   s   t  ¡ dkr	tƒ S tƒ S )zZ
    Return the appropriate HelpRenderer implementation for the
    current platform.
    ÚWindows)ÚplatformÚsystemÚWindowsHelpRendererÚPosixHelpRendererr   r   r   r   Úget_renderer.   s   r$   c                   @   sJ   e Zd ZdZejfdd„ZdZdd„ Zdd„ Z	d	d
„ Z
dd„ Zdd„ ZdS )ÚPagingHelpRendererzˆ
    Interface for a help renderer.

    The renderer is responsible for displaying the help content on
    a particular platform.

    c                 C   s
   || _ d S ©N)Úoutput_stream)r   r'   r   r   r   r   A   s   
zPagingHelpRenderer.__init__Nc                 C   s:   | j }dtjv rtjd }n
dtjv rtjd }t |¡S )NÚMANPAGERÚPAGER)r)   ÚosÚenvironÚshlexÚsplit)r   Úpagerr   r   r   Úget_pager_cmdlineF   s   



z$PagingHelpRenderer.get_pager_cmdlinec                 C   s   |   |¡}|  |¡ dS )z`
        Each implementation of HelpRenderer must implement this
        render method.
        N)Ú_convert_doc_contentÚ_send_output_to_pager)r   ÚcontentsÚconverted_contentr   r   r   ÚrenderN   s   
zPagingHelpRenderer.renderc                 C   s2   |   ¡ }t d|¡ | j|td�}|j|d� d S )NúRunning command: %s©Ústdin©Úinput)r/   ÚLOGÚdebugÚ_popenr   Úcommunicate©r   ÚoutputÚcmdlineÚpr   r   r   r1   V   s   z(PagingHelpRenderer._send_output_to_pagerc                 O   s   t |i |¤ŽS r&   ©r   ©r   ÚargsÚkwargsr   r   r   r<   \   s   zPagingHelpRenderer._popenc                 C   s   |S r&   r   )r   r2   r   r   r   r0   _   s   z'PagingHelpRenderer._convert_doc_content)r   r   r   Ú__doc__ÚsysÚstdoutr   r)   r/   r4   r1   r<   r0   r   r   r   r   r%   9   s    r%   c                   @   s,   e Zd ZdZdZdd„ Zdd„ Zdd„ Zd	S )
r#   z[
    Render help content on a Posix-like system.  This includes
    Linux and MacOS X.
    zless -Rc                 C   s\   t |t ¡ d�}|  d¡stdƒ‚g d¢}t d|¡ | j|tttd�}|j	|d�d }|S )N©ÚwriterÚgroff)rK   z-mÚmanz-TÚasciir5   )r7   rH   Ústderrr8   r   )
r   r   ÚWriterÚ_exists_on_pathr   r:   r;   r<   r   r=   )r   r2   Úman_contentsr@   Úp3Úgroff_outputr   r   r   r0   k   s   
z&PosixHelpRenderer._convert_doc_contentc                 C   sž   |   ¡ }|  |d ¡s&t d|d  ¡ | j | d¡d ¡ | j ¡  d S t d|¡ tƒ � | j	|t
d�}|j|d� W d   ƒ d S 1 sHw   Y  d S )Nr   z0Pager '%s' not found in PATH, printing raw help.zutf-8Ú
r5   r6   r8   )r/   rP   r:   r;   r'   ÚwriteÚdecodeÚflushr   r<   r   r=   r>   r   r   r   r1   u   s   ÿ
"óz'PosixHelpRenderer._send_output_to_pagerc                    s(   t ‡ fdd„tj dd¡ tj¡D ƒƒS )Nc                    s"   g | ]}t j t j |ˆ ¡¡‘qS r   )r*   ÚpathÚexistsÚjoin)Ú.0rA   ©Únamer   r   Ú
<listcomp>�   s    ÿz5PosixHelpRenderer._exists_on_path.<locals>.<listcomp>ÚPATHÚ )Úanyr*   r+   Úgetr-   Úpathsep)r   r]   r   r\   r   rP   �   s   ÿz!PosixHelpRenderer._exists_on_pathN)r   r   r   rF   r)   r0   r1   rP   r   r   r   r   r#   c   s    
r#   c                   @   s$   e Zd ZdZdZdd„ Zdd„ ZdS )r"   z*Render help content on a Windows platform.Úmorec                 C   s   t |tƒ d�}|S )NrI   )r   r   )r   r2   Útext_outputr   r   r   r0   ™   s   ÿz(WindowsHelpRenderer._convert_doc_contentc                 O   s   d|d< t |i |¤ŽS )NTÚshellrB   rC   r   r   r   r<   ž   s   zWindowsHelpRenderer._popenN)r   r   r   rF   r)   r0   r<   r   r   r   r   r"   ”   s
    r"   c                   @   sV   e Zd ZdZdZ	 dd„ Zedd„ ƒZedd„ ƒZed	d
„ ƒZ	edd„ ƒZ
dd„ ZdS )ÚHelpCommandak  
    HelpCommand Interface
    ---------------------
    A HelpCommand object acts as the interface between objects in the
    CLI (e.g. Providers, Services, Operations, etc.) and the documentation
    system (bcdoc).

    A HelpCommand object wraps the object from the CLI space and provides
    a consistent interface to critical information needed by the
    documentation pipeline such as the object's name, description, etc.

    The HelpCommand object is passed to the component of the
    documentation pipeline that fires documentation events.  It is
    then passed on to each document event handler that has registered
    for the events.

    All HelpCommand objects contain the following attributes:

        + ``session`` - A ``botocore`` ``Session`` object.
        + ``obj`` - The object that is being documented.
        + ``command_table`` - A dict mapping command names to
              callable objects.
        + ``arg_table`` - A dict mapping argument names to callable objects.
        + ``doc`` - A ``Document`` object that is used to collect the
              generated documentation.

    In addition, please note the `properties` defined below which are
    required to allow the object to be used in the document pipeline.

    Implementations of HelpCommand are provided here for Provider,
    Service and Operation objects.  Other implementations for other
    types of objects might be needed for customization in plugins.
    As long as the implementations conform to this basic interface
    it should be possible to pass them to the documentation system
    and generate interactive and static help files.
    Nc                 C   sT   || _ || _|d u ri }|| _|d u ri }|| _i | _g | _tƒ | _tdd�| _	d S )NrL   )Útarget)
ÚsessionÚobjÚcommand_tableÚ	arg_tableÚ_subcommand_tableÚ_related_itemsr$   Úrendererr   Údoc)r   ri   rj   rk   rl   r   r   r   r   Ñ   s   zHelpCommand.__init__c                 C   ó   dS )aX  
        Return the ``event_class`` for this object.

        The ``event_class`` is used by the documentation pipeline
        when generating documentation events.  For the event below::

            doc-title.<event_class>.<name>

        The document pipeline would use this property to determine
        the ``event_class`` value.
        Nr   ©r   r   r   r   Úevent_classß   s   zHelpCommand.event_classc                 C   rq   )zÀ
        Return the name of the wrapped object.

        This would be called by the document pipeline to determine
        the ``name`` to be inserted into the event, as shown above.
        Nr   rr   r   r   r   r]   î   s   zHelpCommand.namec                 C   ó   | j S )z=These are the commands that may follow after the help command)rm   rr   r   r   r   Úsubcommand_tableø   ó   zHelpCommand.subcommand_tablec                 C   rt   )z:This is list of items that are related to the help command)rn   rr   r   r   r   Úrelated_itemsý   rv   zHelpCommand.related_itemsc                 C   sv   |r t i | jƒ}| |¡\}}t|dd ƒd ur | j|j ||ƒS |  | ¡}t | j| ¡ | j	 
| j ¡ ¡ | ¡  d S )NÚ
subcommand)r   ru   Úparse_known_argsÚgetattrrx   ÚEventHandlerClassr   Úgenerate_eventsri   ro   r4   rp   ÚgetvalueÚ
unregister)r   rD   Úparsed_globalsÚsubcommand_parserÚparsedÚ	remainingÚinstancer   r   r   Ú__call__  s   ÿ
zHelpCommand.__call__)r   r   r   rF   r{   r   Úpropertyrs   r]   ru   rw   r„   r   r   r   r   rg   ¥   s    %

	

rg   c                   @   sH   e Zd ZdZeZdd„ Zedd„ ƒZedd„ ƒZ	edd	„ ƒZ
d
d„ ZdS )ÚProviderHelpCommandz^Implements top level help command.

    This is what is called when ``aws help`` is run.

    c                 C   s<   t  | |d ||¡ || _|| _|| _d | _d | _dg| _d S )Nzaws help topics)rg   r   ÚdescriptionÚsynopsisÚ
help_usagerm   Ú_topic_tag_dbrn   )r   ri   rk   rl   r‡   rˆ   Úusager   r   r   r     s   
ÿzProviderHelpCommand.__init__c                 C   rq   ©NÚawsr   rr   r   r   r   rs   '  ó   zProviderHelpCommand.event_classc                 C   rq   rŒ   r   rr   r   r   r   r]   +  rŽ   zProviderHelpCommand.namec                 C   s6   | j d u r| jd u rtƒ | _| j ¡  |  ¡ | _ | j S r&   )rm   rŠ   r   Úload_json_indexÚ_create_subcommand_tablerr   r   r   r   ru   /  s   



z$ProviderHelpCommand.subcommand_tablec                 C   sB   i }t | jƒ}||d< | j ¡ }|D ]}t| j|ƒ}|||< q|S ©NÚtopics)ÚTopicListerCommandri   rŠ   Úget_all_topic_namesÚTopicHelpCommand)r   ru   Útopic_lister_commandÚtopic_namesÚ
topic_nameÚtopic_help_commandr   r   r   r�   8  s   


z,ProviderHelpCommand._create_subcommand_tableN)r   r   r   rF   r   r{   r   r…   rs   r]   ru   r�   r   r   r   r   r†     s    


r†   c                       s<   e Zd ZdZeZ‡ fdd„Zedd„ ƒZedd„ ƒZ	‡  Z
S )ÚServiceHelpCommandz�Implements service level help.

    This is the object invoked whenever a service command
    help is implemented, e.g. ``aws ec2 help``.

    c                    s&   t t| ƒ ||||¡ || _|| _d S r&   )r   rš   r   Ú_nameÚ_event_class)r   ri   rj   rk   rl   r]   rs   r   r   r   r   P  s
   ÿ
zServiceHelpCommand.__init__c                 C   rt   r&   ©rœ   rr   r   r   r   rs   W  ó   zServiceHelpCommand.event_classc                 C   rt   r&   ©r›   rr   r   r   r   r]   [  rž   zServiceHelpCommand.name)r   r   r   rF   r   r{   r   r…   rs   r]   r   r   r   r   r   rš   F  s    
rš   c                   @   s4   e Zd ZdZeZdd„ Zedd„ ƒZedd„ ƒZ	dS )	ÚOperationHelpCommandzžImplements operation level help.

    This is the object invoked whenever help for a service is requested,
    e.g. ``aws ec2 describe-instances help``.

    c                 C   s*   t  | ||d |¡ tƒ | _|| _|| _d S r&   )rg   r   r   Úparam_shorthandr›   rœ   )r   ri   Úoperation_modelrl   r]   rs   r   r   r   r   i  s   
zOperationHelpCommand.__init__c                 C   rt   r&   r�   rr   r   r   r   rs   p  rž   z OperationHelpCommand.event_classc                 C   rt   r&   rŸ   rr   r   r   r   r]   t  rž   zOperationHelpCommand.nameN)
r   r   r   rF   r   r{   r   r…   rs   r]   r   r   r   r   r    `  s    
r    c                       ó8   e Zd ZeZ‡ fdd„Zedd„ ƒZedd„ ƒZ‡  Z	S )r“   c                    s   t t| ƒ |d i i ¡ d S r&   )r   r“   r   )r   ri   r   r   r   r   |  s   zTopicListerCommand.__init__c                 C   rq   r‘   r   rr   r   r   r   rs     rŽ   zTopicListerCommand.event_classc                 C   rq   r‘   r   rr   r   r   r   r]   ƒ  rŽ   zTopicListerCommand.name)
r   r   r   r	   r{   r   r…   rs   r]   r   r   r   r   r   r“   y  s    
r“   c                       r£   )r•   c                    s    t t| ƒ |d i i ¡ || _d S r&   )r   r•   r   Ú_topic_name)r   ri   r˜   r   r   r   r   ‹  s   
zTopicHelpCommand.__init__c                 C   s
   d| j  S )Nztopics.r\   rr   r   r   r   rs   �  s   
zTopicHelpCommand.event_classc                 C   rt   r&   )r¤   rr   r   r   r   r]   “  rž   zTopicHelpCommand.name)
r   r   r   r
   r{   r   r…   rs   r]   r   r   r   r   r   r•   ˆ  s    
r•   )/Úloggingr*   rG   r    r,   Ú
subprocessr   r   Údocutils.corer   Údocutils.writersr   Úawscli.clidocsr   r   r   r	   r
   Úawscli.bcdocr   Úawscli.bcdoc.restdocr   Úawscli.bcdoc.textwriterr   Úawscli.argprocessr   Úawscli.argparserr   Úawscli.topictagsr   Úawscli.utilsr   Ú	getLoggerr:   Ú	Exceptionr   r$   Úobjectr%   r#   r"   rg   r†   rš   r    r“   r•   r   r   r   r   Ú<module>   s@   
*1o2