a
    �Ï?e†  ã                   @   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i 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__© úY/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/fastjsonschema/draft06.pyr      s    ûzCodeGeneratorDraft06.__init__c                 C   s4   t |tƒr|  ¡  nd|v 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u r|  d¡ | j du 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 n2 tyX } ztd |¡ƒ‚W Y d}~n
d}~0 0 d}d|v rx|d	j| jd
�7 }d|v sˆd|v r¢d|vr¢|dj| jd
�7 }|  d||¡�& | jdd |¡dd� W d  ƒ n1 sÜ0    Y  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>=   ó    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¡�h t| jd tttjfƒs,tdƒ‚|   d¡� | jddd� W d   ƒ n1 sZ0    Y  W d   ƒ n1 sx0    Y  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}r1   ©	r$   r   r#   ÚintÚfloatÚdecimalÚDecimalr   r%   r&   r   r   r   r   N   s
    z/CodeGeneratorDraft06.generate_exclusive_minimumc              	   C   s†   |   d¡�h t| jd tttjfƒs,tdƒ‚|   d¡� | jddd� W d   ƒ n1 sZ0    Y  W d   ƒ n1 sx0    Y  d S )Nr9   r	   z9exclusiveMaximum must be an integer, a float or a decimalz$if {variable} >= {exclusiveMaximum}:z.{name} must be smaller than {exclusiveMaximum}r1   r:   r&   r   r   r   r   U   s
    z/CodeGeneratorDraft06.generate_exclusive_maximumc                 C   sÀ  | j  di ¡}|du r�n¢|du rf|  ¡  |  d¡� | jddd� W d  ƒ n1 sX0    Y  �nV|  ¡  |  d¡��0 |  ¡  |  d	¡�ü |  d
¡ |  d¡�Œ |  d¡�, | j|d | j	¡| j
dd� W d  ƒ n1 sâ0    Y  |  d¡� |  d¡ W d  ƒ n1 �s0    Y  W d  ƒ n1 �s80    Y  |  d¡� | jddd� W d  ƒ n1 �sr0    Y  W d  ƒ n1 �s’0    Y  W d  ƒ n1 �s²0    Y  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"   r1   Nz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 definition)r#   ÚgetZcreate_variable_keysr$   r%   Zcreate_variable_is_dictZcreate_variable_with_lengthÚgenerate_func_code_blockr4   r5   Ú_variable_name)r   Zproperty_names_definitionr   r   r   r   \   s0    0

ü$Jz,CodeGeneratorDraft06.generate_property_namesc              
   C   st  |   ¡  |  d¡��J | jd }|du r:| jddd� �n|du r||  d¡� | jddd� W d	  ƒ n1 sp0    Y  nÔ|  d
¡ |  d¡�t |  d¡�@ | j|d | j¡| jdd� |  d¡ |  d¡ W d	  ƒ n1 sâ0    Y  |  d¡ W d	  ƒ n1 �s0    Y  |  d¡� | jddd� W d	  ƒ n1 �sF0    Y  W d	  ƒ n1 �sf0    Y  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 invalidr1   Tzif not {variable}:z{name} must not be emptyNz{variable}_contains = Falser?   r@   rA   rB   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%   rD   r4   r5   rE   )r   Zcontains_definitionr   r   r   r   „   s,    
.

ü
(*z&CodeGeneratorDraft06.generate_containsc                 C   sb   | j d }t|tƒr$d |  |¡¡}|  d|¡� | jddd� W d  ƒ n1 sT0    Y  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}r1   N)r#   r   Ústrr4   Úer$   r%   )r   r   r   r   r   r   ¬   s
    

z#CodeGeneratorDraft06.generate_const)Ú__name__Ú
__module__Ú__qualname__Údictr   ZFORMAT_REGEXSr   r    r   r8   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   