
    ]jA                     (   d dl Z d dlZd dlZd dlmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZ d dlmZ d dlmZmZmZmZmZmZ d d	lmZ d d
lmZ d dlmZ d dlmZ d dlmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(  ejR                         Z* e       Z G d d      Z+y)    N)ChainMap)deepcopy)datetime)count)Path)MappingProxyType)AnyDictListOptionalTupleUnion)Faker)validate)confloat)open)
AllOfAllTypesAnyOfArrayJSFEnumJSFTupleObjectOneOf
PrimitivesPrimitiveTypesc                   ,   e Zd ZdZ eeeeee	e
dd       ei       ddfdeeef   deeef   deeef   d	 ed
d      def
dZe eeeeee	e
dd       ei       ddfdedeeef   deeef   d	 ed
d      dedd fd       Zdededeeef   defdZ	 d+dededeeef   dee   def
dZ	 d+dededeeef   dee   def
dZ	 d+dededeeef   dee   def
dZdeeef   de
eef   fdZ	 d+dededeeef   dee   de f
dZ!	 d+dededeeef   dee   de"f
dZ#	 d+dededeeef   dee   de$f
dZ%dededefdZ&	 d+dededeeef   dee   def
dZ'deeef   defd Z(e)d!        Z*	 d+d"d"d#d$ee   d%ed&edefd'Z+d( Z,d,d)Z-deddfd*Z.y)-JSFa  The JSF class generates fake data based on a provided JSON Schema.

    Attributes:
        root_schema (Dict[str, Any]): The JSON schema based on which the fake data is generated.
        definitions (Dict): A dictionary to store definitions used in the JSON schema.
        base_state (Dict[str, Any]): A dictionary that represents the state of the parser. It includes a counter, a list of all JSON paths, and the provided initial state.
    )r   r   r   )fakerrandomr   __internal__g      ?
   schemacontextinitial_stateallow_none_optionalsg        g      ?)gelemax_recursive_depthc                     || _         i | _        t        d      g dd|| _        || _        || _        || _        d| _        | j                  |       y)a{  Initializes the JSF generator with the provided schema and
        configuration options.

        Args:
            schema (Dict[str, Any]): The JSON schema based on which the fake data is generated.
            context (Dict[str, Any], optional): A dictionary that provides additional utilities for handling the schema, such as a faker for generating fake data, a random number generator, and datetime utilities. It also includes an internal dictionary for handling List, Union, and Tuple types. Defaults to a dictionary with "faker", "random", "datetime", and "__internal__" keys.
            initial_state (Dict[str, Any], optional): A dictionary that represents the initial state of the parser. If you wish to extend the state so it can be accesses by your schema you can add any references in here. Defaults to an empty dictionary.
            allow_none_optionals (confloat, optional): A parameter that determines the probability of optional fields being set to None. Defaults to 0.5.
            max_recursive_depth (int, optional): A parameter that determines the maximum depth when generating a recursive schema. Defaults to 10.
           )startr   )__counter____all_json_paths__	__depth__N)	root_schemadefinitionsr   
base_statebase_contextr&   r)   root_parse)selfr#   r$   r%   r&   r)   s         4/root/env/lib/python3.12/site-packages/jsf/parser.py__init__zJSF.__init__+   sa    2 " q>"$
 	
 $$8!#6 	F    pathreturnc                     t        |       5 }t        t        j                  |      ||||      cddd       S # 1 sw Y   yxY w)a  Initializes the JSF generator with the provided schema at the given
        path and configuration options.

        Args:
            path (Path): The path to the JSON schema based on which the fake data is generated.
            context (Dict[str, Any], optional): A dictionary that provides additional utilities for handling the schema, such as a faker for generating fake data, a random number generator, and datetime utilities. It also includes an internal dictionary for handling List, Union, and Tuple types. Defaults to a dictionary with "faker", "random", "datetime", and "__internal__" keys.
            initial_state (Dict[str, Any], optional): A dictionary that represents the initial state of the parser. If you wish to extend the state so it can be accesses by your schema you can add any references in here. Defaults to an empty dictionary.
            allow_none_optionals (confloat, optional): A parameter that determines the probability of optional fields being set to None. Defaults to 0.5.
            max_recursive_depth (int, optional): A parameter that determines the maximum depth when generating a recursive schema. Defaults to 10.
        N)r   r   jsonload)r:   r$   r%   r&   r)   fs         r7   	from_jsonzJSF.from_jsonS   s?    2 $Z 	1		!g}6JL_	 	 	s	   "8Anamec                     | j                  |      \  }}t        j                  |      }|j                  |||| j                  | j
                  d|      S )NrA   r:   is_nullabler&   r)   )_JSF__is_field_nullabler   get	from_dictr&   r)   )r6   rA   r:   r#   	item_typerD   clss          r7   __parse_primitivezJSF.__parse_primitiveq   s`    !%!9!9&!A	;nnY'}}*(,(A(A'+'?'? 	
 		
r9   Nr4   c           	         | j                  |      \  }}t        j                  |||| j                  | j                  d|      }||n|}g }|j                  di       j                         D ].  \  }	}
|j                  | j                  |	| d|	 |
|             0 ||_	        g }|j                  di       j                         D ].  \  }	}
|j                  | j                  |	| d|	 |
|             0 ||_
        |S )NrC   
properties/r:   r#   r4   patternProperties)rE   r   rG   r&   r)   rF   itemsappend_JSF__parse_definitionrL   rO   )r6   rA   r:   r#   r4   _rD   modelprops_name
definitionpattern_propss               r7   __parse_objectzJSF.__parse_object   s3    11&9;  *(,(A(A'+'?'? 	
 u$!'L"!=!C!C!E 	E:LL''tfAeW4Ej_c'd	 !!',?!D!J!J!L 	E:  ''tfAeW4Ej_c'd	 #0r9   c                     | j                  |      \  }}t        j                  |||| j                  | j                  d|      }||n|}| j                  || d|d   |      |_        |S )NrC   z/itemsrP   r4   )rE   r   rG   r&   r)   rR   rP   )r6   rA   r:   r#   r4   rS   rD   arrs           r7   __parse_arrayzJSF.__parse_array   s     11&9;oo*(,(A(A'+'?'? 	
 ls++DTF&/6'?Y]+^	
r9   c                 D   | j                  |      \  }}t        j                  |||| j                  | j                  d|      }||n|}g |_        t        |d         D ]<  \  }}	|j
                  j                  | j                  || d| d| d|	|             > |S )NrC   rP   rM   []rN   )	rE   r   rG   r&   r)   rP   	enumeraterQ   rR   )
r6   rA   r:   r#   r4   rS   rD   r\   iitems
             r7   __parse_tuplezJSF.__parse_tuple   s     11&9;  *(,(A(A'+'?'? 	
 ls	 1 	GAtII''dV1TF!A3a3HQU\`'a	 
r9   c                 Z   |j                  d      }t        |t              rd|v rJt        t	        |            dk\  r3t        |      }|j                  d       t        j                  |      dfS t        t	        |            dk\  r"t        |      }t        j                  |      dfS |dfS )Ntypenull   Tr+   F)	rF   
isinstancelistlensetr   remover    choice)r6   r#   rH   item_type_deep_copys       r7   __is_field_nullablezJSF.__is_field_nullable   s    JJv&	i&"s3y>':a'?&.y&9##**62}}%894??3y>"a'&.y&9#}}%895@@%r9   c           	          t        d||| j                  d|}||n|}g }|d   D ]&  }|j                  | j                  ||||             ( ||_        |S )NrA   r:   r)   anyOfr[    )r   r)   rQ   rR   schemasr6   rA   r:   r#   r4   rT   ru   ds           r7   __parse_anyOfzJSF.__parse_anyOf   r     c4d@X@Xc\bcu$ 	NANN4224qt2LM	Nr9   c                     t        t        |d          }t        d||| j                  d|}||n|}| j	                  ||||      |_        |S )NallOfrr   r[   rt   )dictr   r   r)   rR   combined_schema)r6   rA   r:   r#   r4   r}   rT   s          r7   __parse_allOfzJSF.__parse_allOf   sa     x9:c4d@X@Xc\bcu$ $ 7 7dOZ^ 7 _r9   c           	          t        d||| j                  d|}||n|}g }|d   D ]&  }|j                  | j                  ||||             ( ||_        |S )Nrr   oneOfr[   rt   )r   r)   rQ   rR   ru   rv   s           r7   __parse_oneOfzJSF.__parse_oneOf   ry   r9   def_namec           	          | j                   }d }dD ]z  }|j                  d| d|       rd|_        |c S |j                  |i       j                  |      }|I| j	                  || d| d| ||      }|| j
                  d| d| <   | |S )Nr1   z$defs#/rM   Tz/#/rN   )r0   
startswithis_recursiverF   rR   r1   )r6   r:   r   r4   r#   parsed_definitiondef_tagrW   s           r7   __parse_named_definitionzJSF.__parse_named_definition   s    !! / 		PGG9AhZ89$(!GR044X>J%$($;$;dV3wiq
#CJ]a %< %! ?P  2gYaz!:;		P ! r9   c           
         | j                   d   j                  |       | j                        \  }}dv r	d   gd<   dv rdd   }t        |      dkD  sJ d       t	        d |D              sJ d       t        j                  |||| j                  | j                  d      S d	v r'|d
k(  rdv r| j                  |||      S |d
k(  rdv r| j                  |||      S |d
k(  rdv r| j                  |||      S |d
k(  rdv r| j                  |||      S |dk(  rj                  d      t        j                  d      t              r| j!                  |||      S t        j                  d      t"              rJt	        d j                  dg       D              r'| j%                  |||      S | j'                  ||      S y y dv rd   j)                  d      \  }}	|dk(  rad|	 | j*                  v r(t-        | j*                  j                  d|	             }
n|	j)                  d      d   }| j/                  |||      }
nZt1        |d      5 }t3        t5        j6                  |            }d d d        t-        j*                  j                  d|	             }
|dk7  r|
|k(  r	||
_        |
S |dk7  r||
_        ||
_        |
S dv r| j                  |||      S dv r| j                  |||      S dv r| j                  |||      S t=        fddD              s4| j'                  ||i d	t#        t?        j@                               i      S tC        dtE                     # 1 sw Y   xY w)Nr.   constenumr   zEnum List is Emptyc              3   p   K   | ].  }t        |t        t        t        t        t        d       f       0 y wN)ri   intfloatstrr|   rf   ).0rc   s     r7   	<genexpr>z)JSF.__parse_definition.<locals>.<genexpr>
  s+      JN
4#uc4d!DEs   46z1Enum Type is not null, int, float, string or dictrC   rf   objectrL   rs   r{   r   arraycontainsrP   c              3   <   K   | ]  }t        |t                y wr   )ri   r|   )r   xs     r7   r   z)JSF.__parse_definition.<locals>.<genexpr>#  s      A,-Jq$'As   z$ref# rM   rc              3   &   K   | ]  }|v  
 y wr   rt   )r   keyr#   s     r7   r   z)JSF.__parse_definition.<locals>.<genexpr>B  s     LsSF]Ls   )notifthenelsezCannot parse schema )#r2   rQ   rE   rk   allr   rG   r&   r)   _JSF__parse_object_JSF__parse_anyOf_JSF__parse_allOf_JSF__parse_oneOfrF   ri   r|   _JSF__parse_arrayrj   _JSF__parse_tuple_JSF__parse_primitivesplitr1   r   _JSF__parse_named_definitions_openr   r=   r>   rA   r:   anyr   keys
ValueErrorrepr)r6   rA   r:   r#   r4   rH   rD   	enum_listextfragrI   ref_namer?   external_jsfs      `          r7   __parse_definitionzJSF.__parse_definition   s    	,-44T:!%!9!9&!A	;f$Wo.F6NVvIy>A%;';;% R[  CBC  $$  #.,0,E,E+/+C+C 	 	 vH$)?**4vtDDh&7f+<))$fdCCh&7f+<))$fdCCh&7f+<))$fdCCg%JJz*6:fjjQXFY[_;`--dD&$GGfjj148S A17GR1HA >  --dD&$GG--dD&AA>8 vv,,S1ICbytf:!1!11"4#3#3#7#7!D6
#CDC  $zz#r2H77hMCC% 5#&tyy|#4L5|77;;avJGHs{sd{ J J%%dD&$??%%dD&$??%%dD&$??L.KLL))$6a6ajooN_I`6abb3DL>BCC%5 5s   "M??Nc           	         dD ]n  }|j                  |i       j                         D ]I  \  }}d| d| | j                  vs| j                  |d| d| |      }|| j                  d| d| <   K p | j                  dd|      | _        y )Nr   r   rM   )r:   r#   r4   r   )rA   r:   r#   )rF   rP   r1   rR   r4   )r6   r#   r   rA   rW   rc   s         r7   r5   z
JSF._parseG  s    / 	CG$*JJw$;$A$A$C C jy$(0@0@@22Ry$#8 3 D ?CD$$r'!D6%:;C	C ++c&+Q	r9   c                 L    i | j                   dt        | j                        iS )Nstate)r3   r   r2   r6   s    r7   r$   zJSF.contextR  s#    H$##HWht.GHHr9   Fuse_defaultsuse_examplesnr   r   c                    i | j                   ||d}||dk(  r| j                  j                  |      S t        |      D cg c]  }| j                  j                  |        c}S c c}w )aE  Generates a fake object from the provided schema, and returns the
        output.

        Args:
            n (int, optional): If n is provided, it returns a list of n objects. If n is 1 then it returns a single object.
            use_defaults (bool, optional): prefer the default value as defined in the schema over a randomly generated object. Defaults to False.
            use_examples (bool, optional): prefer an example as defined in the schema over a randomly generated object. This parameter is preceded by the `use_defaults` parameter if set. Defaults to False.
        r   r+   r$   )r$   r4   generaterange)r6   r   r   r   r$   rS   s         r7   r   zJSF.generateV  sh     _T\\^<Q]^9Q99%%g%66=B1XF		""7"3FFFs   #A*c                 T    | j                   j                  | j                        d   S )zeGenerates a fake object from the provided schema and provides the
        output as a Pydantic model.r   r   )r4   rT   r$   r   s    r7   pydanticzJSF.pydanticf  s"     yyt||4Q77r9   c                 ~    | j                   j                  | j                        }t        || j                         y)z_Generates a fake object from the provided schema and performs
        validation on the result.r   )instancer#   N)r4   r   r$   r   r0   )r6   fakes     r7   generate_and_validatezJSF.generate_and_validatek  s/     yy!!$,,!7$t'7'78r9   c                     t        |d      5 }t        j                  | j                         |fi | ddd       y# 1 sw Y   yxY w)z`Generates a fake object from the provided schema and saves the
        output to the given path.wN)r   r=   dumpr   )r6   r:   kwargsr?   s       r7   to_jsonzJSF.to_jsonq  s=     $_ 	4IIdmmoq3F3	4 	4 	4s	   &<Ar   )r;   N)/__name__
__module____qualname____doc__r   r   r    r   r   r   r   r
   r   r	   r   r   r8   staticmethodr   r@   r   r   r   r   r   r   r   r   r   r   boolrE   r   r   r   r   r   r   r   rR   r5   propertyr$   r   r   r   r   rt   r9   r7   r   r   "   s    #3 $)- N	#
 )9(<9<#%&S#X& c3h& CH~& '##6& !&P  #3 $)- N	#
 )9(<9<#%c3h CH~ '##6 ! 
 :
c 
 
d38n 
Q_ 
 X\",0cNBJ8BT	> X\",0cNBJ8BT	& X\",0cNBJ8BT	,
 $sCx. 
 U39=M 
  X\		"	,0cN	BJ8BT			 X\",0cNBJ8BT	 X\		"	,0cN	BJ8BT			!S !C !( !  X\FDFD"FD,0cNFDBJ8BTFD	FDP	RT#s(^ 	R 	R I I "&G?D[`G#G8<GTXG	G 8
94D 4t 4r9   r   ),r=   loggingr    collectionsr   copyr   r   	itertoolsr   pathlibr   typesr   typingr	   r
   r   r   r   r   r   r   
jsonschemar   r   r   
smart_openr   r   jsf.schema_typesr   r   r   r   r   r   r   r   r   r   	getLoggerloggerr   rt   r9   r7   <module>r      sf             " : :    %   
			S4 S4r9   