o
    !d                     @   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   1usr/lib/python3.10/site-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

    Z	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.

        Z
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 )NZ
propertiesshaperequiredFmembers)r   items_json_schema_to_aws_typer   new_shape_namegetappendr   )
r   r   r   structure_shaper"   Zrequired_memberskeyvalueZcurrent_type_nameZ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    memberr+   )r   r   r   Z
list_shapemember_typeZ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   r-   r   r   r   r$      s   z*SchemaTransformer._json_schema_to_aws_typeN)r   r   r   __doc__r3   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)r6   
capitalize)r   r-   Z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
    