a
    �Ï?eJ  ã                   @   s@  U d Z ddlZddlZddlZddlmZmZmZ ddlm	Z	m
Z
mZ ddlmZ eeedœd	d
„Zd(eeeejedœdd„Zd)eeeeejedœdd„Zd*eeeeedœdd„ZededededededediZejejef ed< dd„ Zeje ejeejf dœd d!„Zeed"œd#d$„Z eeeefZ!eed%œd&d'„Z"dS )+aÈ  Implementation using jsonschema provides the following functions.

- validate(data: typing.Dict[str, typing.Any],
           schema: typing.Dict[str, typing.Any],
           ac_schema_safe: bool = True, ac_schema_errors: bool = False,
           **options) -> typing.Tuple[bool, str]:
  validate with schema

- gen_schema(data: typing.Dict[str, typing.Any],
             **options) -> typing.Dict[str, typing.Any]:
  Generate an object represents a schema
é    Né   )ÚValidationErrorÚ	InDataExTÚInDataT)Úfilter_optionsÚis_dict_likeÚis_list_likeé   )ÚResultT)ÚdataÚschemaÚreturnc                 K   s,   t  |¡}t| | ¡ƒ}| dd„ |D ƒfS )zþDo all of the validation checks.

    See the description of :func:`validate` for more details of parameters and
    return value.

    :seealso: https://python-jsonschema.readthedocs.io/en/latest/validate/,
    a section of 'iter_errors' especially
    c                 S   s   g | ]
}|j ‘qS © )Úmessage)Ú.0Úerrr   r   ú^/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/anyconfig/schema/jsonschema.pyÚ
<listcomp>+   ó    z!_validate_all.<locals>.<listcomp>)Ú
jsonschemaZDraft7ValidatorÚlistZiter_errors)r   r   Ú_optionsZvldtrÚerrorsr   r   r   Ú_validate_all   s    	
r   T)r   r   Úac_schema_safeÚoptionsr   c              
   K   sp   zt j| |fdt ji|¤Ž W nL t jt jtfyj } z*|rTdt|ƒfW  Y d}~S ‚ W Y d}~n
d}~0 0 dS )zÆValidate ``data`` with ``schema``.

    See the description of :func:`validate` for more details of parameters and
    return value.

    Validate target object 'data' with given schema object.
    Zformat_checkerFN)TÚ )r   ÚvalidateZdraft7_format_checkerr   ZSchemaErrorÚ	ExceptionÚstr)r   r   r   r   Úexcr   r   r   Ú	_validate.   s    	ÿÿþ

ÿr!   F)r   r   r   Úac_schema_errorsr   r   c                 K   s4   t d|ƒ}|r t| |fi |¤ŽS t| ||fi |¤ŽS )ay  Validate target object with given schema object.

    See also: https://python-jsonschema.readthedocs.org/en/latest/validate/

    :parae data: Target object (a dict or a dict-like object) to validate
    :param schema: Schema object (a dict or a dict-like object)
        instantiated from schema JSON file or schema JSON string
    :param options: Other keyword options such as:

        - ac_schema_safe: Exception (jsonschema.ValidationError or
          jsonschema.SchemaError or others) will be thrown during validation
          process due to any validation or related errors. However, these will
          be catched by default, and will be re-raised if this value is False.

        - ac_schema_errors: Lazily yield each of the validation errors and
          returns all of them if validation fails.

    :return: (True if validation succeeded else False, error message[s])
    )Úcls)r   r   r!   )r   r   r   r"   r   r   r   r   r   E   s    
r   )r   r   r   r"   r   c                 K   s^   |du s|sdS t | |fd|dœ|¤Ž\}}|rZd|›d|›�}|rRt |¡ dS t|ƒ‚dS )zERaise ValidationError if ``data`` was invalidated by schema `schema`.NT)r   r"   zscm=z, err=F)r   ÚwarningsÚwarnr   )r   r   r   r"   r   Z_successZerror_or_errorsÚmsgr   r   r   Úis_validb   s"    ÿþþ

r'   ÚarrayÚbooleanÚintegerÚnumberÚobjectÚstringÚ_SIMPLETYPE_MAPc                  K   s   |   dt¡t|   dd¡ƒfS )zwHelp to process keyword arguments passed to gen_schema.

    :return: A tuple of (typemap :: dict, strict :: bool)
    Úac_schema_typemapZac_schema_strictF)Úgetr.   Úbool©r   r   r   r   Ú_process_options}   s    
ÿr3   )Úiarrr   c                 K   sl   t f i |¤Ž\}}t| ƒ}|t t|r.|d ndfi |¤Ždœ}|rht|ƒ}||d< tt|ƒƒ|k|d< |S )a„  Generate a JSON schema object with type annotation added for ``iaa```.

    :param arr: Array of mapping objects like dicts
    :param options: Other keyword options such as:

        - ac_schema_strict: True if more strict (precise) schema is needed
        - ac_schema_typemap: Type to JSON schema type mappings

    :return: Another mapping objects represents JSON schema of items
    r   r   )ÚtypeÚitemsZminItemsZuniqueItems)r3   r   Ú
gen_schemaÚlenÚset)r4   r   ÚtypemapÚstrictZarrÚscmZnitemsr   r   r   Úarray_to_schema†   s    þr=   )Úobjr   c                    sR   t f i ˆ ¤Ž\}}t‡ fdd„|  ¡ D ƒƒ}|t |dœ}|rNt| ¡ ƒ|d< |S )a¿  Generate a node represents JSON schema object for ``obj``.

    Type annotation will be added for given object node at the same time.

    :param obj: mapping object such like a dict
    :param options: Other keyword options such as:

        - ac_schema_strict: True if more strict (precise) schema is needed
        - ac_schema_typemap: Type to JSON schema type mappings

    :yield: Another mapping objects represents JSON schema of object
    c                 3   s&   | ]\}}|t |fi ˆ ¤ŽfV  qd S )N)r7   )r   ÚkÚvr2   r   r   Ú	<genexpr>°   r   z#object_to_schema.<locals>.<genexpr>)r5   Z
propertiesÚrequired)r3   Údictr6   ÚsortedÚkeys)r>   r   r:   r;   Úpropsr<   r   r2   r   Úobject_to_schema¡   s    rG   )r   r   c                 K   s~   | du rddiS t | ƒ}|tv r:| dt¡}d|| i}n@t| ƒrTt| fi |¤Ž}n&t| ƒrztt 	tj
t | ¡fi |¤Ž}|S )au  Generate a JSON schema object validates ``data``.

    :param data: Configuration data object (dict[-like] or namedtuple)
    :param options: Other keyword options such as:

        - ac_schema_strict: True if more strict (precise) schema is needed
        - ac_schema_typemap: Type to JSON schema type mappings

    :return: A dict represents JSON schema of this node
    Nr5   Únullr/   )r5   Ú_SIMPLE_TYPESr0   r.   r   rG   r   r=   ÚtypingÚcastÚIterabler   )r   r   Ú_typer:   r<   r   r   r   r7   »   s    ÿÿr7   )T)TF)TF)#Ú__doc__rJ   r$   r   Úcommonr   r   r   Úutilsr   r   r   Z	datatypesr
   r   r1   ÚAnyr!   r   r'   r   ÚtupleÚintÚfloatrC   r   r.   ÚDictÚ__annotations__r3   rL   r=   rG   rI   r7   r   r   r   r   Ú<module>   s>   ÿ  ÿþ  ÿÿ
þ	ÿ