B ㊇cã@sdZddlZddlZddlZddlZddlZddlmZddlm Z ddl m Z ddl m Z mZmZmZdZdZd Zdd d „ZGd d„dƒZGdd„deƒZGdd„deƒZGdd„deƒZGdd„deƒZGdd„deeƒZGdd„deƒZeeeeedœZdS)aîProtocol input serializes. This module contains classes that implement input serialization for the various AWS protocol types. These classes essentially take user input, a model object that represents what the expected input should look like, and it returns a dictionary that contains the various parts of a request. A few high level design decisions: * Each protocol type maps to a separate class, all inherit from ``Serializer``. * The return value for ``serialize_to_request`` (the main entry point) returns a dictionary that represents a request. This will have keys like ``url_path``, ``query_string``, etc. This is done so that it's a) easy to test and b) not tied to a particular HTTP library. See the ``serialize_to_request`` docstring for more details. Unicode ------- The input to the serializers should be text (str/unicode), not bytes, with the exception of blob types. Those are assumed to be binary, and if a str/unicode type is passed in, it will be encoded as utf-8. éN)Ú ElementTree)Úvalidate)Ú formatdate)Ú has_headerÚis_json_value_headerÚparse_to_aware_datetimeÚpercent_encodeÚiso8601z%Y-%m-%dT%H:%M:%SZz%Y-%m-%dT%H:%M:%S.%fZTcCs&t|ƒ}|r"t ¡}t ||¡}|S)N)Ú SERIALIZERSrZParamValidatorZParamValidationDecorator)Z protocol_nameZinclude_validationÚ serializerÚ validator©r úm/private/var/folders/8c/hx9_v10d5x38qmnzt13b7b8j1k3n5b/T/pip-target-x6xd5gna/lib/python/botocore/serialize.pyÚcreate_serializer?s   rc@sbeZdZdZeZdZdd„Zdd„Zdd„Z d d „Z d d „Z ddd„Z dd„Z dd„Zdd„Zd S)Ú SerializerÚPOSTzutf-8cCs tdƒ‚dS)aöSerialize parameters into an HTTP request. This method takes user provided parameters and a shape model and serializes the parameters to an HTTP request. More specifically, this method returns information about parts of the HTTP request, it does not enforce a particular interface or standard for an HTTP request. It instead returns a dictionary of: * 'url_path' * 'host_prefix' * 'query_string' * 'headers' * 'body' * 'method' It is then up to consumers to decide how to map this to a Request object of their HTTP library of choice. Below is an example return value:: {'body': {'Action': 'OperationName', 'Bar': 'val2', 'Foo': 'val1', 'Version': '2014-01-01'}, 'headers': {}, 'method': 'POST', 'query_string': '', 'host_prefix': 'value.', 'url_path': '/'} :param parameters: The dictionary input parameters for the operation (i.e the user input). :param operation_model: The OperationModel object that describes the operation. Úserialize_to_requestN)ÚNotImplementedError)ÚselfÚ parametersÚoperation_modelr r rrQs$zSerializer.serialize_to_requestcCsdd|jiddœ}|S)Nú/Úó)Úurl_pathÚ query_stringÚmethodÚheadersÚbody)ÚDEFAULT_METHOD)rÚ serializedr r rÚ_create_default_requestws z"Serializer._create_default_requestcCs|jdkrt}nt}| |¡S)Nr)Ú microsecondÚ ISO8601_MICROÚISO8601Ústrftime)rÚvalueÚtimestamp_formatr r rÚ_timestamp_iso8601†s zSerializer._timestamp_iso8601cCstt | ¡¡ƒS)N)ÚintÚcalendarÚtimegmÚ timetuple)rr&r r rÚ_timestamp_unixtimestamp�sz#Serializer._timestamp_unixtimestampcCs"t|tjƒr| |¡}t|dd�S)NT)Úusegmt)Ú isinstanceÚdatetimer-r)rr&r r rÚ_timestamp_rfc822�s  zSerializer._timestamp_rfc822NcCs:|dkr|j}| ¡}t|ƒ}t|d|›�ƒ}||ƒ}|S)NZ _timestamp_)ÚTIMESTAMP_FORMATÚlowerrÚgetattr)rr&r'Ú datetime_objÚ converterZ final_valuer r rÚ_convert_timestamp_to_str•sz$Serializer._convert_timestamp_to_strcCs|j d|¡S)NÚname)Ú serializationÚget)rÚshapeÚ default_namer r rÚ_get_serialized_namežszSerializer._get_serialized_namecCs,t|tƒr| |j¡}t |¡ ¡ |j¡S)N)r/ÚstrÚencodeÚDEFAULT_ENCODINGÚbase64Ú b64encodeÚstripÚdecode)rr&r r rÚ _get_base64£s  zSerializer._get_base64csZ|j}|dksd|krdS|d}|jj}dd„| ¡Dƒ}‡fdd„|Dƒ}|jf|ŽS)NZ hostPrefixcSs g|]\}}|j d¡r|‘qS)Z hostLabel)r9r:)Ú.0Úmemberr;r r rú ¶sz2Serializer._expand_host_prefix..csi|]}ˆ||“qSr r )rFr8)rr rú ºsz2Serializer._expand_host_prefix..)ZendpointÚ input_shapeÚmembersÚitemsÚformat)rrrZoperation_endpointZhost_prefix_expressionZ input_membersZ host_labelsZ format_kwargsr )rrÚ_expand_host_prefix«s zSerializer._expand_host_prefix)N)Ú__name__Ú __module__Ú __qualname__rÚdictÚMAP_TYPEr@rr!r(r-r1r7r=rErNr r r rrHs& rc@speZdZdZdd„Zddd„Zddd„Zdd d „Zdd d „Zdd d„Z ddd„Z ddd„Z ddd„Z dd„Z dS) ÚQuerySerializerr cCsŒ|j}| ¡}|j d|j¡|d<ddi|d<| ¡}|j|d<|jd|d<|dk rd| |||¡||d<|  ||¡}|dk rˆ||d <|S) Nrz Content-Typez0application/x-www-form-urlencoded; charset=utf-8rZActionZ apiVersionÚVersionrÚ host_prefix) rJr!Úhttpr:rrSr8ÚmetadataÚ _serializerN)rrrr;r Ú body_paramsrVr r rrÃs   z$QuerySerializer.serialize_to_requestrcCs*t|d|j›�|jƒ}|||||d�dS)NZ_serialize_type_)Úprefix)r4Ú type_nameÚ_default_serialize)rr r&r;r[rr r rrYÛs  zQuerySerializer._serializec CsV|j}xJ| ¡D]>\}}||}| ||¡}|r>|›d|›�}| ||||¡qWdS)NÚ.)rKrLr=rY) rr r&r;r[rKÚkeyÚ member_shapeZ member_prefixr r rÚ_serialize_type_structureês z)QuerySerializer._serialize_type_structurec Csº|sd||<dS| |¡r\|}|jj d¡rz|j|jdd�}d | d¡dd…|g¡}n|jj dd¡}|›d|›�}x:t|dƒD],\}} |›d|›�} |j} | || | | ¡q†WdS)Nrr8)r<r^éÿÿÿÿrGé) Ú_is_shape_flattenedrGr9r:r=ÚjoinÚsplitÚ enumeraterY) rr r&r;r[Z list_prefixr8Z list_nameÚiÚelementÚelement_prefixÚ element_shaper r rÚ_serialize_type_listós  z$QuerySerializer._serialize_type_listcCs¢| |¡r|}nd|}|d}|j}|j}|j|dd�} | |d¡} xVt|dƒD]H\} } |j| | d�} |j| | d�}| || || ¡| ||| ||¡qRWdS)Nz%s.entryz .{i}.{suffix}r_)r<r&rc)rhÚsuffix)rdr_r&r=rgrMrY)rr r&r;r[Z full_prefixÚtemplateZ key_shapeZ value_shapeZ key_suffixZ value_suffixrhr_Z key_prefixZ value_prefixr r rÚ_serialize_type_maps  z#QuerySerializer._serialize_type_mapcCs| |¡||<dS)N)rE)rr r&r;r[r r rÚ_serialize_type_blobsz$QuerySerializer._serialize_type_blobcCs| ||j d¡¡||<dS)NÚtimestampFormat)r7r9r:)rr r&r;r[r r rÚ_serialize_type_timestampsz)QuerySerializer._serialize_type_timestampcCs|rd||<nd||<dS)NÚtrueÚfalser )rr r&r;r[r r rÚ_serialize_type_booleans z'QuerySerializer._serialize_type_booleancCs |||<dS)Nr )rr r&r;r[r r rr]%sz"QuerySerializer._default_serializecCs |j d¡S)NÚ flattened)r9r:)rr;r r rrd(sz#QuerySerializer._is_shape_flattenedN)r)r)r)r)r)r)r)r)rOrPrQr2rrYrarlrorprrrur]rdr r r rrT¿s       rTc@s"eZdZdZdd„Zddd„ZdS) Ú EC2SerializeraUEC2 specific customizations to the query protocol serializers. The EC2 model is almost, but not exactly, similar to the query protocol serializer. This class encapsulates those differences. The model will have be marked with a ``protocol`` of ``ec2``, so you don't need to worry about wiring this class up correctly. cCsHd|jkr|jdSd|jkr@|jd}|d ¡|dd…S|SdS)NZ queryNamer8rrc)r9Úupper)rr;r<r8r r rr=6s     z"EC2Serializer._get_serialized_namerc Cs@x:t|dƒD],\}}|›d|›�}|j}| ||||¡q WdS)Nrcr^)rgrGrY) rr r&r;r[rhrirjrkr r rrlCsz"EC2Serializer._serialize_type_listN)r)rOrPrQÚ__doc__r=rlr r r rrw,s rwc@sReZdZdZdd„Zddd„Zdd„Zd d „Zd d „Zd d„Z dd„Z dd„Z dS)ÚJSONSerializerZ unixtimestampc Cs¦d |jd|j¡}|jd}| ¡}|j d|j¡|d<|d|dœ|d<| ¡}|j}|dk rp|  |||¡t   |¡  |j ¡|d<| ||¡}|dk r¢||d <|S) Nz{}.{}Z targetPrefixZ jsonVersionrzapplication/x-amz-json-%s)z X-Amz-Targetz Content-TyperrrV)rMrXr8r!rWr:rrSrJrYÚjsonÚdumpsr?r@rN) rrrÚtargetZ json_versionr rrJrVr r rrMs$  z#JSONSerializer.serialize_to_requestNcCs&t|d|j|jƒ}|||||ƒdS)Nz_serialize_type_%s)r4r\r])rr r&r;r_rr r rrYgs zJSONSerializer._serializec Csx|jr|||<nd|dk r,| ¡}|||<|}|j}x@| ¡D]4\}}||} d| jkr`| jd}| ||| |¡qt||dd…dd�||<qt||ƒ||<qW|jf|ŽS)Nz{(.*?)}ú+rbz/~)Úsafe)ÚreÚfindallÚendswithrrM)rZ uri_templateÚparamsZencoded_paramsZtemplate_paramr r rrŠðs z'BaseRestSerializer._render_uri_templatec Cs®|j d¡}| ||¡r8| |d¡}| |¡}||d<nr|dk rv| |¡}|dk rh| |||¡|d<qª| ¡|d<n4|dr”| |d|¡|d<n| |¡rª| ¡|d<dS)NÚpayloadrrrˆ)r9r:Ú_has_streaming_payloadÚ_encode_payloadÚ_serialize_body_paramsÚ_serialize_empty_bodyÚ_requires_empty_body) rrŽrr r;r�Zpayload_memberZ body_payloadrZr r rr‹s        z%BaseRestSerializer._serialize_payloadcCsdS)Nrr )rr r rr›#sz(BaseRestSerializer._serialize_empty_bodycCsdS)z¡ Some protocols require varied Content-Type headers depending on user input. This allows subclasses to apply this conditionally. Nr )rr r;r�r r rrŒ&sz*BaseRestSerializer._serialize_content_typecCsdS)z“ Some protocols require a specific body to represent an empty payload. This allows subclasses to apply this conditionally. Fr )rr;r r rrœ.sz'BaseRestSerializer._requires_empty_bodycCs|dk o||jdkS)z5Determine if payload is streaming (a blob or string).N)ÚblobÚstring)r\)rr—r�r r rr˜5sz)BaseRestSerializer._has_streaming_payloadcCst|tƒr| |j¡S|S)N)r/r>r?r@)rrr r rr™<s  z"BaseRestSerializer._encode_payloadcCs4||}|j d¡}|j d|¡}|dkr8||d|<nø|dkrÀt|tƒrZ|d |¡ndt|tƒr~t|ƒ ¡}||d|<n@|jdkr²|j d|j ¡} |  || ¡} | |d|<n ||d|<np|d k�r||} |sä| jd krädS|  | |¡} t| ƒ|d |<n.|d k�r$|} |  | |d |¡n ||d |<dS) NÚlocationr8rƒr†r„r‡Ú timestamprqr…Úlistrrˆ) r9r:r/rRÚupdateÚboolr>r3r\ÚQUERY_STRING_TIMESTAMP_FORMATr7Ú_convert_header_valueÚ_do_serialize_header_map)rrŽr�r�r�rGrŸÚkey_nameZbool_strr'r r;r&Ú header_prefixr r rr‰As:         z(BaseRestSerializer._partition_parameterscCs*x$| ¡D]\}}||}|||<q WdS)N)rL)rr¨rZ user_inputr_ÚvalZfull_keyr r rr¦rsz+BaseRestSerializer._do_serialize_header_mapcCs tdƒ‚dS)Nrš)r)rr–r;r r rršwsz)BaseRestSerializer._serialize_body_paramscsˆˆjdkr.ú,)r«ú:)Ú separators)r\rr*r+Ú utctimetupler9r:ÚHEADER_TIMESTAMP_FORMATr7rerrEr{r|)rr;r&r5r r'Zconverted_valuer )rr;rr¥zs      z(BaseRestSerializer._convert_header_valueN)rOrPrQryr¤r¯ZKNOWN_LOCATIONSrrŠr‹r›rŒrœr˜r™r‰r¦ršr¥r r r rr‚£s   ; 1r‚c@s,eZdZdd„Zdd„Zdd„Zdd„Zd S) ÚRestJSONSerializercCsdS)Ns{}r )rr r rr›’sz(RestJSONSerializer._serialize_empty_bodycCs*x$|j ¡D]\}}d|jkr dSq WdS)zq Serialize an empty JSON object whenever the shape has members not targeting a location. rŸTF)rKrLr9)rr;rGr©r r rrœ•s z'RestJSONSerializer._requires_empty_bodycCsN|j d¡}| ||¡rdS|ddk}td|dƒ}|rJ|sJd|dd<dS)z?Set Content-Type to application/json for all structured bodies.r—Nrrz Content-Typerzapplication/json)r9r:r˜r)rr r;r�r—Zhas_bodyZhas_content_typer r rrŒŸs   z*RestJSONSerializer._serialize_content_typecCs(| ¡}| |||¡t |¡ |j¡S)N)rSrYr{r|r?r@)rr–r;Zserialized_bodyr r rrš«sz)RestJSONSerializer._serialize_body_paramsN)rOrPrQr›rœrŒršr r r rr°‘s  r°c@sXeZdZdZdd„Zdd„Zdd„Zdd „Zd d „Zd d „Z dd„Z dd„Z dd„Z dS)ÚRestXMLSerializerr cCs@|jd}t d¡}| ||||¡t|ƒd}tj||jd�S)Nr8rr)Úencoding)r9rZElementrYr¡Útostringr@)rr–r;Z root_nameZ pseudo_rootZ real_rootr r rrš´s    z(RestXMLSerializer._serialize_body_paramscCs&t|d|j|jƒ}|||||ƒdS)Nz_serialize_type_%s)r4r\r])rr;r–Úxmlnoder8rr r rrY»s zRestXMLSerializer._serializec Cs¼t ||¡}d|jkrL|jd}d}| d¡r>|d|d7}|d|j|<xj| ¡D]^\}} |j|} | j d|¡} | dkr‚dS| j d¡r¤| jd} | |j| <qV| | | || ¡qVWdS)NZ xmlNamespaceZxmlnsr[z:%srƒr8Z xmlAttribute)rÚ SubElementr9r:ÚattribrLrKrY) rr´r–r;r8Zstructure_nodeZnamespace_metadataZattribute_namer_r&r`Ú member_nameZxml_attribute_namer r rraÃs"        z+RestXMLSerializer._serialize_type_structurec CsX|j}|j d¡r|}|}n|j dd¡}t ||¡}x|D]}| ||||¡qr¼)rr´r–r;r8rºr r rr]s z$RestXMLSerializer._default_serializeN) rOrPrQr2ršrYrarlrorurprrr]r r r rr±±s  r±)Zec2Úqueryr{z rest-jsonzrest-xml)T)ryrAr*r0r{r“Z xml.etreerZbotocorerZbotocore.compatrZbotocore.utilsrrrrZDEFAULT_TIMESTAMP_FORMATr$r#rrrTrwrzr‚r°r±r r r r rÚ's4    wmYo f