o
    :FdP                  
   @   sZ  d dl Z d dlZd dlZd dlZd dlZd dlZd dlmZ d dlmZ d dl	m
Z
mZ d dlmZmZ d dlmZ e jde jd e jde jded	iZddd
d	dd d ddZddddZddgddgd
dgd	d	gddgd dgddgdZi dfddZdd Zdd Zdd Zd1dd Zd!d" Zd#d$ Zd%d& Z d'd( Z!d)d* Z"d+d, Z#d-d. Z$d/d0 Z%dS )2    N)cli_util)cli_exceptions)CliFromJsonCLI_DATETIME)camelize
underscorestringg        Tz2017-01-01T00:00:00+00:00z
2017-01-01)strr   datedatetimeboolintintegerfloatZMbsZKbsZGbs)ZMBsZKBsZGBsZstring1Zstring2z
2017-01-02F   g      ?)r	   r   r
   r   r   r   r   c                    s    fdd}|S )Nc                    s   t   fdd}|S )Nc              
      s  zMt |  | jd< | jd< | jd r| jd rtd| jd r.t|  td n| jd r@t| | jd  td  | g|R i | W d S  t	j
yw } z| jd r\ d}t|j|  t|d	 W Y d }~d S d }~w t	jy } z| jd r d}t|j|  t|d	 W Y d }~d S d }~w ty } z| jd r d
}t|j|jjt|d W Y d }~d S d }~ww )Ninput_params_to_complex_typesoutput_type generate_full_command_json_inputgenerate_param_json_inputzeCannot specify both the --generate-full-command-json-input and --generate-param-json-input parametersr   debugz{usage}

Error: {details})usagedetailsz{exc}: {details})excr   )r   Z%load_context_obj_values_from_defaultsobjclickZ
UsageError'generate_json_skeleton_for_full_commandsysexit!generate_json_skeleton_for_optionr   Z)RequiredValueNotInDefaultOrUserInputErrorformatZ	get_usager	   Z3RequiredValueNotAvailableInternallyOrUserInputError	Exception	__class____name__)ctxargskwargs	exceptionZtpl)funcr   r    ?usr/lib/python3.10/site-packages/oci_cli/json_skeleton_utils.pywrapped_call=   s@   







*
*
*zOjson_skeleton_generation_handler.<locals>.inner_decorator.<locals>.wrapped_call)	functoolswraps)r'   r*   r   r   )r'   r)   inner_decorator<   s   !z9json_skeleton_generation_handler.<locals>.inner_decoratorr(   )r   r   r.   r(   r-   r)    json_skeleton_generation_handler;   s   %r/   c                 C   s4   |r| j jD ]}|jrd|_|jrd |_d |_q|S )NF)commandparamsrequiredpromptcallback)r#   paramvaluepr(   r(   r)   %generate_json_skeleton_click_callbackk   s   r8   c                 C   sZ   |d u rd S || j d vrtjd|tjd nttjt	| |ddd t
d d S )Nr   zOption {} is not a recognized complex type, so no example JSON can be produced. Invoke help for this command (--help/-h/-?) to see available documentation on this option)messagefile   Tindent	sort_keysr   )r   r   Zechor   r   stderrprintjsondumps generate_input_dict_for_skeletonr   )r#   option_namer(   r(   r)   r   |   s   r   c                 C   s&   t tjt| ddd td d S )Nr;   Tr<   r   )r@   rA   rB   rC   r   r   )r#   r(   r(   r)   r      s   r   c                 C   s  | j d }i }i }t }| jj}d| j v r!dd | j d  D }|D ]n}|j}|j}	|D ]c}
|
ddkr|
dd  }|
|v rAq-|dksQ|d	ksQ|d
ksQ|dkrRq-||vrt|}|	| |	t
v rjt
|	 ||< q-t|	tjr|jr}d|	jg||< q-d|	j||< q-t|	tjrd||< q-q#|d urt|}|r|S t|| t }t|| |S t|D ]"\}}t|}|r||t|< qt|t }t|| ||t|< q|S )Nr   Zparameter_aliasesc                 S   s   i | ]	\}}|d  |qS )r   r(   ).0kvr(   r(   r)   
<dictcomp>   s    z4generate_input_dict_for_skeleton.<locals>.<dictcomp>--r   r;   helpz generate-full-command-json-inputzgenerate-param-json-input	from-json|z/path/to/file)r   setr0   r1   itemsoptstypefindr   addCLICK_TYPES_TO_EXAMPLE_VALUES
isinstancer   ZChoiceZmultiplejoinchoicesZFileget_example_object_for_tags)translate_complex_param_to_example_objectremove_keys_if_dictsix	iteritems)r#   targeted_complex_paramr   Zinput_as_dictZ	inv_aliasZalready_processed_paramsoptionsorO   Zoption_typeopt_nameZleading_dashes_removedZattribute_nametags_objZexample_obj	attr_namecomplex_param_entryr(   r(   r)   rC      sZ   

 


rC   c                 C   s    | dv rt  S | dv rt S d S )N)zdefined-tagsZdefinedTags)zfreeform-tagsZfreeformTags)$generate_input_dict_for_defined_tags%generate_input_dict_for_freeform_tags)r\   r(   r(   r)   rW      s
   rW   c                  C   sH   i } i | d< d| d d< d| d d< i | d< d| d d< d| d d< | S )NZtagNamespace1	tagValue1tagKey1	tagValue2tagKey2ZtagNamespace2r(   dictr(   r(   r)   rc      s   rc   c                  C   s   i } d| d< d| d< | S )Nre   rf   rg   rh   r(   ri   r(   r(   r)   rd      s   rd   c                 C   s  | d }| dr&td|d}t| d |d|t| d |d|gS | ds/|dkrb|dkr8d	}d	}ntd
|d}td
|d}t| d |d|}t| d |t| d |iS |tv rjt| S ztj| d  | }W n t	y   td  Y S w ||v rd S |
| | }t|dd d}t|dkrdg}	|D ]}
|	t| d |
jd| q|	S i }| }t|j D ]:\}}|j| }t||d }t|}|r|||< q|d ur|||< q|tv rt| ||< qt| d |d|||< q|| |S )Nclasszlist[zlist\[(.*)\]r   module)rl   rk   zdict(objectr	   zdict\(([^,]*), (.*)\)r;   r   r   c                 S   s   | j S N)r"   )xr(   r(   r)   <lambda>(  s    z;translate_complex_param_to_example_object.<locals>.<lambda>)keyzwThis parameter should actually be a JSON object rather than an array - pick one of the following object variants to use)
startswithrematchgrouprX   %PRIMITIVE_TYPES_TO_EXAMPLE_KEY_VALUES(PRIMITIVE_TYPES_TO_EXAMPLE_SCALAR_VALUESr   ZMODULE_TO_TYPE_MAPPINGSKeyErrorrR   __subclasses__sortedlenappendr"   Zswagger_typesrN   Zattribute_mapgetattrrW   remove)rb   ZvisitedclsZsub_klsZkey_sub_klsZvalue_sub_klsr6   Zcls_typeZ
subclassesZsubclass_definitionsZscZobj_as_dictinstancera   Z	attr_typeZproperty_nameZattribute_valuer`   r(   r(   r)   rX      s\   





rX   c           
      C   s  t |drt|jtr|jj| jd< td|}|rt|tj	s%t
d i }| jjD ]%}|jD ]}|drA|j||dd  < q0|drO|j||dd  < q0q+| jd u rYi | _t|D ]*\}}tD ]}||rs||t| }qdt|}	|| j|	< |	|v r|| j||	 < q^|S )	NrP   r   rK   z5We expect a JSON object to be provided to --from-jsonrI   r;   -r   )hasattrrT   rP   r   Zjson_input_metadatar   r   Zparse_json_parameterabcMappingr   r   r0   r1   rO   rr   nameZdefault_maprZ   r[   STORAGE_UNIT_MAPendswithreplacer   )
r#   r5   r6   Zparsed_jsonZoption_name_to_cannonical_namer^   rq   valZunitZunderscored_keyr(   r(   r)   cli_json_input_callbackS  s8   






r   c                 C   s   t jdt| dtddS )Nz--from-jsonTa  Provide input to this command as a JSON document from a file using the file://path-to/file syntax.

        The --generate-full-command-json-input option can be used to generate a sample json file to be used with this command option. The key names are pre-populated and match the command option names (converted to camelCase format, e.g. compartment-id --> compartmentId), while the values of the keys need to be populated by the user before using the sample file as an input to this command. For any command option that accepts multiple values, the value of the key can be a JSON array.

        Options can still be provided on the command line. If an option exists in both the JSON document and the command line then the command line specified value will be used.

        For examples on usage of this option, please see our "using CLI with advanced JSON options" link: https://docs.cloud.oracle.com/iaas/Content/API/SDKDocs/cliusing.htm#AdvancedJSONOptions)rP   Zis_eagerr4   rJ   )r   optionr   r   )Zjson_metadatar(   r(   r)   get_cli_json_input_option}  s   r   c                 C   s(   |  dd  |  dd  |  dd  d S )Nr   r   Z	from_json)pop)target_dictr(   r(   r)   %remove_json_skeleton_params_from_dict  s   r   c                 C   s*   t | tu r|D ]
}| |d  qd S d S rn   )rP   rj   r   )r   keysrF   r(   r(   r)   rY     s
   rY   rn   )&r   r+   rA   rs   rZ   r   oci_clir   r   Zoci_cli.custom_typesr   r   Zoci_cli.string_utilsr   r   collections.abcr   STRINGZINTZFLOATZBOOLrS   rw   r   rv   r/   r8   r   r   rC   rW   rc   rd   rX   r   r   r   rY   r(   r(   r(   r)   <module>   sf   0
F	\*