o
    !dx5                     @   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__)selfZexecutable_name	__class__ /usr/lib/python3.10/site-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 )NZMANPAGERPAGER)r'   osenvironshlexsplit)r   Z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   contentsZ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   r.   V   s   z(PagingHelpRenderer._send_output_to_pagerc                 O   s   t |i |S r%   r   r   argskwargsr   r   r   r8   \   s   zPagingHelpRenderer._popenc                 C   s   |S r%   r   )r   r/   r   r   r   r-   _   s   z'PagingHelpRenderer._convert_doc_content)r   r   r   __doc__sysstdoutr   r'   r,   r0   r.   r8   r-   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   sr   t |t d}| drg d}n| drg d}ntdtd| | j|tttd}|j	|d	d
 }|S )Nwritergroff)rG   z-mman-Tasciimandoc)rK   rI   rJ   zgroff or mandocr1   )r3   rD   stderrr4   r   )
r   r   Writer_exists_on_pathr   r6   r7   r8   r   r9   )r   r/   Zman_contentsr<   Zp3r;   r   r   r   r-   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
r1   r2   r4   )r,   rN   r6   r7   r&   writedecodeflushr   r8   r   r9   r:   r   r   r   r.   x   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).0r=   namer   r   
<listcomp>   s    z5PosixHelpRenderer._exists_on_path.<locals>.<listcomp>PATH )anyr(   r)   getr+   pathsep)r   rX   r   rW   r   rN      s   z!PosixHelpRenderer._exists_on_pathN)r   r   r   rB   r'   r-   r.   rN   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 )NrE   )r   r   )r   r/   Ztext_outputr   r   r   r-      s   z(WindowsHelpRenderer._convert_doc_contentc                 O   s   d|d< t |i |S )NTshellr>   r?   r   r   r   r8      s   zWindowsHelpRenderer._popenN)r   r   r   rB   r'   r-   r8   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 )NrH   )target)
sessionobjcommand_table	arg_table_subcommand_table_related_itemsr#   rendererr   doc)r   rc   rd   re   rf   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   rk   )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   rl   r   r   r   rX      s   zHelpCommand.namec                 C      | j S )z=These are the commands that may follow after the help command)rg   rl   r   r   r   subcommand_table      zHelpCommand.subcommand_tablec                 C   rn   )z:This is list of items that are related to the help command)rh   rl   r   r   r   related_items   rp   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   ro   Zparse_known_argsgetattrrr   EventHandlerClassr   Zgenerate_eventsrc   ri   r0   rj   getvalue
unregister)r   r@   Zparsed_globalsZsubcommand_parserparsed	remaininginstancer   r   r   __call__  s   
zHelpCommand.__call__)r   r   r   rB   rt   r   propertyrm   rX   ro   rq   rz   r   r   r   r   ra      s    %

	

ra   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)ra   r   descriptionsynopsisZ
help_usagerg   _topic_tag_dbrh   )r   rc   re   rf   r}   r~   usager   r   r   r     s   
zProviderHelpCommand.__init__c                 C   rk   NZawsr   rl   r   r   r   rm   *     zProviderHelpCommand.event_classc                 C   rk   r   r   rl   r   r   r   rX   .  r   zProviderHelpCommand.namec                 C   s6   | j d u r| jd u rt | _| j  |  | _ | j S r%   )rg   r   r   Zload_json_index_create_subcommand_tablerl   r   r   r   ro   2  s   



z$ProviderHelpCommand.subcommand_tablec                 C   sB   i }t | j}||d< | j }|D ]}t| j|}|||< q|S NZtopics)TopicListerCommandrc   r   Zget_all_topic_namesTopicHelpCommand)r   ro   Ztopic_lister_commandZtopic_names
topic_nameZtopic_help_commandr   r   r   r   ;  s   


z,ProviderHelpCommand._create_subcommand_tableN)r   r   r   rB   r   rt   r   r{   rm   rX   ro   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 )ServiceHelpCommandzImplements 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   rc   rd   re   rf   rX   rm   r   r   r   r   S  s
   
zServiceHelpCommand.__init__c                 C   rn   r%   r   rl   r   r   r   rm   Z     zServiceHelpCommand.event_classc                 C   rn   r%   r   rl   r   r   r   rX   ^  r   zServiceHelpCommand.name)r   r   r   rB   r   rt   r   r{   rm   rX   r   r   r   r   r   r   I  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 )	OperationHelpCommandzImplements 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%   )ra   r   r   Zparam_shorthandr   r   )r   rc   Zoperation_modelrf   rX   rm   r   r   r   r   l  s   
zOperationHelpCommand.__init__c                 C   rn   r%   r   rl   r   r   r   rm   s  r   z OperationHelpCommand.event_classc                 C   rn   r%   r   rl   r   r   r   rX   w  r   zOperationHelpCommand.nameN)
r   r   r   rB   r   rt   r   r{   rm   rX   r   r   r   r   r   c  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   rc   r   r   r   r     s   zTopicListerCommand.__init__c                 C   rk   r   r   rl   r   r   r   rm     r   zTopicListerCommand.event_classc                 C   rk   r   r   rl   r   r   r   rX     r   zTopicListerCommand.name)
r   r   r   r	   rt   r   r{   rm   rX   r   r   r   r   r   r   |  s    
r   c                       r   )r   c                    s    t t| |d i i  || _d S r%   )r   r   r   _topic_name)r   rc   r   r   r   r   r     s   
zTopicHelpCommand.__init__c                 C   s
   d| j  S )Nztopics.rW   rl   r   r   r   rm     s   
zTopicHelpCommand.event_classc                 C   rn   r%   )r   rl   r   r   r   rX     r   zTopicHelpCommand.name)
r   r   r   r
   rt   r   r{   rm   rX   r   r   r   r   r   r     s    
r   )/loggingr(   rC   r   r*   
subprocessr   r   Zdocutils.corer   Zdocutils.writersr   Zawscli.clidocsr   r   r   r	   r
   Zawscli.bcdocr   Zawscli.bcdoc.restdocr   Zawscli.bcdoc.textwriterr   Zawscli.argprocessr   Zawscli.argparserr   Zawscli.topictagsr   Zawscli.utilsr   	getLoggerr6   	Exceptionr   r#   objectr$   r"   r!   ra   r|   r   r   r   r   r   r   r   r   <module>   s@   
*4o2