o
    [hQ                     @   s|   d dl mZ ddlmZ ddlmZ ejdG dd deZejdG dd	 d	eZ	ejdG d
d deZ
dS )   EXT_CONTINUE   )event)utilzsqlalchemy.orm.interfacesc                   @   s   e Zd ZdZedd Zed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S )MapperExtensionae  Base implementation for :class:`_orm.Mapper` event hooks.

    .. deprecated:: 0.7

       :class:`.MapperExtension` is deprecated and will be removed in a future
       release.  Please refer to :func:`.event.listen` in conjunction with
       the :class:`.MapperEvents` listener interface.

    New extension classes subclass :class:`.MapperExtension` and are specified
    using the ``extension`` mapper() argument, which is a single
    :class:`.MapperExtension` or a list of such::

        from sqlalchemy.orm.interfaces import MapperExtension

        class MyExtension(MapperExtension):
            def before_insert(self, mapper, connection, instance):
                print "instance %s before insert !" % instance

        m = mapper(User, users_table, extension=MyExtension())

    A single mapper can maintain a chain of ``MapperExtension``
    objects. When a particular mapping event occurs, the
    corresponding method on each ``MapperExtension`` is invoked
    serially, and each method has the ability to halt the chain
    from proceeding further::

        m = mapper(User, users_table, extension=[ext1, ext2, ext3])

    Each ``MapperExtension`` method returns the symbol
    EXT_CONTINUE by default.   This symbol generally means "move
    to the next ``MapperExtension`` for processing".  For methods
    that return objects like translated rows or new object
    instances, EXT_CONTINUE means the result of the method
    should be ignored.   In some cases it's required for a
    default mapper activity to be performed, such as adding a
    new instance to a result list.

    The symbol EXT_STOP has significance within a chain
    of ``MapperExtension`` objects that the chain will be stopped
    when this symbol is returned.  Like EXT_CONTINUE, it also
    has additional significance in some cases that a default
    mapper activity will not be performed.

    c                 C      |  ||d d S )N)instrument_class_adapt_listener_methodsclsselflistener r   /home/ubuntu/experiments/live_experiments/Pythonexperiments/Otree/venv/lib/python3.10/site-packages/sqlalchemy/orm/deprecated_interfaces.py_adapt_instrument_class<   s   z'MapperExtension._adapt_instrument_classc                 C   r   )N)	init_instanceinit_failedreconstruct_instancebefore_insertafter_insertbefore_updateafter_updatebefore_deleteafter_deleter
   r   r   r   r   _adapt_listener@   s
   zMapperExtension._adapt_listenerc              	      s   |D ]p}t t|}t ||}t||srtd||f  |dkr5 fdd}tj jd||ddd q|d	krM fd
d}tj jd||ddd q|dkre fdd}tj jd||ddd qtj d| |dddd qd S )NzMapperExtension.%s is deprecated.  The MapperExtension class will be removed in a future release.  Please transition to the @event interface, using @event.listens_for(mapped_class, '%s').r   c                        fdd}|S )Nc                    s    |  d S Nr   )instancectxls_methr   r   r   reconstructe   s   zHMapperExtension._adapt_listener_methods.<locals>.go.<locals>.reconstructr   )r"   r#   r   r"   r   god   s   z3MapperExtension._adapt_listener_methods.<locals>.goloadFT)raw	propagater   c                    r   )Nc                    s    j jj| || d S r   )class_class_manageroriginal_initr   argskwargsr!   r   r   r   t   s   zJMapperExtension._adapt_listener_methods.<locals>.go.<locals>.init_instancer   )r"   r   r$   r%   r   r&   s   s   
initr   c                    r   )Nc              	      s    t  jjj| || d S r   )r   Zwarn_exceptionr*   r+   r,   r-   r!   r   r   r      s   zHMapperExtension._adapt_listener_methods.<locals>.go.<locals>.init_failedr   )r"   r   r$   r%   r   r&      s   Zinit_failurez%s)r(   retvalr)   )getattrr   r   methods_equivalentwarn_deprecatedr   listenr+   )r   r   r   methodsmethme_methr"   r&   r   r$   r   r   R   s\   

z'MapperExtension._adapt_listener_methodsc                 C      t S )a  Receive a class when the mapper is first constructed, and has
        applied instrumentation to the mapped class.

        The return value is only significant within the ``MapperExtension``
        chain; the parent mapper's behavior isn't modified by this method.

        r   )r   mapperr*   r   r   r   r	         z MapperExtension.instrument_classc                 C   r9   )at  Receive an instance when its constructor is called.

        This method is only called during a userland construction of
        an object.  It is not called when an object is loaded from the
        database.

        The return value is only significant within the ``MapperExtension``
        chain; the parent mapper's behavior isn't modified by this method.

        r   r   r:   r*   Zoldinitr   r.   r/   r   r   r   r      s   zMapperExtension.init_instancec                 C   r9   )a  Receive an instance when its constructor has been called,
        and raised an exception.

        This method is only called during a userland construction of
        an object.  It is not called when an object is loaded from the
        database.

        The return value is only significant within the ``MapperExtension``
        chain; the parent mapper's behavior isn't modified by this method.

        r   r<   r   r   r   r      s   zMapperExtension.init_failedc                 C   r9   )a  Receive an object instance after it has been created via
        ``__new__``, and after initial attribute population has
        occurred.

        This typically occurs when the instance is created based on
        incoming result rows, and is only called once for that
        instance's lifetime.

        Note that during a result-row load, this method is called upon
        the first row received for this instance.  Note that some
        attributes and collections may or may not be loaded or even
        initialized, depending on what's present in the result rows.

        The return value is only significant within the ``MapperExtension``
        chain; the parent mapper's behavior isn't modified by this method.

        r   )r   r:   r   r   r   r   r      s   z$MapperExtension.reconstruct_instancec                 C   r9   )a  Receive an object instance before that instance is inserted
        into its table.

        This is a good place to set up primary key values and such
        that aren't handled otherwise.

        Column-based attributes can be modified within this method
        which will result in the new value being inserted.  However
        *no* changes to the overall flush plan can be made, and
        manipulation of the ``Session`` will not have the desired effect.
        To manipulate the ``Session`` within an extension, use
        ``SessionExtension``.

        The return value is only significant within the ``MapperExtension``
        chain; the parent mapper's behavior isn't modified by this method.

        r   r   r:   
connectionr   r   r   r   r      s   zMapperExtension.before_insertc                 C   r9   )zReceive an object instance after that instance is inserted.

        The return value is only significant within the ``MapperExtension``
        chain; the parent mapper's behavior isn't modified by this method.

        r   r=   r   r   r   r      r;   zMapperExtension.after_insertc                 C   r9   )a  Receive an object instance before that instance is updated.

        Note that this method is called for all instances that are marked as
        "dirty", even those which have no net changes to their column-based
        attributes. An object is marked as dirty when any of its column-based
        attributes have a "set attribute" operation called or when any of its
        collections are modified. If, at update time, no column-based
        attributes have any net changes, no UPDATE statement will be issued.
        This means that an instance being sent to before_update is *not* a
        guarantee that an UPDATE statement will be issued (although you can
        affect the outcome here).

        To detect if the column-based attributes on the object have net
        changes, and will therefore generate an UPDATE statement, use
        ``object_session(instance).is_modified(instance,
        include_collections=False)``.

        Column-based attributes can be modified within this method
        which will result in the new value being updated.  However
        *no* changes to the overall flush plan can be made, and
        manipulation of the ``Session`` will not have the desired effect.
        To manipulate the ``Session`` within an extension, use
        ``SessionExtension``.

        The return value is only significant within the ``MapperExtension``
        chain; the parent mapper's behavior isn't modified by this method.

        r   r=   r   r   r   r      s   zMapperExtension.before_updatec                 C   r9   )zReceive an object instance after that instance is updated.

        The return value is only significant within the ``MapperExtension``
        chain; the parent mapper's behavior isn't modified by this method.

        r   r=   r   r   r   r      r;   zMapperExtension.after_updatec                 C   r9   )a  Receive an object instance before that instance is deleted.

        Note that *no* changes to the overall flush plan can be made
        here; and manipulation of the ``Session`` will not have the
        desired effect. To manipulate the ``Session`` within an
        extension, use ``SessionExtension``.

        The return value is only significant within the ``MapperExtension``
        chain; the parent mapper's behavior isn't modified by this method.

        r   r=   r   r   r   r   *  s   zMapperExtension.before_deletec                 C   r9   )zReceive an object instance after that instance is deleted.

        The return value is only significant within the ``MapperExtension``
        chain; the parent mapper's behavior isn't modified by this method.

        r   r=   r   r   r   r   9  r;   zMapperExtension.after_deleteN)__name__
__module____qualname____doc__classmethodr   r   r   r	   r   r   r   r   r   r   r   r   r   r   r   r   r   r      s$    -


U

 
r   c                   @   sl   e Zd 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S )SessionExtensiona  Base implementation for :class:`.Session` event hooks.

    .. deprecated:: 0.7

       :class:`.SessionExtension` is deprecated and will be removed in a future
       release.  Please refer to :func:`.event.listen` in conjunction with
       the :class:`.SessionEvents` listener interface.

    Subclasses may be installed into a :class:`.Session` (or
    :class:`.sessionmaker`) using the ``extension`` keyword
    argument::

        from sqlalchemy.orm.interfaces import SessionExtension

        class MySessionExtension(SessionExtension):
            def before_commit(self, session):
                print "before commit!"

        Session = sessionmaker(extension=MySessionExtension())

    The same :class:`.SessionExtension` instance can be used
    with any number of sessions.

    c                 C   sT   dD ]%}t t|}t ||}t||s'td||f  t||t || qd S )N)
before_commitafter_commitafter_rollbackbefore_flushafter_flushafter_flush_postexecafter_beginafter_attachafter_bulk_updateafter_bulk_deletezSessionExtension.%s is deprecated.  The SessionExtension class will be removed in a future release.  Please transition to the @event interface, using @event.listens_for(Session, '%s').)r2   rD   r   r3   r4   r   r5   r   r   r   r7   r8   r"   r   r   r   r   `  s   

z SessionExtension._adapt_listenerc                 C      dS )zExecute right before commit is called.

        Note that this may not be per-flush if a longer running
        transaction is ongoing.Nr   r   sessionr   r   r   rE   {      zSessionExtension.before_commitc                 C   rP   )zExecute after a commit has occurred.

        Note that this may not be per-flush if a longer running
        transaction is ongoing.Nr   rQ   r   r   r   rF     rS   zSessionExtension.after_commitc                 C   rP   )zExecute after a rollback has occurred.

        Note that this may not be per-flush if a longer running
        transaction is ongoing.Nr   rQ   r   r   r   rG     rS   zSessionExtension.after_rollbackc                 C   rP   )zExecute before flush process has started.

        `instances` is an optional list of objects which were passed to
        the ``flush()`` method.Nr   )r   rR   flush_contextZ	instancesr   r   r   rH     rS   zSessionExtension.before_flushc                 C   rP   )a  Execute after flush has completed, but before commit has been
        called.

        Note that the session's state is still in pre-flush, i.e. 'new',
        'dirty', and 'deleted' lists still show pre-flush state as well
        as the history settings on instance attributes.Nr   r   rR   rT   r   r   r   rI     rS   zSessionExtension.after_flushc                 C   rP   )ab  Execute after flush has completed, and after the post-exec
        state occurs.

        This will be when the 'new', 'dirty', and 'deleted' lists are in
        their final state.  An actual commit() may or may not have
        occurred, depending on whether or not the flush started its own
        transaction or participated in a larger transaction.Nr   rU   r   r   r   rJ     rS   z%SessionExtension.after_flush_postexecc                 C   rP   )zExecute after a transaction is begun on a connection

        `transaction` is the SessionTransaction. This method is called
        after an engine level transaction is begun on a connection.Nr   )r   rR   Ztransactionr>   r   r   r   rK     rS   zSessionExtension.after_beginc                 C   rP   )zjExecute after an instance is attached to a session.

        This is called after an add, delete or merge.Nr   )r   rR   r   r   r   r   rL     rS   zSessionExtension.after_attachc                 C   rP   )aG  Execute after a bulk update operation to the session.

        This is called after a session.query(...).update()

        `query` is the query object that this update operation was
        called on. `query_context` was the query context object.
        `result` is the result object returned from the bulk operation.
        Nr   r   rR   queryZquery_contextresultr   r   r   rM     rS   z"SessionExtension.after_bulk_updatec                 C   rP   )aG  Execute after a bulk delete operation to the session.

        This is called after a session.query(...).delete()

        `query` is the query object that this delete operation was
        called on. `query_context` was the query context object.
        `result` is the result object returned from the bulk operation.
        Nr   rV   r   r   r   rN     rS   z"SessionExtension.after_bulk_deleteN)r?   r@   rA   rB   rC   r   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rN   r   r   r   r   rD   D  s    
	
rD   c                   @   s:   e Zd ZdZdZ	 edd Zdd Zdd Zd	d
 Z	dS )AttributeExtensiona  Base implementation for :class:`.AttributeImpl` event hooks, events
    that fire upon attribute mutations in user code.

    .. deprecated:: 0.7

       :class:`.AttributeExtension` is deprecated and will be removed in a
       future release.  Please refer to :func:`.event.listen` in conjunction
       with the :class:`.AttributeEvents` listener interface.

    :class:`.AttributeExtension` is used to listen for set,
    remove, and append events on individual mapped attributes.
    It is established on an individual mapped attribute using
    the `extension` argument, available on
    :func:`.column_property`, :func:`_orm.relationship`, and
    others::

        from sqlalchemy.orm.interfaces import AttributeExtension
        from sqlalchemy.orm import mapper, relationship, column_property

        class MyAttrExt(AttributeExtension):
            def append(self, state, value, initiator):
                print "append event !"
                return value

            def set(self, state, value, oldvalue, initiator):
                print "set event !"
                return value

        mapper(SomeClass, sometable, properties={
            'foo':column_property(sometable.c.foo, extension=MyAttrExt()),
            'bar':relationship(Bar, extension=MyAttrExt())
        })

    Note that the :class:`.AttributeExtension` methods
    :meth:`~.AttributeExtension.append` and
    :meth:`~.AttributeExtension.set` need to return the
    ``value`` parameter. The returned value is used as the
    effective value, and allows the extension to change what is
    ultimately persisted.

    AttributeExtension is assembled within the descriptors associated
    with a mapped class.

    Tc                 C   s   dD ]}t t|}t ||}t||std||f  qtj|d|j|jddd tj|d|j	|jddd tj|d|j
|jddd d S )N)appendremovesetzAttributeExtension.%s is deprecated.  The AttributeExtension class will be removed in a future release.  Please transition to the @event interface, using @event.listens_for(Class.attribute, '%s').rZ   T)active_historyr(   r1   r[   r\   )r2   rY   r   r3   r4   r   r5   rZ   r]   r[   r\   rO   r   r   r   r     sD   


z"AttributeExtension._adapt_listenerc                 C      |S )zReceive a collection append event.

        The returned value will be used as the actual value to be
        appended.

        r   r   statevalue	initiatorr   r   r   rZ   $     zAttributeExtension.appendc                 C   rP   )zFReceive a remove event.

        No return value is defined.

        Nr   r_   r   r   r   r[   -  s   zAttributeExtension.removec                 C   r^   )znReceive a set event.

        The returned value will be used as the actual value to be
        set.

        r   )r   r`   ra   Zoldvaluerb   r   r   r   r\   5  rc   zAttributeExtension.setN)
r?   r@   rA   rB   r]   rC   r   rZ   r[   r\   r   r   r   r   rY     s    -
'	rY   N)Z
interfacesr    r   r   ZlanghelpersZdependency_forobjectr   rD   rY   r   r   r   r   <module>   s   
  
8
