
    ]*i!                    X   d Z ddlm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mZmZmZmZ ddlmZ ddlmZ ddlmZ d	Z ej.                  d
      j0                  Z ej.                  d      j0                  Z ej.                  d
      j6                  ZddZddZ G d d      Zy)z)Tools for working with MongoDB ObjectIds.    )annotationsN)SystemRandom)AnyNoReturnOptionalTypeUnion)_datetime_to_millis	InvalidId)utci z>Iz>I5sc                    t        d| z        )NzS%r is not a valid ObjectId, it must be a 12-byte input or a 24-character hex stringr   )oids    G/home/alumno/antonio/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                  T   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dZedd       Zed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 dZ$y)!ObjectIdzA MongoDB ObjectId.r   )__id   Nc                   |t         j                  5  t         j                  }|dz   t        dz   z  t         _        ddd       t	        t        t        j                               t         j                               t              dd z   | _	        yt        |t              rt        |      dk(  r|| _	        yt        |t              r6t        |      dk(  r	 t        j                  |      | _	        yt#        |       yt        |t               r|j$                  | _	        yt        dt'        |             # 1 sw Y   xY w# t        t         f$ r t#        |       Y yw xY w)aZ  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`.

        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:`str` of 24 hex digits::

          >>> ObjectId('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.

        :param oid: a valid ObjectId.

        .. seealso:: The MongoDB documentation on  `ObjectIds <http://dochub.mongodb.org/core/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            z6id must be an instance of (bytes, str, ObjectId), not )r   	_inc_lock_inc_MAX_COUNTER_VALUE_PACK_INT_RANDOMinttime_random	_PACK_INT_ObjectId__id
isinstancebyteslenstrfromhex	TypeError
ValueErrorr   binarytype)selfr   incs      r   __init__zObjectId.__init__>   s    J ;## Emm!$q-?!-C DE
 )TYY[)98;K;K;MNQZ[^Q_`abcQddDIU#CBDIS!3x2~+ %c 2DI "#&X&

DITUYZ]U^T_`aa'E E ":. +%c*+s   )D7E 7E E E c                F    t        t        |      dz        dz   } | |      S )aJ  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}})

        :param generation_time: :class:`~datetime.datetime` to be used
            as the generation time for the resulting ObjectId.
        i  s           )r)   r
   )clsgeneration_timer   s      r   from_datetimezObjectId.from_datetimez   s/    8 )/:dBC12 	 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.

        :param oid: the object id to validate

        .. versionadded:: 2.3
        FT)r   r   r0   )r8   r   s     r   is_validzObjectId.is_valid   s0     	SM9% 		s    ##c                    t        j                         }|| j                  k7  r|| _        t               | _        | j                  S )z1Generate a 5-byte random number once per process.)r   getpid_pidr   _ObjectId__random)r8   pids     r   r(   zObjectId._random   s4     iik#((?CH(?CL||r   c                    | j                   S )z/12-byte binary representation of this ObjectId.r*   r4   s    r   r2   zObjectId.binary   s     yyr   c                    t        | 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   )_UNPACK_INTr*   datetimefromtimestampr   )r4   	timestamps     r   r9   zObjectId.generation_time   s6      		!A/2	  ..y#>>r   c                    | j                   S )zdReturn value of object for pickling.
        needed explicitly because __slots__() defined.
        rC   rD   s    r   __getstate__zObjectId.__getstate__   s     yyr   c                    t        |t              r|d   }n|}t        |t              r|j                  d      | _        y|| _        y)z Explicit state set from picklingr*   zlatin-1N)r+   dictr.   encoder*   )r4   valuer   s      r   __setstate__zObjectId.__setstate__   s@     eT"(CC c3

9-DIDIr   c                6    | j                   j                         S N)r*   hexrD   s    r   __str__zObjectId.__str__   s    yy}}r   c                    d| dS )Nz
ObjectId('z')r   rD   s    r   __repr__zObjectId.__repr__   s    D82&&r   c                `    t        |t              r| j                  |j                  k(  S t        S rR   r+   r   r*   r2   NotImplementedr4   others     r   __eq__zObjectId.__eq__   %    eX&99,,r   c                `    t        |t              r| j                  |j                  k7  S t        S rR   rX   rZ   s     r   __ne__zObjectId.__ne__   r]   r   c                `    t        |t              r| j                  |j                  k  S t        S rR   rX   rZ   s     r   __lt__zObjectId.__lt__   %    eX&99u||++r   c                `    t        |t              r| j                  |j                  k  S t        S rR   rX   rZ   s     r   __le__zObjectId.__le__   r]   r   c                `    t        |t              r| j                  |j                  kD  S t        S rR   rX   rZ   s     r   __gt__zObjectId.__gt__   rb   r   c                `    t        |t              r| j                  |j                  k\  S t        S rR   rX   rZ   s     r   __ge__zObjectId.__ge__   r]   r   c                ,    t        | j                        S )z,Get a hash value for this :class:`ObjectId`.)hashr*   rD   s    r   __hash__zObjectId.__hash__   s    DIIr   rR   )r   z%Optional[Union[str, ObjectId, bytes]]returnNone)r8   Type[ObjectId]r9   datetime.datetimerl   r   )r8   rn   r   r   rl   boolrl   r,   )rl   ro   )rO   r   rl   rm   )rl   r.   )r[   r   rl   rp   )rl   r&   )%__name__
__module____qualname____doc__r   r>   r?   r   randintr$   r#   	threadingLockr"   r   r@   	__slots___type_markerr6   classmethodr:   r<   r(   propertyr2   r9   rK   rP   rT   rV   r\   r_   ra   rd   rf   rh   rk   r   r   r   r   r   0   s    299;D>!!!%78D	 IHIL:bx  @        	? 	? '





r   r   )r   r.   rl   r   rq   ) ru   
__future__r   rG   r   structrw   r'   randomr   typingr   r   r   r   r	   bson.datetime_msr
   bson.errorsr   bson.tz_utilr   r$   Structpackr)   r%   unpackrF   r   r   r   r   r   r   <module>r      s    0 "  	     7 7 0 !  FMM$$$	 6==(-- fmmD!((
R Rr   