o
    [h]}                     @   s  d 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 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 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 a a!dd Z"dd Z#dd Z$d2dd Z%d!d" Z&d#d$ Z'G d%d& d&e(Z)G d'd( d(e)Z*d)d* Z+d+d, Z,d-d. Z-d/e-_.d0d1 Z/dS )3z(Internal implementation for declarative.    N)instrumentation   )clsregistry   )event)exc)util)class_mapper)mapper)	mapperlib)synonym)QueryableAttribute)_is_mapped_class)InspectionAttr)MapperProperty)ColumnProperty)CompositeProperty)Column)Table)
expression)topologicalc                 C   s,   t | r
t | S t| rt| ddS d S )NF)	configure)_DeferredMapperConfighas_clsconfig_for_clsr   r	   cls r   /home/ubuntu/experiments/live_experiments/Pythonexperiments/Otree/venv/lib/python3.10/site-packages/sqlalchemy/ext/declarative/base.py_declared_mapping_info%   s
   

r   c                 C   sZ   | t u rd S t| dddr!| jD ]}t|}|d ur|  S qd S t| }|d ur+|S | S )N__abstract__Tstrict)object_get_immediate_cls_attr	__bases__"_resolve_for_abstract_or_classical"_dive_for_classically_mapped_class)r   supZ	classicalr   r   r   r&   0   s   
r&   c                 C   sL   t | drd S t| }|d ur| S | jD ]}t|}|d ur#|  S qd S )N_decl_class_registry)hasattrr   Zmanager_of_classr%   r'   )r   managerr(   r
   r   r   r   r'   C   s   


r'   Fc                 C   st   t | tsdS | jD ]-}t|d}| ot|du}||jv r7|| u s0|r+|| jv r7n	 |s7|s7t||  S q
dS )a  return an attribute of the class that is either present directly
    on the class, e.g. not on a superclass, or is from a superclass but
    this superclass is a non-mapped mixin, that is, not a descendant of
    the declarative base and is also not classically mapped.

    This is used to detect attributes that indicate something about
    a mapped class independently from any mapped classes that it may
    inherit from.

    Nr)   )
issubclassr#   __mro__r*   r'   __dict__r%   getattr)r   attrnamer"   baseZ_is_declarative_inheritsZ_is_classicial_inheritsr   r   r   r$   W   s&   




r$   c                 C   sB   t d u rddlm a  t tjfat| dddrd S t| || d S )Nr   )declared_attrr    Tr!   )r2   apir   classpropertydeclarative_propsr$   _MapperConfigsetup_mapping)r   	classnamedict_r   r   r   _as_declarativey   s   
r:   c                 C   s0   t | trt| ddrtd||f  dS dS )N
_cascadingFz~@declared_attr.cascading is not supported on the %s attribute on class %s.  This attribute invokes for subclasses in any case.T)
isinstancer5   r/   r   warn)objnamer   r   r   r   _check_declared_props_nocascade   s   
r@   c                   @   sx   e Zd Zedd Zdd Zdd Zdd Zd	d
 Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd ZdS )r6   c                 C   s6   t |dddpt|d}|rt}nt}|||| d S )N_sa_decl_prepare_nocascadeTr!   Z_sa_decl_prepare)r$   r*   r   r6   )r   cls_r8   r9   Z	defer_mapZcfg_clsr   r   r   r7      s   z_MapperConfig.setup_mappingc                 C   s   || _ t|| _|| _d | _t | _t | _	i | _
|   i | j _|   tj  z$t| j| j  |   |   |   |   |   W tj  d S tj  w N)r   dictr9   r8   persist_selectabler   OrderedDict
propertiessetdeclared_columnscolumn_copies_setup_declared_events_sa_declared_attr_reg_scan_attributesr   Z_CONFIGURE_MUTEXacquirer   Z	add_class_extract_mappable_attributes_extract_declared_columns_setup_table_setup_inheritance_early_mappingrelease)selfrB   r8   r9   r   r   r   __init__   s&   



z_MapperConfig.__init__c                 C   s   |    d S rC   )maprU   r   r   r   rS      s   z_MapperConfig._early_mappingc                    sP   t  jdrttd fdd}t  jdr&ttd fdd}d S d S )	N__declare_last__after_configuredc                          j   d S rC   )r   rY   r   rX   r   r   rZ         z>_MapperConfig._setup_declared_events.<locals>.after_configured__declare_first__before_configuredc                      r[   rC   )r   r]   r   rX   r   r   r^      r\   z?_MapperConfig._setup_declared_events.<locals>.before_configured)r$   r   r   Zlistens_forr
   )rU   rZ   r^   r   rX   r   rK      s   

z$_MapperConfig._setup_declared_eventsc              	      s  | j  | j}| j}d }d  }}d } jD ]}| uo(t|d uo(t|ddd }|s4| ur4| | t| D ]\}	}
|	dkrUt	|
|	 }|sT|rN|rT fdd}q:|	dkrit	|
|	 }|sh|re|rh j
}q:|	dkrt	|
|	 }|s|ry|r j}t|tttd fstd	| urd}q:|rt|
trtd
|j|	| f  q:| ur0t|
trq:t|
trtdt|
tr)t|
tj}|s|
jr|	|v rtd|	 f  |
|
  ||	<  ||
< }t |	| n(|rtd t |	}t|tr|j rt|j!ts|j"}| ||	< ||
< t|ttfr(|j#d u r(|
j$|_#q:| %||	|
 q:q|r:|s:d }|| _&|| _'|| _(d S )NrA   Tr!   __mapper_args__c                      s
   t  jS rC   )rD   r_   r   r   r   r   mapper_args_fn   s   
z6_MapperConfig._scan_attributes.<locals>.mapper_args_fn__tablename____table_args__z3__table_args__ value must be a tuple, dict, or NonezRegular (i.e. not __special__) attribute '%s.%s' uses @declared_attr, but owning class %s is mapped - not applying to subclass %s.zMapper properties (i.e. deferred,column_property(), relationship(), etc.) must be declared as @declared_attr callables on declarative mixin classes.zXAttribute '%s' on class %s cannot be processed due to @declared_attr.cascading; skippingzJUse of sqlalchemy.util.classproperty on declarative classes is deprecated.))r   r9   rJ   r-   r   r$   _produce_column_copiesvarsitemsr@   ra   rb   r<   tuplerD   typer   ArgumentErrorr5   r   r=   __name__r   r   InvalidRequestErrorr4   r;   __get__setattrZwarn_deprecatedr/   r   Z_is_internal_proxyZoriginal_propertyZ
descriptordoc__doc___warn_for_decl_attributes
table_args	tablenamer`   )rU   r9   rJ   r`   rp   Zinherited_table_argsrq   r1   Zclass_mappedr?   r>   Z
check_declZoldclasspropretr   r   r   rM      s   








r
z_MapperConfig._scan_attributesc                 C   s&   t |tjrtd||f  d S d S )NzAttribute '%s' on class %s appears to be a non-schema 'sqlalchemy.sql.column()' object; this won't be part of the declarative mapping)r<   r   ZColumnClauser   r=   )rU   r   keycr   r   r   ro   c  s   z'_MapperConfig._warn_for_decl_attributesc                 C   s   | j }| j}| j}t| D ]A\}}t|trPt|||ur q|jr(t	
d||vrPd|v r:|jp4||d jv sP|  ||< }|j|_t||| |||< qd S )NzvColumns with foreign keys to other columns must be declared as @declared_attr callables on declarative mixin classes. 	__table__)r   r9   rJ   rd   re   r<   r   r/   Zforeign_keysr   rj   r?   rt   copy_creation_orderrl   )rU   r1   r   r9   rJ   r?   r>   Zcopy_r   r   r   rc   l  s(   
z$_MapperConfig._produce_column_copiesc                 C   sN  | j }| j}| j}t|ddd}t|D ]}|dv rq|| }t|tr:t|tr4|jr4t	
d| j   t||}nt|trT|j|urT|j|krTt|j}t||| t|trpt|dkrpt|d ttfrpt	
d|  qt|ttfs|d	s|| | ||| |st||| q|d
krtdt||}|||< qd S )NrA   Tr!   )ru   ra   r_   zUse of @declared_attr.cascading only applies to Declarative 'mixin' and 'abstract' classes.  Currently, this flag is ignored on mapped class %sr   r   zIgnoring declarative-like tuple value of attribute '%s': possibly a copy-and-paste error with a comma accidentally placed at the end of the line?__metadatazdAttribute name 'metadata' is reserved for the MetaData instance when using a declarative base class.)r   r9   rG   r$   listr<   r5   r2   r;   r   r=   r/   r   class_rs   r   rl   rf   lenr   r   
startswithpopro   r   rj   r   _deferred_relationship)rU   r   r9   	our_stuffZlate_mappedkvaluepropr   r   r   rO     sb   






z*_MapperConfig._extract_mappable_attributesc              
      s  | j   j fddd | j}tt}t  D ]R\}}t|t	t
frN|jD ]#}t|trL|jd u rLt|| t|t
sG||j | || q)qt|trmt|| ||j | || ||jkrm |= q| D ]\}}t|dkrtd| j|dt|f  qrd S )Nc                    s
    |  j S rC   rw   rs   r   r   r   <lambda>  s   
 z9_MapperConfig._extract_declared_columns.<locals>.<lambda>r   r   zzOn class %r, Column object %r named directly multiple times, only one will be used: %s. Consider using orm.synonym insteadz, )rG   sortrI   collectionsdefaultdictrH   rz   re   r<   r   r   columnsr   table_undefer_column_namer?   addrs   r|   r   r=   r8   joinsorted)rU   rI   Zname_to_prop_keyrs   rt   colr?   keysr   r   r   rP     s<   








z'_MapperConfig._extract_declared_columnsc                 C   s4  | j }| j}| j}| j}| j}t|dd d }| _d }t|dr(t|j	}nt
}d|vr}|d ur|di }}	|r[t|trA|}	nt|tr[t|d trY|dd |d }}	n|}|d	}
|
rfd
|	d< |||jgt|t| R i |	 |_}n|j}|r|D ]}|j|std|j q|| _d S )Nc                 S   s   | j S rC   r   )rt   r   r   r   r     s    z,_MapperConfig._setup_table.<locals>.<lambda>r   __table_cls__ru   r   r   Z__autoload__Tautoloadz8Can't add additional column %r when specifying __table__)r   rq   rp   r9   rI   r   r*   r   unbound_method_to_callabler   r   r<   rD   rf   getry   ru   rt   Zcontains_columnr   rh   rs   local_table)rU   r   rq   rp   r9   rI   r   Z	table_clsargsZtable_kwr   rt   r   r   r   rQ     sZ   





z_MapperConfig._setup_tablec           
      C   sr  | j }| j}| j}| j}g }|jD ]}t|}|d u rqt|d ur.t|ddds.|| q|rFt	|dkr@t
d||f |d | _nd | _|d u r^| jd u r^t|ds^t
d| | jrt| j}|j }|j}	|d u r|rwt
d	|D ]=}|j|jv r|j|j |u rqyt
d
|||j|j f |jrt
d|| |	d ur|	|ur|	| qyd S d S d S )NrA   Tr!   r   z&Class %s has multiple mapped bases: %rr   Z__no_table__zwClass %r does not have a __table__ or __tablename__ specified and does not inherit from an existing table-mapped class.z?Can't place __table_args__ on an inherited class with no table.z;Column '%s' on class %s conflicts with existing column '%s'zDCan't place primary key columns on an inherited class with no table.)r   r   rp   rI   r%   r&   r   r$   appendr|   r   rj   inheritsrE   rh   r?   rt   Zprimary_keyappend_columnZ_refresh_for_new_column)
rU   r   r   rp   rI   r   rt   inherited_mapperinherited_tableZinherited_persist_selectabler   r   r   rR   '  s   






	z _MapperConfig._setup_inheritancec           
         s>  | j }| jr|  }ni }dD ]}||v r"|| }| j||||< qd|vs+J d| jr3| j|d< | jr|ddst| j  j}d|vrit fdd|jD 	 j
pXd	 |d< }|d
d | jD  t| D ]"\}}t|tjszqo| jv r j| }t|tr|g|j ||< qo| }	||	d< |	| _d S )N)Zversion_id_colZpolymorphic_onr   z=Can't specify 'inherits' explicitly with declarative mappingsZconcreteFexclude_propertiesc                    s   g | ]
}| j vr|jqS r   )Z_columntopropertyrs   .0rt   r   r   r   
<listcomp>  s
    
z;_MapperConfig._prepare_mapper_arguments.<locals>.<listcomp>r   c                 S   s   g | ]}|j qS r   r   r   r   r   r   r     s    rG   )rG   r`   rJ   r   r   r   r   rH   rt   unionr   difference_updaterI   rz   re   r<   r   ZColumnElementZ_propsr   r   rv   mapper_args)
rU   rG   r   r   vr   r   r   pZresult_mapper_argsr   r   r   _prepare_mapper_argumentsr  sN   










z'_MapperConfig._prepare_mapper_argumentsc                 C   sR   |    t| jdrt| jj}nt}|| j| jfi | j | j_	}| j`
|S )N__mapper_cls__)r   r*   r   r   r   r   r
   r   r   
__mapper__rL   )rU   Z
mapper_clsZmp_r   r   r   rW     s   z_MapperConfig.mapN)ri   
__module____qualname__classmethodr7   rV   rS   rK   rM   ro   rc   rO   rP   rQ   rR   r   rW   r   r   r   r   r6      s     
$ 	F'3K<r6   c                       s   e Zd Ze Zdd Zedd Zej	dd Ze
dd Ze
dd	 Ze
d
d Ze
dd Ze
dddZ fddZ  ZS )r   c                 C   s   d S rC   r   rX   r   r   r   rS     s   z$_DeferredMapperConfig._early_mappingc                 C   s   |   S rC   )_clsrX   r   r   r   r     s   z_DeferredMapperConfig.clsc                 C   s    t || j| _| | j| j< d S rC   )weakrefref_remove_config_clsr   _configs)rU   r{   r   r   r   r     s   c                 C   s   | j |d  d S rC   )r   r~   )r   r   r   r   r   r     s   z(_DeferredMapperConfig._remove_config_clsc                 C   s   t |tot|| jv S rC   )r<   rg   r   r   r   r   r{   r   r   r   r     s   z_DeferredMapperConfig.has_clsc                 C   s*   t |dr	|  tj|dt| d)N_sa_raise_deferred_configzOClass %s has a deferred mapping on it.  It is not yet usable as a mapped class.)msg)r*   r   orm_excZUnmappedClassErrorZ_safe_cls_namer   r   r   r   raise_unmapped_for_cls  s   
z,_DeferredMapperConfig.raise_unmapped_for_clsc                 C   s   | j t| S rC   )r   r   r   r   r   r   r   r     s   z$_DeferredMapperConfig.config_for_clsTc                    sv   fdddd | j  D D }|s|S tdd |D  g } D ]| fddjD  q"tt||S )Nc                    s&   g | ]\}}|d urt | r|qS rC   )r,   )r   mrB   )base_clsr   r   r     s
    z:_DeferredMapperConfig.classes_for_base.<locals>.<listcomp>c                 S   s   g | ]}||j fqS r   r   r   r   r   r   r   r     s    c                 s   s    | ]}|j |fV  qd S rC   r   r   r   r   r   	<genexpr>  s    z9_DeferredMapperConfig.classes_for_base.<locals>.<genexpr>c                 3   s(    | ]}| v r |   fV  qd S rC   r   )r   r   )all_m_by_clsm_clsr   r   r     s    )r   valuesrD   extendr%   rz   r   r   )r   r   r   classes_for_baseZtuplesr   )r   r   r   r   r     s   

z&_DeferredMapperConfig.classes_for_basec                    s   | j | jd  tt|  S rC   )r   r~   r   superr   rW   rX   	__class__r   r   rW     s   z_DeferredMapperConfig.map)T)ri   r   r   r   rF   r   rS   propertyr   setterr   r   r   r   r   r   rW   __classcell__r   r   r   r   r     s$    






r   c                 C   s  d| j v rt|trt|| | j| | j|| dS t|trG|j	D ]}t|tr=|j
du r=t|| | j| q&| j|| dS t|trY| j|t| | dS t|tru|j|krut|j}| j|t| | dS t| || | j  dS t| || dS )zadd an attribute to an existing declarative class.

    This runs through the logic to determine MapperProperty,
    adds it to the Mapper, adds a column to the mapped Table, etc.

    r   N)r.   r<   r   r   ru   r   r   Zadd_propertyr   r   r   r   r   r   r   rs   r   rg   __setattr___expire_memoizations)r   rs   r   r   r   r   r   _add_attribute   s0   







r   c                 C   sj   d| j v r-|| j v r-| jjs-| j | }t|ttttfr tdt	
| | | j  d S t	
| | d S )Nr   z<Can't un-map individual mapped attributes on a mapped class.)r.   r   Z_dispose_calledr<   r   r   r   r   NotImplementedErrorrg   __delattr__r   )r   rs   r   r   r   r   _del_attribute%  s   


r   c                 K   sB   t | }|D ]}t||std||jf t| |||  qdS )a=  A simple constructor that allows initialization from kwargs.

    Sets attributes on the constructed instance using the names and
    values in ``kwargs``.

    Only keys that are present as
    attributes of the instance's class are allowed. These could be,
    for example, any mapped columns or relationships.
    z(%r is an invalid keyword argument for %sN)rg   r*   	TypeErrorri   rl   )rU   kwargsrB   r   r   r   r   _declarative_constructor:  s   

r   rV   c                 C   s(   |j d u r| |_ |jd u r| |_d S d S rC   )rs   r?   )rs   columnr   r   r   r   P  s
   


r   )F)0rn   r   r   Zsqlalchemy.ormr    r   r   r   r   Zormr	   r   r
   r   r   Zorm.attributesr   Zorm.baser   r   Zorm.interfacesr   Zorm.propertiesr   r   Zschemar   r   sqlr   r   r2   r5   r   r&   r'   r$   r:   r@   r#   r6   r   r   r   r   ri   r   r   r   r   r   <module>   sR   
"    .B%