
    9 f{2                         d Z ddlZddlmZmZmZ  ej        dg d          Z G d d          Zd	 Z	dd
Z
 G d d          Z G d d          Z G d d          ZdS )zBuild factory instances.    N   )enumserrorsutilsDeclarationWithContextnamedeclarationcontextc                       e Zd ZdZddZed             Ze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S )DeclarationSeta  A set of declarations, including the recursive parameters.

    Attributes:
        declarations (dict(name => declaration)): the top-level declarations
        contexts (dict(name => dict(subfield => value))): the nested parameters related
            to a given top-level declaration

    This object behaves similarly to a dict mapping a top-level declaration name to a
    DeclarationWithContext, containing field name, declaration object and extra context.
    Nc                 ~    i | _         t          j        t                    | _        |                     |pi            d S N)declarationscollectionsdefaultdictdictcontextsupdate)selfinitials     E/var/www/equiseq/venv/lib/python3.11/site-packages/factory/builder.py__init__zDeclarationSet.__init__   s8    #/55GMr"""""    c                 f    t           j        |v r |                    t           j        d          S |dfS )a)  Split a declaration name into a (declaration, subpath) tuple.

        Examples:
        >>> DeclarationSet.split('foo__bar')
        ('foo', 'bar')
        >>> DeclarationSet.split('foo')
        ('foo', None)
        >>> DeclarationSet.split('foo__bar__baz')
        ('foo', 'bar__baz')
        r   N)r   SPLITTERsplit)clsentrys     r   r   zDeclarationSet.split   s1     >U"";;u~q1114= r   c                 L    ||S t           j                            ||f          S )zyRebuild a full declaration name from its components.

        for every string x, we have `join(split(x)) == x`.
        )r   r   join)r   rootsubkeys      r   r!   zDeclarationSet.join/   s(     >K~""D&>222r   c                 P    |                      |                                           S r   )	__class__as_dictr   s    r   copyzDeclarationSet.copy9   s    ~~dllnn---r   c                 n    |                                 D ]:\  }}                     |          \  }}|| j        |<   *| j        |         |<   ;t	           j                  t	           j                  z
  }|r9t          j        d fd|D             dt           j                  d          dS )zAdd new declarations to this set/

        Args:
            values (dict(name, declaration)): the declarations to ingest.
        Nz*Received deep context for unknown fields: c                     i | ]>}j         |                                         D ]\  }}                    ||          |?S  )r   itemsr!   ).0r"   subvr   s       r   
<dictcomp>z)DeclarationSet.update.<locals>.<dictcomp>M   sg        &*mD&9&?&?&A&A  #C 		$,,a   r   z (known=))r,   r   r   r   setr   InvalidDeclarationErrorsorted)r   valueskr/   r"   r.   extra_context_keyss   `      r   r   zDeclarationSet.update<   s     LLNN 	- 	-DAq

1ID#{*+!$''+,d#C(( //#d6G2H2HH 
	00   $6    
 4,----	 	 	
	 
	r   c                        fd|D             S )zFilter a set of declarations: keep only those related to this object.

        This will keep:
        - Declarations that 'override' the current ones
        - Declarations that are parameters to current ones
        c                 X    g | ]&}                     |          d          j        v $|'S )r   )r   r   )r-   r   r   s     r   
<listcomp>z)DeclarationSet.filter.<locals>.<listcomp>]   sB     
 
 
zz%  #t'888 888r   r+   )r   entriess   ` r   filterzDeclarationSet.filterV   s.    
 
 
 
&
 
 
 	
r   c                 @     t          j         j         fd          S )Nc                     j         |          S r   r   )r   r   s    r   <lambda>z'DeclarationSet.sorted.<locals>.<lambda>e   s    !25!9 r   )getter)r   sort_ordered_objectsr   r'   s   `r   r4   zDeclarationSet.sortedb   s/    )9999
 
 
 	
r   c                     || j         v S r   r?   r   keys     r   __contains__zDeclarationSet.__contains__h   s    d'''r   c                 R    t          || j        |         | j        |                   S )Nr   )r   r   r   rD   s     r   __getitem__zDeclarationSet.__getitem__k   s1    %)#.M#&
 
 
 	
r   c                 *    t          | j                  S r   )iterr   r'   s    r   __iter__zDeclarationSet.__iter__r   s    D%&&&r   c              #   (   K   | D ]}| |         V  dS )z6Retrieve the list of declarations, with their context.Nr+   )r   r	   s     r   r5   zDeclarationSet.valuesu   s2       	 	Dt*	 	r   c              #      K   | j         D ]R}|| j         |         fV  | j        |                                         D ]\  }}|                     ||          |fV   SdS )z@Extract a list of (key, value) pairs, suitable for our __init__.N)r   r   r,   r!   )r   r	   r#   values       r   _itemszDeclarationSet._itemsz   s      % 	5 	5D)$/////!%t!4!:!:!<!< 5 5iif--u444445	5 	5r   c                 D    t          |                                           S )z*Return a dict() suitable for our __init__.)r   rO   r'   s    r   r&   zDeclarationSet.as_dict   s    DKKMM"""r   c                 0    d|                                  z  S )Nz<DeclarationSet: %r>)r&   r'   s    r   __repr__zDeclarationSet.__repr__   s    %66r   r   )__name__
__module____qualname____doc__r   classmethodr   r!   r(   r   r<   r4   rF   rH   rK   r5   rO   r&   rR   r+   r   r   r   r      s       	 	# # # #
 ! ! [!  3 3 [3. . .  4

 

 


 
 
( ( (
 
 
' ' '  
5 5 5# # #7 7 7 7 7r   r   c                 p    | j         }t          j        |          t          j        j        k    r|j        S dS )NF)r
   r   get_builder_phaseBuilderPhaseATTRIBUTE_RESOLUTIONCAPTURE_OVERRIDES)declaration_with_contextr
   s     r   _captures_overridesr^      s4    *6K{++u/A/VVV,,ur   c           
      4   |r|                                 nt                      }|r|                                 nt                      }i }i }|                                 D ]~\  }}t          j        |          t          j        j        k    r-||v r#t          j        d|d|d||                   |||<   Y||v r|	                    |d          }	|||	<   y|||<   |
                    |           i }
i }|                    |          }|                                D ]I\  }}||v r|||<   ||v r1t          ||                   r|	                    |d          }	||
|	<   D||
|<   J|
                    |
           |
                    |           ||fS )NzPostGenerationDeclaration =z shadows declaration  )r(   r   r,   r   rY   rZ   POST_INSTANTIATIONr   r3   r!   r   r<   r^   )declsbase_pre	base_postpre_declarationspost_declarations
extra_postextra_maybenonpostr6   r/   	magic_keyextra_pre_declarationsextra_post_declarationspost_overridess                r   parse_declarationsrn      s   *2Hx}}8H8H,5K	(((>;K;K J & &1"1%%);)NNN$$$ 44qq!!!-a002   JqMM### *..q"55I$%Jy!!$%q!! Z(((   &--.@AAN"((** * *1)*#A&&"""':;KA;N'O'O" )--a44I01"9-- )*"1%%23334555...r   c                   @    e Zd ZddZd Zed             ZddZd ZdS )	BuildStepNc                 L    || _         || _        i | _        || _        d | _        d S r   )buildersequence
attributesparent_stepstub)r   rr   rs   ru   s       r   r   zBuildStep.__init__   s*     &			r   c                     t          || | j                  | _        |D ]}t          | j        |          | j        |<    d S )N)r   steprs   )Resolverrs   rv   getattrrt   )r   r   
field_names      r   resolvezBuildStep.resolve   s]    %]
 
 
	 ' 	I 	IJ*1$)Z*H*HDOJ''	I 	Ir   c                 D    | j         r| j         j        }nd}| j        f|z   S )Nr+   )ru   chainrv   )r   parent_chains     r   r~   zBuildStep.chain   s.     	+1LLL	|l**r   c                     ddl m} t          ||j                  st	          j        | d|          | j                            |j        |          }|	                    | |          S )Nr   )basez4: Attempting to recursing into a non-factory object )ru   force_sequence)
ra   r   
issubclassBaseFactoryr   AssociatedClassErrorrr   recurse_metabuild)r   factoryr   r   r   rr   s         r   r   zBuildStep.recurse   s    '4#344 	#-44"# # # ,&&w}lCC}}n}MMMr   c                     d| j         dS )Nz<BuildStep for >)rr   r'   s    r   rR   zBuildStep.__repr__   s    22222r   r   )	rS   rT   rU   r   r|   propertyr~   r   rR   r+   r   r   rp   rp      s|           I I I + + X+N N N N3 3 3 3 3r   rp   c                   ,    e Zd ZdZd ZddZd Zd ZdS )StepBuilderzA factory instantiation step.

    Attributes:
    - parent: the parent StepBuilder, or None for the root step
    - extras: the passed-in kwargs for this branch
    - factory: the factory class being built
    - strategy: the strategy to use
    c                 f    || _         || _        || _        |                    dd           | _        d S )N
__sequence)factory_metastrategyextraspopforce_init_sequence)r   r   r   r   s       r   r   zStepBuilder.__init__   s4    ( #)::lD#A#A   r   Nc                 N   t          | j        | j        j        | j        j                  \  }}||}n(| j        | j        }n| j                                        }t          | ||          }|                    |           | j        	                    |j
                  \  }}| j                            |||          }	i }
|                                D ]/}||         }|j                            |	||j                  |
|<   0| j                            |	||
           |	S )zBuild a factory instance.)rd   re   N)rr   rs   ru   )rx   argskwargsinstancerx   	overrides)r   rx   results)rn   r   r   rf   rg   r   next_sequencerp   r|   prepare_argumentsrt   instantiater4   r
   evaluate_postr   use_postgeneration_results)r   ru   r   prepostrs   rx   r   r   r   postgen_resultsdeclaration_namer
   s                r   r   zStepBuilder.build   sk    'K&7'9
 
 
	T %%HH%1/HH(6688H#
 
 

 	S(::4?KKf$00 1 
 
  $ 	 	/0K0;0G0U0U!%- 1V 1 1O,--
 	44# 	5 	
 	
 	

 r   c                 <    |                      ||| j                  S )z Recurse into a sub-factory call.)r   )r%   r   )r   r   r   s      r   r   zStepBuilder.recurse'  s    ~~lFT]~KKKr   c                 (    d| j         d| j        dS )Nz<StepBuilder(z, strategy=z)>)r   r   r'   s    r   rR   zStepBuilder.__repr__+  s     Rt0RRt}RRRRr   NN)rS   rT   rU   rV   r   r   r   rR   r+   r   r   r   r      sk         B B B, , , ,\L L LS S S S Sr   r   c                   N     e Zd ZdZdZd Zed             Zd Zd Z	 fdZ
 xZS )ry   a  Resolve a set of declarations.

    Attributes are set at instantiation time, values are computed lazily.

    Attributes:
        __initialized (bool): whether this object's __init__ as run. If set,
            setting any attribute will be prevented.
        __declarations (dict): maps attribute name to their declaration
        __values (dict): maps attribute name to computed value
        __pending (str list): names of the attributes whose value is being
            computed. This allows to detect cyclic lazy attribute definition.
        __step (BuildStep): the BuildStep related to this resolver.
            This allows to have the value of a field depend on the value of
            another field
    Fc                 L    || _         || _        i | _        g | _        d| _        d S )NT)_Resolver__declarations_Resolver__step_Resolver__values_Resolver__pending_Resolver__initialized)r   r   rx   rs   s       r   r   zResolver.__init__B  s-    *!r   c                 @    | j         j        r| j         j        j        nd S r   )r   ru   rv   r'   s    r   factory_parentzResolver.factory_parentK  s    /3{/FPt{&++DPr   c                     d| j         z  S )Nz<Resolver for %r>)r   r'   s    r   rR   zResolver.__repr__O  s    "T[00r   c           	      `   || j         v r t          j        d|d| j         d          || j        v r| j        |         S || j        v r| j        |         }|j        }t          j        |          t          j        j	        k    r}| j         
                    |           	 |                    | | j        |j                  }| j                                         }n# | j                                         }w xY w||k    sJ || j        |<   |S t          d|d| j        d| j        d          )zRetrieve an attribute's value.

        This will compute it if needed, unless it is already on the list of
        attributes being computed.
        z%Cyclic lazy attribute definition for z; cycle found in .r   zThe parameter z& is unknown. Evaluated attributes are z, definitions are )r   r   CyclicDefinitionErrorr   r   r
   r   rY   rZ   r[   appendevaluate_prer   r   r   AttributeError)r   r	   r
   rN   lasts        r   __getattr__zResolver.__getattr__R  sf    4>!!..t~~~'( ( ( T]""=&&T(((-d3K+E&u--1C1XXX%%d+++0!..!%!["-"5 /  E  >--//DD4>--//D////t||||"'DM$L .)-t}}}d>Q>Q>QST T Ts   "C C6c                 t    | j         s"t                                          ||          S t          d          )z1Prevent setting attributes once __init__ is done.z+Setting of object attributes is not allowed)r   super__setattr__r   )r   r	   rN   r%   s      r   r   zResolver.__setattr__t  s8    ! 	P77&&tU333 !NOOOr   )rS   rT   rU   rV   r   r   r   r   rR   r   r   __classcell__)r%   s   @r   ry   ry   /  s           M" " " Q Q XQ1 1 1 T  T  TDP P P P P P P P Pr   ry   r   )rV   r   ra   r   r   r   
namedtupler   r   r^   rn   rp   r   ry   r+   r   r   <module>r      sS         " " " " " " " " " "//&&&  y7 y7 y7 y7 y7 y7 y7 y7x  // // // //d$3 $3 $3 $3 $3 $3 $3 $3NBS BS BS BS BS BS BS BSJJP JP JP JP JP JP JP JP JP JPr   