
     i!                     8   d dl m Z  d dlmZmZmZmZmZ ddlmZm	Z	m
Z
 ddlmZmZ ddlmZ ddlmZmZ ddlmZmZmZ dd	lmZmZ dd
lmZ ddlmZ  ee         dddej        ej                  Z  G d de          Z! ed          Z" G d de          Z#dS )    )copy)AnyDictOptionalTypeTypeVar   )AbstractFactoryParser
Serializer)create_schemaneed_ref)	NameStyle)create_parserget_lazy_parser)merge_schemaSchemaUnknown)create_serializerget_lazy_serializer)is_generic_concrete)COMMON_SCHEMASTF)trim_trailing_underscoreskip_internalonly_mapped
name_styleunknownc                   H    e Zd ZdZd ZdefdZdefdZdefdZdefdZ	dS )	StackedFactorystackfactoryc                 "    g | _         || _        d S Nr    )selfr"   s     a/root/.openclaw/workspace/.venvs/songid/lib/python3.11/site-packages/dataclass_factory/factory.py__init__zStackedFactory.__init__   s    
    class_c                 8    | j                             ||           S r$   )r"    _json_schema_ref_name_with_stackr%   r)   s     r&   json_schema_ref_namez#StackedFactory.json_schema_ref_name   s    |<<VTJJJr(   c                     || j         v rd S | j                             |           	 | j                            ||           | j                                          S # | j                                          w xY wr$   )r!   appendr"   _json_schema_with_stackpopr,   s     r&   json_schemazStackedFactory.json_schema    sq    TZF
&!!!	<77EEJNNDJNNs   A A6c                    || j         v rt          | j        |          S | j                             |           	 | j                            ||           | j                                          S # | j                                          w xY wr$   )r!   r   r"   r/   _parser_with_stackr1   r,   s     r&   parserzStackedFactory.parser)   s~    TZ"4<888
&!!!	<2264@@JNNDJNNs   A. .B	c                    || j         v rt          | j                  S | j                             |           	 | j                            ||           | j                                          S # | j                                          w xY wr$   )r!   r   r"   r/   _serializer_with_stackr1   r,   s     r&   
serializerzStackedFactory.serializer2   s|    TZ&t|444
&!!!	<66vtDDJNNDJNNs   A- -BN)
__name__
__module____qualname__	__slots__r'   r   r-   r2   r5   r8    r(   r&   r   r      s        $I  K4 K K K K$    T          r(   r   Tc            	          e Zd ZdZ	 	 	 	 ddee         deeeef                  dede	fd	Z
d
ee         dee         fdZd
ee         dee         fdZd
ee         dedee         fdZd
ee         fdZd
ee         defdZd
ee         dee	ef         fdZdee	ef         fdZd
ee         dedee	ef         fdZd
ee         dee         fdZd
ee         dedee         fdZded
ee         defdZdded
ee         defdZdS )Factoryz9
    Facade class for all data conversion operations
    NF/definitionsdefault_schemaschemas
debug_pathjson_schema_definitions_pathc                      | _         | _        t          j                     _        |r8 j                             fd|                                D                        i  _        i  _        | _	        dS )aG  

        :param default_schema: schema used if no specific schema is provided
        :param schemas: dictionary with specific schemas for classes
                        that can be converted. Settings from default schema
                        will be used if they are not set in more specific one
        :param debug_path: show path to broken field in parsing exceptions
                           (InvalidFieldError will be raised)
        :param json_schema_definitions_path: path to definitions of jsonschemas
                       in overall schema, used by $ref

        c                 N    i | ]!\  }}|t          |j        t                    "S r=   )r   rB   DEFAULT_SCHEMA).0type_schemar%   s      r&   
<dictcomp>z$Factory.__init__.<locals>.<dictcomp>Z   s>     ! ! !!E6 |FD,?PP! ! !r(   N)
rD   rB   r   r   rC   updateitemsjson_schemasjson_schema_namesrE   )r%   rB   rC   rD   rE   s   `    r&   r'   zFactory.__init__C   s    & %,+9+>+@+@ 	L ! ! ! !%,]]__! ! !    .024,H)))r(   r)   returnc                    t          |          r|j        }nd}| j                            |          }|sQ|r| j                            |          }|st	                      }t          || j        t                    }|| j        |<   |S )zP
        Finds or creates `Schema` describing `class_` conversion rules
        N)r   
__origin__rC   getr   r   rB   rH   )r%   r)   
base_classrK   s       r&   rK   zFactory.schemab   s     v&& 	*JJJ!!&)) 	* 6))*55 "!&$*=~NNF#)DL r(   c                 H    |                      |t          |                     S )z
        Returns preconfigure parser to create `class_` instances
        from simple data structures using previously set schemas
        )r4   r   r,   s     r&   r5   zFactory.parseru   s"    
 &&v~d/C/CDDDr(   stacked_factoryc                    |                      |          }|j        s`|j        r=t          |          }|                    ||| j                  |_        || j        |<   |}nt          ||| j        |          |_        |j        S r$   )rK   r5   
get_parserr   rD   rC   r   r%   r)   rW   rK   
new_schemas        r&   r4   zFactory._parser_with_stack|   s    V$$} 	`  `!&\\
$*$5$5fot$_$_
!'1V$# -ovtX^ _ _}r(   c                 H    |                      |t          |                     S )zv
        Create name of jsonschema reference used to locate description
        of `class_` in overall schema
        )r+   r   r,   s     r&   r-   zFactory.json_schema_ref_name   s"    
 44V^D=Q=QRRRr(   c           	         |                      |          }|j        _|j        | j        vr|j        S | j        |j                 |k    r-t          d|j         d| j        |j                  d|           |j        S t	          |dd          pt	          |dd          pt          |          }|| j        v r$t          d| d| j        |          d| d          |                    |           |S )NzAlready found type with name `z`: z#. Please, specify another name for r;    r9   z! in schema or rename class itself)rK   namerP   
ValueErrorgetattrstrr2   )r%   r)   rW   rK   r_   s        r&   r+   z(Factory._json_schema_ref_name_with_stack   sU   V$$;"{$"888{"%fk2f<<  "N&+ "N "N$($:6;$G"N "NEK"N "N O O O ;v~r22dgfjRT6U6UdY\]cYdYd4))) Ad A A $ 6t <A AAGA A A B B B 	##F+++r(   c                 H    |                      |t          |                     S )zi
        Create json schema describing `class_`.
        Can contain references to other classes
        )r0   r   r,   s     r&   r2   zFactory.json_schema   s"    
 ++FN44H4HIIIr(   c                 H    d | j                                         D             S )z
        Create mapping for all crated json schemas

        Note: it is filled only with schemas which are requested at least
        once and their references
        c                     i | ]\  }}||	S r=   r=   )rI   kvs      r&   rL   z3Factory.json_schema_definitions.<locals>.<dictcomp>   s.     
 
 
1 q
 
 
r(   )rO   rN   )r%   s    r&   json_schema_definitionszFactory.json_schema_definitions   s2    
 
)//11
 
 
 	
r(   c                     |                      |          }|                     ||          }|| j        v r| j        |         S t          |||| j                  }t          |          r
|| j        |<   |S r$   )rK   r+   rO   r   rE   r   )r%   r)   rW   rK   r_   r2   s         r&   r0   zFactory._json_schema_with_stack   s    V$$44V_MM4$$$$T**#VVT-N
 
 F 	2&1Dd#r(   c                 H    |                      |t          |                     S )z
        Returns preconfigured serializer to convert `class_` instances
        to simple data structures using previously set schemas
        )r7   r   r,   s     r&   r8   zFactory.serializer   s"    
 **6>$3G3GHHHr(   c                    |                      |          }|j        s`|j        r=t          |          }|                    ||| j                  |_        || j        |<   |}nt          ||| j        |          |_        |j        S r$   )rK   r8   get_serializerr   rD   rC   r   rZ   s        r&   r7   zFactory._serializer_with_stack   s    V$$  	h$ h!&\\
(.(=(=foW[Wf(g(g
%'1V$#$5ovt`f$g$g!  r(   datac                 >     |                      |          |          S )z6
        Create `class_` instance form `data`
        )r5   r%   rm   r)   s      r&   loadzFactory.load   s      #t{{6""4(((r(   c                 `    |t          |          } |                     |          |          S )zx
        Convert `data` to plain structures.
        If `class_` is not provided then `type(data)` will be used
        )typer8   ro   s      r&   dumpzFactory.dump   s0    
 >$ZZF&tv&&t,,,r(   )NNFrA   r$   )r9   r:   r;   __doc__r   r   r   r   boolrb   r'   r>   rK   r   r5   r   r4   r-   r+   r   r2   rh   r0   r   r8   r7   rp   rs   r=   r(   r&   r@   r@   ?   s        
 ,004 ,:I I (I $tV|,-I 	I
 '*I I I I>T!W     &ET!W E E E E Ea > V\]^V_    S47 S S S StAw Q_    &J$q' Jd38n J J J J
c3h 
 
 
 
d1g  [_`ceh`h[i    Ia IZ] I I I I!T!W !~ !ZdefZg ! ! ! !) )d1g )! ) ) ) )- - -DG -s - - - - - -r(   r@   N)$r   typingr   r   r   r   r   commonr
   r   r   
jsonschemar   r   namingr   parsersr   r   rK   r   r   r   serializersr   r   type_detectionr   schema_helpersr   ignoreSKIPrH   r   r>   r@   r=   r(   r&   <module>r      s         5 5 5 5 5 5 5 5 5 5 5 5 5 5 7 7 7 7 7 7 7 7 7 7 / / / / / / / /       3 3 3 3 3 3 3 3 1 1 1 1 1 1 1 1 1 1 ? ? ? ? ? ? ? ? / / / / / / * * * * * *!L  # # # # #_ # # #L GCLLh- h- h- h- h-o h- h- h- h- h-r(   