
    j                         d dl m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 dlmZ d dlmZ dd	lmZ dd
lmZ ddlmZ  G d dej                  Z G d de          Zd ZdS )    )IterableN)hybrid_property)
attributesclass_mapperColumnProperty)MapperPropertyPropComparator)_state_session)set_creation_order   )ImproperlyConfigured)identity)_get_class_registryc                   F    e Zd Zej        fdZd Zd Zej        ddfdZdS )GenericAttributeImplc                    | j         |v r|| j                  S t          |          }|d S |                     |          }t          |j                                      |          }|d S |                     |          }	 |                    ||          }n8# t          $ r+ |                    |                              |          }Y nw xY w|S N)	keyr
   get_state_discriminatorr   class_getget_state_idAttributeErrorquery)	selfstatedict_passivesessiondiscriminatortarget_classidtargets	            b/lsinfo/ai/hellotax_ai/base_platform/venv/lib/python3.11/site-packages/sqlalchemy_utils/generic.pyr   zGenericAttributeImpl.get   s    8u?" !''?4 44U;;*5<88<<]KK4u%%	9[[r22FF 	9 	9 	9]]<0044R88FFF	9
 s    B 2CCc                     | j         j        }t          |t                    r't	          |                                |j                  S |j        |j                 j	        S r   )
parent_tokenr    
isinstancer   getattrobj__name__attrsr   value)r   r   r    s      r$   r   z,GenericAttributeImpl.get_state_discriminator/   sM    )7m_55 	8599;;(>???;}0177    c                 N    t          fd| j        j        D                       S )Nc              3   F   K   | ]}j         |j                 j        V  d S r   )r+   r   r,   ).0r"   r   s     r$   	<genexpr>z4GenericAttributeImpl.get_state_id.<locals>.<genexpr>8   s/      NN2U[(.NNNNNNr-   )tupler&   r"   )r   r   s    `r$   r   z!GenericAttributeImpl.get_state_id6   s,    NNNN9J9MNNNNNNr-   NFc                    ||| j         <   |/| j        j        D ]}d ||j         <   d || j        j        j         <   d S t	          |          }t          |          }	|	                    |          d         }
|j        }t          | j        j                  D ]\  }}|
|         ||j         <   ||| j        j        j         <   d S )Nr   )	r   r&   r"   r    typer   identity_key_from_instancer*   	enumerate)r   r   r   	initiatorr   	check_oldpopr"   r   mapperpkr    indexs                r$   setzGenericAttributeImpl.set:   s     $dh'* % % $bf9=E$#15666 )__F!&))F229==a@B #OM&t'8';<< * *	r "5	bf9FE$#15666r-   )	r*   
__module____qualname__r   PASSIVE_OFFr   r   r   r=    r-   r$   r   r      sw        (2(>    >8 8 8O O O
 *G G G G G Gr-   r   c                   R     e Zd ZdZd	 fd	Zd Zd Z G d de          Zd Z	 xZ
S )
GenericRelationshipPropertyaR  A generic form of the relationship property.

    Creates a 1 to many relationship between the parent model
    and any other models using a descriminator (the table name).

    :param discriminator
        Field to discriminate which model we are referring to.
    :param id:
        Field to point to the model we are referring to.
    Nc                     t                                                       || _        || _        d | _        d | _        || _        t          |            d S r   )super__init___discriminator_col_id_cols_id_discriminatordocr   )r   r    r"   rK   	__class__s       r$   rF   z$GenericRelationshipProperty.__init__c   sR    "/"4     r-   c                 H   t          |t                    r7|j        }| j        j                                        D ]\  }}||k    r|c S d S | j        j                                        D ]6}t          |t                    r|j	        d         j
        |j
        k    r|c S 7d S )Nr   )r'   r   r*   parentall_orm_descriptorsitemsr+   valuesr   columnsname)r   columnattr_keyr   attrs        r$   _column_to_propertyz/GenericRelationshipProperty._column_to_propertym   s    fo.. 		$H![<BBDD    	T(??KKK #    )0022 $ $dN33 $|A+v{::#$ $r-   c                      fd} | j                    _          | j                   _        t           j        t                    r(t	          t          | j                             _        n j        g _                              j                    _         j        t          d          t	          t           j         j                             _	        d S )Nc                 V    t          | t                    rj        j        |          S | S r   )r'   strrN   rR   )rT   r   s    r$   convert_stringsz9GenericRelationshipProperty.init.<locals>.convert_stringsz   s*    &#&& 3{*622Mr-   z(Could not find discriminator descriptor.)
rG   rH   r'   r   listmaprW   r    r   r"   )r   r[   s   ` r$   initz GenericRelationshipProperty.inity   s    	 	 	 	 	
 #2/$2I"J"J'66dmX.. 	, _dm!D!DEEDMM!]ODM!55d6MNN%&:   s43T]CCDDr-   c                   &    e Zd Zd Zd Zd Zd ZdS )&GenericRelationshipProperty.Comparatorc                 "    || _         || _        d S r   )property_parententity)r   propparentmappers      r$   rF   z/GenericRelationshipProperty.Comparator.__init__   s     DM!-Dr-   c                     t          |          j        }| j        j        |k    }t	          |          }t          | j        j                  D ]\  }}||||         k    z  }|S r   )r4   r*   rb   rG   r   r6   rH   )r   otherr    qother_idr<   r"   s          r$   __eq__z-GenericRelationshipProperty.Comparator.__eq__   sd     KK0M0MAAH&t}'=>> + +	rR8E?**Hr-   c                     | |k     S r   rA   )r   rg   s     r$   __ne__z-GenericRelationshipProperty.Comparator.__ne__   s    U]##r-   c                     t          j        |          }|j        g}|                    d |j        D                        | j        j                            |          S )Nc                 &    g | ]}|j         j        S rA   )r   r*   )r0   	submappers     r$   
<listcomp>zBGenericRelationshipProperty.Comparator.is_type.<locals>.<listcomp>   s.            )     r-   )sainspectr*   extend_inheriting_mappersrb   rG   in_)r   rg   r:   class_namess       r$   is_typez.GenericRelationshipProperty.Comparator.is_type   so    Z&&F !>*K    !'!;        
 =377DDDr-   N)r*   r>   r?   rF   rj   rl   rw   rA   r-   r$   
Comparatorr`      sU        	. 	. 	.	 	 		$ 	$ 	$
	E 
	E 
	E 
	E 
	Er-   rx   c           	          t          j        |j        | j        |                     | |          || j        t          |            d S )N)
comparatorparententityrK   
impl_classr&   )r   register_attributer   r   rx   rK   r   )r   r:   s     r$   instrument_classz,GenericRelationshipProperty.instrument_class   sO    %MHtV44+	
 	
 	
 	
 	
 	
r-   r   )r*   r>   r?   __doc__rF   rW   r^   r	   rx   r~   __classcell__)rL   s   @r$   rC   rC   W   s        	 	! ! ! ! ! !
$ 
$ 
$E E E.E E E E E^ E E E8	
 	
 	
 	
 	
 	
 	
r-   rC   c                      t          | i |S r   )rC   )argskwargss     r$   generic_relationshipr      s    &7777r-   )collections.abcr   
sqlalchemyrq   sqlalchemy.ext.hybridr   sqlalchemy.ormr   r   r   sqlalchemy.orm.interfacesr   r	   sqlalchemy.orm.sessionr
   sqlalchemy.utilr   
exceptionsr   	functionsr   functions.ormr   ScalarAttributeImplr   rC   r   rA   r-   r$   <module>r      sO   $ $ $ $ $ $     1 1 1 1 1 1 C C C C C C C C C C D D D D D D D D 1 1 1 1 1 1 . . . . . . , , , , , ,       . . . . . .EG EG EG EG EG:9 EG EG EGP^
 ^
 ^
 ^
 ^
. ^
 ^
 ^
B8 8 8 8 8r-   