U afL$@sddlZddlZddlmZmZddlmZmZmZmZm Z m Z m Z ddl m Z ddlmZmZmZddlmZe e eeefZe eefZGdddeZd*eeeed d d Zd+eeeed ddZeeZeedddZeedddZeedddZeedddZ eedddZ!e"dZ#eedddZ$d,e ee eee eddd Z%d-eeefeeefeeeefd!d"d#Z&d.e e ee eeefeed$d%d&Z'eed'd(d)Z(dS)/N)datetime timedelta)AnyCallableDictIterableListOptionalUnion)UUID)SchemaUrlMultipleInvalid)CANONICAL_TIMESTRING_FORMATc@seZdZdZdS)BadParameterExceptionz/To be raised when a validation operation fails.N)__name__ __module__ __qualname____doc__rrE/opt/nydus/tmp/pip-target-53d1vnqk/lib/python/primordial/validator.pyrsrF) validator param_valuecoerce_returnc Cspt|r`z ||}Wn4tk rH}ztd||f|W5d}~XYnX|dkrX|}n|}|Std|dS)aRun a validation operation. Validate a particular parameter with a particular validator and possibly coerce the value into the validator's return type. :param validator: The validator to be run :param param_value: The value to be validated :param coerce_: Whether to return a type coerced value :raises ValueError: If the parameter could not be validated. z#Parameter %s failed validation (%s)NTz6Cannot use a non-callable as a parameter validator: %s)callable Exceptionr ValueError)rrrmyvaleretrrrvalidates  $r!)param_validatorrrrc Cs|dk rt|tr|D]X}|dkr2|dkr2|Szt|||d}|WStk rl}zW5d}~XYqXqtd||ft|||dS|S)aValidate a parameter. :param param_validator: The validator (or list of validators) to be run :param param_value: The value to be validated :param coerce_: Whether to return a type coerced value :raises ValueError: If the parameter could not be validated. Nrz&No validator in list validated %s (%s)) isinstancerr!rr)r"rrrretval_rrrvalidate_param1s  r')valrc CsFz t|Wn4tk r@}ztd||f|W5d}~XYnX|S)aValidate that a string looks like a URL. url is intended to be used like str or int to be a basic callable that will except on type mismatch or non- coercible value. :param val: The value to be checked :raises ValueError: If the value does not look like a URL. zNot a url: %s (%s)N) URL_SCHEMArr)r(rrrrurlUs  $r*cCs.zt|t}WdStk r(YdSXdS)zValidate that we can parse a datetime from a string. Catch exception and return false if strptime doesn't work. :param val: The value to be checked TFN)rstrptimerr)r(r&rrrparseable_datetimehs  r,cCs&t|tst|r|Std|dS)zValidate that a value represents a datetime. :param val: The value to be checked :raises ValueError: If the value does not represent a datetime. zNot a datetime: %sN)r$rr,rr(rrr is_datetimevsr.cCst|tr|Std|dS)zValidate that a value is a timedelta. :param val: The value to be checked :raises ValueError: If the value is not a timedelta. zNot a timedelta: %sN)r$rrr-rrr is_timedeltas r/cCs&t|tr|ikr|Std|dS)zValidate that a value is a non-empty dictionary. :param val: The value to be checked :raises ValueError: If the value is not a dictionary, or is empty. zNot a non-empty dict: %sN)r$dictrr-rrris_non_empty_dictsr1z^[a-z_][a-z0-9_]*$cCs$t|r|Stdtj|fdS)zValidate that argument is a valid Postgres identifier. :param val: The value to be checked :raises ValueError: If the value is not a valid Postgres identifier. z"Not a valid Postgres name (%s): %sN)POSTGRES_NAME_REGEXmatchrpatternr-rrris_postgres_names  r5)positional_argspositional_arg_valuesrrc Cstg}t|D]b\}}z ||}Wn&tk rF}zd}W5d}~XYnX|dk r`t|||d}n|}||q |S)azValidate a list of positional arguments. If we run out of stated positionals, we simply dump the originals unvalidated (we're saying the validators are optional) :param positional_args: The validators to be run, in order, against the values :param positional_arg_values: The values to be validated :param coerce_: Whether to return type coerced values Nr#) enumerate IndexErrorr'append) r6r7routargsindexZmyvaluerr&rrrrinternal_validate_positionalss   r=) keyword_argskeyword_arg_valuesrrc Csri}|D]`\}}z ||}Wn&tk rF}zd}W5d}~XYnX|dk r`t|||d}n|}|||<q |S)arValidate a dictionary of keyword arguments. If there is no matching validator for a particular keyword, then the original value is passed along in the output unvalidated. :param keyword_args: The validators to be run against the values :param keyword_arg_values: The values to be validated :param coerce_: Whether to return type coerced values Nr#)itemsKeyErrorr') r>r?rZoutdictkeyvaluerr&rrrrinternal_validate_keywordss   rD) positionalskeywordsrrcsfdd}|S)a>Either coerce the arguments in the suggested way or die with error back to the client. :param positionals: A list of validators to be run against positional arguments :param keywords: A dictionary of validators to be run against keyword arguments :param coerce_: Whether to return type coerced values cs tfdd}|S)ZInnerc sz<dk rt|d}n|}dk r6t|d}n|}Wn<tk rx}ztdt|t|f|W5d}~XYnX||S)zActual wrapperNr#zFailed to validate: %s, %s)r=rDrrstr)argskwargsr;Z outkwargsr)rfunctionrFrErrwrappers,z3coerce_or_error..decorator..wrapper) functoolswraps)rJrKrrFrE)rJr decoratorsz"coerce_or_error..decoratorr)rErFrrOrrNrcoerce_or_errors rP) uuid_maybercCs*zt|WdStk r$YdSXdS)z]Validate that a value represents a UUID. :param uuid_maybe: The value to be checked TFN)r r)rQrrris_uuids rR)F)F)F)F)NNF))rLrerrtypingrrrrrr r uuidr Z voluptuousr r rZprimordial.constantsrZ ValidatorTyperGZ DateTimeTyperrboolr!r'r)r*r,r.r/r1compiler2r5r=rDrPrRrrrrsR$   !