File: //lib/python3.6/site-packages/botocore/__pycache__/parsers.cpython-36.pyc
3
�T_=� � @ sz d Z ddlZddlZddlZddlZddlmZmZmZ ddl m
Z
mZ ddlm
Z
mZmZmZ eje�Ze
ZG dd� de�Zdd � Zd
d� ZG dd
� d
e�ZG dd� de�ZG dd� de�ZG dd� de�ZG dd� de�ZG dd� de�ZG dd� de�Z G dd� de e�Z!G dd� de e�Z"G dd� de�Z#G d d!� d!e�Z$G d"d#� d#e$e�Z%G d$d%� d%e$e�Z&eee#e%e&d&�Z'dS )'a� Response parsers for the various protocol types.
The module contains classes that can take an HTTP response, and given
an output shape, parse the response into a dict according to the
rules in the output shape.
There are many similarities amongst the different protocols with regard
to response parsing, and the code is structured in a way to avoid
code duplication when possible. The diagram below is a diagram
showing the inheritance hierarchy of the response classes.
::
+--------------+
|ResponseParser|
+--------------+
^ ^ ^
+--------------------+ | +-------------------+
| | |
+----------+----------+ +------+-------+ +-------+------+
|BaseXMLResponseParser| |BaseRestParser| |BaseJSONParser|
+---------------------+ +--------------+ +--------------+
^ ^ ^ ^ ^ ^
| | | | | |
| | | | | |
| ++----------+-+ +-+-----------++ |
| |RestXMLParser| |RestJSONParser| |
+-----+-----+ +-------------+ +--------------+ +----+-----+
|QueryParser| |JSONParser|
+-----------+ +----------+
The diagram above shows that there is a base class, ``ResponseParser`` that
contains logic that is similar amongst all the different protocols (``query``,
``json``, ``rest-json``, ``rest-xml``). Amongst the various services there
is shared logic that can be grouped several ways:
* The ``query`` and ``rest-xml`` both have XML bodies that are parsed in the
same way.
* The ``json`` and ``rest-json`` protocols both have JSON bodies that are
parsed in the same way.
* The ``rest-json`` and ``rest-xml`` protocols have additional attributes
besides body parameters that are parsed the same (headers, query string,
status code).
This is reflected in the class diagram above. The ``BaseXMLResponseParser``
and the BaseJSONParser contain logic for parsing the XML/JSON body,
and the BaseRestParser contains logic for parsing out attributes that
come from other parts of the HTTP response. Classes like the
``RestXMLParser`` inherit from the ``BaseXMLResponseParser`` to get the
XML body parsing logic and the ``BaseRestParser`` to get the HTTP
header/status code/query string parsing.
Additionally, there are event stream parsers that are used by the other parsers
to wrap streaming bodies that represent a stream of events. The
BaseEventStreamParser extends from ResponseParser and defines the logic for
parsing values from the headers and payload of a message from the underlying
binary encoding protocol. Currently, event streams support parsing bodies
encoded as JSON and XML through the following hierarchy.
+--------------+
|ResponseParser|
+--------------+
^ ^ ^
+--------------------+ | +------------------+
| | |
+----------+----------+ +----------+----------+ +-------+------+
|BaseXMLResponseParser| |BaseEventStreamParser| |BaseJSONParser|
+---------------------+ +---------------------+ +--------------+
^ ^ ^ ^
| | | |
| | | |
+-+----------------+-+ +-+-----------------+-+
|EventStreamXMLParser| |EventStreamJSONParser|
+--------------------+ +---------------------+
Return Values
=============
Each call to ``parse()`` returns a dict has this form::
Standard Response
{
"ResponseMetadata": {"RequestId": <requestid>}
<response keys>
}
Error response
{
"ResponseMetadata": {"RequestId": <requestid>}
"Error": {
"Code": <string>,
"Message": <string>,
"Type": <string>,
<additional keys>
}
}
� N)�six�ETree�
XMLParseError)�EventStream�NoInitialResponseError)�parse_timestamp�merge_dicts�is_json_value_header�lowercase_dictc @ s$ e Zd Zdd� Zdd� Zdd� ZdS )�ResponseParserFactoryc C s
i | _ d S )N)� _defaults)�self� r �/usr/lib/python3.6/parsers.py�__init__� s zResponseParserFactory.__init__c K s | j j|� dS )aO Set default arguments when a parser instance is created.
You can specify any kwargs that are allowed by a ResponseParser
class. There are currently two arguments:
* timestamp_parser - A callable that can parse a timetsamp string
* blob_parser - A callable that can parse a blob type
N)r �update)r
�kwargsr r r �set_parser_defaults� s
z)ResponseParserFactory.set_parser_defaultsc C s t | }|f | j�S )N)�PROTOCOL_PARSERSr )r
Z
protocol_nameZ
parser_clsr r r �
create_parser� s z#ResponseParserFactory.create_parserN)�__name__�
__module__�__qualname__r r r r r r r r � s r c C s t � j| �S )N)r r )Zprotocolr r r r � s r c s � fdd�}|S )Nc s. t |d�r|j}|d kr"d}n|}� | ||�S )N�text� )�hasattrr )r
�shapeZnode_or_stringr )�funcr r �_get_text_content� s
z(_text_content.<locals>._get_text_contentr )r r r )r r �
_text_content� s r c @ s e Zd ZdS )�ResponseParserErrorN)r r r r r r r r � s r c @ s� e Zd ZdZdZdZddd�Zdd� Zdd � Zd
d� Z dd
� Z
dd� Zdd� Zdd� Z
dd� Zdd� Zdd� Zdd� Zdd� ZdS )�ResponseParserao Base class for response parsing.
This class represents the interface that all ResponseParsers for the
various protocols must implement.
This class will take an HTTP response and a model shape and parse the
HTTP response into a dictionary.
There is a single public method exposed: ``parse``. See the ``parse``
docstring for more info.
zutf-8Nc C sH |d krt }|| _|d kr | j}|| _d | _| jd k rD| j||�| _d S )N)�DEFAULT_TIMESTAMP_PARSER�_timestamp_parser�_default_blob_parser�_blob_parser�_event_stream_parser�EVENT_STREAM_PARSER_CLS)r
�timestamp_parser�blob_parserr r r r � s
zResponseParser.__init__c C s
t j|�S )N)�base64� b64decode)r
�valuer r r r$ � s z#ResponseParser._default_blob_parserc C s� t jd|d � t jd|d � |d dkrj| j|�rB| j|�}qv| j|�r\| j||�}|S | j||�}n| j||�}|r�|jj d�r�|S t
|t�r�|j di �}|d |d <