o
    .&ßaj<  ã                   @   s¤   d dl Z 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 d dl	m
Z
 d dlmZ d dlm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)ÚCOLORAMA_KWARGS)Úsix)ÚHistorySubcommand)ÚRegexFilterc                   @   s.   e Zd Zd
dd„Zdd„ Zdd„ Zdd	„ ZdS )Ú	FormatterNc                 C   s8   || _ | j du rtj| _ |r|rtdƒ‚|| _|| _dS )a#  Formats and outputs CLI history events

        :type output: File-like obj
        :param output: The stream to write the formatted event to. By default
            sys.stdout is used.

        :type include: list
        :param include: A filter specifying which event to only be displayed.
            This parameter is mutually exclusive with exclude.

        :type exclude: list
        :param exclude: A filter specifying which events to exclude from being
            displayed. This parameter is mutually exclusive with include.

        Nz4Either input or exclude can be provided but not both)Ú_outputÚsysÚstdoutÚ
ValueErrorÚ_includeÚ_exclude)ÚselfÚoutputÚincludeÚexclude© r   úD/usr/lib/python3/dist-packages/awscli/customizations/history/show.pyÚ__init__   s   
ÿ
zFormatter.__init__c                 C   s   |   |¡r|  |¡ dS dS )z Displays a formatted version of the event record

        :type event_record: dict
        :param event_record: The event record to format and display.
        N)Ú_should_displayÚ_display©r   Úevent_recordr   r   r   Údisplay5   s   
ÿzFormatter.displayc                 C   s   t dƒ‚)Nz
_display())ÚNotImplementedErrorr   r   r   r   r   >   s   zFormatter._displayc                 C   s,   | j r
|d | j v S | jr|d | jvS dS )NÚ
event_typeT)r   r   r   r   r   r   r   A   s
   zFormatter._should_display)NNN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r   r   r   r      s
    
	r   c                       s@  e Zd ZeddƒZdddigdœdddigid	d
ddœdddœddddœgdœddddœdddœdddegdœddddœgdœddd dœddddœddddœgdœd!d"dd#œgdœd$dd%igdœd&œZejjej	j
d'œZd@‡ fd*d+„	Zd,d-„ Zd.d/„ Zd0d1„ Zd2d3„ Zd4d5„ Zd6d7„ Zd8d9„ Zd:d;„ ZdAd<d=„Zd>d?„ Z‡  ZS )BÚDetailedFormatterz#Signature=([a-z0-9]{4})[a-z0-9]{60}zSignature=\1...zAWS CLI command enteredÚdescriptionzwith AWS CLI version)ÚtitleÚvaluesr!   zwith argumentszAPI call madez
to serviceÚservice)r   Úpayload_keyzusing operationÚ	operationzwith parametersÚparamsÚ
dictionary)r   r#   Úvalue_formatzHTTP request sentzto URLÚurlzwith methodÚmethodzwith headersÚheaders)r   r#   r'   Úfiltersz	with bodyÚbodyÚ	http_bodyzHTTP response receivedzwith status codeÚstatus_codezHTTP response parsedz	parsed to)r   r'   zAWS CLI command exitedzwith return code)ÚCLI_VERSIONÚCLI_ARGUMENTSÚAPI_CALLÚHTTP_REQUESTÚHTTP_RESPONSEÚPARSED_RESPONSEÚCLI_RC)r    r   NTc                    sL   t t| ƒ |||¡ i | _d| _|| _tƒ | _| jr$tj	di t
¤Ž d S d S )Nr   r   )Úsuperr   r   Ú_request_id_to_api_numÚ_num_api_callsÚ	_colorizeÚSectionValuePrettyFormatterÚ_value_pformatterÚcoloramaÚinitr   )r   r   r   r   Úcolorize©Ú	__class__r   r   r   °   s   ÿzDetailedFormatter.__init__c                 C   s,   | j  |d ¡}|d ur|  ||¡ d S d S )Nr   )Ú	_SECTIONSÚgetÚ_display_section)r   r   Úsection_definitionr   r   r   r   ¹   s   ÿzDetailedFormatter._displayc                 C   s6   d|v r|   |d |¡ |d D ]}|  ||¡ qd S )Nr    r!   )Ú_display_titleÚ_display_value)r   r   rD   Úvalue_definitionr   r   r   rC   ¾   s
   ÿz"DetailedFormatter._display_sectionc                 C   s   |   ||¡}|  |¡ d S ©N)Ú_format_section_titleÚ_write_output)r   r    r   Úformatted_titler   r   r   rE   Ä   s   z DetailedFormatter._display_titlec                 C   sx   |d }|d }|}d|v r||d  }|   |¡}||  ||| d¡¡7 }d|v r5|d D ]}| |¡}q-|  |¡ d S )Nr   Úpayloadr#   r'   r+   )Ú_format_descriptionÚ_format_valuerB   Úfilter_textrJ   )r   rG   r   Úvalue_descriptionÚevent_record_payloadÚvalueÚformatted_valueÚtext_filterr   r   r   rF   È   s   
ÿz DetailedFormatter._display_valuec                 C   s&   t |tjƒr| d¡}| j |¡ d S )Nzutf-8)Ú
isinstancer   Ú	text_typeÚencoder   Úwrite)r   Úcontentr   r   r   rJ   ×   s   
zDetailedFormatter._write_outputc                 C   sd   |}|   |¡}|d urd| | }|  |d¡}|d7 }|  d¡}|| j|d |dd�7 }d| | S )Nz[%s] r    Ú
zat timeÚ	timestamp)r'   )Ú_get_api_numÚ_color_if_configuredrM   rN   )r   r    r   rK   Úapi_numÚformatted_timestampr   r   r   rI   Ü   s   


ÿz'DetailedFormatter._format_section_titlec                 C   s>   |d }|r|| j vr| j| j |< |  jd7  _| j | S d S )NÚ
request_idé   )r7   r8   )r   r   r`   r   r   r   r\   ê   s   
ÿÿ
ûzDetailedFormatter._get_api_numc                 C   s   |   |d d¡S )Nz: r   )r]   )r   rP   r   r   r   rM   ó   s   ÿz%DetailedFormatter._format_descriptionc                 C   s,   |r| j  |||¡}|d S t|ƒ}|d S )NrZ   )r;   ÚpformatÚstr)r   rR   r   r'   rS   r   r   r   rN   ÷   s   ÿÿzDetailedFormatter._format_valuec                 C   s$   | j r| j| }|| tjj S |S rH   )r9   Ú_COMPONENT_COLORSr<   ÚStyleÚ	RESET_ALL)r   ÚtextÚ	componentÚcolorr   r   r   r]   ÿ   s   
z&DetailedFormatter._color_if_configured)NNNTrH   )r   r   r   r   Ú_SIG_FILTERrA   r<   re   ÚBRIGHTÚForeÚCYANrd   r   r   rC   rE   rF   rJ   rI   r\   rM   rN   r]   Ú__classcell__r   r   r?   r   r   J   s¦    þÿþÿÿþþý÷þþþüýñþþýýöþþÿþ
ÿþ­\þ		
r   c                   @   sX   e Zd Zdd„ Zd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dS )r:   c                 C   s   t | d| ƒ||ƒS )NÚ	_pformat_)Úgetattr)r   rR   r'   r   r   r   r   rb     s   z#SectionValuePrettyFormatter.pformatNc                 C   s   t j  |d ¡ d¡d d… S )Ng     @�@z%Y-%m-%d %H:%M:%S.%féýÿÿÿ)ÚdatetimeÚfromtimestampÚstrftime)r   Úevent_timestampr   r   r   r   Ú_pformat_timestamp
  s
   ÿÿz.SectionValuePrettyFormatter._pformat_timestampc                 C   s   t j|ddd�S )NTé   )ÚobjÚ	sort_keysÚindent)ÚjsonÚdumps)r   rx   r   r   r   r   Ú_pformat_dictionary  s   z/SectionValuePrettyFormatter._pformat_dictionaryc                 C   sH   |sdS |d   dd¡rdS |  |¡r|  |¡S |  |¡r"|  |¡S |S )NzThere is no associated bodyrL   Ú	streamingFz.The body is a stream and will not be displayed)rB   Ú_is_xmlÚ_get_pretty_xmlÚ_is_json_structureÚ_get_pretty_json)r   r,   r   r   r   r   Ú_pformat_http_body  s   



z.SectionValuePrettyFormatter._pformat_http_bodyc                 C   s&   |   |¡}tjj |¡}|jddd�S )Nz    rZ   )rz   Únewl)Ú_strip_whitespaceÚxmlÚdomÚminidomÚparseStringÚtoprettyxml)r   r,   Ústripped_bodyÚxml_domr   r   r   r€   #  s   
z+SectionValuePrettyFormatter._get_pretty_xmlc                 C   s   t  |¡}|  |¡S rH   )r{   Úloadsr}   )r   r,   rx   r   r   r   r‚   .  s   

z,SectionValuePrettyFormatter._get_pretty_jsonc                 C   s0   z
t jj |¡ W dS  t jjjy   Y dS w )NFT)r†   r‡   rˆ   r‰   ÚparsersÚexpatÚ
ExpatError©r   r,   r   r   r   r   4  s   þÿz#SectionValuePrettyFormatter._is_xmlc                 C   s*   t jj |¡}d dd„ | ¡  ¡ D ƒ¡S )NÚ c                 S   s   g | ]}|  ¡ ‘qS r   )Ústrip)Ú.0Úliner   r   r   Ú
<listcomp>>  s    zASectionValuePrettyFormatter._strip_whitespace.<locals>.<listcomp>)r†   r‡   rˆ   r‰   ÚjoinÚtoxmlÚ
splitlines)r   Ú
xml_stringrŒ   r   r   r   r…   ;  s   ÿz-SectionValuePrettyFormatter._strip_whitespacec                 C   s8   |  d¡rzt |¡ W dS  tjjy   Y dS w dS )Nú{TF)Ú
startswithr{   r�   ÚdecoderÚJSONDecodeErrorr‘   r   r   r   r�   A  s   

ÿz.SectionValuePrettyFormatter._is_json_structurerH   )r   r   r   rb   rv   r}   rƒ   r€   r‚   r   r…   r�   r   r   r   r   r:     s    

r:   c                   @   sr   e Zd ZdZdZdeiZdddddd	œd
dddœddddœde ¡ dddœgZdd„ Z	dd„ Z
dd„ Zdd„ ZdS )ÚShowCommandÚshowzµShows the various events related to running a specific CLI command. If this command is ran without any positional arguments, it will display the events for the last CLI command ran.ÚdetailedÚ
command_idú?ÚlatestTzuThe ID of the CLI command to show. If this positional argument is omitted, it will show the last the CLI command ran.)ÚnameÚnargsÚdefaultÚpositional_argÚ	help_textr   ú+z€Specifies which events to **only** include when showing the CLI command. This argument is mutually exclusive with ``--exclude``.)r¥   r¦   r©   r   zwSpecifies which events to exclude when showing the CLI command. This argument is mutually exclusive with ``--include``.ÚformataE  Specifies which format to use in showing the events for the specified CLI command. The following formats are supported:

<ul><li> detailed - This the default format. It prints out a detailed overview of the CLI command ran. It displays all of the key events in the command lifecycle where each important event has a title and its important values underneath. The events are ordered by timestamp and events of the same API call are associated together with the [``api_id``] notation where events that share the same ``api_id`` belong to the lifecycle of the same API call.</li></ul>)r¥   Úchoicesr§   r©   c                 C   sŠ   |   ¡  z;|  |¡ |  ¡ �}|  |||¡}|  |¡D ]}| |¡ qW d   ƒ n1 s-w   Y  W |  ¡  dS W |  ¡  dS |  ¡  w )Nr   )Ú_connect_to_history_dbÚ_validate_argsÚ_get_output_streamÚ_get_formatterÚ_get_record_iteratorr   Ú_close_history_db)r   Úparsed_argsÚparsed_globalsÚoutput_streamÚ	formatterÚrecordr   r   r   Ú	_run_mainy  s"   

ÿÿýù
ÿzShowCommand._run_mainc                 C   s   |j r
|jrtdƒ‚d S d S )Nz:Either --exclude or --include can be provided but not both)r   r   r
   ©r   r³   r   r   r   r®   †  s
   ÿÿzShowCommand._validate_argsc                 C   s@   |j }|j|j|dœ}|dkr|  |¡|d< | j| di |¤ŽS )N)r   r   r   r¡   r>   r   )r«   r   r   Ú_should_use_colorÚ
FORMATTERS)r   r³   r´   rµ   Úformat_typeÚformatter_kwargsr   r   r   r°   ‹  s   ýÿzShowCommand._get_formatterc                 C   s"   |j dkr
| j ¡ S | j |j ¡S )Nr¤   )r¢   Ú
_db_readerÚiter_latest_recordsÚiter_recordsr¹   r   r   r   r±   —  s   

z ShowCommand._get_record_iteratorN)r   r   r   ÚNAMEÚDESCRIPTIONr   r»   ÚkeysÚ	ARG_TABLEr¸   r®   r°   r±   r   r   r   r   rŸ   K  s2    ÿÿýþþþð$rŸ   )rr   r{   r   Úxml.parsers.expatr†   Úxml.dom.minidomr<   Úawscli.tabler   Úawscli.compatr   Ú&awscli.customizations.history.commandsr   Ú%awscli.customizations.history.filtersr   Úobjectr   r   r:   rŸ   r   r   r   r   Ú<module>   s   / =E