U
    kuf¥  ã                   @   sD   d dl Z ddlmZmZ ddlmZ ddlmZ G dd„ deƒZdS )é    Né   )ÚCodeGeneratorDraft04ÚJSON_TYPE_TO_PYTHON_TYPE)ÚJsonSchemaDefinitionException)Úenforce_listc                       s|   e Zd ZeejfddddœŽZdi ddf‡ f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‡  ZS )ÚCodeGeneratorDraft06z^(/(([^/~])|(~[01]))*)*\Zz'^(\w+:(\/?\/?))?[^#\\\s]*(#[^\\\s]*)?\Zz³^(?:(?:[^\x00-\x20\"\'<>%\\^`{|}]|%[0-9a-f]{2})|\{[+#./;?&=,!@|]?(?:[a-z0-9_]|%[0-9a-f]{2})+(?::[1-9][0-9]{0,3}|\*)?(?:,(?:[a-z0-9_]|%[0-9a-f]{2})+(?::[1-9][0-9]{0,3}|\*)?)*\})*\Z)zjson-pointerzuri-referencezuri-templateNTc                    sL   t ƒ  |||||¡ | j d| jfd| jfd| jfd| jfd| jff¡ d S )NÚexclusiveMinimumÚexclusiveMaximumÚpropertyNamesÚcontainsÚconst)	ÚsuperÚ__init__Z_json_keywords_to_functionÚupdateÚgenerate_exclusive_minimumÚgenerate_exclusive_maximumÚgenerate_property_namesÚgenerate_containsÚgenerate_const)ÚselfÚ
definitionÚresolverÚformatsZuse_defaultZuse_formats©Ú	__class__© ú:/tmp/pip-unpacked-wheel-8kk9cw5n/fastjsonschema/draft06.pyr      s    ûzCodeGeneratorDraft06.__init__c                 C   s4   t |tƒr|  ¡  nd|kr&|  ¡  n
|  |¡ d S )Nz$ref)Ú
isinstanceÚboolÚgenerate_boolean_schemaZgenerate_refZrun_generate_functions)r   r   r   r   r   Ú_generate_func_code_block   s
    


z.CodeGeneratorDraft06._generate_func_code_blockc                 C   s,   | j dkr|  d¡ | j dkr(|  d¡ dS )z…
        Means that schema can be specified by boolean.
        True means everything is valid, False everything is invalid.
        TÚpassFú{name} must not be thereN)Ú_definitionÚlÚexc©r   r   r   r   r   &   s    


z,CodeGeneratorDraft06.generate_boolean_schemac              
   C   sÔ   t | jd ƒ}zd dd„ |D ƒ¡}W n0 tk
rV } ztd |¡ƒ‚W 5 d}~X Y nX d}d|krv|d	j| jd
7 }d|ks†d|kr d|kr |dj| jd
7 }|  d||¡ | jdd |¡dd W 5 Q R X dS )zù
        Validation of type. Can be one type or list of types.

        Since draft 06 a float without fractional part is an integer.

        .. code-block:: python

            {'type': 'string'}
            {'type': ['string', 'number']}
        Útypez, c                 s   s   | ]}t | V  qd S )N)r   )Ú.0Útr   r   r   Ú	<genexpr>=   s     z5CodeGeneratorDraft06.generate_type.<locals>.<genexpr>zUnknown type: {}NÚ ÚintegerzD and not (isinstance({variable}, float) and {variable}.is_integer()))ÚvariableÚnumberÚbooleanz  or isinstance({variable}, bool)z&if not isinstance({variable}, ({})){}:z{name} must be {}z or ©Zrule)	r   r#   ÚjoinÚKeyErrorr   ÚformatÚ	_variabler$   r%   )r   ÚtypesZpython_typesr%   Úextrar   r   r   Úgenerate_type0   s     ÿz"CodeGeneratorDraft06.generate_typec              
   C   s^   |   d¡J t| jd tttjfƒs,tdƒ‚|   d¡ | jddd W 5 Q R X W 5 Q R X d S )Nú1if isinstance({variable}, (int, float, Decimal)):r   z9exclusiveMinimum must be an integer, a float or a decimalz$if {variable} <= {exclusiveMinimum}:z-{name} must be bigger than {exclusiveMinimum}r0   ©	r$   r   r#   ÚintÚfloatÚdecimalÚDecimalr   r%   r&   r   r   r   r   N   s
    z/CodeGeneratorDraft06.generate_exclusive_minimumc              
   C   s^   |   d¡J t| jd tttjfƒs,tdƒ‚|   d¡ | jddd W 5 Q R X W 5 Q R X d S )Nr8   r	   z9exclusiveMaximum must be an integer, a float or a decimalz$if {variable} >= {exclusiveMaximum}:z.{name} must be smaller than {exclusiveMaximum}r0   r9   r&   r   r   r   r   U   s
    z/CodeGeneratorDraft06.generate_exclusive_maximumc                 C   s&  | j  di ¡}|dkrn|dkrP|  ¡  |  d¡ | jddd W 5 Q R X nÒ|  ¡  |  d¡º |  ¡  |  d¡œ |  d	¡ |  d
¡X |  d¡" | j|d | j	¡| j
dd W 5 Q R X |  d¡ |  d¡ W 5 Q R X W 5 Q R X |  d¡ | jddd W 5 Q R X W 5 Q R X W 5 Q R X dS )aB  
        Means that keys of object must to follow this definition.

        .. code-block:: python

            {
                'propertyNames': {
                    'maxLength': 3,
                },
            }

        Valid keys of object for this definition are foo, bar, ... but not foobar for example.
        r
   TFzif {variable}_keys:r"   r0   zif {variable}_is_dict:zif {variable}_len != 0:z {variable}_property_names = Trueú!for {variable}_key in {variable}:útry:ú{}_key©Zclear_variablesz except JsonSchemaValueException:z!{variable}_property_names = Falsez!if not {variable}_property_names:z/{name} must be named by propertyName definitionN)r#   ÚgetZcreate_variable_keysr$   r%   Zcreate_variable_is_dictZcreate_variable_with_lengthÚgenerate_func_code_blockr3   r4   Ú_variable_name)r   Zproperty_names_definitionr   r   r   r   \   s0    

üz,CodeGeneratorDraft06.generate_property_namesc                 C   s  |   ¡  |  d¡ê | jd }|dkr6| jddd nÂ|dkrd|  d¡ | jddd W 5 Q R X n”|  d	¡ |  d
¡V |  d¡6 | j|d | j¡| jdd |  d¡ |  d¡ W 5 Q R X |  d¡ W 5 Q R X |  d¡ | jddd W 5 Q R X W 5 Q R X dS )a  
        Means that array must contain at least one defined item.

        .. code-block:: python

            {
                'contains': {
                    'type': 'number',
                },
            }

        Valid array is any with at least one number.
        zif {variable}_is_list:r   Fz{name} is always invalidr0   Tzif not {variable}:z{name} must not be emptyz{variable}_contains = Falser>   r?   r@   rA   z{variable}_contains = TrueÚbreakz%except JsonSchemaValueException: passzif not {variable}_contains:z.{name} must contain one of contains definitionN)Zcreate_variable_is_listr$   r#   r%   rC   r3   r4   rD   )r   Zcontains_definitionr   r   r   r   „   s,    


ü
z&CodeGeneratorDraft06.generate_containsc              	   C   sN   | j d }t|tƒr$d |  |¡¡}|  d|¡ | jddd W 5 Q R X dS )zÚ
        Means that value is valid when is equeal to const definition.

        .. code-block:: python

            {
                'const': 42,
            }

        Only valid value is 42 in this example.
        r   z"{}"zif {variable} != {}:z:{name} must be same as const definition: {definition_rule}r0   N)r#   r   Ústrr3   Úer$   r%   )r   r   r   r   r   r   ¬   s
    

z#CodeGeneratorDraft06.generate_const)Ú__name__Ú
__module__Ú__qualname__Údictr   ZFORMAT_REGEXSr   r    r   r7   r   r   r   r   r   Ú__classcell__r   r   r   r   r      s   ü
	
((r   )	r<   Zdraft04r   r   Ú
exceptionsr   Ú	generatorr   r   r   r   r   r   Ú<module>   s   