
    ԟfA                        d dl mZ d dlZd dlZd dlmZ d dlmZmZm	Z	 ddl
mZmZ dZdd	 	 	 	 	 dd	Zdd
d	 	 	 	 	 	 	 ddZddZd ZddZddZddZ G d d      Zy)    )annotationsN)Iterable)AnyOptionalCallable   )inspectto_builtins)schemaschema_componentsschema_hookc               :    t        | f|      \  \  }}|r||d<   |S )a  Generate a JSON Schema for a given type.

    Any schemas for (potentially) shared components are extracted and stored in
    a top-level ``"$defs"`` field.

    If you want to generate schemas for multiple types, or to have more control
    over the generated schema you may want to use ``schema_components`` instead.

    Parameters
    ----------
    type : type
        The type to generate the schema for.
    schema_hook : callable, optional
        An optional callback to use for generating JSON schemas of custom
        types. Will be called with the custom type, and should return a dict
        representation of the JSON schema for that type.

    Returns
    -------
    schema : dict
        The generated JSON Schema.

    See Also
    --------
    schema_components
    r   z$defs)r   )typer   out
componentss       O/var/www/cvtools/html/venv/lib/python3.12/site-packages/msgspec/_json_schema.pyr   r      s*    : +D7LFSJ!GJ    #/$defs/{name})r   ref_templatec          	       
 t        j                  |       }t        |      }t        |      }t	        |||      
t        
fd|D              }|j                         D ci c]  \  }}||   
j                  |d       }	}}||	fS c c}}w )a  Generate JSON Schemas for one or more types.

    Any schemas for (potentially) shared components are extracted and returned
    in a separate ``components`` dict.

    Parameters
    ----------
    types : Iterable[type]
        An iterable of one or more types to generate schemas for.
    schema_hook : callable, optional
        An optional callback to use for generating JSON schemas of custom
        types. Will be called with the custom type, and should return a dict
        representation of the JSON schema for that type.
    ref_template : str, optional
        A template to use when generating ``"$ref"`` fields. This template is
        formatted with the type name as ``template.format(name=name)``. This
        can be useful if you intend to store the ``components`` mapping
        somewhere other than a top-level ``"$defs"`` field. For example, you
        might use ``ref_template="#/components/{name}"`` if generating an
        OpenAPI schema.

    Returns
    -------
    schemas : tuple[dict]
        A tuple of JSON Schemas, one for each type in ``types``.
    components : dict
        A mapping of name to schema for any shared components used by
        ``schemas``.

    See Also
    --------
    schema
    c              3  @   K   | ]  }j                  |        y wN)	to_schema).0tgens     r   	<genexpr>z$schema_components.<locals>.<genexpr>_   s     9CMM!$9s   F)mimulti_type_info_collect_component_types_build_name_map_SchemaGeneratortupleitemsr   )typesr   r   
type_infoscomponent_typesname_mapschemasclsr   r   r   s             @r   r   r   0   s    N ##E*J.z:O/H
8[,
?C9j99G >M=R=R=T393s}}Q..J  Js   ! Bc                8    i fd| D ]
  } |        S )zFind all types in the type tree that are "nameable" and worthy of being
    extracted out into a shared top-level components mapping.

    Currently this looks for Struct, Dataclass, NamedTuple, TypedDict, and Enum
    types.
    c                p   t        | t        j                  t        j                  t        j                  t        j
                  f      rB| j                  vr3| | j                  <   | j                  D ]  } |j                          y y t        | t        j                        r| | j                  <   y t        | t        j                        r | j                         y t        | t        j                        r | j                         y t        | t        j                        r| j                  D ]
  } |        y t        | t        j                        r% | j                           | j"                         y t        | t        j$                        r| j&                  D ]
  } |        y y r   )
isinstancer   
StructTypeTypedDictTypeDataclassTypeNamedTupleTyper+   fieldsr   EnumTypeMetadataCollectionType	item_type	TupleType
item_typesDictTypekey_type
value_type	UnionTyper&   )r   fstcollectr   s      r   r@   z)_collect_component_types.<locals>.collectp   s<   r//1A1A2CTCTU
 uuJ&$%
155! $AAFFO$ ' 2;;' !Jquu2;;'AFFO2,,-AKK 2<<(ll 2;;'AJJALL!2<<(gg  )r    )r'   r   r@   r   s     @@r   r!   r!   g   s/     J0  
 r   c                P    t        | t              r| j                  S t        |       S r   )r.   r   __name__repr)objs    r   
_type_reprrF      s    %c403<<?d3i?r   c                    t        | d      r@| j                  j                  }dj                  d | j                  D              }| d| dS | j                  S )N
__origin__z, c              3  2   K   | ]  }t        |        y wr   )rF   )r   as     r   r   z"_get_class_name.<locals>.<genexpr>   s     =1A=s   [])hasattrrH   rC   join__args__)r+   nameargss      r   _get_class_namerR      sP    sL!~~&&yy===qa  <<r   c                   t        | d      sJ t        | j                  d| j                        }t        |dd      }|syt        j                  |      j                  d      }t        | t        j                        r|dk(  ry|S t        | t        j                  t        j                  f      r0|j                  |j                   d      r|j                  d      ry|S )	Nr+   rH   __doc__ z
zAn enumeration.())rM   getattrr+   textwrapdedentstripr.   r   r4   r2   r1   
startswithrC   endswith)r   r+   docs      r   _get_docr_      s    1e
!%%quu
-C
#y"
%C
//#

$
$V
,C!R[[!## J 
A))2+;+;<	=>>S\\N!,-#,,s2CJr   c                <  	 d 		fd}t               }i }| D ]Y  } 	t        |            }||v r-|j                  |      }|j                  |       || ||      <   ||v r|| ||      <   U|||<   [ |j	                         D ci c]  \  }}||
 c}}S c c}}w )zA mapping from nameable subcomponents to a generated name.

    The generated name is usually a normalized version of the class name. In
    the case of conflicts, the name will be expanded to also include the full
    import path.
    c                0    t        j                  dd|       S )Nz[^a-zA-Z0-9.\-_]_)resubrP   s    r   	normalizez"_build_name_map.<locals>.normalize   s    vv)355r   c                F     | j                    d| j                         S )N.)
__module____qualname__)r+   rf   s    r   fullnamez!_build_name_map.<locals>.fullname   s%    CNN+1S-=-=,>?@@r   )setrR   popaddr%   )
r(   rk   	conflictsnamesr+   rP   oldkvrf   s
            @r   r"   r"      s    6A IE 	-.5=))D/CMM$#&E(3- 9#&E(3- E$K	 #[[]+TQAqD+++s   Bc                  .    e Zd Z	 	 d	 	 	 	 	 ddZdddZy)r#   Nc                .    || _         || _        || _        y r   )r)   r   r   )selfr)   r   r   s       r   __init__z_SchemaGenerator.__init__   s     !&(r   c           	     d   i }t        |t        j                        rGt        j                  ||j                        }|j
                  }t        |t        j                        rG|rTt        |d      rH| j                  j                  |j                        x}r!| j                  j                  |      |d<   |S t        |t        j                  t        j                  f      r	 |S t        |t        j                        rd|d<   |S t        |t        j                        rd|d<   |S t        |t        j                   t        j"                  f      rt        |t        j                         rdnd|d<   |j$                  |j$                  |d
<   |j&                  |j&                  |d<   |j(                  |j(                  |d<   |j*                  |j*                  |d<   |j,                  |j,                  |d<   |S t        |t        j.                        rXd|d<   |j0                  |j0                  |d<   |j2                  |j2                  |d<   |j4                  |j4                  |d<   |S t        |t        j6                  t        j8                  t        j:                  f      rTd|d<   d|d<   |j0                  d|j0                  dz   dz  z  |d<   |j2                  d|j2                  dz   dz  z  |d<   |S t        |t        j<                        rd|d<   |j>                  du rd|d<   |S t        |t        j@                        r/d|d<   |j>                  du rd|d<   |S |j>                  du rd|d<   |S t        |t        jB                        rd|d<   d|d<   |S t        |t        jD                        rd|d<   d|d<   |S t        |t        jF                        rd|d<   d |d<   |S t        |t        jH                        rd|d<   d!|d<   |S t        |t        jJ                        rd"|d<   t        |jL                  t        j                        s| jO                  |jL                        |d#<   |j0                  |j0                  |d$<   |j2                  |j2                  |d%<   |S t        |t        jP                        rad"|d<   tS        |jT                        x|d%<   |d$<   |jT                  r1|jT                  D cg c]  }| jO                  |       c}|d&<   d|d#<   |S t        |t        jV                        rd'|d<   t        |jX                  x}t        j.                        rZi }|j2                  |j2                  |d<   |j0                  |j0                  |d<   |j4                  |j4                  |d<   |r||d(<   t        |jZ                  t        j                        s| jO                  |jZ                        |d)<   |j0                  |j0                  |d*<   |j2                  |j2                  |d+<   |S t        |t        j\                        ri }g }	d	}
|j^                  D ]  }|}t        |t        j                        r'|j
                  }t        |t        j                        r't        |t        j`                        r(|jb                  s|jd                  }
|||jf                  <   |	ji                  |        |	D cg c]  }| jO                  |       }}tS        |      dk\  r|jk                         D ci c]9  \  }}|| j                  j                  | j                  |j                           ; }}}|jm                         D cg c]  }| jO                  |       c}|
|d,d-}|r|ji                  |       ||d.<   |S |jo                  |       |S tS        |      d/k(  r:|jq                         \  }}|ji                  | jO                  |             ||d.<   |S ||d.<   |S t        |t        jr                        rtu        |jl                        |d0<   |S t        |t        jv                        rf|jy                  d1|j                  jz                         t}        |      x}r|jy                  d2|       tu        d3 |j                  D              |d0<   |S t        |t        j`                        rD|jy                  d1t        |j                               t}        |      x}r|jy                  d2|       g }g }g }|jd                  T|ji                  |jd                         |ji                  |jd                         |ji                  d0|jf                  gi       |j                  D ]  }| jO                  |j
                        }|j                  r|ji                  |j                         nl|j                  t        j                  urt        |j                  d4      |d5<   n5|j                  t        t        t        t        fv r|j                         |d5<   |ji                  |j                         |ji                  |        |jb                  rmd6}t        t        |j                              D ]  \  }}|j                  s n d"|d<   ||d&<   tS        |      |z
  |d%<   |j                  rCtS        |      |d$<   |S d'|d<   t        t        ||            |d7<   ||d8<   |j                  rd|d)<   |S t        |t        j                  t        j                  t        j                  f      rf|jy                  d1t        |j                               t}        |      x}r|jy                  d2|       g }g }g }|j                  D ]  }| jO                  |j
                        }|j                  r|ji                  |j                         n6|j                  t        j                  urt        |j                  d4      |d5<   |ji                  |j                         |ji                  |        t        |t        j                        r(d"|d<   ||d&<   tS        |      |d%<   tS        |      |d$<   |S d'|d<   t        t        ||            |d7<   ||d8<   |S t        |t        j                        rt        d9      t        |t        j                        rY| j                  r0	 t        j                  | j                  |j                        |      }|st        d:|j                  d;      |S t        d<|      c c}w c c}w c c}}w c c}w # t        $ r Y Kw xY w)=z!Converts a Type to a json-schema.r+   re   z$refnullr   booleanintegernumberNminimumexclusiveMinimummaximumexclusiveMaximum
multipleOfstring	maxLength	minLengthpatternbase64contentEncoding         Tz	date-timeformattimeFzpartial-timedatedurationuuiddecimalarrayr%   maxItemsminItemsprefixItemsobjectpropertyNamesadditionalPropertiesmaxPropertiesminProperties)propertyNamemapping)anyOfdiscriminatorr   r   enumtitledescriptionc              3  4   K   | ]  }|j                     y wr   )value)r   es     r   r   z-_SchemaGenerator.to_schema.<locals>.<genexpr>b  s     #;AGG#;s   )str_keysdefaultr   
propertiesrequiredz-json-schema doesn't support msgpack Ext typeszGenerating JSON schema for custom types requires either:
- specifying a `schema_hook`
- annotating the type with `Meta(extra_json_schema=...)`

type z is not supportedz!json-schema doesn't support type )Wr.   r   r5   _merge_jsonextra_json_schemar   rM   r)   getr+   r   r   AnyTypeRawTypeNoneTypeBoolTypeIntType	FloatTypegegtleltmultiple_ofStrType
max_length
min_lengthr   	BytesTypeByteArrayTypeMemoryViewTypeDateTimeTypetzTimeTypeDateTypeTimeDeltaTypeUUIDTypeDecimalTyper6   r7   r   r8   lenr9   r:   r;   r<   r=   r&   r/   
array_like	tag_fieldtagappendr%   valuesupdatepopitemLiteralTypesortedr4   
setdefaultrC   r_   rR   r3   r   encode_namer   	NODEFAULTr
   default_factorylistdictrl   	bytearray	enumeratereversedforbid_unknown_fieldszipr0   r1   r2   ExtType	TypeError
CustomTyper   NotImplementedError)rv   r   	check_refr   rP   ir;   property_namesstructsotherr   subtype	real_typerJ   optionsrr   rs   r   struct_schemarb   r^   r   rp   r3   fieldfield_scheman_trailing_defaultsr>   s                               r   r   z_SchemaGenerator.to_schema   sv   !#BKK(^^FA,?,?@FA BKK( E*}}((//t/!%!2!2!9!9t!9!Dva"**bjj12l k 2;;'#F6Nh g 2;;'&F6Nd c BJJ56*4Q

*CYF6Ntt$%DDy!tt-.TT)*tt$%DDy!tt-.TT)*}}('(}}|$L K 2::&%F6N||'&'ll{#||'&'ll{#yy$$%IIy!| { BLL"*:*:B<M<MNO%F6N(0F$%||'&'ALL1,<+B&C{#||'&'ALL1,<+B&C{#n m 2??+%F6Nttt|#.x f e 2;;'%F6Nttt|#)x ^ ] #1x Z Y 2;;'%F6N%F8T S 2++,%F6N)F8N M 2;;'%F6N%F8H G 2>>*%F6N(F8B A 2,,-$F6Nakk2::6"&.."=w||'%&\\z"||'%&\\z"r q 2<<($F6N69!,,6GGF:
!3||DELL(Qq):(Q}%"'wf e 2;;'%F6Najj0("**=13&&22:2E2EN;/&&22:2E2EN;/##/080@0@N9-!.<F?+allBJJ7151M-.||'*+,,'||'*+,,'@  2<<(GEI77 *#	 BKK8 )I !BKK8i7	@T@T ) 3 3I-6GIMM*LL)* 388Qt~~a(8G87|q  !(1 t((//T]]1555I/JJ 
 :A9IJAdnnQ/J6?G%T! NN=1&-F7OH E MM-0D C W"$__.
7t~~g67")w| y #*wx w 2>>*#AHH-F6Nt s 2;;'gquu~~6qk!s!!!-5##;QUU#;;F6Nj i 2==)gquu'=>qk!s!!!-5HEF{{&,Q[[)vw/0 	,#~~ejj9>>OOE$5$56]]",,6.9%--RV.WL+**tT3	.JJ.3.C.C.EL+U../l+	, ||&'#.78J.K *'zz ")v(.}%%([3F%Fz"**),VF:&f c "*v'+Cv,>'?|$%-z"**5:F12Z Y B,,b.>.>@Q@QRSgquu'=>qk!s!!!-5EFH ,#~~ejj9>>OOE$5$56]]",,6.9%--RV.WL+U../l+, !R../!(v(.}%%(]z"%([z"2 / "*v'+Cv,>'?|$%-z"* ) 2::&KLL2==)^^D,<,<QUU,CVLF  EE9$5	7   ?uEFFe )RF 9
 Kv + s*    v
v>vv4/v# #	v/.v/)Nr   )r)   dict[Any, str]r   *Optional[Callable[[type], dict[str, Any]]]r   str)T)r   mi.Typer   boolreturndict[str, Any])rC   ri   rj   rw   r   rA   r   r   r#   r#      s4     CG,	) ) @) 	)dr   r#   )r   r   r   r   r   r   )r&   zIterable[Any]r   r   r   r   r   z1tuple[tuple[dict[str, Any], ...], dict[str, Any]])r'   zIterable[mi.Type]r   dict[Any, mi.Type])r+   r   r   r   )r   r   r   r   )r(   r   r   r   )
__future__r   rc   rY   collections.abcr   typingr   r   r   rU   r	   r   r
   __all__r   r   r!   rF   rR   r_   r"   r#   rA   r   r   <module>r      s    " 	  $ * * (
) MQ 
 I  L ?C(	44 <4 	4
 74n$N@ ,<o or   