
    W
h&                     N   S SK r S SKrS SK Jr  S SKJr  S SKJr  S SKJr  S SK	J
r
  S SKJrJr  S SKJr  S	r " S
 S\R"                  5      r\" 5       rS rS rS r " S S5      r " S S5      r\ R                  S 5       r " S S5      r " S S5      r\S 5       r\S 5       rg)    N)contextmanager)ReadConcern)WriteConcern)_no_dereference_for_fields)_import_class)DEFAULT_CONNECTION_NAMEget_db)count_documents)	switch_dbswitch_collectionno_dereferenceno_sub_classesquery_counterset_write_concernset_read_write_concern!no_dereferencing_active_for_classc                       \ rS rSrS rSrg)MyThreadLocals   c                     0 U l         g Nno_dereferencing_classselfs    dC:\Users\julio\Documents\inmuebles-mzt\backend\env\Lib\site-packages\mongoengine/context_managers.py__init__MyThreadLocals.__init__   s
    &(#    r   N)__name__
__module____qualname____firstlineno__r   __static_attributes__ r   r   r   r      s    )r   r   c                 (    U [         R                  ;   $ r   )thread_localsr   clss    r   r   r   "   s    -6666r   c                 z    [         R                  R                  U S5        [         R                  U ==   S-  ss'   g )Nr      )r'   r   
setdefaultr(   s    r   $_register_no_dereferencing_for_classr-   &   s.    ((33C;((-2-r   c                     [         R                  U ==   S-  ss'   [         R                  U    S:X  a   [         R                  R                  U 5        g g )Nr+   r   )r'   r   popr(   s    r   &_unregister_no_dereferencing_for_classr0   +   sD    ((-2-++C0A5,,005 6r   c                   *    \ rS rSrSrS rS rS rSrg)r   1   a  switch_db alias context manager.

Example ::

    # Register connections
    register_connection('default', 'mongoenginetest')
    register_connection('testdb-1', 'mongoenginetest2')

    class Group(Document):
        name = StringField()

    Group(name='test').save()  # Saves in the default db

    with switch_db(Group, 'testdb-1') as Group:
        Group(name='hello testdb!').save()  # Saves in testdb-1
c                     Xl         UR                  5       U l        X l        UR                  R                  S[        5      U l        g)zConstruct the switch_db context manager

:param cls: the class to change the registered db
:param db_alias: the name of the specific database to use
db_aliasN)r)   _get_collection
collectionr4   _metagetr   ori_db_alias)r   r)   r4   s      r   r   switch_db.__init__C   s6     --/ IIMM*6MNr   c                     U R                   U R                  R                  S'   SU R                  l        U R                  $ )z4Change the db_alias and clear the cached collection.r4   N)r4   r)   r7   _collectionr   s    r   	__enter__switch_db.__enter__N   s-    %)]]z"#xxr   c                     U R                   U R                  R                  S'   U R                  U R                  l        g)z"Reset the db_alias and collection.r4   N)r9   r)   r7   r6   r<   r   tvalue	tracebacks       r   __exit__switch_db.__exit__T   s*    %)%6%6z"#r   )r)   r6   r4   r9   N	r    r!   r"   r#   __doc__r   r=   rD   r$   r%   r   r   r   r   1   s    "	O/r   r   c                   *    \ rS rSrSrS rS rS rSrg)r   Z   a(  switch_collection alias context manager.

Example ::

    class Group(Document):
        name = StringField()

    Group(name='test').save()  # Saves in the default db

    with switch_collection(Group, 'group1') as Group:
        Group(name='hello testdb!').save()  # Saves in group1 collection
c                 h    Xl         UR                  5       U l        UR                  U l        X l        g)zConstruct the switch_collection context manager.

:param cls: the class to change the registered db
:param collection_name: the name of the collection to use
N)r)   r5   ori_collection_get_collection_nameori_get_collection_namecollection_name)r   r)   rN   s      r   r   switch_collection.__init__h   s.     !113'*'?'?$.r   c                 ~   ^  [         U 4S j5       nUT R                  l        ST R                  l        T R                  $ )z@Change the _get_collection_name and clear the cached collection.c                    > TR                   $ r   )rN   )r)   r   s    r   rL   9switch_collection.__enter__.<locals>._get_collection_namev   s    '''r   N)classmethodr)   rL   r<   )r   rL   s   ` r   r=   switch_collection.__enter__s   s:     
	( 
	( )=%#xxr   c                 p    U R                   U R                  l        U R                  U R                  l        g)zReset the collection.N)rK   r)   r<   rM   rL   r@   s       r   rD   switch_collection.__exit__~   s&    #22(,(D(D%r   )r)   rN   rK   rM   NrF   r%   r   r   r   r   Z   s    	/	Er   r   c           
   #   |  #     U n [        S5      n[        S5      n[        S5      nU R                  R                  5        VVs/ s H  u  pE[        XQX#45      (       d  M  UPM     nnn[	        U 5        [        U6    Sv   SSS5        [        U 5        gs  snnf ! , (       d  f       N = f! [        U 5        f = f7f)zno_dereference context manager.

Turns off all dereferencing in Documents for the duration of the context
manager::

    with no_dereference(Group):
        Group.objects()
ReferenceFieldGenericReferenceFieldComplexBaseFieldN)r   _fieldsitems
isinstancer-   r   r0   )r)   rX   rY   rZ   namefieldderef_fieldss          r   r   r      s     4&'78 -.E F();<  #{{002
2(=P 2 	 
 	-S1'6J 7 	/s3
 76 	/s3sM   B<A B, B B&B, <BB, 	B<B, 
B)%B, ,B99B<c                   *    \ rS rSrSrS rS rS rSrg)r      zno_sub_classes context manager.

Only returns instances of this class and no sub (inherited) classes::

    with no_sub_classes(Group) as Group:
        Group.objects.find()
c                     Xl         SU l        g)zdConstruct the no_sub_classes context manager.

:param cls: the class to turn querying subclasses on
Nr)   cls_initial_subclasses)r   r)   s     r   r   no_sub_classes.__init__   s    
 &*#r   c                     U R                   R                  U l        U R                   R                  4U R                   l        U R                   $ )z8Change the objects default and _auto_dereference values.)r)   _subclassesre   _class_namer   s    r   r=   no_sub_classes.__enter__   s6    &*hh&:&:# $ 4 46xxr   c                 :    U R                   U R                  l        g)z/Reset the default and _auto_dereference values.N)re   r)   rh   r@   s       r   rD   no_sub_classes.__exit__   s    #::r   rd   NrF   r%   r   r   r   r      s    +;r   r   c                   r    \ rS rSrSr\4S jrS rS rS r	S r
S rS	 rS
 rS rS rS rS rS rS rSrg)r      aH  Query_counter context manager to get the number of queries.
This works by updating the `profiling_level` of the database so that all queries get logged,
resetting the db.system.profile collection at the beginning of the context and counting the new entries.

This was designed for debugging purpose. In fact it is a global counter so queries issued by other threads/processes
can interfere with it

Usage:

.. code-block:: python

    class User(Document):
        name = StringField()

    with query_counter() as q:
        user = User(name='Bob')
        assert q == 0       # no query fired yet
        user.save()
        assert q == 1       # 1 query was fired, an 'insert'
        user_bis = User.objects().first()
        assert q == 2       # a 2nd query was fired, a 'find_one'

Be aware that:

- Iterating over large amount of documents (>101) makes pymongo issue `getmore` queries to fetch the next batch of documents (https://www.mongodb.com/docs/manual/tutorial/iterate-a-cursor/#cursor-batches)
- Some queries are ignored by default by the counter (killcursors, db.system.indexes)
c                     [        US9U l        S U l        SU l        SSU R                  R                  -  0SS0SS0S.U l        g )	N)aliasr   z$nez%s.system.indexeskillcursorsz$existsF)nsopzcommand.killCursors)r	   dbinitial_profiling_level_ctx_query_counterr^   _ignored_query)r   rp   s     r   r   query_counter.__init__   sP    u%'+$"# -<=-($-u#5
r   c                     U R                   R                  SS05      nUS   U l        U R                   R                  R                  R                  5         U R                   R                  SS05        g )Nprofiler   was   )rt   commandru   systemrz   drop)r   profile_update_ress     r   _turn_on_profiling query_counter._turn_on_profiling   sT    !WW__i^<'9%'@$##%A'r   c                 R    U R                   R                  SU R                  05        g )Nrz   )rt   r}   ru   r   s    r   _resets_profilingquery_counter._resets_profiling   s    D$@$@ABr   c                 &    U R                  5         U $ r   )r   r   s    r   r=   query_counter.__enter__   s    !r   c                 $    U R                  5         g r   )r   r@   s       r   rD   query_counter.__exit__   s     r   c                 *    U R                  5       nX:H  $ r   
_get_count)r   rB   counters      r   __eq__query_counter.__eq__   s    //#r   c                 .    U R                  U5      (       + $ r   )r   r   rB   s     r   __ne__query_counter.__ne__   s    ;;u%%%r   c                 (    U R                  5       U:  $ r   r   r   s     r   __lt__query_counter.__lt__       5((r   c                 (    U R                  5       U:*  $ r   r   r   s     r   __le__query_counter.__le__       E))r   c                 (    U R                  5       U:  $ r   r   r   s     r   __gt__query_counter.__gt__  r   r   c                 (    U R                  5       U:  $ r   r   r   s     r   __ge__query_counter.__ge__
  r   r   c                 "    U R                  5       $ r   r   r   s    r   __int__query_counter.__int__  s      r   c                 (    SU R                  5       -  $ )z,repr query_counter as the number of queries.z%sr   r   s    r   __repr__query_counter.__repr__  s    doo'''r   c                     [        U R                  R                  R                  U R                  5      U R
                  -
  nU =R
                  S-  sl        U$ )zGet the number of queries by counting the current number of entries in db.system.profile
and substracting the queries issued by this context. In fact everytime this is called, 1 query is
issued so we need to balance that
r+   )r
   rt   r~   rz   rw   rv   )r   counts     r   r   query_counter._get_count  sR     DGGNN22D4G4GH%%& 	 		
 r   )rv   rw   rt   ru   N)r    r!   r"   r#   rG   r   r   r   r   r=   rD   r   r   r   r   r   r   r   r   r   r$   r%   r   r   r   r      sS    8 5 	
(C! &)*)*!(r   r   c              #      #    [        U R                  R                  R                  5       5      nUR	                  U5        U R                  [        S0 UD6S9v   g 7f)N)write_concernr%   )dictr   documentr\   updatewith_optionsr   )r6   write_concernscombined_concernss      r   r   r   #  sP     Z55>>DDFG^,

!
!0Q?P0Q
!
RRs   AAc              #   X  #    [        U R                  R                  R                  5       5      nUb  UR	                  U5        [        U R
                  R                  R                  5       5      nUb  UR	                  U5        U R                  [        S0 UD6[        S0 UD6S9v   g 7f)N)r   read_concernr%   )	r   r   r   r\   r   r   r   r   r   )r6   r   read_concernscombined_write_concernscombined_read_concernss        r   r   r   *  s     ":#;#;#D#D#J#J#LM!&&~6!*"9"9"B"B"H"H"JK %%m4

!
!"=%<= :#9: "  s   B(B*)
contextlib	threadingr   pymongo.read_concernr   pymongo.write_concernr   mongoengine.base.fieldsr   mongoengine.commonr   mongoengine.connectionr   r	   mongoengine.pymongo_supportr
   __all__localr   r'   r   r-   r0   r   r   r   r   r   r   r   r%   r   r   <module>r      s      % , . > , B 7	)Y__ )  73
6&/ &/R'E 'ET 4 4@; ;8_ _D S S  r   