
    i!                    \   S r SSKJr  SSKrSSKrSSKrSSKrSSKrSSKJ	r	  SSK
JrJrJrJrJr  SSKJr  SSKJr  SSKJr  S	r\R.                  " S
5      R0                  r\R.                  " S5      R0                  r\R.                  " S
5      R6                  rSS jrSS jr " S S5      rg)z)Tools for working with MongoDB ObjectIds.    )annotationsN)SystemRandom)AnyNoReturnOptionalTypeUnion)_datetime_to_millis	InvalidId)utci z>Iz>I5sc                    [        SU -  5      e)NzS%r is not a valid ObjectId, it must be a 12-byte input or a 24-character hex stringr   )oids    :/app/mltbenv/lib/python3.13/site-packages/bson/objectid.py_raise_invalid_idr   $   s    
	(*-	.     c                 .    [         R                  " S5      $ )z+Get the 5-byte random field of an ObjectId.   )osurandom r   r   _random_bytesr   +   s    ::a=r   c                     \ rS rSrSr\R                  " 5       r\" 5       R                  S\
5      r\R                  " 5       r\" 5       rSrSrSSS jjr\SS j5       r\SS	 j5       r\SS
 j5       r\SS j5       r\SS j5       rSS jrSS jrSS jrSS jrS S jrS S jr S S jr!S S jr"S S jr#S S jr$S!S jr%Sr&g)"ObjectId0   zA MongoDB ObjectId.r   )__id   Nc                   Uc  [         R                     [         R                  nUS-   [        S-   -  [         l        SSS5        [	        [        [        R                  " 5       5      [         R                  5       5      [        W5      SS -   U l	        g[        U[        5      (       a  [        U5      S:X  a  Xl	        g[        U[        5      (       a7  [        U5      S:X  a   [        R                  U5      U l	        g[#        U5        g[        U[         5      (       a  UR$                  U l	        g[        S['        U5       35      e! , (       d  f       GN= f! [        [         4 a    [#        U5         gf = f)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`.

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__ObjectId.__init__>   s   J ;##mm!$q-?!-C D $
 )TYY[)98;K;K;MNQZ[^Q_`abcQddDIU##CBIS!!3x2~+ %c 2DI "#&X&&

DITUYZ]U^T_`aa' $# ":. +%c*+s   )E	$E 	
EE98E9c                F    [        [        U5      S-  5      S-   nU " U5      $ )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}})

: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_datetimeObjectId.from_datetimez   s/    8 )/:dBC12 	 3xr   c                X    U(       d  g [        U5        g! [        [        4 a     gf = f)zhChecks if a `oid` string is valid or not.

:param oid: the object id to validate

.. versionadded:: 2.3
FT)r   r   r1   )r:   r   s     r   is_validObjectId.is_valid   s0     	SM9% 		s    ))c                    [         R                  " 5       nXR                  :w  a  Xl        [        5       U l        U R                  $ )z1Generate a 5-byte random number once per process.)r   getpid_pidr   _ObjectId__random)r:   pids     r   r)   ObjectId._random   s0     iik((?H(?CL||r   c                    U R                   $ )z/12-byte binary representation of this ObjectId.r+   r5   s    r   r3   ObjectId.binary   s     yyr   c                    [        U R                  SS 5      S   n[        R                  R                  U[        5      $ )zA :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   )r5   	timestamps     r   r;   ObjectId.generation_time   s6      		!A/2	  ..y#>>r   c                    U R                   $ )zTReturn value of object for pickling.
needed explicitly because __slots__() defined.
rH   rI   s    r   __getstate__ObjectId.__getstate__   s     yyr   c                    [        U[        5      (       a  US   nOUn[        U[        5      (       a  UR                  S5      U l        gX l        g)z Explicit state set from picklingr+   zlatin-1N)r,   dictr/   encoder+   )r5   valuer   s      r   __setstate__ObjectId.__setstate__   sD     eT""(CC c3

9-DIIr   c                6    U R                   R                  5       $ N)r+   hexrI   s    r   __str__ObjectId.__str__   s    yy}}r   c                    SU < S3$ )Nz
ObjectId('z')r   rI   s    r   __repr__ObjectId.__repr__   s    D82&&r   c                j    [        U[        5      (       a  U R                  UR                  :H  $ [        $ r[   r,   r   r+   r3   NotImplementedr5   others     r   __eq__ObjectId.__eq__   (    eX&&99,,r   c                j    [        U[        5      (       a  U R                  UR                  :g  $ [        $ r[   rc   re   s     r   __ne__ObjectId.__ne__   ri   r   c                j    [        U[        5      (       a  U R                  UR                  :  $ [        $ r[   rc   re   s     r   __lt__ObjectId.__lt__   (    eX&&99u||++r   c                j    [        U[        5      (       a  U R                  UR                  :*  $ [        $ r[   rc   re   s     r   __le__ObjectId.__le__   ri   r   c                j    [        U[        5      (       a  U R                  UR                  :  $ [        $ r[   rc   re   s     r   __gt__ObjectId.__gt__   rp   r   c                j    [        U[        5      (       a  U R                  UR                  :  $ [        $ r[   rc   re   s     r   __ge__ObjectId.__ge__   ri   r   c                ,    [        U R                  5      $ )z,Get a hash value for this :class:`ObjectId`.)hashr+   rI   s    r   __hash__ObjectId.__hash__   s    DIIr   r[   )r   z%Optional[Union[str, ObjectId, bytes]]returnNone)r:   Type[ObjectId]r;   datetime.datetimer~   r   )r:   r   r   r   r~   boolr~   r-   )r~   r   )rW   r   r~   r   )r~   r/   )rf   r   r~   r   )r~   r'   )'__name__
__module____qualname____firstlineno____doc__r   rB   rC   r   randintr%   r$   	threadingLockr#   r   rD   	__slots___type_markerr7   classmethodr<   r?   r)   propertyr3   r;   rR   rX   r]   r`   rg   rk   rn   rr   ru   rx   r|   __static_attributes__r   r   r   r   r   0   s    99;D>!!!%78D IHIL:bx  @        	? 	? '





r   r   )r   r/   r~   r   r   ) r   
__future__r   rM   r   structr   r(   randomr   typingr   r   r   r   r	   bson.datetime_msr
   bson.errorsr   bson.tz_utilr   r%   Structpackr*   r&   unpackrL   r   r   r   r   r   r   <module>r      s    0 "  	     7 7 0 !  MM$$$	==(-- mmD!((
R Rr   