GXc@@s]ddlmZmZddlZddlZddlmZddlZddlZ ddl Z ddl Z ddl Z ddl Z ddlmZmZddlmZddlmZddlmZddlmZdd lmZd efd YZd efd YZddZdZde fdYZ!de fdYZ"dS(i(tabsolute_importtprint_functionN(tpartial(tEMPTYtPARAMS(t BaseCommand(t ServerError(t BaseService(taggregate_subclass_fields(tparse_listdelimited_aws_xmlt BaseRequestcB@seZdZeZdZdZdZe fZ dddZ e dZ e jdZ e dZedZdZdZd Ze d Zd Zd Zd ZdZdZdZdZdZRS(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|jjdn|jdk rk|jt|jjdn|S(NtARGS(Rtcollect_arg_objsRRtextendRt __class__R(Rtarg_objs((s:/usr/lib/python2.7/site-packages/requestbuilder/request.pyR,}scC@satj||jdk r5|jjj|jn|jdk r]|jjj|jndS(N(Rtdistribute_argsRRtargstupdateR(R((s:/usr/lib/python2.7/site-packages/requestbuilder/request.pyR0s  cC@sXtj||jdk r,|jjn|jdk rK|jjnt|_dS(N(Rt configureRRRtTrueR(R((s:/usr/lib/python2.7/site-packages/requestbuilder/request.pyR3s  cC@s|jp|jjS(N(tNAMER.t__name__(R((s:/usr/lib/python2.7/site-packages/requestbuilder/request.pytnamescC@s|js|jjdnt|jp+i}|jd|jjyb|jj d|j d|j d|d|j d|j d|jd |j|_|j|jSWn]tk r }|j|_y |jdk r|jjnWntk rnX|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.pytsends$    cC@sg|jjdditd6|jj|jjditd6|jjd|jjddS(Ns-- response content -- textratappends-- end of response content --sresult: failure(R"tdebugR4Rttexttinfo(RRB((s:/usr/lib/python2.7/site-packages/requestbuilder/request.pyRAs #cC@s|S(N((RR((s:/usr/lib/python2.7/site-packages/requestbuilder/request.pyR>scK@sy|jjdditd6t|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_responses  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 preprocessRCt postprocess(RR((s:/usr/lib/python2.7/site-packages/requestbuilder/request.pytmains   cC@sdS(N((R((s:/usr/lib/python2.7/site-packages/requestbuilder/request.pyRSscC@sdS(N((RR((s:/usr/lib/python2.7/site-packages/requestbuilder/request.pyRTscC@st|trldjtjjtjd|j}t |dtj |j r\ntj dnt j||dS(Ns{0}: {1}itfilei(t isinstanceRtformattosRtbasenametsystargvtformat_for_clitprinttstderrRFtexitRthandle_cli_exception(RRBtmsg((s:/usr/lib/python2.7/site-packages/requestbuilder/request.pyRas N(R6t __module__t__doc__RR%RR R5R RtDEFAULT_ROUTESRtpropertyRtsetterRt classmethodR*R,R0R3R7RCRAR>RRRURSRTRa(((s:/usr/lib/python2.7/site-packages/requestbuilder/request.pyR $s."         tAWSQueryRequestcB@seeZdZgZgZdZdZedZ dZ dZ ddZ e dZRS(cC@sytj||||jru|jddddddddd d ttd |j|j|_d |j d((R((s:/usr/lib/python2.7/site-packages/requestbuilder/request.pytst__repr__sparameters: %stPOSTs)sending flattened parameters as form datas,sending flattened parameters as query string((Rtcopytdeepcopytflatten_paramsRkt API_VERSIONRR:tlowertendswithRoR"RHRtupperRFRR RC(Rt orig_paramsRtredacted_paramstkey((s:/usr/lib/python2.7/site-packages/requestbuilder/request.pyRCs(    '   cC@s3|j|td|j}|t|jdS(Nt list_tagsi(RRR t LIST_TAGStlisttkeys(RRt response_dict((s:/usr/lib/python2.7/site-packages/requestbuilder/request.pyR>sc C@si}|dkrnnt|trRx\tj|D]\}}|r[dj||}n t|}t|tst|tr|j|j ||q4t|t rt|j ||RRRu(((s:/usr/lib/python2.7/site-packages/requestbuilder/request.pyRis    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_argvalRXRtArgumentTypeErrortconvert(t filter_strRptobjRb((s:/usr/lib/python2.7/site-packages/requestbuilder/request.pyRts 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&RERR(t cli_filterst filter_argsRRR7tvaluesRm((s:/usr/lib/python2.7/site-packages/requestbuilder/request.pyR{s3RIcB@sGeZdZdZdZedZdZddZRS(cC@s(||_g|_t|_d|_dS(Ni(t_sourcet_buflistRt_closedt_len(Rtsource((s:/usr/lib/python2.7/site-packages/requestbuilder/request.pyRs   cC@s|S(N((R((s:/usr/lib/python2.7/site-packages/requestbuilder/request.pyt __enter__scG@s|jdS(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.pytclosedscC@s"|jsd|_t|_ndS(N(RRRR4(R((s:/usr/lib/python2.7/site-packages/requestbuilder/request.pyRs  icC@s2|dks|dkrcx!|jD]}|jj|q"Wdj|j}g|_d|_nx`|j|kry8t|j}|jj||jt|7_Wqftk rPqfXqfWdj|j}t||}g|_d|_|dkr.|| g|_||_|| }n|S(NiR ( RRRRERRtnextRt StopIteration(Rtsizetchunktresultt extra_len((s:/usr/lib/python2.7/site-packages/requestbuilder/request.pyRs,        ( R6RcRRRRfRRR(((s:/usr/lib/python2.7/site-packages/requestbuilder/request.pyRIs     RKcB@seZdZddZRS(cC@s||_||_||_dS(N(tfileobjtloggerR#(RRRR#((s:/usr/lib/python2.7/site-packages/requestbuilder/request.pyRs  icC@s9|jj|}|jj|j|ditd6|S(NRDRE(RRRR"R#R4(RRR((s:/usr/lib/python2.7/site-packages/requestbuilder/request.pyRs#(R6RcRR(((s:/usr/lib/python2.7/site-packages/requestbuilder/request.pyRKs (#t __future__RRRRt functoolsRRLtos.pathRYR[RtwarningsRtrequestbuilderRRtrequestbuilder.commandRtrequestbuilder.exceptionsRtrequestbuilder.serviceRtrequestbuilder.utilRtrequestbuilder.xmlparseR R RiRRtR{tobjectRIRK(((s:/usr/lib/python2.7/site-packages/requestbuilder/request.pyts*          0