o
    2&ßaü  ã                   @   s@   d dl mZ G dd„ deƒZG dd„ deƒZG dd„ deƒZdS )	é    )Údefaultdictc                   @   s   e Zd ZdS )ÚParameterRequiredErrorN)Ú__name__Ú
__module__Ú__qualname__© r   r   ú//usr/lib/python3/dist-packages/awscli/schema.pyr      s    r   c                   @   sb   e Zd ZdZdddœZdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ ZdS )ÚSchemaTransformeraE  
    Transforms a custom argument parameter schema into an internal
    model representation so that it can be treated like a normal
    service model. This includes shorthand JSON parsing and
    automatic documentation generation. The format of the schema
    follows JSON Schema, which can be found here:

    http://json-schema.org/

    Only a relevant subset of features is supported here:

    * Types: `object`, `array`, `string`, `integer`, `boolean`
    * Properties: `type`, `description`, `required`, `enum`

    For example::

    {
        "type": "array",
        "items": {
            "type": "object",
            "properties": {
                "arg1": {
                    "type": "string",
                    "required": True,
                    "enum": [
                        "Value1",
                        "Value2",
                        "Value3"
                    ]
                },
                "arg2": {
                    "type": "integer",
                    "description": "The number of calls"
                }
            }
        }
    }

    Assuming the schema is applied to a service named `foo`, with an
    operation named `bar` and that the parameter is called `baz`, you
    could call it with the shorthand JSON like so::

        $ aws foo bar --baz arg1=Value1,arg2=5 arg1=Value2

    Ú	structureÚlist)ÚobjectÚarrayc                 C   s   t ƒ | _d S ©N)ÚShapeNameGeneratorÚ_shape_namer©Úselfr   r   r   Ú__init__G   s   zSchemaTransformer.__init__c                 C   s   i }|   ||d¡ |S )aø  Convert JSON schema to the format used internally by the AWS CLI.

        :type schema: dict
        :param schema: The JSON schema describing the argument model.

        :rtype: dict
        :return: The transformed model in a form that can be consumed
            internally by the AWS CLI.  The dictionary returned will
            have a list of shapes, where the shape representing the
            transformed schema is always named ``InputShape`` in the
            returned dictionary.

        Ú
InputShape)Ú
_transform)r   ÚschemaÚshapesr   r   r   Ú	transformJ   s   zSchemaTransformer.transformc                 C   s‚   d|vrt dƒ‚|d dkr|  ||¡||< |S |d dkr(|  ||¡||< |S |d dkr8|  ||¡||< |S |  |¡||< |S )NÚtypeúMissing required key: 'type'r   r   Úmap)r   Ú_transform_structureÚ_transform_listÚ_transform_mapÚ_transform_scalar)r   r   r   Ú
shape_namer   r   r   r   \   s   úüÿzSchemaTransformer._transformc                 C   s
   |   |¡S r   )Ú_populate_initial_shape)r   r   r   r   r   r   i   s   
z#SchemaTransformer._transform_scalarc           
      C   s†   |   |¡}i }g }|d  ¡ D ]'\}}|  |¡}| j |¡}	d|	i||< | dd¡r/| |¡ |  |||	¡ q||d< |rA||d< |S )NÚ
propertiesÚshapeÚrequiredFÚmembers)r!   ÚitemsÚ_json_schema_to_aws_typer   Únew_shape_nameÚgetÚappendr   )
r   r   r   Ústructure_shaper%   Úrequired_membersÚkeyÚvalueÚcurrent_type_nameÚcurrent_shape_namer   r   r   r   l   s    

ÿ
z&SchemaTransformer._transform_structurec                 C   sP   |   |¡}dD ]}|  || ¡}| j |¡}d|i||< |  || ||¡ q|S )N)r-   r.   r#   ©r!   r'   r   r(   r   )r   r   r   r+   Ú	attributeÚ	type_namer    r   r   r   r   �   s   
z SchemaTransformer._transform_mapc                 C   sF   |   |¡}|  |d ¡}| j |¡}d|i|d< |  |d ||¡ |S )Nr&   r#   Úmemberr1   )r   r   r   Ú
list_shapeÚmember_typeÚmember_shape_namer   r   r   r   Š   s   
z!SchemaTransformer._transform_listc                 C   s:   d|   |¡i}d|v r|d |d< d|v r|d |d< |S )Nr   ÚdescriptionÚdocumentationÚenum)r'   )r   r   r#   r   r   r   r!   •   s   z)SchemaTransformer._populate_initial_shapec                 C   s&   d|vrt dƒ‚|d }| j ||¡S )Nr   r   )r   ÚJSON_SCHEMA_TO_AWS_TYPESr)   )r   r   r3   r   r   r   r'   �   s   z*SchemaTransformer._json_schema_to_aws_typeN)r   r   r   Ú__doc__r;   r   r   r   r   r   r   r   r!   r'   r   r   r   r   r	      s    .þ	r	   c                   @   s   e Zd Zdd„ Zdd„ ZdS )r   c                 C   s   t tƒ| _d S r   )r   ÚintÚ_name_cacher   r   r   r   r   ¥   s   zShapeNameGenerator.__init__c                 C   s,   | j |  d7  < | j | }d| ¡ |f S )Né   z%sType%s)r>   Ú
capitalize)r   r3   Úcurrent_indexr   r   r   r(   ¨   s   
z!ShapeNameGenerator.new_shape_nameN)r   r   r   r   r(   r   r   r   r   r   ¤   s    r   N)Úcollectionsr   Ú
ValueErrorr   r   r	   r   r   r   r   r   Ú<module>   s
    