
    Vh.                        d dl mZ d dlmZ d dlmZmZmZmZm	Z	m
Z
mZmZmZmZ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mZmZmZmZmZmZm Z   G d d	      Z! G d
 d      Z" G d de"      Z#y)    )annotations)deepcopy)_ADDITIONAL_CHECKS_get_legal_inputs_get_unsupported_parameters_handle_aliases_list_deprecations_list_no_log_values_set_defaults_validate_argument_types_validate_argument_values_validate_sub_specset_fallbacks)	to_native)	deprecatewarn)check_mutually_exclusivecheck_required_arguments)
AliasErrorAnsibleValidationErrorMultipleDeprecationErrorMutuallyExclusiveError
NoLogErrorRequiredDefaultErrorRequiredErrorUnsupportedErrorc                  F    e Zd ZdZd Zed        Zed        Zed        Zy)ValidationResultzResult of argument spec validation.

    This is the object returned by :func:`ArgumentSpecValidator.validate()
    <ansible.module_utils.common.arg_spec.ArgumentSpecValidator.validate()>`
    containing the validated parameters and any errors.
    c                    t               | _        	 t               | _        t               | _        t        |      | _        g | _        g | _        i | _	        t               | _        y)zx
        :arg parameters: Terms to be validated and coerced to the correct type.
        :type parameters: dict
        N)set_no_log_values_unsupported_parametersdict_supported_parametersr   _validated_parameters_deprecations	_warnings_aliasesr   errors)self
parameterss     T/home/dcms/DCMS/lib/python3.12/site-packages/ansible/module_utils/common/arg_spec.py__init__zValidationResult.__init__3   s[    
 "e	 (+u$%)V"%-j%9"46	    c                    | j                   S )z!Validated and coerced parameters.)r%   r*   s    r,   validated_parametersz%ValidationResult.validated_parametersJ   s     )))r.   c                    | j                   S )z,:class:`set` of unsupported parameter names.)r"   r0   s    r,   unsupported_parametersz'ValidationResult.unsupported_parametersO   s     +++r.   c                .    | j                   j                  S )zJ:class:`list` of all error messages from each exception in :attr:`errors`.)r)   messagesr0   s    r,   error_messageszValidationResult.error_messagesT   s     {{###r.   N)	__name__
__module____qualname____doc__r-   propertyr1   r3   r6    r.   r,   r   r   +   sH    . * * , , $ $r.   r   c                  (    e Zd ZdZ	 	 	 	 	 ddZd Zy)ArgumentSpecValidatorzArgument spec validation class

    Creates a validator based on the ``argument_spec`` that can be used to
    validate a number of parameters using the :meth:`validate` method.
    Nc                   || _         || _        || _        || _        || _        t               | _        || _        t        | j                  j                               D ]  }| j                  |   j                  d      }|rF| j                  j                  dj                  |dj                  t        |                  g       i| j                  j                  |g        y)a  
        :arg argument_spec: Specification of valid parameters and their type. May
            include nested argument specs.
        :type argument_spec: dict[str, dict]

        :kwarg mutually_exclusive: List or list of lists of terms that should not
            be provided together.
        :type mutually_exclusive: list[str] or list[list[str]]

        :kwarg required_together: List of lists of terms that are required together.
        :type required_together: list[list[str]]

        :kwarg required_one_of: List of lists of terms, one of which in each list
            is required.
        :type required_one_of: list[list[str]]

        :kwarg required_if: List of lists of ``[parameter, value, [parameters]]`` where
            one of ``[parameters]`` is required if ``parameter == value``.
        :type required_if: list

        :kwarg required_by: Dictionary of parameter names that contain a list of
            parameters required by each key in the dictionary.
        :type required_by: dict[str, list[str]]
        aliasesz{key} ({aliases}), )keyr@   N)_mutually_exclusive_required_together_required_one_of_required_if_required_byr    _valid_parameter_namesargument_specsortedkeysgetupdateformatjoin)	r*   rI   mutually_exclusiverequired_togetherrequired_one_ofrequired_ifrequired_byrB   r@   s	            r,   r-   zArgumentSpecValidator.__init__a   s    B $6 "3 /''&)e#*$,,1134 	:C((-11)<G++224G4N4NSV`d`i`ijpqxjy`z4N4{3|}++22C59	:r.   c           
        t        |      }|j                  j                  t        | j                  |j
                               g }g }	 |j                  j                  t        | j                  |j
                  ||             t        | j                  |j
                  |j                        }|D ]#  \  }	}
|j                  j                  |	|
d       % |D ]U  }|j                   j                  d|d   z  |j#                  d      |j#                  d      |j#                  d      d       W 	 |j                  j                  t%        | j                  |j
                               	 |j                   j)                  t+        | j                  |j
                               	 |j.                  j                  t1        | j                  |j
                  ||j2                  	             	 t7        | j8                  |j
                         |j                  j                  t=        | j                  |j
                  d
             	 t?        | j                  |j
                         tC        | j                  |j
                  |j                         tE        | j                  |j
                  |j                         tF        D ]6  }	  |d   tI        | djK                  |d               |j
                         8 |j                  j                  t=        | j                  |j
                               g }tM        | j                  |j
                  |j                  |j                  |j.                  |j2                  |       |D ]U  }|j                   j                  d|d   z  |j#                  d      |j#                  d      |j#                  d      d       W |j.                  rAg }|j.                  D ]D  }tO        |tP              r!|j                  djS                  |             4|j                  |       F djS                  tU        tW        |                  }g x}}|j2                  j#                        rHtU        tW        |j2                  |   d               }tU        tW        |j2                  |   d               }djS                  |      }|rdjS                  |      }|d|z  z  }djK                  ||      }|j                  j                  tY        |             |S # t        t        f$ r8}|j                  j                  t        t        |                   Y d}~d}~ww xY w# t        $ r8}|j                  j                  t'        t        |                   Y d}~Md}~ww xY w# t        $ r8}|j                  j                  t-        t        |                   Y d}~Wd}~ww xY w# t        $ r8}|j                  j                  t5        t        |                   Y d}~Td}~wt        $ r8}|j                  j                  t        t        |                   Y d}~d}~ww xY w# t        $ r8}|j                  j                  t;        t        |                   Y d}~d}~ww xY w# t        $ r8}|j                  j                  tA        t        |                   Y d}~d}~ww xY w# t        $ r8}|j                  j                   |d   t        |                   Y d}~d}~ww xY w)aL  Validate ``parameters`` against argument spec.

        Error messages in the :class:`ValidationResult` may contain no_log values and should be
        sanitized with :func:`~ansible.module_utils.common.parameters.sanitize_keys` before logging or displaying.

        :arg parameters: Parameters to validate against the argument spec
        :type parameters: dict[str, dict]

        :return: :class:`ValidationResult` containing validated parameters.

        :Simple Example:

            .. code-block:: text

                argument_spec = {
                    'name': {'type': 'str'},
                    'age': {'type': 'int'},
                }

                parameters = {
                    'name': 'bo',
                    'age': '42',
                }

                validator = ArgumentSpecValidator(argument_spec)
                result = validator.validate(parameters)

                if result.error_messages:
                    sys.exit("Validation failed: {0}".format(", ".join(result.error_messages))

                valid_params = result.validated_parameters
        NoptionaliaszBAlias '%s' is deprecated. See the module docs for more informationnameversiondatecollection_name)msgrZ   r[   r\   )store_supportedF)r)   funcz_{attr}attr)r`   err)r)   no_log_valuesr3   supported_parametersalias_deprecations.rA   r      z (%s)z'{0}. Supported parameters include: {1}.)-r   r!   rM   r   rI   r%   r(   r   	TypeError
ValueErrorr)   appendr   r   r   r'   r&   rL   r
   r   extendr	   r   r"   r   r$   r   r   rC   r   r   r   r   r   r   r   getattrrN   r   
isinstancetuplerO   rJ   listr   )r*   r+   argskwargsresultalias_warningsrd   elegal_inputsrW   rX   deprecationtevecheckflattened_namesitemunsupported_stringsupported_paramssupported_aliasessupported_stringaliases_stringr]   s                          r,   validatezArgumentSpecValidator.validate   s4   D "*-$$]43E3EvGcGc%de	;OO""?43E3EvGcGces  vH  $I  J )););V=Y=Y[a[j[jk+ 	HMFE##v$FG	H . 	K  ''[^ijp^qq&??95#/#.??3D#E	) 		<!!(()<T=O=OQWQmQm)no	B  ''(:4;M;MvOkOk(lm	<**11+&&00 $*$@$@		H$T%=%=v?[?[\ 	$$]43E3EvGcGcej%kl	>$T%7%79U9UV 	!!3!3V5Q5QZ`ZgZgh!$"4"4f6R6R[a[h[hi' 	BEBfgdI,<,<%-,<,PQSYSoSop	B 	$$]43E3EvGcGc%de4--v/K/K"(--)/)>)>282P2P060L0L.@	C . 	K  ''[^ijp^qq&??95#/#.??3D#E	) 	 )) O66 1dE*#**388D>:#**40	1 "&6$2G+H!I3550++//5#)$v/K/KD/QRS/T*U#V $*40L0LT0RST0U+V$W!#yy)9: !%+<!= Gn$<< ;BBCUWghCMM  !1#!67G :& 	;MM  IaL!9::	;$  	<MM  IbM!:;;	<
  	BMM  !1)B-!@AA	B  	FMM  !5im!DEE 	<MM  IbM!:;;	<
  	HMM  !7	"!FGG	H  	>MM  y|!<==	>  B$$\U5\)B-%@AABs   
;T3 39U= -9W 'AX . Z 	 [ 3\3U:-U55U:=	V>-V99V>	X
-W==X	Z-YZ-Z  Z	[	-[[		\-\\	]-]])NNNNN)r7   r8   r9   r:   r-   r   r<   r.   r,   r>   r>   Z   s$     %)#'!%!!.:`Mr.   r>   c                  ,     e Zd ZdZ fdZ fdZ xZS )ModuleArgumentSpecValidatorzArgument spec validation class used by :class:`AnsibleModule`.

    This is not meant to be used outside of :class:`AnsibleModule`. Use
    :class:`ArgumentSpecValidator` instead.
    c                ,    t        t        | 
  |i | y )N)superr   r-   )r*   ro   rp   	__class__s      r,   r-   z$ModuleArgumentSpecValidator.__init__(  s    )494J6Jr.   c           	     4   t         t        |   |      }|j                  D ]A  }t	        |d   |j                  d      |j                  d      |j                  d             C |j                  D ]$  }t        dj                  |d   |d   	             & |S )
Nr]   rZ   r[   r\   )rZ   r[   r\   z3Both option {option} and its alias {alias} are set.rW   rX   rV   )	r   r   r   r&   r   rL   r'   r   rN   )r*   r+   rq   dwr   s        r,   r   z$ModuleArgumentSpecValidator.validate+  s    2DB:N%% 	@AaheeI.QUU6]&'ee,=&>@	@
 !! 	uAFMMUVW_U`hijqhrMst	u r.   )r7   r8   r9   r:   r-   r   __classcell__)r   s   @r,   r   r   !  s    K r.   r   N)$
__future__r   copyr   &ansible.module_utils.common.parametersr   r   r   r   r	   r
   r   r   r   r   r   +ansible.module_utils.common.text.convertersr   $ansible.module_utils.common.warningsr   r   &ansible.module_utils.common.validationr   r   ansible.module_utils.errorsr   r   r   r   r   r   r   r   r   r>   r   r<   r.   r,   <module>r      sc   
 #     B @
	 	 	,$ ,$^D DN"7 r.   