U
    3dH                     @   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Zd dlZd dlZzd dl	Z
W n ek
rp   d dlZ
Y nX dd ZG dd deZG dd dejjZG dd	 d	ejjZG d
d dejjZG dd deZG dd deZdS )    Nc                    s    fdd}|S )Nc                    s   | t jkrtd tjt_rZtj rZtj rZt	j
dkrNt	|| | qt	| n0 rxtt| td nt| td d S )N   epdb)bdbZBdbQuitsysexit__excepthook__
excepthookstdoutisattystdindebugger__name__Zpost_mortemprint	tracebackprint_tb)typvaluetb
debug_flagdebugger_flag ?/tmp/pip-unpacked-wheel-d7dsrkjd/boto/roboto/awsqueryrequest.pyr   &   s    


z$boto_except_hook.<locals>.excepthookr   )r   r   r   r   r   r   boto_except_hook%   s    r   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )Linec                 C   s&   || _ || _|| _d| | _d| _d S )N%s	F)fmtdatalabellineprinted)selfr   r   r   r   r   r   __init__;   s
    
zLine.__init__c                 C   s   |  j d| 7  _ d S )Nr   )r   )r!   Zdatumr   r   r   appendB   s    zLine.appendc                 C   s   | j st| j d| _ d S )NT)r    r   r   r!   r   r   r   print_itE   s    
zLine.print_itN)r   
__module____qualname__r"   r#   r%   r   r   r   r   r   9   s   r   c                       s   e Zd Z fddZ  ZS )RequiredParamErrorc                    s$   || _ d| j  }tt| | d S )Nz#Required parameters are missing: %s)requiredsuperr(   r"   )r!   r)   s	__class__r   r   r"   L   s    
zRequiredParamError.__init__r   r&   r'   r"   __classcell__r   r   r,   r   r(   J   s   r(   c                       s   e Zd Z fddZ  ZS )EncoderErrorc                    s   d| }t t| | d S )NzError encoding value (%s))r*   r0   r"   )r!   	error_msgr+   r,   r   r   r"   S   s    zEncoderError.__init__r.   r   r   r,   r   r0   Q   s   r0   c                       s   e Zd Z fddZ  ZS )FilterErrorc                    s$   || _ d| j  }tt| | d S )NUnknown filters: %s)filtersr*   r2   r"   )r!   r4   r+   r,   r   r   r"   Y   s    
zFilterError.__init__r.   r   r   r,   r   r2   W   s   r2   c                   @   s^   e Zd ZedddZedd ZeZeZedd Zedd	 Z	ed
d Z
edd ZdS )EncoderNc                 C   sZ   |j drd S z"t| d|j }||||| W n" tk
rT   td|j Y nX d S )N_Zencode_zUnknown type: %s)name
startswithgetattrptypeAttributeErrorr0   )clsprpvr   Zmthdr   r   r   encode`   s    zEncoder.encodec                 C   s   |r
|}n|j }|||< d S Nr7   r<   r=   r>   r?   lr   r   r   r   encode_stringj   s    zEncoder.encode_stringc                 C   s    |r
|}n|j }d| ||< d S )Nz%drB   rC   r   r   r   encode_integeru   s    zEncoder.encode_integerc                 C   s*   |r
|}n|j }|rd}nd}|||< d S )NtruefalserB   rC   r   r   r   encode_boolean}   s    zEncoder.encode_booleanc                 C   s   |r
|}n|j }|||< d S rA   rB   rC   r   r   r   encode_datetime   s    zEncoder.encode_datetimec                 C   sJ   t j|}|r|}n|j}|d }t|D ]\}}||||d  < q,d S )Nz.%dr   )botoutilsmklistr7   	enumerate)r<   r=   r>   r?   rD   r   ir   r   r   r   encode_array   s    zEncoder.encode_array)N)r   r&   r'   classmethodr@   rE   Zencode_fileZencode_enumrF   rI   rJ   rP   r   r   r   r   r5   ^   s   	



r5   c                	   @   s   e Zd ZdZdZg Zg Zg Zi ZdddddddddZ	e
dd Zd	d
 Zdd Zdd Zedd Zedd Zedd Zd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(d)Zd*d+ ZdS )/AWSQueryRequestN stringintchoice)rT   integerrU   enumdatetimeZdateTimefilebooleanc                 C   s   | j S rA   )r   )r<   r   r   r   r7      s    zAWSQueryRequest.namec                 K   s@   || _ d | _d | _d | _d | _d | _g | _g | _i | _d | _	d S rA   )
argsparsercli_optionscli_argsZcli_output_format
connectionlist_markersitem_markersrequest_paramsconnection_argsr!   r\   r   r   r   r"      s    zAWSQueryRequest.__init__c                 C   s   |   S rA   rB   r$   r   r   r   __repr__   s    zAWSQueryRequest.__repr__c                 K   s   | j d kr| jf || _ | j S rA   )r`   ServiceClassre   r   r   r   get_connection   s    
zAWSQueryRequest.get_connectionc                 C   s   d }| j d k	r| j j}|S rA   )http_responsestatusr!   retvalr   r   r   rj      s    
zAWSQueryRequest.statusc                 C   s   d }| j d k	r| j j}|S rA   )ri   reasonrk   r   r   r   rm      s    
zAWSQueryRequest.reasonc                 C   s   d }| j d k	rt| j d}|S )NZ	requestId)aws_responser9   rk   r   r   r   
request_id   s    
zAWSQueryRequest.request_idc                    s   | j dg }dd | jD   fdd|D }|r@td| t| jD ]j\}}|d }||krJ|| jd|d  < ttj|| D ]*\}}t	
|| j|d	|d |d f  qqJd S )
Nr4   c                 S   s   g | ]}|d  qS rB   r   .0fr   r   r   
<listcomp>   s     z3AWSQueryRequest.process_filters.<locals>.<listcomp>c                    s   g | ]}| kr|qS r   r   rp   Zfilter_namesr   r   rs      s      r3   r7   zFilter.%d.Namer   zFilter.%d.Value.%d)r\   getFiltersr2   rN   rc   rK   rL   rM   r5   r@   )r!   r4   Zunknown_filtersrO   filterr7   jr   r   rt   r   process_filters   s    zAWSQueryRequest.process_filtersc                 K   s  | j | t| j | _d| j kr@| j d dkr@t|   dd | j| j D }| j| j D ]}|j	r||j	
dd}ntj|jd}d}|| j kr| j | }|dkr|j}|dk	r|j|kr||j |jr|jr||| j| nt|| j| || j krb| j|= qb|rg }| j| j D ]d}|j|kr(|jr`|j	r`|d|j|jf  n*|jrz|d	|j  n|d	|j  q(td
|tjd| j  | | j dS )a  
        Responsible for walking through Params defined for the request and:

        * Matching them with keyword parameters passed to the request
          constructor or via the command line.
        * Checking to see if all required parameters have been specified
          and raising an exception, if not.
        * Encoding each value into the set of request parameters that will
          be sent in the request to the AWS service.
        debug   c                 S   s   g | ]}|j s|jqS r   )optionalr7   )rq   r=   r   r   r   rs      s      z0AWSQueryRequest.process_args.<locals>.<listcomp>-r6   Nz(%s, %s)z(%s),zrequest_params: %s)r\   updatecopyrd   rK   Zset_stream_loggerr7   ParamsArgs	long_namereplacerL   pythonize_namedefaultremoverequest_paramencoderrc   r5   r@   
short_namer#   optparse_short_nameoptparse_long_namer(   joinlogrz   process_markersResponse)r!   r\   r)   parampython_namer   rD   r=   r   r   r   process_args   sJ    






zAWSQueryRequest.process_argsc                 C   s`   |r0|d dkr0|d D ]}|  ||d  qn,|r\|d dkr\| j| | j|d  d S )Ntypeobject
propertiesr7   array)r   ra   r#   rb   )r!   r   Z	prev_namepropr   r   r   r     s    zAWSQueryRequest.process_markersGETc                 K   s   | j f | |   | jf | j}|j|  | j|d| _| j | _	t
j| j	 | jjdkrt
jj| j| jd| _t
j| j| }|| j	 | jS t
jd| jj| jjf  t
jd| j	  || jj| jj| j	d S )N)verb   )Zlist_markerZitem_markerz%s %sz%s)r   ry   rh   rd   Zmake_requestr7   rc   ri   readbodyrK   r   rz   rj   ZjsonresponseElementra   rb   rn   Z
XmlHandlerparseerrorrm   ResponseError)r!   r   r\   connhr   r   r   send%  s0    


zAWSQueryRequest.sendc                 C   s   t | jd}|jddddd |jdddd	d
 |jddddd |jdddd |jddddd |jddddd |jdddd | jr| jjdddd | jjddddd | j| d S ) NzStandard Optionsz-Dz--debug
store_truezTurn on all debugging output)actionhelpz
--debuggerFz$Enable interactive debugger on error)r   r   r   z-Uz--urlstorez(Override service URL with value providedz--regionz Name of the region to connect toz-Iz--access-key-idzOverride access key valuez-Sz--secret-keyzOverride secret key valuez	--versionzDisplay version stringz--help-filtersz!Display list of available filtersz--filterr#   z name=valuez!A filter for limiting the results)r   metavarr   )optparseOptionGroupr]   
add_optionrv   groupadd_option_group)r!   r   r   r   r   add_standard_options<  s@    





z$AWSQueryRequest.add_standard_optionsc                 C   s   t |drB|jrBtd | jD ]}td|j|jf  qtd |jrRd| j	d< |j
rd|j
| j	d< |jrv|j| j	d< |jr|j| j	d	< |jr|j| j	d
< |jrtd td t|j|jt_d S )Nhelp_filterszAvailable filters:z%s	%sr   r{   rz   urlregionZaws_access_key_idZaws_secret_access_keyzversion x.xx)hasattrr   r   rv   r7   docr   r   rz   r\   r   r   Zaccess_key_idZ
secret_keyversionr   r   r   )r!   optionsr\   drw   r   r   r   process_standard_optionsV  s*    


z(AWSQueryRequest.process_standard_optionsc                 C   sL   d}dd | j D }|d|7 }| j D ]}|jr(|d|j|jf 7 }q(|S )Nzusage: %prog [options] c                 S   s   g | ]
}|j qS r   )r   )rq   ar   r   r   rs   o  s     z-AWSQueryRequest.get_usage.<locals>.<listcomp> z


	%s - %s)r   r   r   r   )r!   r+   rD   r   r   r   r   	get_usagem  s    
zAWSQueryRequest.get_usagec              	   C   s   t j| j|  d| _|   | jD ]}d  } }}|j| jkrP| j|j }d}|jdkr`d}n:|jdkrt	|j
dkr|j
d d }d	}n|jdkrd	}|s|dkr$|jr| jj|j|j|||j|jd
 q$|jr$| jj|j|||j|jd
 q$d S )N)descriptionusager   r[   r   r   r   r   r   r#   )r   r   choicesr   )r   OptionParserDescriptionr   r]   r   r   r:   
CLITypeMaplenitemscardinalityr   r   r   r   r   r   r   )r!   r   r:   r   r   r   r   r   build_cli_parserv  sB    




 
 z AWSQueryRequest.build_cli_parserc              
   C   s  | j s|   | j  \| _| _i }| | j| j| | jD ]}|jrV|jdd}nt	j
|j}t| j|}|jdkr|r|dkrtj }nNtj|}tj|}tj|rt|}| }|  n| j d|  |||< q<| jD ]d}|jr|jdd}nt	j
|j}d }|jdkrFt| jdkrL| jd }n| j}|||< q| j| t| jdr| jj ri }| jj D ]}|!d\}	}|||	< qd	| jkr| jd	 | n
|| jd	< z| " }
| #|
 W n t$k
r } zt%| t&d W 5 d }~X Y n | j'j(k
r\ } z"t%d
|j)|j*f  t&d W 5 d }~X Y np t	j+j,j-k
r } zt%d t&d W 5 d }~X Y n6 t.k
r } zt%| t&d W 5 d }~X Y nX d S )Nr}   r6   rZ   zUnable to read file: %sr   r   rw   =r4   zError(%s): %szUnable to find credentials.)/r]   r   
parse_argsr^   r_   r   r   r   r   rK   rL   r   r7   r9   r:   r   r   r   ospath
expanduser
expandvarsisfileopencloser   r   r   r   r\   r   r   rw   splitmaincli_formatterr(   r   r   rg   r   Z
error_codeerror_messageZrobotoZawsqueryserviceZNoCredentialsError	Exception)r!   r   r   Zp_namer   r   fpargrw   r7   responseeerrr   r   r   do_cli  sn    





zAWSQueryRequest.do_clic                 C   s   |d dkr|d D ]h}d|krn|d |kr8||d  }|d | j krn|d }|d dkrf|dd }| }| ||| qnb|d dkr|D ]P}t|||}t|tr|D ]}|||  qnt|tr|| |  qd S )	Nr   r   r   r7   r+   r   r   )	ra   upper_generic_cli_formatterr   
isinstancedictr#   
basestringr%   )r!   r   r   r   r   itemr   
field_namer   r   r   r     s(    


z&AWSQueryRequest._generic_cli_formatterc                 C   s   |r|  | j| dS )a  
        This method is responsible for formatting the output for the
        command line interface.  The default behavior is to call the
        generic CLI formatter which attempts to print something
        reasonable.  If you want specific formatting, you should
        override this method and do your own thing.

        :type data: dict
        :param data: The data returned by AWS.
        N)r   r   )r!   r   r   r   r   r     s    zAWSQueryRequest.cli_formatter)N)r   )rS   )r   r&   r'   rg   r   r   r   rv   r   r   rQ   r7   r"   rf   rh   propertyrj   rm   ro   ry   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rR      sJ   	



3

	?
rR   )r   r   rK   r   r   Zboto.exceptionZboto.roboto.awsqueryservicer   r   r   r   ImportErrorpdbr   r   r   	exceptionZBotoClientErrorr(   r0   r2   r5   rR   r   r   r   r   <module>   s&   >