
    VhL                    "   d dl m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 d dlmZ d dlmZ d dlmZ d d	lmZ d d
lmZmZmZ 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 Z!d Z"d Z#d Z$d Z%d Z&d Z'd Z(d Z)d Z*y)"    )annotationsN)literal_eval)	to_native)is_iterable)jsonify)human_to_bytes)	deprecate)boolean)binary_typestring_types	text_typec                d    t        |       s| g} t        t        |       j                  |            S )a  Count the number of occurrences of a key in a given dictionary

    :arg terms: String or iterable of values to check
    :arg parameters: Dictionary of parameters

    :returns: An integer that is the number of occurrences of the terms values
        in the provided dictionary.
    )r   lensetintersection)terms
parameterss     V/home/dcms/DCMS/lib/python3.12/site-packages/ansible/module_utils/common/validation.pycount_termsr      s-     us5z&&z233    c                2   t        dd       t        | t              s|r| d fS | S t        j                  d|       r|r| d fS | S t        j                  d|       r|r| d fS | S 	 t        |       }|r|d fS |S # t        $ r}|r	| |fcY d }~S | cY d }~S d }~ww xY w)Nz*The safe_eval function should not be used.z2.21)versionz	\w\.\w+\(z
import \w+)r	   
isinstancer   researchr   	Exception)valuelocalsinclude_exceptionsresultes        r   	safe_evalr"   *   s    4
 e\*4= 	yyu%4= 	yy&4= 	e$D>!M 1:s0   #A6 4A6 6	B?BB
BBBc                F   g }| |S | D ]%  }t        ||      }|dkD  s|j                  |       ' |ri|D cg c]  }dj                  |       }}ddj                  |      z  }|r!dj                  |dj                  |            }t	        t        |            |S c c}w )a  Check mutually exclusive terms against argument parameters

    Accepts a single list or list of lists that are groups of terms that should be
    mutually exclusive with one another

    :arg terms: List of mutually exclusive parameters
    :arg parameters: Dictionary of parameters
    :kwarg options_context: List of strings of parent key names if ``terms`` are
        in a sub spec.

    :returns: Empty list or raises :class:`TypeError` if the check fails.
       |z%parameters are mutually exclusive: %s, {0} found in {1} -> r   appendjoinformat	TypeErrorr   )r   r   options_contextresultscheckcount	full_listmsgs           r   check_mutually_exclusiver4   J   s     G} "E:.19NN5!"
 29:SXXe_:	:5		)8LL$++C_1MNC	#''N ;s   Bc                   g }| |S | D ]%  }t        ||      }|dk(  s|j                  |       ' |rQ|D ]L  }ddj                  |      z  }|r!dj                  |dj                  |            }t	        t        |             |S )a  Check each list of terms to ensure at least one exists in the given module
    parameters

    Accepts a list of lists or tuples

    :arg terms: List of lists of terms to check. For each list of terms, at
        least one is required.
    :arg parameters: Dictionary of parameters
    :kwarg options_context: List of strings of parent key names if ``terms`` are
        in a sub spec.

    :returns: Empty list or raises :class:`TypeError` if the check fails.
    r   z$one of the following is required: %sr&   r'   r(   r)   )r   r   r.   r/   termr1   r3   s          r   check_required_one_ofr7   k   s     G} !D*-A:NN4 !
  	,D8499T?JC(//V[[5QRIcN++		, Nr   c                   g }| |S | D ]U  }|D cg c]  }t        ||       }}|D cg c]
  }|dkD  s	| }}t        |      dkD  s@d|v sE|j                  |       W |rQ|D ]L  }ddj                  |      z  }	|r!dj	                  |	dj                  |            }	t        t        |	             |S c c}w c c}w )a  Check each list of terms to ensure every parameter in each list exists
    in the given parameters.

    Accepts a list of lists or tuples.

    :arg terms: List of lists of terms to check. Each list should include
        parameters that are all required when at least one is specified
        in the parameters.
    :arg parameters: Dictionary of parameters
    :kwarg options_context: List of strings of parent key names if ``terms`` are
        in a sub spec.

    :returns: Empty list or raises :class:`TypeError` if the check fails.
    r   z$parameters are required together: %sr&   r'   r(   )r   r   r*   r+   r,   r-   r   )
r   r   r.   r/   r6   fieldcountscnon_zeror3   s
             r   check_required_togetherr=      s      G} %>BCU+eZ0CC%/!QA//x=1F{t$%  	,D8499T?JC(//V[[5QRIcN++		, N D/s   B6
B;B;c                   i }| |S | j                         D ]L  \  }}||vs||   g ||<   t        |t              r|g}|D ]   }||vs||   ||   j                  |       " N |rt|j                         D ]a  \  }}t	        |      dkD  sd|ddj                  |      }|r!dj                  |dj                  |            }t        t        |             |S )a  For each key in requirements, check the corresponding list to see if they
    exist in parameters.

    Accepts a single string or list of values for each key.

    :arg requirements: Dictionary of requirements
    :arg parameters: Dictionary of parameters
    :kwarg options_context: List of strings of parent key names if ``requirements`` are
        in a sub spec.

    :returns: Empty dictionary or raises :class:`TypeError` if the
    r   z"missing parameter(s) required by 'z': r&   r'   r(   )	itemsr   r   r*   r   r+   r,   r-   r   )	requirementsr   r.   r    keyr   requiredmissingr3   s	            r   check_required_byrD      s    F$**, 	-ej JsO$;se\*GE 	-Hz)Z-A-Is""8,	-	- "LLN 	0LC7|aEH$))T[J\]",33C_9UVC	#//	0 Mr   c                D   g }| |S | j                         D ]0  \  }}|j                  dd      }|s||vs |j                  |       2 |rTddj                  t	        |            z  }|r!dj                  |dj                  |            }t        t        |            |S )a  Check all parameters in argument_spec and return a list of parameters
    that are required but not present in parameters.

    Raises :class:`TypeError` if the check fails

    :arg argument_spec: Argument spec dictionary containing all parameters
        and their specification
    :arg parameters: Dictionary of parameters
    :kwarg options_context: List of strings of parent key names if ``argument_spec`` are
        in a sub spec.

    :returns: Empty list or raises :class:`TypeError` if the check fails.
    rB   Fmissing required arguments: %sr&   r'   r(   )r?   getr*   r+   sortedr,   r-   r   )argument_specr   r.   rC   kvrB   r3   s           r   check_required_argumentsrL      s     G%%' A55U++NN1
 .6'?1KK$++C_1MNC	#''Nr   c                `   g }| |S | D ]  }i }g |d<   d}d}t        |      dk(  r|\  }}	} }n|\  }}	} |rt        |       }d|d<   nd|d<   ||v r5||   |	k(  r-| D ](  }
t        |
|      }|dk(  s|d   j                  |
       * t        |d         st        |d         |k\  s||d<   |	|d	<   | |d
<   |j                  |        |re|D ]`  }|d   d|d	   d|d   ddj                  |d         }|r!dj	                  |dj                  |            }t        t        |             |S )aT  Check parameters that are conditionally required

    Raises :class:`TypeError` if the check fails

    :arg requirements: List of lists specifying a parameter, value, parameters
        required when the given parameter is the specified value, and optionally
        a boolean indicating any or all parameters are required.

    :Example:

    .. code-block:: python

        required_if=[
            ['state', 'present', ('path',), True],
            ['someint', 99, ('bool_param', 'string_param')],
        ]

    :arg parameters: Dictionary of parameters

    :returns: Empty list or raises :class:`TypeError` if the check fails.
        The results attribute of the exception contains a list of dictionaries.
        Each dictionary is the result of evaluating each item in requirements.
        Each return dictionary contains the following keys:

            :key missing: List of parameters that are required but missing
            :key requires: 'any' or 'all'
            :key parameter: Parameter name that has the requirement
            :key value: Original value of the parameter
            :key requirements: Original required parameters

        :Example:

        .. code-block:: python

            [
                {
                    'parameter': 'someint',
                    'value': 99
                    'requirements': ('bool_param', 'string_param'),
                    'missing': ['string_param'],
                    'requires': 'all',
                }
            ]

    :kwarg options_context: List of strings of parent key names if ``requirements`` are
        in a sub spec.
    rC   r   F   anyrequiresall	parameterr   r@   z is z but z of the following are missing: r&   r'   r(   )r   r   r*   r+   r,   r-   r   )r@   r   r.   r/   reqrC   max_missing_count	is_one_ofrA   valr0   r1   r3   s                r   check_required_ifrW      s   ` G $		s8q=03-ClI%("Cl  #L 1"'GJ"'GJ*CC!7% 5#E:6A:I&--e45 wy!"s79+='>BS'S#&GK "GG&2GN#NN7#7$:  	,G$gg&6
8KTYYW^_hWiMjlC(//V[[5QRIcN++	, Nr   c                    g }||S |D ]%  }| j                  |      r|j                  |       ' |r(ddj                  |      z  }t        t	        |            |S )a  This is for checking for required params when we can not check via
    argspec because we need more information than is simply given in the argspec.

    Raises :class:`TypeError` if any required parameters are missing

    :arg parameters: Dictionary of parameters
    :arg required_parameters: List of parameters to look for in the given parameters.

    :returns: Empty list or raises :class:`TypeError` if the check fails.
    rF   r&   )rG   r*   r+   r-   r   )r   required_parametersmissing_paramsparamr3   s        r   check_missing_parametersr\   V  sk     N"$ )~~e$!!%() .>1JJ	#''r   c                    t        | t              r| S |r| t        | d      S dj                  |       }t	        t        |            )as  Verify that the value is a string or convert to a string.

    Since unexpected changes can sometimes happen when converting to a string,
    ``allow_conversion`` controls whether or not the value will be converted or a
    TypeError will be raised if the value is not a string and would be converted

    :arg value: Value to validate or convert to a string
    :arg allow_conversion: Whether to convert the string and return it or raise
        a TypeError

    :returns: Original value if it is a string, the value converted to a string
        if allow_conversion=True, or raises a TypeError if allow_conversion=False.
    surrogate_or_strict)errorsz5'{0!r}' is not a string and conversion is not allowed)r   r   r   r,   r-   )r   allow_conversionr[   prefixr3   s        r   check_type_strrb   s  sH     %&E-'<==
A
H
H
OC
IcN
##r   c                    t        | t              r| S t        | t              r| j                  d      S t        | t              st        | t
              rt        |       gS t        dt        |       z        )a  Verify that the value is a list or convert to a list

    A comma separated string will be split into a list. Raises a :class:`TypeError`
    if unable to convert to a list.

    :arg value: Value to validate or convert to a list

    :returns: Original value if it is already a list, single item list if a
        float, int, or string without commas, or a multi-item list if a
        comma-delimited string.
    ,z %s cannot be converted to a list)	r   listr   splitintfloatstrr-   typer   s    r   check_type_listrl     s`     %%&{{3	E3	:eU#;E
|
6eD
EEr   c                8   t        | t              r| S t        | t              r| j                  d      r	 t	        j
                  |       S d| v rg }g }d}d}| j                         D ]q  }|r|j                  |       d}|dk(  rd}!|s|dv r|}*|r||k(  rd}4|s+|dv r'd	j                  |      }|r|j                  |       g }a|j                  |       s d	j                  |      }|r|j                  |       	 t        d
 |D              S t        d      t        dt        |       z        # t        $ rE 	 t        |       }t        |t              r|cY S # t        $ r Y t        d      w xY wt        d      w xY w# t        $ r t        d      w xY w)a  Verify that value is a dict or convert it to a dict and return it.

    Raises :class:`TypeError` if unable to convert to a dict

    :arg value: Dict or string to convert to a dict. Accepts ``k1=v2, k2=v2`` or ``k1=v2 k2=v2``.

    :returns: value converted to a dictionary
    {z'unable to evaluate string as dictionary=F\T)'")rd     c              3  @   K   | ]  }|j                  d d        yw)ro   r$   N)rf   ).0xs     r   	<genexpr>z"check_type_dict.<locals>.<genexpr>  s     <AGGCO<s   zAunable to evaluate string in the "key=value" format as dictionaryz7dictionary requested, could not parse JSON or key=valuez %s cannot be converted to a dict)r   dictr   
startswithjsonloadsr   r   r-   stripr*   r+   
ValueErrorrj   )r   r    fieldsfield_bufferin_quote	in_escaper;   r9   s           r   check_type_dictr     s    %%&C 
Kzz%(( E\FLHI[[] + ''* %I$Y $I!a;&6 H(a-$H!a:oGGL1Ee,#%L ''*!+$ GGL)Ee$e<V<<<
 UVV
6eD
EEW  K&)%0F "&$/%	 !    IJJ
   IJJKJ  e cddes;   D3 ?F 3	F=EF	E2%F1E22FFc                    t        | t              r| S t        | t              st        | t        t        f      rt        |       S t        dt        |       z        )aX  Verify that the value is a bool or convert it to a bool and return it.

    Raises :class:`TypeError` if unable to convert to a bool

    :arg value: String, int, or float to convert to bool. Valid booleans include:
         '1', 'on', 1, '0', 0, 'n', 'f', 'false', 'true', 'y', 't', 'yes', 'no', 'off'

    :returns: Boolean True or False
    z %s cannot be converted to a bool)r   boolr   rg   rh   r
   r-   rj   rk   s    r   check_type_boolr     sI     %%&*US%L*Iu~
6eD
EEr   c                    t        | t              s6	 t        j                  |       x}t        |      x}k7  rt	        d      |} 	 | S | S # t        j
                  t        t        f$ r}t        d| d      |d}~ww xY w)zVerify that the value is an integer and return it or convert the value
    to an integer and return it

    Raises :class:`TypeError` if unable to convert to an int

    :arg value: String or int to convert of verify

    :return: int of given value
    zSignificant decimal part foundrr   z" cannot be converted to an intN)r   rg   decimalDecimalr~   DecimalExceptionr-   )r   decimal_value	int_valuer!   s       r   check_type_intr     s     eS!	Q!(!77#mJ\=\Y] !ABB! L5L (()Z@ 	Qay(GHIqP	Qs   2A	 	A<'A77A<c                    t        | t              s	 t        |       } | S | S # t        t        f$ r}t        t	        |        d      d}~ww xY w)zVerify that value is a float or convert it to a float and return it

    Raises :class:`TypeError` if unable to convert to a float

    :arg value: float, int, str, or bytes to verify or convert and return.

    :returns: float of given value.
    z cannot be converted to a floatN)r   rh   r-   r~   rj   )r   r!   s     r   check_type_floatr     sX     eU#	M%LE L5L :& 	MtE{m+JKLL	Ms   ! AAAc                    t        |       } t        j                  j                  t        j                  j	                  |             S )zgVerify the provided value is a string or convert it to a string,
    then return the expanded path
    )rb   ospath
expanduser
expandvarsrk   s    r   check_type_pathr     s2     5!E77bgg00788r   c                    | S )zReturns the raw value rk   s    r   check_type_rawr   !  s    Lr   c                b    	 t        |       S # t        $ r t        dt        |       z        w xY w)zqConvert a human-readable string value to bytes

    Raises :class:`TypeError` if unable to convert the value
    z&%s cannot be converted to a Byte valuer   r~   r-   rj   rk   s    r   check_type_bytesr   &  s9    
Pe$$ P@4;NOOPs   
 !.c                f    	 t        | d      S # t        $ r t        dt        |       z        w xY w)zConvert a human-readable string bits value to bits in integer.

    Example: ``check_type_bits('1Mb')`` returns integer 1048576.

    Raises :class:`TypeError` if unable to convert the value.
    T)isbitsz%%s cannot be converted to a Bit valuer   rk   s    r   check_type_bitsr   1  s;    OeD11 O?$u+MNNOs    !0c                    t        | t        t        f      r| j                         S t        | t        t
        t        f      rt        |       S t        dt        |       z        )zReturn a jsonified string. Sometimes the controller turns a json string
    into a dict/list so transform it back into json here

    Raises :class:`TypeError` if unable to convert the value

    z'%s cannot be converted to a json string)
r   r   r   r}   re   tuplery   r   r-   rj   rk   s    r   check_type_jsonargr   >  sO     %)[12{{}	ED%.	/u~
=UK
LLr   )NF)N)TNrt   )+
__future__r   r   r{   r   r   astr   +ansible.module_utils.common.text.convertersr   'ansible.module_utils.common.collectionsr   r   +ansible.module_utils.common.text.formattersr   $ansible.module_utils.common.warningsr	   )ansible.module_utils.parsing.convert_boolr
   ansible.module_utils.sixr   r   r   r   r"   r4   r7   r=   rD   rL   rW   r\   rb   rl   r   r   r   r   r   r   r   r   r   r   r   r   <module>r      s   
 #   	 	  A ? ? F : = 4 @BD!H%PBYx:$0F.;F|F&*"9
P
OMr   