Current File : //usr/lib/python2.7/site-packages/requestbuilder/request.pyo
�
�GXc@@s]ddlmZmZddlZddlZddlmZddlZddlZ	ddl
Z
ddlZddlZddl
Z
ddlmZmZddlmZddlmZddlmZddlmZdd	lmZd
efd��YZdefd
��YZdd�Zd�Zde fd��YZ!de fd��YZ"dS(i(tabsolute_importtprint_functionN(tpartial(tEMPTYtPARAMS(tBaseCommand(tServerError(tBaseService(taggregate_subclass_fields(tparse_listdelimited_aws_xmltBaseRequestcB@s�eZdZeZdZdZdZe	fZ
ddd�Zed��Z
e
jd��Z
ed��Zed��Zd�Zd�Zd	�Zed
��Zd�Zd�Zd
�Zd�Zd�Zd�Zd�Zd�ZRS(s
    The basis for a command line tool that represents a request.  The data for
    this request are stored in a few instance members:
     - method:   the HTTP method to use (e.g. 'GET').  Defaults to self.METHOD.
     - path:     the path to append to the service's path (e.g. 'sub/dir')
     - headers:  a dict of HTTP headers
     - params:   a dict of query parameters
     - body:     a string or file object containing a request body, or a dict
                 to pass to the server as form data

    This specialization of BaseCommand that implements main() as a three-step
    process:
     - preprocess():   do any processing needed before sending the request,
                       such as parsing complex command line arguments and
                       storing them in self.params, self.headers, and so forth.
     - send():         send this request to the server using the data stored
                       in its attributes, parse it using self.parse_result(),
                       and return it
     - postprocess():  given a parsed response, do any processing needed before
                       main() returns the response.

    Most requests need only implement preprocess().  Requests whose workflows
    involve sending other requests often do so in postprocess(), where the
    result of the request is known.

    Important members of this class, in addition to those inherited from
    BaseCommand, include:
     - SERVICE_CLASS:  a class corresponding to the web service in use
     - AUTH_CLASS:     a class corresponding to the authentication method
                       to use, if any
     - NAME:           a string representing the name of this request.
                       This defaults to the class's name.
     - METHOD:         the HTTP method to use by default
    tGETcK@sq|j|_d|_i|_i|_d|_i|_d|_t	|_
||_||_t
j||�dS(Nt(tMETHODtmethodtNonetpaththeaderstparamstbodytfilestresponsetFalset_BaseRequest__configuredt_BaseRequest__autht_BaseRequest__serviceRt__init__(tselftservicetauthtkwargs((s:/usr/lib/python2.7/site-packages/requestbuilder/request.pyROs									cC@sD|jr=|jdk	r=|j|jd|jj�|_n|jS(Ntloglevel(Rt
AUTH_CLASSRtconfigtlogtlevel(R((s:/usr/lib/python2.7/site-packages/requestbuilder/request.pyRas$cC@s
||_dS(N(R(Rtnewval((s:/usr/lib/python2.7/site-packages/requestbuilder/request.pyRiscC@sD|jr=|jdk	r=|j|jd|jj�|_n|jS(NR(Rt
SERVICE_CLASSRR!R"R#(R((s:/usr/lib/python2.7/site-packages/requestbuilder/request.pyRmscK@sU|jd|j�|jd|j�|jd|jj�|d|j|�}|S(NRRRR!(t
setdefaultRRR"R#R!(tclstotherRtnew((s:/usr/lib/python2.7/site-packages/requestbuilder/request.pyt
from_otherts
cC@sotj|�}|jdk	r=|jt|jjd��n|jdk	rk|jt|jjd��n|S(NtARGS(Rtcollect_arg_objsRRtextendRt	__class__R(Rtarg_objs((s:/usr/lib/python2.7/site-packages/requestbuilder/request.pyR,}scC@satj|�|jdk	r5|jjj|j�n|jdk	r]|jjj|j�ndS(N(Rtdistribute_argsRRtargstupdateR(R((s:/usr/lib/python2.7/site-packages/requestbuilder/request.pyR0�s

cC@sXtj|�|jdk	r,|jj�n|jdk	rK|jj�nt|_dS(N(Rt	configureRRRtTrueR(R((s:/usr/lib/python2.7/site-packages/requestbuilder/request.pyR3�s
cC@s|jp|jjS(N(tNAMER.t__name__(R((s:/usr/lib/python2.7/site-packages/requestbuilder/request.pytname�scC@s|js|jjd�nt|jp+i�}|jd|jj��yb|jj	d|j
d|jd|d|jd|j
d|jd	|j�|_|j|j�SWn]tk
r}|j|_y |jdk	r�|jjnWntk
r�nX|j|�SXdS(
Ns1send() called before configure(); bugs may results
User-AgentRRRRtdataRR(RR"twarntdictRR&tsuitetget_user_agentRtsend_requestRRRRRRRtparse_responseRRtcontenttRuntimeErrorthandle_server_error(RRterr((s:/usr/lib/python2.7/site-packages/requestbuilder/request.pytsend�s$	
cC@sg|jjdditd6�|jj|jjditd6�|jjd�|jjd��dS(Ns-- response content --
textratappends-- end of response content --sresult: failure(R"tdebugR4Rttexttinfo(RRB((s:/usr/lib/python2.7/site-packages/requestbuilder/request.pyRA�s
#cC@s|S(N((RR((s:/usr/lib/python2.7/site-packages/requestbuilder/request.pyR>�scK@sy|jjdditd6�t|jjd���.}t||jtj�}|||�}WdQX|jjd�|S(Ns-- response content --
RDREi@s-- end of response content --(	R"RFR4t_IteratorFileObjAdapterRtiter_contentt_ReadLoggingFileWrappertloggingtDEBUG(RRt
parse_funcRtcontent_fileobjtlogged_fileobjtparsed_response((s:/usr/lib/python2.7/site-packages/requestbuilder/request.pytlog_and_parse_response�scC@s'|j�|j�}|j|�|S(s
        The main processing method for this type of request.  In this method,
        inheriting classes generally populate self.headers, self.params, and
        self.body with information gathered from self.args or elsewhere,
        call self.send, and return the response.
        (t
preprocessRCtpostprocess(RR((s:/usr/lib/python2.7/site-packages/requestbuilder/request.pytmain�s

cC@sdS(N((R((s:/usr/lib/python2.7/site-packages/requestbuilder/request.pyRS�scC@sdS(N((RR((s:/usr/lib/python2.7/site-packages/requestbuilder/request.pyRT�scC@s�t|t�rldjtjjtjd�|j��}t	|dtj
�|jr\�ntjd�nt
j||�dS(Ns{0}: {1}itfilei(t
isinstanceRtformattosRtbasenametsystargvtformat_for_clitprinttstderrRFtexitRthandle_cli_exception(RRBtmsg((s:/usr/lib/python2.7/site-packages/requestbuilder/request.pyRa�s	N(R6t
__module__t__doc__RR%RR R5R
RtDEFAULT_ROUTESRtpropertyRtsetterRtclassmethodR*R,R0R3R7RCRAR>RRRURSRTRa(((s:/usr/lib/python2.7/site-packages/requestbuilder/request.pyR
$s."			
										tAWSQueryRequestcB@seeZdZgZgZd�Zd�Zed��Z	d�Z
d�Zdd�Ze
d�ZRS(cC@sytj|||�|jru|jddddddddd	d
ttd|j��|j�|_d|j	d<ndS(
Ns--filtertmetavars
NAME=VALUEtactionREtdesttfiltersthelps,restrict results to those that meet criteriattypetfilter_objs(N(
R
t_populate_parsertFILTERStadd_argumentRt
_parse_filtert#_AWSQueryRequest__build_filter_helptepilogRt_arg_routes(RtparserR/((s:/usr/lib/python2.7/site-packages/requestbuilder/request.pyRq�s	cC@sUtj|�d|jkrQt|jjd��|jd<|jf|jd<ndS(NRmtFilter(R
tprocess_cli_argsR1t_process_filterstpopRRw(R((s:/usr/lib/python2.7/site-packages/requestbuilder/request.pyRz�s
cC@s|jS(N(R7(R((s:/usr/lib/python2.7/site-packages/requestbuilder/request.pyRk�scC@s3|j}tj|j�}|j|j�}|j|d<|jpL|jj|d<t|�}xI|D]A}|j�j	d�rft
ddid�d6��||<qfqfW|jjd|�|j
j�dkr�|jjd	�||_i|_n|jjd
�||_ztj|�SWd||_XdS(NtActiontVersiontpasswordtREDACTEDcS@sdS(Ns
<redacted>((R((s:/usr/lib/python2.7/site-packages/requestbuilder/request.pyt<lambda>st__repr__sparameters: %stPOSTs)sending flattened parameters as form datas,sending flattened parameters as query string((Rtcopytdeepcopytflatten_paramsRktAPI_VERSIONRR:tlowertendswithRoR"RHRtupperRFRR
RC(Rtorig_paramsRtredacted_paramstkey((s:/usr/lib/python2.7/site-packages/requestbuilder/request.pyRCs(	

'		cC@s3|j|td|j�}|t|j��dS(Nt	list_tagsi(RRR	t	LIST_TAGStlisttkeys(RRt
response_dict((s:/usr/lib/python2.7/site-packages/requestbuilder/request.pyR>sc	C@s�i}|dkrnnt|t�rRx\tj|�D]\}}|r[dj||�}nt|�}t|t�s�t|t�r�|j|j	||��q4t|t
�r�t|�j�||<q4t|t�r�|j
�||<q4t|t�r
t|�||<q4|s|dkr2t|�||<q4|tkr4d||<q4q4Wn1t|t�rmxt|d�D]�\}}|r�dj||�}nt|�}t|t�s�t|t�r�|j|j	||��qqt|t
�rt|�j�||<qqt|t�r(|j
�||<qq|s:|dkrMt|�||<qq|tkrqd||<qqqqWntd|jj��|S(s
        Given a possibly-nested dict of args and an arg routing
        destination, transform each element in the dict that matches the
        corresponding arg routing table into a simple dict containing
        key-value pairs suitable for use as query parameters.  This
        implementation flattens dicts and lists into the format given
        by AWS query APIs, which use dotted lists of dict keys and list
        indices to indicate nested structures.

        Keys with non-boolean, non-zero values that evaluate as false
        are ignored.  To include an empty string as a parameter, pass
        EMPTY (the object, not the string) as its value.

        Examples:
          in:  {'InstanceId': 'i-12345678', 'PublicIp': '1.2.3.4'}
          out: {'InstanceId': 'i-12345678', 'PublicIp': '1.2.3.4'}

          in:  {'RegionName': ['us-east-1', 'us-west-1']}
          out: {'RegionName.1': 'us-east-1',
                'RegionName.2': 'us-west-1'}

          in:  {'Filter': [{'Name':  'image-id',
                            'Value': ['ami-12345678']},
                           {'Name':  'instance-type',
                            'Value': ['m1.small', 't1.micro']}],
                'InstanceId': ['i-24680135']}
          out: {'Filter.1.Name':    'image-id',
                'Filter.1.Value.1': 'ami-12345678',
                'Filter.2.Name':    'instance-type',
                'Filter.2.Value.1': 'm1.small',
                'Filter.2.Value.2': 't1.micro',
                'InstanceId.1':     'i-24680135'}
        s{0}.{1}iRisnon-flattenable type: N(RRWR:tsixt	iteritemsRXtstrR�R2R�tboolR�RVtreadtfloatRt	enumeratet	TypeErrorR.R6(	RR1tprefixt	flattenedR�tvaltprefixed_keyti_itemtitem((s:/usr/lib/python2.7/site-packages/requestbuilder/request.pyR�&sJ"c
C@s�dtjkr)dtjkr)|r)dSdg}xB|jD]7}|jtjkrZq<q<|jr_|jjd�\}}}|jd�r�|jd�r�|dtj	|�}n	|j}t
|j�dkrdj|j�}tj
d	td
|ddd�}	n:|jd
|j�tj
d	td
ddddd�}	|j|	j|��q<|jd
|j�q<Wdj|�S(s�
        Return a pre-formatted help string for all of the filters defined in
        self.FILTERS.  The result is meant to be used as command line help
        output.
        s-hs--helpRsallowed filter names:s
t is  {0:<20}  tfix_sentence_endingstinitial_indenttsubsequent_indentis  (R[R\RrRntargparsetSUPPRESSt	partitiont
startswithttextwraptdedenttlenR7RXtTextWrapperR4RER-twraptjoin(
Rtforcet	helplinest
filter_objtfirstt_trestR?t	firstlinetwrapper((s:/usr/lib/python2.7/site-packages/requestbuilder/request.pyt__build_filter_helpus.%					
N(R6RcRR�RrR�RqRzRfRkRCR>R�RRu(((s:/usr/lib/python2.7/site-packages/requestbuilder/request.pyRi�s				OcC@sfg|pgD]}|j|�r
|^q
}|sUdj|�}tj|��n|dj|�S(s
    Given a "key=value" string given as a command line parameter, return a pair
    with the matching filter's dest member and the given value after converting
    it to the type expected by the filter.  If this is impossible, an
    ArgumentTypeError will result instead.
    s""{0}" matches no available filtersi(tmatches_argvalRXR�tArgumentTypeErrortconvert(t
filter_strRptobjRb((s:/usr/lib/python2.7/site-packages/requestbuilder/request.pyRt�scC@s~i}x;|piD]-\}}|j|g�||j|�qWgtj|�D] \}}i|d6|d6^qT}|S(s�
    Change filters from the [(key, value), ...] format given at the command
    line to [{'Name': key, 'Value': [value, ...]}, ...] format, which
    flattens to the form the server expects.
    tNametValue(R&RER�R�(tcli_filterstfilter_argsR�R�R7tvaluesRm((s:/usr/lib/python2.7/site-packages/requestbuilder/request.pyR{�s3RIcB@sGeZd�Zd�Zd�Zed��Zd�Zdd�ZRS(cC@s(||_g|_t|_d|_dS(Ni(t_sourcet_buflistRt_closedt_len(Rtsource((s:/usr/lib/python2.7/site-packages/requestbuilder/request.pyR�s			cC@s|S(N((R((s:/usr/lib/python2.7/site-packages/requestbuilder/request.pyt	__enter__�scG@s|j�dS(N(tclose(RR1((s:/usr/lib/python2.7/site-packages/requestbuilder/request.pyt__exit__�scC@s|jS(N(R�(R((s:/usr/lib/python2.7/site-packages/requestbuilder/request.pytclosed�scC@s"|jsd|_t|_ndS(N(R�RR�R4(R((s:/usr/lib/python2.7/site-packages/requestbuilder/request.pyR��s		i����cC@s2|dks|dkrcx!|jD]}|jj|�q"Wdj|j�}g|_d|_n�x`|j|kr�y8t|j�}|jj|�|jt|�7_Wqftk
r�PqfXqfWdj|j�}t|�|}g|_d|_|dkr.||g|_||_|| }n|S(NiR(	RR�R�RER�R�tnextR�t
StopIteration(Rtsizetchunktresultt	extra_len((s:/usr/lib/python2.7/site-packages/requestbuilder/request.pyR��s,	
				(	R6RcRR�R�RfR�R�R�(((s:/usr/lib/python2.7/site-packages/requestbuilder/request.pyRI�s				RKcB@seZd�Zdd�ZRS(cC@s||_||_||_dS(N(tfileobjtloggerR#(RR�R�R#((s:/usr/lib/python2.7/site-packages/requestbuilder/request.pyR�s		i����cC@s9|jj|�}|jj|j|ditd6�|S(NRDRE(R�R�R�R"R#R4(RR�R�((s:/usr/lib/python2.7/site-packages/requestbuilder/request.pyR��s#(R6RcRR�(((s:/usr/lib/python2.7/site-packages/requestbuilder/request.pyRK�s	(#t
__future__RRR�R�t	functoolsRRLtos.pathRYR[R�twarningsR�trequestbuilderRRtrequestbuilder.commandRtrequestbuilder.exceptionsRtrequestbuilder.serviceRtrequestbuilder.utilRtrequestbuilder.xmlparseR	R
RiRRtR{tobjectRIRK(((s:/usr/lib/python2.7/site-packages/requestbuilder/request.pyt<module>s*��	0