o
    y¬´\^x  ã                   @   sh  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 aadd„ Z dd„ Z!dd„ Z"d/d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,e*_+d-d.„ Z,dS )0z(Internal implementation for declarative.é    N)Úinstrumentationé   )Úclsregistryé   )Úevent)Úexc)Úutil)Úclass_mapper)Úmapper)Ú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   úQ/var/www/html/env/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   ÚsupÚ	classicalr   r   r   r%   -   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   Úmanager_of_classr$   r&   )r   Úmanagerr'   r
   r   r   r   r&   @   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!   ÚbaseÚ_is_declarative_inheritsÚ_is_classicial_inheritsr   r   r   r#   T   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    )r5   Úapir   ÚclasspropertyÚdeclarative_propsr#   Ú_MapperConfigÚsetup_mapping)r   Ú	classnameÚdict_r   r   r   Ú_as_declarativev   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)Ú
isinstancer8   r0   r   Úwarn)ÚobjÚnamer   r   r   r   Ú_check_declared_props_nocascadeƒ   s   
þÿrC   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 )r9   c                 C   s6   t |ddd�pt|dƒ}|rt}nt}||||ƒ d S )NÚ_sa_decl_prepare_nocascadeTr    Ú_sa_decl_prepare)r#   r*   r   r9   )r   Úcls_r;   r<   Ú	defer_mapÚcfg_clsr   r   r   r:   ’   s   ÿþz_MapperConfig.setup_mappingc                 C   sˆ   || _ t|ƒ| _|| _d | _t ¡ | _tƒ | _	i | _
|  ¡  i | j _|  ¡  t | j| j ¡ |  ¡  |  ¡  |  ¡  |  ¡  |  ¡  d S ©N)r   Údictr<   r;   Úmapped_tabler   ÚOrderedDictÚ
propertiesÚsetÚdeclared_columnsÚcolumn_copiesÚ_setup_declared_eventsÚ_sa_declared_attr_regÚ_scan_attributesr   Ú	add_classÚ_extract_mappable_attributesÚ_extract_declared_columnsÚ_setup_tableÚ_setup_inheritanceÚ_early_mapping)ÚselfrF   r;   r<   r   r   r   Ú__init__ž   s    

z_MapperConfig.__init__c                 C   s   |   ¡  d S rI   )Úmap©rZ   r   r   r   rY   ¾   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 rI   )r   r^   r   r]   r   r   r_   Ä   ó   z>_MapperConfig._setup_declared_events.<locals>.after_configuredÚ__declare_first__Úbefore_configuredc                      r`   rI   )r   rb   r   r]   r   r   rc   Ê   ra   z?_MapperConfig._setup_declared_events.<locals>.before_configured)r#   r   r   Úlistens_forr
   )rZ   r_   rc   r   r]   r   rQ   Á   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r’t	|
|	ˆ ƒ}|s‘|ry|r‘ˆ j}t|tttd ƒfƒs‹t d	¡‚|ˆ ur‘d}q:|r¦t|
tƒr¥t d
|j|	|ˆ f ¡ q:|ˆ u�r0t|
tƒr±q:t|
tƒr»t d¡‚t|
tƒ�r)t|
tjƒ}|sî|
jrî|	|v rÙt d|	ˆ f ¡ |
 |
ˆ ¡ ||	<  ||
< }tˆ |	|ƒ n(|rõt 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 )NrD   Tr    Ú__mapper_args__c                      s
   t ˆ jƒS rI   )rJ   re   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 Nonez‚Regular (i.e. not __special__) attribute '%s.%s' uses @declared_attr, but owning class %s is mapped - not applying to subclass %s.z”Mapper 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   r<   rP   r.   r   r#   Ú_produce_column_copiesÚvarsÚitemsrC   rg   rh   r?   ÚtuplerJ   Útyper   ÚArgumentErrorr8   r   r@   Ú__name__r   r   ÚInvalidRequestErrorr7   r>   Ú__get__ÚsetattrÚwarn_deprecatedr0   r   Ú_is_internal_proxyÚoriginal_propertyÚ
descriptorÚdocÚ__doc__Ú_warn_for_decl_attributesÚ
table_argsÚ	tablenamerf   )rZ   r<   rP   rf   rz   Úinherited_table_argsr{   r2   Úclass_mappedrB   rA   Ú
check_declÚoldclasspropÚretr   r   r   rS   Î   sÐ   
ÿÿý
ÿ€ÿ€ÿÿÿ€
üÿ


ÿ
ýÿ
þÿÿ
ÿþÿýÿ€€�r
z_MapperConfig._scan_attributesc                 C   s&   t |tjƒrt d||f ¡ d S d S )Nz…Attribute '%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   ÚColumnClauser   r@   )rZ   r   ÚkeyÚcr   r   r   ry   [  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   r<   rP   rj   rk   r?   r   r0   Úforeign_keysr   rp   rB   rƒ   ÚcopyÚ_creation_orderrr   )rZ   r2   r   r<   rP   rB   rA   Úcopy_r   r   r   ri   d  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�| |¡ |  |||¡ |s�t|||ƒ q|d
kršt d¡‚t ||¡}|||< qd S )NrD   Tr    )r„   rg   re   z�Use of @declared_attr.cascading only applies to Declarative 'mixin' and 'abstract' classes.  Currently, this flag is ignored on mapped class %sr   r   z‘Ignoring 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   r<   rM   r#   Úlistr?   r8   r5   r>   r   r@   r0   r   Úclass_r‚   r   rr   rl   Úlenr   r   Ú
startswithÚpopry   r   rp   r   Ú_deferred_relationship)rZ   r   r<   Ú	our_stuffÚlate_mappedÚkÚvalueÚpropr   r   r   rU     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rŒt d| j|d t|ƒ¡f ¡ qrd S )Nc                    s
   ˆ |  j S rI   ©r‡   ©r‚   ©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, )rM   ÚsortrO   ÚcollectionsÚdefaultdictrN   r‹   rk   r?   r   r   Úcolumnsr   ÚtableÚ_undefer_column_namerB   Úaddr‚   r�   r   r@   r;   ÚjoinÚsorted)rZ   rO   Úname_to_prop_keyr‚   rƒ   ÚcolrB   Úkeysr   r˜   r   rV   Å  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 |¡s”t d|j ¡‚q„|| _d S )Nc                 S   s   | j S rI   r–   )rƒ   r   r   r   r™   ô  s    z,_MapperConfig._setup_table.<locals>.<lambda>r—   Ú__table_cls__r„   r   éÿÿÿÿr   Ú__autoload__TÚautoloadz8Can't add additional column %r when specifying __table__)r   r{   rz   r<   rO   r¢   r*   r   Úunbound_method_to_callabler¦   r   r?   rJ   rl   ÚgetrŠ   r„   rƒ   Úcontains_columnr   rn   r‚   Úlocal_table)rZ   r   r{   rz   r<   rO   rž   Ú	table_clsÚargsÚtable_kwr©   rƒ   r   r   r   rW   ì  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r³t| jƒ}|j }|j}	|d u rµ|rwt
 d	¡‚|D ]=}|j|jv r˜|j|j |u rŠqyt
 d
|||j|j f ¡‚|jr t
 d¡‚| |¡ |	d ur²|	|ur²|	 |¡ qyd S d S d S )NrD   Tr    r   z&Class %s has multiple mapped bases: %rr   Ú__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   rz   rO   r$   r%   r   r#   Úappendr�   r   rp   ÚinheritsrK   rn   rB   rƒ   Úprimary_keyÚappend_columnÚ_refresh_for_new_column)
rZ   rž   r   rz   rO   r³   rƒ   Úinherited_mapperÚinherited_tableÚinherited_mapped_tabler   r   r   rX     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¡s’t| 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)Úversion_id_colÚpolymorphic_onr³   z=Can't specify 'inherits' explicitly with declarative mappingsÚconcreteFÚexclude_propertiesc                    s   g | ]
}|ˆ j vr|j‘qS r   )Ú_columntopropertyr‚   ©Ú.0rƒ   ©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    rM   )rM   rf   rP   r«   r³   r   r­   rN   rƒ   Úunionr½   Údifference_updaterO   r‹   rk   r?   r   ÚColumnElementÚ_propsr   r�   r†   Úmapper_args)
rZ   rM   rÇ   r“   Úvr¸   r½   r¤   ÚpÚresult_mapper_argsr   rÁ   r   Ú_prepare_mapper_argumentsj  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__rR   )rZ   Ú
mapper_clsÚmp_r   r   r   r\   ¦  s   ÿÿÿz_MapperConfig.mapN)ro   Ú
__module__Ú__qualname__Úclassmethodr:   r[   rY   rQ   rS   ry   ri   rU   rV   rW   rX   rË   r\   r   r   r   r   r9   ‘   s     
  	F'3K<r9   c                       sx   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d„ƒZ‡ fdd„Z‡  ZS )r   c                 C   s   d S rI   r   r]   r   r   r   rY   ¹  s   z$_DeferredMapperConfig._early_mappingc                 C   s   |   ¡ S rI   )Ú_clsr]   r   r   r   r   ¼  s   z_DeferredMapperConfig.clsc                 C   s    t  || j¡| _| | j| j< d S rI   )ÚweakrefÚrefÚ_remove_config_clsrÓ   Ú_configs)rZ   rŒ   r   r   r   r   À  s   c                 C   s   | j  |d ¡ d S rI   )r×   r�   )r   rÕ   r   r   r   rÖ   Å  s   z(_DeferredMapperConfig._remove_config_clsc                 C   s   t |tƒot |¡| jv S rI   )r?   rm   rÔ   rÕ   r×   ©r   rŒ   r   r   r   r   É  s   z_DeferredMapperConfig.has_clsc                 C   s   | j t |¡ S rI   )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 rI   )r-   )rÀ   ÚmrF   )Ú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 rI   r   rÛ   r   r   r   Ú	<genexpr>Ý  s   € z9_DeferredMapperConfig.classes_for_base.<locals>.<genexpr>c                 3   s(   � | ]}|ˆ v rˆ | ˆ ˆ fV  qd S rI   r   )rÀ   rÚ   )Úall_m_by_clsÚm_clsr   r   rÜ   á  s   € þý)r×   ÚvaluesrJ   Úextendr$   r‹   r   rš   )r   rÚ   rš   Úclasses_for_baseÚtuplesr   )rÝ   rÚ   rÞ   r   rá   Ò  s   
þ
þz&_DeferredMapperConfig.classes_for_basec                    s   | j  | jd ¡ tt| ƒ ¡ S rI   )r×   r�   rÓ   Úsuperr   r\   r]   ©Ú	__class__r   r   r\   è  s   z_DeferredMapperConfig.map)T)ro   rÐ   rÑ   r   rL   r×   rY   Úpropertyr   ÚsetterrÒ   rÖ   r   r   rá   r\   Ú__classcell__r   r   rä   r   r   ¶  s     




r   c                 C   s  d| j v r~t|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 | ||¡ dS t | ||¡ dS )z¹add 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Ÿ   r„   rµ   rÍ   Úadd_propertyr   r�   rž   r   r   r�   r   r‚   r   rm   Ú__setattr__)r   r‚   r”   r¤   r   r   r   Ú_add_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)rm   r*   Ú	TypeErrorro   rr   )rZ   ÚkwargsrF   r“   r   r   r   Ú_declarative_constructor  s   

ÿûrî   r[   c                 C   s(   |j d u r| |_ |jd u r| |_d S d S rI   )r‚   rB   )r‚   Úcolumnr   r   r   rŸ   '  s
   


ÿrŸ   )F)-rx   r›   rÔ   Úsqlalchemy.ormr   Ú r   r   r   r   Úormr	   r
   r   Úorm.attributesr   Úorm.baser   r   Úorm.interfacesr   Úorm.propertiesr   r   Úschemar   r   Úsqlr   r   r5   r8   r   r%   r&   r#   r=   rC   r"   r9   r   rë   rî   ro   rŸ   r   r   r   r   Ú<module>   sL   
"    )7$