a ì)g Jã@sJddlmZmZmZeZddlZddlZddlm Z ddl m Z ddl m Z ddlmZddlmZddlmZdd lmZdd lmZmZmZmZd d „Zd6dd„Zd7dd„Zd8dd„Zd9dd„Zd:dd„Z d;dd„Z!dd d!„Z$d"d#„Z%d$d%„Z&d&d'„Z'd(d)„Z(d*d+„Z)d,d-„Z*d.d/„Z+d0d1„Z,d2d3„Z-d4d5„Z.dS)?é)Úabsolute_importÚdivisionÚprint_functionN)Ú literal_eval)Ú to_native)Újson)Ú is_iterable)Újsonify)Úhuman_to_bytes)Úboolean)Ú binary_typeÚ integer_typesÚ string_typesÚ text_typecCs t|ƒs|g}tt|ƒ |¡ƒS)aCount 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Ú parameters©rúJ/usr/lib/python3.9/site-packages/ansible/module_utils/common/validation.pyÚ count_termss rFc Cs¸t|tƒs|r|dfS|St d|¡r6|r2|dfS|St d|¡rR|rN|dfS|Sz t|ƒ}|rj|dfWS|WSWn@ty²}z(|rš||fWYd}~S|WYd}~Sd}~00dS)Nz \w\.\w+\(z import \w+)Ú isinstancerÚreÚsearchrÚ Exception)ÚvalueÚlocalsÚinclude_exceptionsÚresultÚerrrÚ safe_eval*s*     r!cCs|g}|dur|S|D] }t||ƒ}|dkr| |¡q|rxdd„|Dƒ}dd |¡}|rld |d |¡¡}tt|ƒƒ‚|S) 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. NécSsg|]}d |¡‘qS)ú|)Újoin)Ú.0ÚcheckrrrÚ ^óz,check_mutually_exclusive..z%parameters are mutually exclusive: %sú, ú{0} found in {1}ú -> ©rÚappendr$ÚformatÚ TypeErrorr)rrÚoptions_contextÚresultsr&ÚcountZ full_listÚmsgrrrÚcheck_mutually_exclusiveFs   r4cCsxg}|dur|S|D] }t||ƒ}|dkr| |¡q|rt|D]4}dd |¡}|rfd |d |¡¡}tt|ƒƒ‚q>|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. Nrz$one of the following is required: %sr)r*r+r,)rrr0r1Útermr2r3rrrÚcheck_required_one_ofgs  r6csšg}|dur|S|D]B}‡fdd„|Dƒ}dd„|Dƒ}t|ƒdkrd|vr| |¡q|r–|D]4}dd |¡}|rˆd |d  |¡¡}tt|ƒƒ‚q`|S) aCheck 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. Ncsg|]}t|ˆƒ‘qSr)r)r%Úfield©rrrr'žr(z+check_required_together..cSsg|]}|dkr|‘qS)rr)r%Úcrrrr'Ÿr(rz$parameters are required together: %sr)r*r+)rr-r$r.r/r)rrr0r1r5ZcountsZnon_zeror3rr8rÚcheck_required_together‰s   r:c CsÖi}|dur|S| ¡D]b\}}||vs||dur6qg||<t|tƒrN|g}|D]&}||vsj||durR|| |¡qRq|rÒ| ¡D]H\}}t|ƒdkrˆd|d |¡f}|rÄd |d |¡¡}tt|ƒƒ‚qˆ|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 Nrz)missing parameter(s) required by '%s': %sr)r*r+) Úitemsrrr-rr$r.r/r) Ú requirementsrr0rÚkeyrÚrequiredÚmissingr3rrrÚcheck_required_by­s(  r@cCs€g}|dur|S| ¡D]*\}}| dd¡}|r||vr| |¡q|r|dd t|ƒ¡}|rpd |d |¡¡}tt|ƒƒ‚|S)aCheck 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. Nr>Fúmissing required arguments: %sr)r*r+)r;Úgetr-r$Úsortedr.r/r)Z argument_specrr0r?ÚkÚvr>r3rrrÚcheck_required_argumentsÕs    rFc CsJg}|dur|S|D]Ö}i}g|d<d}d}t|ƒdkrF|\}} }}n |\}} }|rft|ƒ}d|d<nd|d<||vr¬||| kr¬|D]$} t| |ƒ} | dkr†|d | ¡q†t|dƒrt|dƒ|kr||d <| |d <||d <| |¡q|rF|D]N}d |d |d |dd  |d¡f} |r8d | d |¡¡} tt| ƒƒ‚qö|S)aTCheck 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. Nr?rFéÚanyZrequiresÚallZ parameterrr<z0%s is %s but %s of the following are missing: %sr)r*r+)rrr-r$r.r/r) r<rr0r1Zreqr?Zmax_missing_countZ is_one_ofr=Úvalr&r2r3rrrÚcheck_required_ifösD0     ÿrKcCsPg}|dur|S|D]}| |¡s| |¡q|rLdd |¡}tt|ƒƒ‚|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. NrAr))rBr-r$r/r)rZrequired_parametersZmissing_paramsÚparamr3rrrÚcheck_missing_parametersRs    rMTÚcCs8t|tƒr|S|rt|ddSd |¡}tt|ƒƒ‚dS)asVerify 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. Zsurrogate_or_strict)Úerrorsz5'{0!r}' is not a string and conversion is not allowedN)rrrr.r/)rZallow_conversionrLÚprefixr3rrrÚcheck_type_stros    rQcCsTt|tƒr|St|tƒr"| d¡St|tƒs6t|tƒr@t|ƒgStdt|ƒƒ‚dS)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 listN) rÚlistrÚsplitÚintÚfloatÚstrr/Útype©rrrrÚcheck_type_list‡s    rZc Cs^t|tƒr|St|tƒrJ| d¡rnz t |¡WStyjt|tƒdd\}}|durbtdƒ‚|YS0nÜd|vrBg}g}d}d}|  ¡D]„}|r¨|  |¡d}q|dkr¶d}q|sÈ|d vrÈ|}q|rÚ||krÚd}q|s |d vr d   |¡}|r|  |¡g}q|  |¡qd   |¡}|r0|  |¡td d „|DƒƒStdƒ‚tdt |ƒƒ‚dS)aVerify 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``. :returns: value converted to a dictionary ú{T)rNz'unable to evaluate string as dictionaryú=Fú\)ú'ú")rRú rNcss|]}| dd¡VqdS)r\r"N)rT)r%ÚxrrrÚ Ír(z"check_type_dict..z7dictionary requested, could not parse JSON or key=valuez %s cannot be converted to a dict) rÚdictrÚ startswithrÚloadsrr!r/Ústripr-r$rX) rrÚexcÚfieldsZ field_bufferZin_quoteZ in_escaper9r7rrrÚcheck_type_dictžsL                ricCsBt|tƒr|St|tƒs&t|ttfƒr.t|ƒStdt|ƒƒ‚dS)aXVerify 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 boolN)rÚboolrrUrVr r/rXrYrrrÚcheck_type_boolÔs rkcCsJt|tƒr|St|tƒr6z t|ƒWSty4Yn0tdt|ƒƒ‚dS)zþVerify 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 %s cannot be converted to an intN)rr rrUÚ ValueErrorr/rXrYrrrÚcheck_type_intçs    rmcCsPt|tƒr|St|tttfƒrs>         ! " $ ( ! \  6