o
    !dk<                     @   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   Fusr/lib/python3.10/site-packages/awscli/customizations/history/show.py__init__   s   

zFormatter.__init__c                 C   s   |  |r| | dS dS )zDisplays 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 serviceZservice)r   payload_keyzusing operationZ	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bodyZ	http_bodyzHTTP response receivedzwith status codeZstatus_codezHTTP response parsedz	parsed to)r   r%   zAWS CLI command exitedzwith return code)ZCLI_VERSIONZCLI_ARGUMENTSZAPI_CALLZHTTP_REQUESTZHTTP_RESPONSEZPARSED_RESPONSEZ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   r9   value_definitionr   r   r   r8      s
   z"DetailedFormatter._display_sectionc                 C   s   |  ||}| | d S N)_format_section_title_write_output)r   r    r   formatted_titler   r   r   r:      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_valuer7   Zfilter_textr?   )r   r<   r   value_descriptionZevent_record_payloadvalueformatted_valueZtext_filterr   r   r   r;      s   
z DetailedFormatter._display_valuec                 C   s&   t |tjr|d}| j| d S )Nzutf-8)
isinstancer   Z	text_typeencoder   write)r   contentr   r   r   r?      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_configuredrB   rC   )r   r    r   r@   Zapi_numZformatted_timestampr   r   r   r>      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   )r,   r-   )r   r   rO   r   r   r   rM      s   

zDetailedFormatter._get_api_numc                 C   s   |  |d dS )Nz: r   )rN   )r   rD   r   r   r   rB      s   z%DetailedFormatter._format_descriptionc                 C   s,   |r| j |||}|d S t|}|d S )NrK   )r0   pformatstr)r   rE   r   r%   rF   r   r   r   rC      s   zDetailedFormatter._format_valuec                 C   s$   | j r| j| }|| tjj S |S r=   )r.   _COMPONENT_COLORSr1   StyleZ	RESET_ALL)r   textZ	componentZcolorr   r   r   rN      s   
z&DetailedFormatter._color_if_configured)NNNTr=   )r   r   r   r   Z_SIG_FILTERr6   r1   rT   ZBRIGHTZForeZCYANrS   r   r   r8   r:   r;   r?   r>   rM   rB   rC   rN   __classcell__r   r   r4   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 )NZ	_pformat_)getattr)r   rE   r%   r   r   r   r   rQ     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   Z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   r^   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 bodyrA   Z	streamingFz.The body is a stream and will not be displayed)r7   _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    rK   )r`   Znewl)_strip_whitespacexmldomminidomparseStringZtoprettyxml)r   r*   Zstripped_bodyxml_domr   r   r   re   #  s   
z+SectionValuePrettyFormatter._get_pretty_xmlc                 C   s   t |}| |S r=   )ra   loadsrc   )r   r*   r^   r   r   r   rg   .  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)rj   rk   rl   rm   parsersexpat
ExpatErrorr   r*   r   r   r   rd   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>)rj   rk   rl   rm   joinZtoxml
splitlines)r   Z
xml_stringrn   r   r   r   ri   ;  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)
startswithra   ro   decoderJSONDecodeErrorrs   r   r   r   rf   A  s   

z.SectionValuePrettyFormatter._is_json_structurer=   )r   r   r   rQ   r\   rc   rh   re   rg   rd   ri   rf   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 )ShowCommandZshowzShows 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defaultZpositional_arg	help_textr   +zSpecifies 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   )Z_connect_to_history_db_validate_argsZ_get_output_stream_get_formatter_get_record_iteratorr   Z_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   r3   r   )r   r   r   Z_should_use_color
FORMATTERS)r   r   r   r   Zformat_typeZformatter_kwargsr   r   r   r     s   zShowCommand._get_formatterc                 C   s"   |j dkr
| j S | j|j S )Nr   )r   Z
_db_readerZiter_latest_recordsZiter_recordsr   r   r   r   r     s   

z ShowCommand._get_record_iteratorN)r   r   r   NAMEZDESCRIPTIONr   r   keysZ	ARG_TABLEr   r   r   r   r   r   r   r   r   K  s2    $r   )rY   ra   r   xml.parsers.expatrj   Zxml.dom.minidomr1   Zawscli.tabler   Zawscli.compatr   Z&awscli.customizations.history.commandsr   Z%awscli.customizations.history.filtersr   objectr   r   r/   r   r   r   r   r   <module>   s   / =E