
    h$                         d Z ddlZddlZddlZddlZddlZddlZddlZddlm	Z	 ddl
mZ ddlmZmZmZmZ ddlmZ dZd Zd	 Z G d
 de      Zy)zXTools for working with MongoDB `ObjectIds
<http://dochub.mongodb.org/core/objectids>`_.
    N)SystemRandom	InvalidId)PY3bytes_from_hexstring_type	text_type)utci c                     t        d| z        )NzS%r is not a valid ObjectId, it must be a 12-byte input or a 24-character hex stringr   )oids    P/var/www/html/ranktracker/api/venv/lib/python3.12/site-packages/bson/objectid.py_raise_invalid_idr   %   s    
	(*-	./ /    c                  ,    t        j                  d      S )z+Get the 5-byte random field of an ObjectId.   )osurandom r   r   _random_bytesr   +   s    ::a=r   c                   >   e Zd ZdZ ej
                         Z e       j                  de	      Z
 ej                         Z e       ZdZdZddZed        Zed        Zed	        Zd
 Zd Zed        Zed        Zd Zd Zd Zd Zd Z d Z!d Z"d Z#d Z$d Z%d Z&y)ObjectIdzA MongoDB ObjectId.
    r   )__id   Nc                     || j                          yt        |t              rt        |      dk(  r|| _        y| j                  |       y)a  Initialize a new ObjectId.

        An ObjectId is a 12-byte unique identifier consisting of:

          - a 4-byte value representing the seconds since the Unix epoch,
          - a 5-byte random value,
          - a 3-byte counter, starting with a random value.

        By default, ``ObjectId()`` creates a new unique identifier. The
        optional parameter `oid` can be an :class:`ObjectId`, or any 12
        :class:`bytes` or, in Python 2, any 12-character :class:`str`.

        For example, the 12 bytes b'foo-bar-quux' do not follow the ObjectId
        specification but they are acceptable input::

          >>> ObjectId(b'foo-bar-quux')
          ObjectId('666f6f2d6261722d71757578')

        `oid` can also be a :class:`unicode` or :class:`str` of 24 hex digits::

          >>> ObjectId('0123456789ab0123456789ab')
          ObjectId('0123456789ab0123456789ab')
          >>>
          >>> # A u-prefixed unicode literal:
          >>> ObjectId(u'0123456789ab0123456789ab')
          ObjectId('0123456789ab0123456789ab')

        Raises :class:`~bson.errors.InvalidId` if `oid` is not 12 bytes nor
        24 hex digits, or :class:`TypeError` if `oid` is not an accepted type.

        :Parameters:
          - `oid` (optional): a valid ObjectId.

        .. mongodoc:: objectids

        .. versionchanged:: 3.8
           :class:`~bson.objectid.ObjectId` now implements the `ObjectID
           specification version 0.2
           <https://github.com/mongodb/specifications/blob/master/source/
           objectid.rst>`_.
        N   )_ObjectId__generate
isinstancebyteslen_ObjectId__id_ObjectId__validateselfr   s     r   __init__zObjectId.__init__?   s;    T ;OOU#CBDIOOC r   c                     |j                         ||j                         z
  }t        j                  |j                               }t	        j
                  dt        |            dz   } | |      S )a^  Create a dummy ObjectId instance with a specific generation time.

        This method is useful for doing range queries on a field
        containing :class:`ObjectId` instances.

        .. warning::
           It is not safe to insert a document containing an ObjectId
           generated using this method. This method deliberately
           eliminates the uniqueness guarantee that ObjectIds
           generally provide. ObjectIds generated with this method
           should be used exclusively in queries.

        `generation_time` will be converted to UTC. Naive datetime
        instances will be treated as though they already contain UTC.

        An example using this helper to get documents where ``"_id"``
        was generated before January 1, 2010 would be:

        >>> gen_time = datetime.datetime(2010, 1, 1)
        >>> dummy_id = ObjectId.from_datetime(gen_time)
        >>> result = collection.find({"_id": {"$lt": dummy_id}})

        :Parameters:
          - `generation_time`: :class:`~datetime.datetime` to be used
            as the generation time for the resulting ObjectId.
        >Is           )	utcoffsetcalendartimegm	timetuplestructpackint)clsgeneration_time	timestampr   s       r   from_datetimezObjectId.from_datetimep   sf    8 $$&2-0I0I0KKOOOO$=$=$?@	kk#i."$GH3xr   c                 L    |sy	 t        |       y# t        t        f$ r Y yw xY w)zChecks if a `oid` string is valid or not.

        :Parameters:
          - `oid`: the object id to validate

        .. versionadded:: 2.3
        FT)r   r   	TypeError)r.   r   s     r   is_validzObjectId.is_valid   s0     	SM9% 		s    ##c                     t        j                         }|| j                  k7  r|| _        t               | _        | j                  S )z:Generate a 5-byte random number once per process.
        )r   getpid_pidr   _ObjectId__random)r.   pids     r   _randomzObjectId._random   s4     iik#((?CH(?CL||r   c                    t        j                  dt        t        j                                     }|t        j                         z  }t        j                  5  |t        j                  dt        j                        dd z  }t        j                  dz   t        dz   z  t        _        ddd       || _	        y# 1 sw Y   || _	        yxY w)z0Generate a new value for this ObjectId.
        r&         N)
r+   r,   r-   timer   r:   	_inc_lock_inc_MAX_COUNTER_VALUEr    r"   s     r   
__generatezObjectId.__generate   s    
 kk$DIIK 01 	x!!  	K6;;tX]]3Aa88C%]]Q.3E3IJHM	K 			K 	s   AB::C
c                 N   t        |t              r|j                  | _        yt        |t              r,t        |      dk(  r	 t        |      | _        yt        |       yt        dt        j                  dt        |            # t        t        f$ r t        |       Y yw xY w)a;  Validate and use the given id for this ObjectId.

        Raises TypeError if id is not an instance of
        (:class:`basestring` (:class:`str` or :class:`bytes`
        in python 3), ObjectId) and InvalidId if it is not a
        valid ObjectId.

        :Parameters:
          - `oid`: a valid ObjectId
           z"id must be an instance of (bytes, z, ObjectId), not N)r   r   binaryr    r   r   r   r3   
ValueErrorr   r	   __name__typer"   s     r   
__validatezObjectId.__validate   s     c8$

DI[)3x2~+ .s 3DI "#&(1(:(:DIG H H ":. +%c*+s   B B$#B$c                     | j                   S )z812-byte binary representation of this ObjectId.
        r    r#   s    r   rE   zObjectId.binary        yyr   c                     t        j                  d| j                  dd       d   }t        j                  j	                  |t
              S )a	  A :class:`datetime.datetime` instance representing the time of
        generation for this :class:`ObjectId`.

        The :class:`datetime.datetime` is timezone aware, and
        represents the generation time in UTC. It is precise to the
        second.
        r&   r   r=   )r+   unpackr    datetimefromtimestampr
   )r#   r0   s     r   r/   zObjectId.generation_time   s<     MM$		!A7:	  ..y#>>r   c                     | j                   S )zdreturn value of object for pickling.
        needed explicitly because __slots__() defined.
        rK   rL   s    r   __getstate__zObjectId.__getstate__   rM   r   c                     t        |t              r|d   }n|}t        r't        |t              r|j	                  d      | _        y|| _        y)z)explicit state set from pickling
        r    zlatin-1N)r   dictr   r	   encoder    )r#   valuer   s      r   __setstate__zObjectId.__setstate__   sB    
 eT"(CC :c9-

9-DIDIr   c                     t         r-t        j                  | j                        j	                         S t        j                  | j                        S N)r   binasciihexlifyr    decoderL   s    r   __str__zObjectId.__str__  s8    ##DII.5577		**r   c                      dt        |       dS )Nz
ObjectId('z'))strrL   s    r   __repr__zObjectId.__repr__  s    #&t9..r   c                 `    t        |t              r| j                  |j                  k(  S t        S rZ   r   r   r    rE   NotImplementedr#   others     r   __eq__zObjectId.__eq__  %    eX&99,,r   c                 `    t        |t              r| j                  |j                  k7  S t        S rZ   rc   re   s     r   __ne__zObjectId.__ne__  rh   r   c                 `    t        |t              r| j                  |j                  k  S t        S rZ   rc   re   s     r   __lt__zObjectId.__lt__  %    eX&99u||++r   c                 `    t        |t              r| j                  |j                  k  S t        S rZ   rc   re   s     r   __le__zObjectId.__le__  rh   r   c                 `    t        |t              r| j                  |j                  kD  S t        S rZ   rc   re   s     r   __gt__zObjectId.__gt__  rm   r   c                 `    t        |t              r| j                  |j                  k\  S t        S rZ   rc   re   s     r   __ge__zObjectId.__ge__$  rh   r   c                 ,    t        | j                        S )z,Get a hash value for this :class:`ObjectId`.)hashr    rL   s    r   __hash__zObjectId.__hash__)  s    DIIr   rZ   )'rG   
__module____qualname____doc__r   r6   r7   r   randintrA   r@   	threadingLockr?   r   r8   	__slots___type_markerr$   classmethodr1   r4   r:   r   r!   propertyrE   r/   rS   rX   r^   ra   rg   rj   rl   ro   rq   rs   rv   r   r   r   r   r   0   s     299;D>!!!%78D	 IHIL/!b    D  "  "H4  
 	? 	?"+
/





r   r   )ry   r[   r(   rP   r   r+   r{   r>   randomr   bson.errorsr   bson.py3compatr   r   r   r	   bson.tz_utilr
   rA   r   r   objectr   r   r   r   <module>r      sP       	     ! F F   /
{v {r   