o
    !d!%                     @   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	   ZhydrateZhydrate_value	__class__ Ausr/lib/python3.10/site-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}]NZbooleanfalse)integerlong)floatZdoublelistr   )r
   argument_modelr   r   r   LOGdebugformatr	   lowerintr   r   r   )r   
parametersvaluer   Z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   r0   r/   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)r0   r/   registerr!   flatten_args)r   cliZserviceZ	operationr   r   r   r1      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   TZkeepr   )r/   r   itemsr   r    r!   joincopy_find_nested_argr   _merge_member_configr   )r   commandZargument_tablekwargsr   argumentZargument_from_tableZoverwrittenZsub_argumentZ
new_configconfigZ_argZnew_argr   r   r   r2      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}NzInvalid piece {0})SEPr   r    r!   splitmembersr9   
ValueError)r   r@   r   Zpiecemember_namememberr   r   r   r<      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.
        rB   r   r   r	   N)rE   r9   rD   rC   documentationZrequired_members	type_name)r   r@   r   rA   rG   rH   r   r   r   r=      s   

z%FlattenArguments._merge_member_configN)	r(   r)   r*   r+   r   r1   r2   r<   r=   r   r   r   r   r.   W   s    A%r.   )
loggingZawscli.argumentsr   	getLoggerr(   r   rC   r   objectr.   r   r   r   r   <module>   s   
?