B
    ÔùÓ`e  ã               @   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Ý
    See the descritpion 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   ú?/tmp/pip-unpacked-wheel-bpftjxoo/anyconfig/schema/jsonschema.pyú
<listcomp>*   s    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   sb   yt j| |fdt ji|—Ž W n> t jt jtfk
r\ } z|rJdt|ƒfS ‚ W dd}~X Y nX dS )z£
    See the descritpion 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   s,   t d|ƒ}|rt| |f|ŽS t| ||f|ŽS )a—  
    Validate target object with given schema object, loaded from JSON schema.

    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   C   s    
r   )r   r   r   r!   r   c             K   s^   |dks|sdS t | |fd|dœ|—Ž\}}|rZd|›d|›�}|rRt |¡ dS t|ƒ‚dS )zR
    Raise ValidationError if data `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_valida   s    
r&   ÚarrayÚbooleanÚintegerÚnumberÚobjectÚstringÚ_SIMPLETYPE_MAPc              K   s   |   dt¡t|   dd¡ƒfS )z‡
    Helper function 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    
r1   )Úiarrr   c             K   sd   t f |Ž\}}t| ƒ}|t t|r*|d ndf|Ždœ}|r`t|ƒ}||d< tt|ƒƒ|k|d< |S )a�  
    Generate a JSON schema object with type annotation added for given object.

    :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)r1   r   Ú
gen_schemaÚlenÚset)r2   r   ÚtypemapÚstrictZarrÚscmZnitemsr   r   r   Úarray_to_schemaˆ   s    r;   )Úobjr   c                sN   t f ˆ Ž\}}t‡ fdd„|  ¡ D ƒƒ}|t |dœ}|rJt| ¡ ƒ|d< |S )a¢  
    Generate a node represents JSON schema object with type annotation added
    for given object node.

    :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ˆ ŽfV  qd S )N)r5   )r   ÚkÚv)r   r   r   ú	<genexpr>³   s    z#object_to_schema.<locals>.<genexpr>)r3   Z
propertiesÚrequired)r1   Údictr4   ÚsortedÚkeys)r<   r   r8   r9   Úpropsr:   r   )r   r   Úobject_to_schema¤   s    rE   )r   r   c             K   sv   | dkrddiS t | ƒ}|tkr:| dt¡}d|| i}n8t| ƒrPt| f|Ž}n"t| ƒrrtt 	tj
t | ¡f|Ž}|S )a¬  
    Generate a node represents JSON schema object with type annotation added
    for given object node.

    :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
    Nr3   Únullr.   )r3   Ú_SIMPLE_TYPESr/   r-   r   rE   r   r;   ÚtypingÚcastÚIterabler   )r   r   Ú_typer8   r:   r   r   r   r5   ¾   s    r5   )T)TF)TF)#Ú__doc__rH   r#   r   Úcommonr   r   r   Úutilsr   r   r   Z	datatypesr
   r   r0   ZAnyr    r   r&   r   ÚtupleÚintÚfloatrA   r   r-   ÚDictÚ__annotations__r1   rJ   r;   rE   rG   r5   r   r   r   r   Ú<module>   s,     "
