
    h+                         d 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	      Z
 G d d	e
      Z G d
 de
      Z G d de
      Z G d de
      Zy)a0  **DEPRECATED**: Manipulators that can edit SON objects as they enter and exit
a database.

The :class:`~pymongo.son_manipulator.SONManipulator` API has limitations as a
technique for transforming your data. Instead, it is more flexible and
straightforward to transform outgoing documents in your own code before passing
them to PyMongo, and transform incoming documents after receiving them from
PyMongo. SON Manipulators will be removed from PyMongo in 4.0.

PyMongo does **not** apply SON manipulators to documents passed to
the modern methods :meth:`~pymongo.collection.Collection.bulk_write`,
:meth:`~pymongo.collection.Collection.insert_one`,
:meth:`~pymongo.collection.Collection.insert_many`,
:meth:`~pymongo.collection.Collection.update_one`, or
:meth:`~pymongo.collection.Collection.update_many`. SON manipulators are
**not** applied to documents returned by the modern methods
:meth:`~pymongo.collection.Collection.find_one_and_delete`,
:meth:`~pymongo.collection.Collection.find_one_and_replace`, and
:meth:`~pymongo.collection.Collection.find_one_and_update`.
    )DBRefObjectId)abcSONc                   "    e Zd ZdZd Zd Zd Zy)SONManipulatorziA base son manipulator.

    This manipulator just saves and restores objects without changing them.
    c                      y)ae  Will this SON manipulator make a copy of the incoming document?

        Derived classes that do need to make a copy should override this
        method, returning True instead of False. All non-copying manipulators
        will be applied first (so that the user's document will be updated
        appropriately), followed by copying manipulators.
        F selfs    Z/var/www/html/ranktracker/api/venv/lib/python3.12/site-packages/pymongo/son_manipulator.py	will_copyzSONManipulator.will_copy0   s         c                 <    | j                         rt        |      S |S )zManipulate an incoming SON object.

        :Parameters:
          - `son`: the SON object to be inserted into the database
          - `collection`: the collection the object is being inserted into
        r   r   r   son
collections      r   transform_incomingz!SONManipulator.transform_incoming:        >>s8O
r   c                 <    | j                         rt        |      S |S )zManipulate an outgoing SON object.

        :Parameters:
          - `son`: the SON object being retrieved from the database
          - `collection`: the collection this object was stored in
        r   r   s      r   transform_outgoingz!SONManipulator.transform_outgoingE   r   r   N)__name__
__module____qualname____doc__r   r   r   r   r   r   r
   r
   *   s    
		r   r
   c                       e Zd ZdZd Zy)ObjectIdInjectorzA son manipulator that adds the _id field if it is missing.

    .. versionchanged:: 2.7
       ObjectIdInjector is no longer used by PyMongo, but remains in this
       module for backwards compatibility.
    c                 (    d|vrt               |d<   |S )z+Add an _id field if it is missing.
        _idr   r   s      r   r   z#ObjectIdInjector.transform_incomingY   s     |!CJ
r   Nr   r   r   r   r   r   r   r   r    r    Q   s    r   r    c                       e Zd ZdZd Zd Zy)ObjectIdShufflerz<A son manipulator that moves _id to the first position.
    c                      y)zMWe need to copy to be sure that we are dealing with SON, not a dict.
        Tr   r   s    r   r   zObjectIdShuffler.will_copyh        r   c                 T    d|vr|S t        d|d   i      }|j                  |       |S )z-Move _id to the front if it's there.
        r"   )r   update)r   r   r   transformeds       r   r   z#ObjectIdShuffler.transform_incomingm   s7     |J5#e*-.3r   N)r   r   r   r   r   r   r   r   r   r%   r%   d   s    
r   r%   c                       e Zd ZdZd Zy)NamespaceInjectorz/A son manipulator that adds the _ns field.
    c                 $    |j                   |d<   |S )z1Add the _ns field to the incoming object
        _ns)namer   s      r   r   z$NamespaceInjector.transform_incoming{   s      __E

r   Nr#   r   r   r   r,   r,   w   s    r   r,   c                   (    e Zd ZdZd Zd Zd Zd Zy)AutoReferencea<  Transparently reference and de-reference already saved embedded objects.

    This manipulator should probably only be used when the NamespaceInjector is
    also being used, otherwise it doesn't make too much sense - documents can
    only be auto-referenced if they have an *_ns* field.

    NOTE: this will behave poorly if you have a circular reference.

    TODO: this only works for documents that are in the same database. To fix
    this we'll need to add a DatabaseInjector that adds *_db* and then make
    use of the optional *database* support for DBRefs.
    c                     || _         y N)database)r   dbs     r   __init__zAutoReference.__init__   s	    r   c                      y)zMWe need to copy so the user's document doesn't get transformed refs.
        Tr   r   s    r   r   zAutoReference.will_copy   r'   r   c                 >    fdfd t        |            S )z0Replace embedded documents with DBRefs.
        c                     t        | t        j                        r1d| v rd| v rt        | d    | d               S  t	        |             S t        | t
              r| D cg c]
  } |       c}S | S c c}w )Nr"   r.   )
isinstancer   MutableMappingr   r   list)valuevtransform_dicttransform_values     r   r@   z9AutoReference.transform_incoming.<locals>.transform_value   ss    %!3!34E>eun uuU|/LMM)#e*55E4(49:q*::L ;s   !A5c                 N    | j                         D ]  \  }} |      | |<    | S r3   itemsobjectkeyr=   r@   s      r   r?   z8AutoReference.transform_incoming.<locals>.transform_dict   /     & 5e-e4s5Mr   r   r   r   r   r?   r@   s      @@r   r   z AutoReference.transform_incoming   s    		
 c#h''r   c                 B      fdfd t        |            S )z0Replace DBRefs with embedded documents.
        c                 
   t        | t              rj                  j                  |       S t        | t              r| D cg c]
  } |       c}S t        | t
        j                        r t        |             S | S c c}w r3   )r:   r   r4   dereferencer<   r   r;   r   )r=   r>   r   r?   r@   s     r   r@   z9AutoReference.transform_outgoing.<locals>.transform_value   sk    %'}}0077E4(49:q*::E3#5#56%c%j11L ;s   B c                 N    | j                         D ]  \  }} |      | |<    | S r3   rB   rD   s      r   r?   z8AutoReference.transform_outgoing.<locals>.transform_dict   rG   r   r   rH   s   `  @@r   r   z AutoReference.transform_outgoing   s    		
 c#h''r   N)r   r   r   r   r6   r   r   r   r   r   r   r1   r1      s    
(*(r   r1   N)r   
bson.dbrefr   bson.objectidr   bson.py3compatr   bson.sonr   rE   r
   r    r%   r,   r1   r   r   r   <module>rQ      sU   *  "  $V $N~ &~ & =(N =(r   