
    ~fw                     ~    d dl Z d dlZddlmZ ddlmZmZ ddddd	d
ddZ ej                  d      Z	 G d de      Z
y)    N   )JsonSchemaDefinitionException)CodeGeneratorenforce_listNoneTypeboolzint, float, Decimalintstrzlist, tupledict)nullbooleannumberintegerstringarrayobjectz	(?<!\\)\$c                        e Zd ZdddddddZdi d	d	f fd
	Ze fd       Zd Zd Zd Z	d Z
d Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd  Zd! Zd" Zd# Zd$ Z xZ S )%CodeGeneratorDraft04zk^\d{4}-[01]\d-[0-3]\d(t|T)[0-2]\d:[0-5]\d:[0-5]\d(?:\.\d+)?(?:[+-][0-2]\d:[0-5]\d|[+-][0-2]\d[0-5]\d|z|Z)\Zz^[^@]+@[^@]+\.[^@]+\Zzv^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]{0,61}[A-Za-z0-9])\ZzV^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\Za  ^(?:(?:[0-9A-Fa-f]{1,4}:){6}(?:[0-9A-Fa-f]{1,4}:[0-9A-Fa-f]{1,4}|(?:(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))|::(?:[0-9A-Fa-f]{1,4}:){5}(?:[0-9A-Fa-f]{1,4}:[0-9A-Fa-f]{1,4}|(?:(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))|(?:[0-9A-Fa-f]{1,4})?::(?:[0-9A-Fa-f]{1,4}:){4}(?:[0-9A-Fa-f]{1,4}:[0-9A-Fa-f]{1,4}|(?:(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))|(?:[0-9A-Fa-f]{1,4}:[0-9A-Fa-f]{1,4})?::(?:[0-9A-Fa-f]{1,4}:){3}(?:[0-9A-Fa-f]{1,4}:[0-9A-Fa-f]{1,4}|(?:(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))|(?:(?:[0-9A-Fa-f]{1,4}:){,2}[0-9A-Fa-f]{1,4})?::(?:[0-9A-Fa-f]{1,4}:){2}(?:[0-9A-Fa-f]{1,4}:[0-9A-Fa-f]{1,4}|(?:(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))|(?:(?:[0-9A-Fa-f]{1,4}:){,3}[0-9A-Fa-f]{1,4})?::[0-9A-Fa-f]{1,4}:(?:[0-9A-Fa-f]{1,4}:[0-9A-Fa-f]{1,4}|(?:(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))|(?:(?:[0-9A-Fa-f]{1,4}:){,4}[0-9A-Fa-f]{1,4})?::(?:[0-9A-Fa-f]{1,4}:[0-9A-Fa-f]{1,4}|(?:(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))|(?:(?:[0-9A-Fa-f]{1,4}:){,5}[0-9A-Fa-f]{1,4})?::[0-9A-Fa-f]{1,4}|(?:(?:[0-9A-Fa-f]{1,4}:){,6}[0-9A-Fa-f]{1,4})?::)\Zz^\w+:(\/?\/?)[^\s]+\Z)z	date-timeemailhostnameipv4ipv6uriNTc                    t         |   ||       || _        || _        || _        | j
                  j                  d| j                  fd| j                  fd| j                  fd| j                  fd| j                  fd| j                  fd| j                  fd| j                  fd	| j                  fd
| j                   fd| j"                  fd| j$                  fd| j&                  fd| j(                  fd| j*                  fd| j,                  fd| j.                  fd| j0                  fd| j2                  fd| j4                  fd| j6                  fd| j8                  fd| j:                  fd| j<                  ff       d| _        y )NtypeenumallOfanyOfoneOfnot	minLength	maxLengthpatternformatminimummaximum
multipleOfminItemsmaxItemsuniqueItemsitemsminPropertiesmaxPropertiesrequireddependencies
propertiespatternPropertiesadditionalPropertiesr   ) super__init___custom_formats_use_formats_use_default_json_keywords_to_functionupdategenerate_typegenerate_enumgenerate_all_ofgenerate_any_ofgenerate_one_ofgenerate_notgenerate_min_lengthgenerate_max_lengthgenerate_patterngenerate_formatgenerate_minimumgenerate_maximumgenerate_multiple_ofgenerate_min_itemsgenerate_max_itemsgenerate_unique_itemsgenerate_itemsgenerate_min_propertiesgenerate_max_propertiesgenerate_requiredgenerate_dependenciesgenerate_propertiesgenerate_pattern_propertiesgenerate_additional_properties_any_or_one_of_count)self
definitionresolverformatsuse_defaultuse_formats	__class__s         Q/var/www/cvtools/html/venv/lib/python3.12/site-packages/fastjsonschema/draft04.pyr4   zCodeGeneratorDraft04.__init__%   s   X.&''''..T''(T''(d**+d**+d**+D%%&$223$223--.t++,--.--.4445001001D667d))*d::;d::;//0T7784334 $"B"BC#T%H%HI30
 	6 %&!    c                 :    t         |   }| j                  |d<   |S )Ncustom_formats)r3   global_stater5   )rS   resrY   s     rZ   r^   z!CodeGeneratorDraft04.global_stateG   s#    g" $ 4 4
r[   c                    t        | j                  d         }	 dj                  d |D              }d}d|v sd|v r d	|vrd
j                  | j                        }| j                  d||      5  | j                  ddj                  |      d       ddd       y# t        $ r}t	        dj                  |            d}~ww xY w# 1 sw Y   yxY w)z
        Validation of type. Can be one type or list of types.

        .. code-block:: python

            {'type': 'string'}
            {'type': ['string', 'number']}
        r   z, c              3   .   K   | ]  }t         |     y wN)JSON_TYPE_TO_PYTHON_TYPE).0ts     rZ   	<genexpr>z5CodeGeneratorDraft04.generate_type.<locals>.<genexpr>X   s     $PQ%=a%@$Ps   zUnknown type: {}N r   r   r   z  or isinstance({variable}, bool))variablez&if not isinstance({variable}, ({})){}:{name} must be {}z or rule)	r   _definitionjoinKeyErrorr   r$   	_variablelexc)rS   typespython_typesrq   extras        rZ   r:   z"CodeGeneratorDraft04.generate_typeM   s     T--f56	P99$P%$PPL e!3%9O6==t~~=VEVV<lER 	KHH(&++e*<6HJ	K 	K  	P/0B0I0I#0NOO	P	K 	Ks#   B /$C	C%B??CCc                     | j                   d   }t        |t        t        f      st	        d      | j                  d      5  | j                  d| j                  |      d       ddd       y# 1 sw Y   yxY w)z
        Means that only value specified in the enum is valid.

        .. code-block:: python

            {
                'enum': ['a', 'b'],
            }
        r   zenum must be an arrayzif {variable} not in {enum}:z{name} must be one of {}rj   N)rl   
isinstancelisttupler   rp   rq   e)rS   r   s     rZ   r;   z"CodeGeneratorDraft04.generate_enumc   so     '$u./0GHHVV23 	LHH/FHK	L 	L 	Ls   $A//A8c                 ~    | j                   d   D ]+  }| j                  || j                  | j                  d       - y)a  
        Means that value have to be valid by all of those definitions. It's like put it in
        one big definition.

        .. code-block:: python

            {
                'allOf': [
                    {'type': 'number'},
                    {'minimum': 5},
                ],
            }

        Valid values for this definition are 5, 6, 7, ... but not 4 or 'abc' for example.
        r   Tclear_variablesN)rl   generate_func_code_blockro   _variable_name)rS   definition_items     rZ   r<   z$CodeGeneratorDraft04.generate_all_ofs   sB       $//8 	vO))/4>>4K^K^pt)u	vr[   c           	      H   | xj                   dz  c_         | j                   }| j                  d|       | j                  d   D ]  }| j                  d|d      5  | j                  dd	      5  | j                  || j                  | j
                  d
       | j                  d|       ddd       | j                  d       ddd        | j                  d|d      5  | j                  dd       ddd       y# 1 sw Y   UxY w# 1 sw Y   xY w# 1 sw Y   yxY w)a  
        Means that value have to be valid by any of those definitions. It can also be valid
        by all of them.

        .. code-block:: python

            {
                'anyOf': [
                    {'type': 'number', 'minimum': 10},
                    {'type': 'number', 'maximum': 5},
                ],
            }

        Valid values for this definition are 3, 4, 5, 10, 11, ... but not 8 for example.
        r   z"{variable}_any_of_count{count} = 0countr   z&if not {variable}_any_of_count{count}:Fr   optimizetry:r   Tr{   z#{variable}_any_of_count{count} += 1N%except JsonSchemaValueException: passz,{name} cannot be validated by any definitionrj   rR   rp   rl   r}   ro   r~   rq   )rS   r   r   s      rZ   r=   z$CodeGeneratorDraft04.generate_any_of   s2     	!!Q&!))35A#//8 	@O@X]^ @VVFUV3 O11/4>>SWSfSfx|1}FF@FNO >?	@ @	@ VV<ETYVZ 	SHHC'HR	S 	SO O@ @	S 	Ss0   D/=D ,D#D D	DD	D!c           	      N   | xj                   dz  c_         | j                   }| j                  d|       | j                  d   D ]  }| j                  d|d      5  | j                  dd	      5  | j                  || j                  | j
                  d
       | j                  d|       ddd       | j                  d       ddd        | j                  d|      5  d}| j                  d||d       ddd       y# 1 sw Y   XxY w# 1 sw Y   xY w# 1 sw Y   yxY w)a  
        Means that value have to be valid by only one of those definitions. It can't be valid
        by two or more of them.

        .. code-block:: python

            {
                'oneOf': [
                    {'type': 'number', 'multipleOf': 3},
                    {'type': 'number', 'multipleOf': 5},
                ],
            }

        Valid values for this definition are 3, 5, 6, ... but not 15 for example.
        r   z"{variable}_one_of_count{count} = 0r   r   z&if {variable}_one_of_count{count} < 2:Fr   r   r   Tr{   z#{variable}_one_of_count{count} += 1Nr   z'if {variable}_one_of_count{count} != 1:z9" (" + str({variable}_one_of_count{}) + " matches found)"z.{name} must be valid exactly by one definition)append_to_msgrk   r   )rS   r   r   dynamics       rZ   r>   z$CodeGeneratorDraft04.generate_one_of   s:     	!!Q&!))35A#//8 	@O@X]^ @VVFUV3 O11/4>>SWSfSfx|1}FF@FNO >?	@ @	@ VV=UVK 	sQGHHEu\cjqHr	s 	sO O@ @	s 	ss0   D/=D,D"DDDD	D$c                    | j                   d   }|du r| j                  dd       y|du ry|s9| j                  d| j                        5  | j                  dd       ddd       y| j                  d	d
      5  | j	                  || j                  | j
                         ddd       | j                  d       | j                  d      5  | j                  dd       ddd       y# 1 sw Y   yxY w# 1 sw Y   TxY w# 1 sw Y   yxY w)aT  
        Means that value have not to be valid by this definition.

        .. code-block:: python

            {'not': {'type': 'null'}}

        Valid values for this definition are 'hello', 42, {} ... but not None.

        Since draft 06 definition can be boolean. False means nothing, True
        means everything is invalid.
        r    T{name} must not be thererj   FNzif {}:z-{name} must NOT match a disallowed definitionr   r   r   zelse:)rl   rq   rp   ro   r}   r~   )rS   not_definitions     rZ   r?   z!CodeGeneratorDraft04.generate_not   s    ))%0T!HH/eH<u$$..1 VHuUV V / c--ndnndNaNabcFF:; VHuUV VV Vc cV Vs$   C*;(C6D*C36C?Dc                 2   | j                  d      5  | j                          t        | j                  d   t              st        d      | j                  d      5  | j                  dd       d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)Nif isinstance({variable}, str):r!   zminLength must be a numberz if {variable}_len < {minLength}:z={name} must be longer than or equal to {minLength} charactersrj   rp   create_variable_with_lengthrv   rl   r	   r   rq   rS   s    rZ   r@   z(CodeGeneratorDraft04.generate_min_length   s    VV56 	l,,.d..{;SA34PQQ:; lX_jkl		l 	ll l		l 	l$   A
BB0BB
	BBc                 2   | j                  d      5  | j                          t        | j                  d   t              st        d      | j                  d      5  | j                  dd       d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)Nr   r"   zmaxLength must be a numberz if {variable}_len > {maxLength}:z>{name} must be shorter than or equal to {maxLength} charactersrj   r   r   s    rZ   rA   z(CodeGeneratorDraft04.generate_max_length   s    VV56 	m,,.d..{;SA34PQQ:; mY`klm		m 	mm m		m 	mr   c                    | j                  d      5  | j                  d   }|j                  dd      j                  dd      }t        j	                  d|      }t        j                  |      | j                  |<   | j                  dt        |            5  | j                  d	|d
       d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)Nr   r#   \z\\"z\"z\\Zz-if not REGEX_PATTERNS[{}].search({variable}):z{name} must match pattern {}rj   )
rp   rl   replaceDOLLAR_FINDERsubrecompile_compile_regexpsreprrq   )rS   r#   safe_patternend_of_string_fixed_patterns       rZ   rB   z%CodeGeneratorDraft04.generate_pattern   s    VV56 	W&&y1G"??48@@eLL*7*;*;FG*L'-/ZZ8S-TD!!'*GgW W7IVW	W 	W
W W	W 	Ws$   BC	B=,C	=C	C		Cc                    | j                   sy| j                  d      5  | j                  d   }|| j                  v rf| j                  |   }t	        |t
              r| j                  ||dz   |       n| j                  d|      5  | j                  d|d       ddd       n|| j                  v r&| j                  |   }| j                  ||dz   |       nz|dk(  r[| j                  d	d
      5  | j                  d       ddd       | j                  d      5  | j                  dd       ddd       nt        dj                  |            ddd       y# 1 sw Y   xY w# 1 sw Y   fxY w# 1 sw Y   *xY w# 1 sw Y   yxY w)z
        Means that value have to be in specified format. For example date, email or other.

        .. code-block:: python

            {'format': 'email'}

        Valid value for this definition is user@example.com but not @username
        Nr   r$   _re_patternz(if not custom_formats["{}"]({variable}):ri   rj   regexr   Fr   zre.compile({variable})zexcept Exception:z{name} must be a valid regexzUnknown format: {})r6   rp   rl   r5   rv   r
   _generate_formatrq   FORMAT_REGEXSr   r$   )rS   format_custom_formatformat_regexs       rZ   rC   z$CodeGeneratorDraft04.generate_format   s      VV56 	Z&&x0G$... $ 4 4W =mS1))'7]3JMZ JGT N!4gHMN ND...#11':%%gw/FUG#VVFUV3 5FF345VV/0 LHH;(HKL L 44H4O4OPW4XYY)	Z 	ZN N5 5L L#	Z 	ZsU   A&E>EAE>/E&E>E2.#E>E#	E>&E/	+E>2E;	7E>>Fc                     | j                   d   |k(  r`|| j                  vr"t        j                  |      | j                  |<   | j	                  d|      5  | j                  d|d       d d d        y y # 1 sw Y   y xY w)Nr$   z.if not REGEX_PATTERNS["{}"].match({variable}):ri   rj   )rl   r   r   r   rp   rq   )rS   format_nameregexp_nameregexps       rZ   r   z%CodeGeneratorDraft04._generate_format  s    H%4$"7"7757ZZ5G%%k2H+V J,kIJ J 5J Js   A44A=c                    | j                  d      5  t        | j                  d   t        t        t
        j                  f      st        d      | j                  j                  dd      r.| j                  d      5  | j                  dd       d d d        n-| j                  d	      5  | j                  d
d       d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   y xY w)N1if isinstance({variable}, (int, float, Decimal)):r%   zminimum must be a numberexclusiveMinimumFzif {variable} <= {minimum}:z${name} must be bigger than {minimum}rj   zif {variable} < {minimum}:z0{name} must be bigger than or equal to {minimum}
rp   rv   rl   r	   floatdecimalDecimalr   getrq   r   s    rZ   rD   z%CodeGeneratorDraft04.generate_minimum"  s    VVGH 	ad..y9C;XY34NOO##$6>VV9: UHHC)HTU U VV89 aHHOV_H`a	a 	aU Ua a	a 	a<   A+C(=CC(+C?C(C	C(C%	!C((C1c                    | j                  d      5  t        | j                  d   t        t        t
        j                  f      st        d      | j                  j                  dd      r.| j                  d      5  | j                  dd       d d d        n-| j                  d	      5  | j                  d
d       d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   y xY w)Nr   r&   zmaximum must be a numberexclusiveMaximumFzif {variable} >= {maximum}:z%{name} must be smaller than {maximum}rj   zif {variable} > {maximum}:z1{name} must be smaller than or equal to {maximum}r   r   s    rZ   rE   z%CodeGeneratorDraft04.generate_maximum-  s    VVGH 	bd..y9C;XY34NOO##$6>VV9: VHHD9HUV V VV89 bHHPW`Hab	b 	bV Vb b	b 	br   c                    | j                  d      5  t        | j                  d   t        t        t
        j                  f      st        d      t        | j                  d   t              r| j                  d       n| j                  d       | j                  d      5  | j                  dd       d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)	Nr   r'   zmultipleOf must be a numberzBquotient = Decimal(repr({variable})) / Decimal(repr({multipleOf}))z$quotient = {variable} / {multipleOf}zif int(quotient) != quotient:z'{name} must be multiple of {multipleOf}rj   )	rp   rv   rl   r	   r   r   r   r   rq   r   s    rZ   rF   z)CodeGeneratorDraft04.generate_multiple_of8  s    VVGH 
	Wd..|<sE7??>[\34QRR $**<8%@[\=>78 WBVW
	W 
	WW W
	W 
	Ws$   BC!C5CC	CCc                 R   | j                          | j                  d      5  t        | j                  d   t              st        d      | j                          | j                  d      5  | j                  dd       d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)Nif {variable}_is_list:r(   zminItems must be a numberzif {variable}_len < {minItems}:z-{name} must contain at least {minItems} itemsrj   create_variable_is_listrp   rv   rl   r	   r   r   rq   r   s    rZ   rG   z'CodeGeneratorDraft04.generate_min_itemsE  s    $$&VV,- 	[d..z:C@34OPP,,.9: [HzZ[		[ 	[[ [		[ 	[$   A
B,B BB	BB&c                 R   | j                          | j                  d      5  t        | j                  d   t              st        d      | j                          | j                  d      5  | j                  dd       d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)Nr   r)   zmaxItems must be a numberzif {variable}_len > {maxItems}:z:{name} must contain less than or equal to {maxItems} itemsrj   r   r   s    rZ   rH   z'CodeGeneratorDraft04.generate_max_itemsN  s    $$&VV,- 	hd..z:C@34OPP,,.9: hU\fgh		h 	hh h		h 	hr   c                 H   | j                   d   }|sy| j                          | j                  d      5  | j                  d       | j                          | j                  d      5  | j	                  dd       ddd       ddd       y# 1 sw Y   xY w# 1 sw Y   yxY w)a  
        With Python 3.4 module ``timeit`` recommended this solutions:

        .. code-block:: python

            >>> timeit.timeit("len(x) > len(set(x))", "x=range(100)+range(100)", number=100000)
            0.5839540958404541
            >>> timeit.timeit("len({}.fromkeys(x)) == len(x)", "x=range(100)+range(100)", number=100000)
            0.7094449996948242
            >>> timeit.timeit("seen = set(); any(i in seen or seen.add(i) for i in x)", "x=range(100)+range(100)", number=100000)
            2.0819358825683594
            >>> timeit.timeit("np.unique(x).size == len(x)", "x=range(100)+range(100); import numpy as np", number=100000)
            2.1439831256866455
        r*   Nr   zdef fn(var): return frozenset(dict((k, fn(v)) for k, v in var.items()).items()) if hasattr(var, "items") else tuple(fn(v) for v in var) if isinstance(var, (dict, list)) else str(var) if isinstance(var, bool) else varzNif {variable}_len > len(set(fn({variable}_x) for {variable}_x in {variable})):z {name} must contain unique itemsrj   )rl   r   rp   r   rq   )rS   unique_definitions     rZ   rI   z*CodeGeneratorDraft04.generate_unique_itemsW  s     !,,]; $$&VV,- 	QFF45 ,,.hi Q;-PQ	Q 	QQ Q	Q 	Qs#   3B'B;BB	BB!c                 0   | j                   d   }|du ry| j                          | j                  d      5  | j                          |du r/| j                  d      5  | j	                  dd       ddd       nLt        |t              rt        |      D ]  \  }}| j                  d	|      5  | j                  d
|       | j                  |dj                  | j                  |      dj                  | j                  |             ddd       | j                  st        |t              sd|v s| j                  dt        |d                 d| j                   v rd| j                   d   du r9| j                  d	t        |            5  | j	                  dd       ddd       n| j                  dt        |            5  | j                  | j                   d   dj                  | j                        dj                  | j                  | j                              }|dk(  r| j                  d       ddd       n|r| j                  d      5  | j                  |dj                  | j                        dj                  | j                  | j                              }|dk(  r| j                  d       ddd       ddd       y# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   +xY w# 1 sw Y   7xY w# 1 sw Y   CxY w# 1 sw Y   yxY w)a  
        Means array is valid only when all items are valid by this definition.

        .. code-block:: python

            {
                'items': [
                    {'type': 'integer'},
                    {'type': 'string'},
                ],
            }

        Valid arrays are those with integers or strings, nothing else.

        Since draft 06 definition can be also boolean. True means nothing, False
        means everything is invalid.
        r+   TNr   Fzif {variable}:r   rj   zif {variable}_len > {}:z!{variable}__{0} = {variable}[{0}]{}__{}z{}[{}]defaultzelse: {variable}.append({})additionalItemsz({name} must contain only specified itemszFfor {variable}_x, {variable}_item in enumerate({variable}[{0}:], {0}):z{}_itemz{}[{{{}_x}}]r   passz;for {variable}_x, {variable}_item in enumerate({variable}):)rl   r   rp   r   rq   rv   rw   	enumerater}   r$   ro   r~   r7   r   r   len)rS   items_definitionidxitem_definitionr   s        rZ   rJ   z#CodeGeneratorDraft04.generate_itemsx  s   $  ++G4t#$$&VV,- '	+,,.5(VV,- GHH7gHFG G,d3,56F,G 	`(C 93? BCH55+$OODNNC@$OOD,?,?E ((Z-NS\`oSo<d?S\C]>^_	` %(8(88''(9:eC!VV$=sCS?TU _ HH%OV]H^_ _ "VV$lnq  sC  oD  E /$($A$A $ 0 01B C ) 0 0 @ . 5 5d6I6I4>> Z%E
  %z $v/ / $ ]^ + $ = =,%,,T^^<*11$2E2Et~~V!
 !A: FF6N+A'	+ '	+G G _ _/ /+ +A'	+ '	+s   &LK0>L.AKLL.L3ALK("%LA4K4;LA'L >LK	LK% L(K1	-L4K=	9L L		LLc                 R   | j                          | j                  d      5  t        | j                  d   t              st        d      | j                          | j                  d      5  | j                  dd       d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)Nif {variable}_is_dict:r,   zminProperties must be a numberz$if {variable}_len < {minProperties}:z7{name} must contain at least {minProperties} propertiesrj   create_variable_is_dictrp   rv   rl   r	   r   r   rq   r   s    rZ   rK   z,CodeGeneratorDraft04.generate_min_properties  s    $$&VV,- 	jd..?E34TUU,,.>? jRYhij		j 	jj j		j 	jr   c                 R   | j                          | j                  d      5  t        | j                  d   t              st        d      | j                          | j                  d      5  | j                  dd       d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)Nr   r-   zmaxProperties must be a numberz$if {variable}_len > {maxProperties}:zD{name} must contain less than or equal to {maxProperties} propertiesrj   r   r   s    rZ   rL   z,CodeGeneratorDraft04.generate_max_properties  s    $$&VV,- 	wd..?E34TUU,,.>? w_fuvw		w 	ww w		w 	wr   c                    | j                          | j                  d      5  t        | j                  d   t        t
        f      st        d      t        | j                  d         t        t        | j                  d               k7  rt        d      | j                  j                  dd      s| j                  d   D cg c]P  | j                  j                  di       vr0t        fd| j                  j                  d	i       D              sR }}|r%t        d
j                  | j                  |            | j                  d       | j                  d      5  d}| j                  d| j                  | j                  d         d|       d d d        d d d        y c c}w # 1 sw Y   xY w# 1 sw Y   y xY w)Nr   r.   zrequired must be an arrayz%required must contain unique elementsr2   Tr0   c              3   J   K   | ]  }t        j                  |        y wrb   )r   search)rd   r   props     rZ   rf   z9CodeGeneratorDraft04.generate_required.<locals>.<genexpr>  s     #vuBIIeT$:#vs    #r1   z){}: items {} are required but not allowedz>{variable}__missing_keys = set({required}) - {variable}.keys()zif {variable}__missing_keys:z5str(sorted({variable}__missing_keys)) + " properties"z{name} must contain )rk   r   )r   rp   rv   rl   rw   rx   r   r   setr   anyr$   ro   rq   ry   )rS   r   not_possibler   s    `  rZ   rM   z&CodeGeneratorDraft04.generate_required  s   $$&VV,- 	d..z:T5MJ34OPP4##J/0CD<L<LZ<X8Y4ZZ34[\\##''(>E !% 0 0 < D$4$4$8$8r$JJ ##vHXHXH\H\]prtHu#v v	      78c8j8jkokyky  |H  9I  J  JFFST67 Q/8H8H8T1U\fv}~!	 	  !	 	s8   B"F;AF*AF;%4F/F;*F;/F8	4F;;Gc                    | j                          | j                  d      5  | j                          | j                  d   j	                         D ]q  \  }}t        j                  dd|      }t        |t        t        f      s%t        dj                  | j                  |            | j                  d| j                  |            5  | j                  d| j                  |             | j                  d|| j                  |             | j                  |d	j                  | j                  |      d
j                  | j                  | j                  |            d       ddd       | j                   s,t        |t              s>d|v sD| j                  d| j                  |      t#        |d                t 	 ddd       y# 1 sw Y   jxY w# 1 sw Y   yxY w)a  
        Means object with defined keys.

        .. code-block:: python

            {
                'properties': {
                    'key': {'type': 'number'},
                },
            }

        Valid object is containing key called 'key' and value any number.
        r   r0   z($[^a-zA-Z]|[^a-zA-Z0-9])rg   z{}[{}] must be objectzif "{}" in {variable}_keys:z{variable}_keys.remove("{}")z#{variable}__{0} = {variable}["{1}"]r   z{}.{}Tr{   Nr   zelse: {variable}["{}"] = {})r   rp   create_variable_keysrl   r+   r   r   rv   r   r   r   r$   ro   ry   r}   r~   r7   r   )rS   keyprop_definitionkey_names       rZ   rO   z(CodeGeneratorDraft04.generate_properties  s    	$$&VV,- 	i%%'(,(8(8(F(L(L(N i$_66">CH!/D$<@78O8V8VW[WeWego8pqqVV9466#;G FF9466#;GFF@(DFFSVKX11' At':':DFF3KH(,	 2  $$OT)Jy\kOkFF8$&&+tO\eLfGghi	i 	i 	i 	is7   B(G
BG&G<GG1GGGG$c                 (   | j                          | j                  d      5  | j                          | j                  d   j	                         D ]'  \  }}t        j                  |      | j                  |<   ) | j                  d      5  | j                  d   j	                         D ]  \  }}| j                  dt        |            5  | j                  d      5  | j                  d       ddd       | j                  |dj                  | j                        d	j                  | j                  | j                        d
       ddd        	 ddd       ddd       y# 1 sw Y   wxY w# 1 sw Y   xY w# 1 sw Y   *xY w# 1 sw Y   yxY w)a4  
        Means object with defined keys as patterns.

        .. code-block:: python

            {
                'patternProperties': {
                    '^x': {'type': 'number'},
                },
            }

        Valid object is containing key starting with a 'x' and value any number.
        r   r1   z9for {variable}_key, {variable}_val in {variable}.items():z-if REGEX_PATTERNS[{}].search({variable}_key):z%if {variable}_key in {variable}_keys:z&{variable}_keys.remove({variable}_key)Nz{}_val{}.{{{}_key}}Tr{   )r   rp   r   rl   r+   r   r   r   r   r}   r$   ro   r~   )rS   r#   rT   s      rZ   rP   z0CodeGeneratorDraft04.generate_pattern_properties  sx    	$$&VV,- 	%%''+'7'78K'L'R'R'T E#13G1D%%g.EST 
+/+;+;<O+P+V+V+X 	'GZ OQUV]Q^_ !VV$KL M FF#KLM55&$OODNN;+2243F3FW,0	 6  	
		 	M M 
 
		 	sU   A)F?E<
E0	E$.AE0	
E<F$E-)E0	0E95E<<F	FFc                    | j                          | j                  d      5  | j                          | j                  d   }|du s|i k(  r
	 ddd       y|rt	        | j                  j                  di       j                               }| j                  d      5  | j                  d|      5  | j                  d       | j                  |d	j                  | j                        d
j                  | j                  | j                               ddd       ddd       n-| j                  d      5  | j                  dd       ddd       ddd       y# 1 sw Y   HxY w# 1 sw Y   xY w# 1 sw Y   *xY w# 1 sw Y   yxY w)a  
        Means object with keys with values defined by definition.

        .. code-block:: python

            {
                'properties': {
                    'key': {'type': 'number'},
                }
                'additionalProperties': {'type': 'string'},
            }

        Valid object is containing key called 'key' and it's value any number and
        any other key with any string.
        r   r2   TNr0   z&for {variable}_key in {variable}_keys:zif {variable}_key not in {}:z1{variable}_value = {variable}.get({variable}_key)z{}_valuer   zif {variable}_keys:z;{name} must not contain "+str({variable}_keys)+" propertiesrj   )r   rp   r   rl   rw   r   keysr}   r$   ro   r~   rq   )rS   add_prop_definitionproperties_keyss      rZ   rQ   z3CodeGeneratorDraft04.generate_additional_properties   ss     	$$&VV,- 	y%%'"&"2"23I"J"d*.AR.G		y 	y
 #"&t'7'7';';L"'M'R'R'T"UVVDE  >P RS55/&--dnn=+2243F3FW  VV12 yHHZawHxy!	y 	y  y y!	y 	ys[   *E;AE;E#.A"EE#E;2E/E;E E##E,	(E;/E8	4E;;Fc                    | j                          | j                  d      5  d}| j                  d   j                         D ]  \  }}|g k(  s|du rd}| j                  d| j	                  |            5  |du r| j                  d|d       nt        |t              re|D ]_  }| j                  d| j	                  |            5  | j                  d	| j	                  |      | j	                  |      d       d
d
d
       a n)| j                  || j                  | j                  d       d
d
d
        |r| j                  d       d
d
d
       y
# 1 sw Y   xY w# 1 sw Y   'xY w# 1 sw Y   y
xY w)a  
        Means when object has property, it needs to have also other property.

        .. code-block:: python

            {
                'dependencies': {
                    'bar': ['foo'],
                },
            }

        Valid object is containing only foo, both bar and foo or none of them, but not
        object with only bar.

        Since draft 06 definition can be boolean or empty array. True and empty array
        means nothing, False means that key cannot be there at all.
        r   Tr/   Fzif "{}" in {variable}:z{} in {name} must not be thererj   zif "{}" not in {variable}:z#{name} missing dependency {} for {}Nr{   r   )r   rp   rl   r+   ry   rq   rv   rw   r}   ro   r~   )rS   is_emptyr   valuesvalues        rZ   rN   z*CodeGeneratorDraft04.generate_dependenciesD  s   $ 	$$&VV,- 	H#//?EEG yVR<6T> VV4dffSkB y!A3^\#FD1%+ AE!%(DdffUm!T A $)NPTPVPVW\P]_c_e_efi_jq  !AA AA 55fdnndNaNasw5xy y	y v!	 	A Ay y	 	s=   AE,5AE4E94E-E,EEE)$E,,E5)!__name__
__module____qualname__r   r4   propertyr^   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   r   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rO   rP   rQ   rN   __classcell__)rY   s   @rZ   r   r      s     D) Ni y'M -1"$\`  &D  
K,L v&S<s>V8lmW ZFJ	a	bW[hQB>+@jw,iB>"yH#r[   r   )r   r   
exceptionsr   	generatorr   r   rc   r   r   r    r[   rZ   <module>r      sP     	 5 2 #  

<(Q	= Q	r[   