B
    ÔùÓ`í  ã               @   s<   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 )é   )ÚCodeGeneratorDraft04ÚJSON_TYPE_TO_PYTHON_TYPE)ÚJsonSchemaDefinitionException)Úenforce_listc                   sz   e Zd ZeejfddddœŽZdi 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                sJ   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_default)Ú	__class__© ú:/tmp/pip-unpacked-wheel-qoh_jfy7/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¡ dS )z…
        Means that schema can be specified by boolean.
        True means everything is valid, False everything is invalid.
        Fz{name} must not be thereN)Ú_definitionÚexc)r   r   r   r   r   %   s    
z,CodeGeneratorDraft06.generate_boolean_schemac          
   C   sÔ   t | jd ƒ}yd dd„ |D ƒ¡}W n0 tk
rV } ztd |¡ƒ‚W d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 d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 )Úrule)	r   r   ÚjoinÚKeyErrorr   ÚformatÚ	_variableÚlr    )r   ÚtypesZpython_typesr    Úextrar   r   r   Úgenerate_type-   s     z"CodeGeneratorDraft06.generate_typec          
   C   sZ   |   d¡�F t| jd ttfƒs(tdƒ‚|   d¡� | jddd� W d Q R X W d Q R X d S )Nz(if isinstance({variable}, (int, float)):r   z.exclusiveMinimum must be an integer or a floatz$if {variable} <= {exclusiveMinimum}:z-{name} must be bigger than {exclusiveMinimum})r*   )r/   r   r   ÚintÚfloatr   r    )r   r   r   r   r   K   s
    z/CodeGeneratorDraft06.generate_exclusive_minimumc          
   C   sZ   |   d¡�F t| jd ttfƒs(tdƒ‚|   d¡� | jddd� W d Q R X W d Q R X d S )Nz(if isinstance({variable}, (int, float)):r   z.exclusiveMaximum must be an integer or a floatz$if {variable} >= {exclusiveMaximum}:z.{name} must be smaller than {exclusiveMaximum})r*   )r/   r   r   r3   r4   r   r    )r   r   r   r   r   R   s
    z/CodeGeneratorDraft06.generate_exclusive_maximumc             C   s&  | j  di ¡}|dkr�n|dkrP|  ¡  |  d¡� | jddd� W dQ R X nÒ|  ¡  |  d¡�º |  ¡  |  d	¡�œ |  d
¡ |  d¡�X |  d¡�" | j|d | j	¡| j
dd� W dQ R X |  d¡� |  d¡ W dQ R X W dQ R X |  d¡� | jddd� W dQ R X W dQ R X W d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:z{name} must not be there)r*   Nzif {variable}_is_dict:zif {variable}_len != 0:z {variable}_property_names = Truez!for {variable}_key in {variable}:ztry:z{}_key)Úclear_variablesz except JsonSchemaValueException:z!{variable}_property_names = Falsez!if not {variable}_property_names:z/{name} must be named by propertyName definition)r   ÚgetZcreate_variable_keysr/   r    Zcreate_variable_is_dictZcreate_variable_with_lengthÚgenerate_func_code_blockr-   r.   Ú_variable_name)r   Zproperty_names_definitionr   r   r   r   Y   s.    

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 d	Q R X n”|  d
¡ |  d¡�V |  d¡�6 | j|d | j¡| jdd� |  d¡ |  d¡ W d	Q R X |  d¡ W d	Q R X |  d¡� | jddd� W d	Q R X W d	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 invalid)r*   Tzif not {variable}:z{name} must not be emptyNz{variable}_contains = Falsez!for {variable}_key in {variable}:ztry:z{}_key)r5   z{variable}_contains = TrueÚbreakz%except JsonSchemaValueException: passzif not {variable}_contains:z.{name} must contain one of contains definition)Zcreate_variable_is_listr/   r   r    r7   r-   r.   r8   )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 d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})r*   N)r   r   Ústrr-   Úer/   r    )r   r   r   r   r   r   ©   s
    

z#CodeGeneratorDraft06.generate_const)Ú__name__Ú
__module__Ú__qualname__Údictr   ZFORMAT_REGEXSr   r   r   r2   r   r   r   r   r   Ú__classcell__r   r   )r   r   r      s   

	((r   N)Zdraft04r   r   Ú
exceptionsr   Ú	generatorr   r   r   r   r   r   Ú<module>   s   