o
    .&ßa!%  ã                   @   sF   d dl Z d dlmZ e  e¡ZdZG dd„ deƒZG dd„ deƒZ	dS )é    N)ÚCustomArgumentÚ.c                       s:   e Zd ZdZ		d
‡ fdd„	Zedd„ ƒZdd	„ Z‡  ZS )ÚFlattenedArgumenta)  
    A custom argument which has been flattened from an existing structure. When
    added to the call params it is hydrated back into the structure.

    Supports both an object and a list of objects, in which case the flattened
    parameters will hydrate a list with a single object in it.
    Ú Nc	           	         s8   || _ || _|| _|| _|| _tt| ƒj|||d� d S )N)ÚnameÚ	help_textÚrequired)ÚtypeÚ
_containerÚ	_propertyÚ_hydrateÚ_hydrate_valueÚsuperr   Ú__init__)	Úselfr   Ú	containerÚpropr   r   r	   ÚhydrateÚhydrate_value©Ú	__class__© ú?/usr/lib/python3/dist-packages/awscli/customizations/flatten.pyr       s   
ÿzFlattenedArgument.__init__c                 C   s   | j S ©N)r	   )r   r   r   r   Úcli_type_name*   s   zFlattenedArgument.cli_type_namec                 C   sò   | j jj}| j j}| j}t d ||¡¡ |durw| jdkr&| 	¡ dk }n| jdv r0t
|ƒ}n	| jdv r9t|ƒ}| jrG|  |||||¡ dS ||vrY|dkrUi g||< ni ||< | jra|  |¡}|dkro||| d |< dS ||| |< dS dS )	a  
        Hydrate the original structure with the value of this flattened
        argument.

        TODO: This does not hydrate nested structures (``XmlName1.XmlName2``)!
              To do this for now you must provide your own ``hydrate`` method.
        zHydrating {0}[{1}]NÚbooleanÚfalse)ÚintegerÚlong)ÚfloatÚdoubleÚlistr   )r
   Úargument_modelr   r   r   ÚLOGÚdebugÚformatr	   ÚlowerÚintr   r   r   )r   Ú
parametersÚvaluer   Úcli_typeÚkeyr   r   r   Úadd_to_params.   s.   





èzFlattenedArgument.add_to_params)r   NNNN)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úpropertyr   r,   Ú__classcell__r   r   r   r   r      s    ÿ

r   c                   @   s8   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ ZdS )ÚFlattenArgumentsaþ	  
    Flatten arguments for one or more commands for a particular service from
    a given configuration which maps service call parameters to flattened
    names. Takes in a configuration dict of the form::

        {
            "command-cli-name": {
                "argument-cli-name": {
                    "keep": False,
                    "flatten": {
                        "XmlName": {
                            "name": "flattened-cli-name",
                            "type": "Optional custom type",
                            "required": "Optional custom required",
                            "help_text": "Optional custom docs",
                            "hydrate_value": Optional function to hydrate value,
                            "hydrate": Optional function to hydrate
                        },
                        ...
                    }
                },
                ...
            },
            ...
        }

    The ``type``, ``required`` and ``help_text`` arguments are entirely
    optional and by default are pulled from the model. You should only set them
    if you wish to override the default values in the model.

    The ``keep`` argument determines whether the original command is still
    accessible vs. whether it is removed. It defaults to ``False`` if not
    present, which removes the original argument.

    The keys inside of ``flatten`` (e.g. ``XmlName`` above) can include nested
    references to structures via a colon. For example, ``XmlName1:XmlName2``
    for the following structure::

        {
            "XmlName1": {
                "XmlName2": ...
            }
        }

    The ``hydrate_value`` function takes in a value and should return a value.
    It is only called when the value is not ``None``. Example::

        "hydrate_value": lambda (value): value.upper()

    The ``hydrate`` function takes in a list of existing parameters, the name
    of the container, its type, the name of the container key and its set
    value. For the example above, the container would be
    ``'argument-cli-name'``, the key would be ``'XmlName'`` and the value
    whatever the user passed in. Example::

        def my_hydrate(params, container, cli_type, key, value):
            if container not in params:
                params[container] = {'default': 'values'}

            params[container][key] = value

    It's possible for ``cli_type`` to be ``list``, in which case you should
    ensure that a list of one or more objects is hydrated rather than a
    single object.
    c                 C   s   || _ || _d S r   )ÚconfigsÚservice_name)r   r5   r4   r   r   r   r   ™   s   
zFlattenArguments.__init__c                 C   s,   | j }| jD ]}| d ||¡| j¡ qdS )z”
        Register with a CLI instance, listening for events that build the
        argument table for operations in the configuration dict.
        zbuilding-argument-table.{0}.{1}N)r5   r4   Úregisterr%   Úflatten_args)r   ÚcliÚserviceÚ	operationr   r   r   r6   �   s   

ÿþÿzFlattenArguments.registerc                 K   sæ   | j |j  ¡ D ]h\}}|| }d}t d |j|d dd„ |d  ¡ D ƒ¡¡¡ |d  ¡ D ]3\}}	|	 ¡ }
||
d< ||
d< |  |j	|¡}|  
|||
¡ tdi |
¤Ž}|||	d	 < ||	d	 krbd
}q/|spd|vsm|d sp||= qd S )NFz$Flattening {0} argument {1} into {2}z, c                 S   s   g | ]\}}|d  ‘qS )r   r   )Ú.0ÚkÚvr   r   r   Ú
<listcomp>±   s    z1FlattenArguments.flatten_args.<locals>.<listcomp>Úflattenr   r   r   TÚkeepr   )r4   r   Úitemsr#   r$   r%   ÚjoinÚcopyÚ_find_nested_argr"   Ú_merge_member_configr   )r   ÚcommandÚargument_tableÚkwargsr   ÚargumentÚargument_from_tableÚoverwrittenÚsub_argumentÚ
new_configÚconfigÚ_argÚnew_argr   r   r   r7   ©   s4   þÿ€ÿ€ßzFlattenArguments.flatten_argsc                 C   sf   t |v r1t d |¡¡ | t ¡dd… D ]}|j ¡ D ]\}}||kr(|} nqtd |¡ƒ‚q|S )zâ
        Find and return a nested argument, if it exists. If no nested argument
        is requested then the original argument is returned. If the nested
        argument cannot be found, then a ValueError is raised.
        zFinding nested argument in {0}NéÿÿÿÿzInvalid piece {0})ÚSEPr#   r$   r%   ÚsplitÚmembersrA   Ú
ValueError)r   rI   r   ÚpieceÚmember_nameÚmemberr   r   r   rD   Î   s   þþz!FlattenArguments._find_nested_argc                 C   sj   |j  ¡ D ]-\}}|| t¡d kr2d|vr|j|d< d|vr&||jv |d< d|vr/|j|d<  dS qdS )a4  
        Merges an existing config taken from the configuration dict with an
        existing member of an existing argument object. This pulls in
        attributes like ``required`` and ``help_text`` if they have not been
        overridden in the configuration dict. Modifies the config in-place.
        rQ   r   r   r	   N)rT   rA   rS   rR   ÚdocumentationÚrequired_membersÚ	type_name)r   rI   r   rN   rW   rX   r   r   r   rE   á   s   

öÿz%FlattenArguments._merge_member_configN)	r-   r.   r/   r0   r   r6   r7   rD   rE   r   r   r   r   r3   W   s    A%r3   )
ÚloggingÚawscli.argumentsr   Ú	getLoggerr-   r#   rR   r   Úobjectr3   r   r   r   r   Ú<module>   s   
?