
    FCf#                        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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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	InvalidId)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    M/var/www/highfloat_scraper/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                  d   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dd
Z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                    || j                          yt        |t              rt        |      dk(  r|| _        y| j                  |       y)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   )_ObjectId__generate
isinstancebyteslen_ObjectId__id_ObjectId__validateselfr   s     r   __init__zObjectId.__init__<   s;    J ;OOU#CBDIOOC r   c                    |j                         }|||z
  }t        j                  |j                               }t	        j
                  dt        |            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.
        >Is           )	utcoffsetcalendartimegm	timetuplestructpackint)clsgeneration_timeoffset	timestampr   s        r   from_datetimezObjectId.from_datetimeh   s\    6 !**,-6OOOO$=$=$?@	kk$I/2UU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   	TypeError)r0   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)r0   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)z'Generate 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                @   t        |t              r|j                  | _        yt        |t              r6t        |      dk(  r	 t        j                  |      | _        yt        |       yt        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:`str`,
        :class:`bytes`, or ObjectId. Raises InvalidId if it is not a
        valid ObjectId.

        :param oid: a valid ObjectId
           z6id must be an instance of (bytes, str, ObjectId), not N)r   r   binaryr"   strr!   r    fromhexr6   
ValueErrorr   typer$   s     r   
__validatezObjectId.__validate   s     c8$

DIS!3x2~+ %c 2DI "#&TUYZ]U^T_`aa ":. +%c*+s   B   BBc                    | j                   S )z/12-byte binary representation of this ObjectId.r"   r%   s    r   rH   zObjectId.binary   s     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%   r3   s     r   r1   zObjectId.generation_time   s<     MM$		!A7:	  ..y#>>r   c                    | j                   S )zdReturn value of object for pickling.
        needed explicitly because __slots__() defined.
        rO   rP   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   dictrI   encoder"   )r%   valuer   s      r   __setstate__zObjectId.__setstate__   s@     eT"(CC c3

9-DIDIr   c                \    t        j                  | j                        j                         S N)binasciihexlifyr"   decoderP   s    r   __str__zObjectId.__str__   s     		*1133r   c                    d| dS )Nz
ObjectId('z')r   rP   s    r   __repr__zObjectId.__repr__   s    D82&&r   c                `    t        |t              r| j                  |j                  k(  S t        S r]   r   r   r"   rH   NotImplementedr%   others     r   __eq__zObjectId.__eq__   %    eX&99,,r   c                `    t        |t              r| j                  |j                  k7  S t        S r]   re   rg   s     r   __ne__zObjectId.__ne__   rj   r   c                `    t        |t              r| j                  |j                  k  S t        S r]   re   rg   s     r   __lt__zObjectId.__lt__   %    eX&99u||++r   c                `    t        |t              r| j                  |j                  k  S t        S r]   re   rg   s     r   __le__zObjectId.__le__  rj   r   c                `    t        |t              r| j                  |j                  kD  S t        S r]   re   rg   s     r   __gt__zObjectId.__gt__
  ro   r   c                `    t        |t              r| j                  |j                  k\  S t        S r]   re   rg   s     r   __ge__zObjectId.__ge__  rj   r   c                ,    t        | j                        S )z,Get a hash value for this :class:`ObjectId`.)hashr"   rP   s    r   __hash__zObjectId.__hash__  s    DIIr   r]   )r   z%Optional[Union[str, ObjectId, bytes]]returnNone)r0   Type[ObjectId]r1   datetime.datetimery   r   )r0   r{   r   r   ry   boolry   r    )ry   rz   )r   r   ry   rz   )ry   r|   )rZ   r   ry   rz   )ry   rI   )rh   r   ry   r}   )ry   r/   )'__name__
__module____qualname____doc__r   r9   r:   r   randintrD   rC   	threadingLockrB   r   r;   	__slots___type_markerr&   classmethodr4   r7   r=   r   r#   propertyrH   r1   rV   r[   ra   rc   ri   rl   rn   rq   rs   ru   rx   r   r   r   r   r   .   s    299;D>!!!%78D	 IHIL*!X  B     b,   	? 	? 4'





r   r   )r   rI   ry   r   r~   )r   
__future__r   r^   r*   rS   r   r-   r   rA   randomr   typingr   r   r   r   r	   bson.errorsr   bson.tz_utilr   rD   r   r   r   r   r   r   <module>r      sM    0 "    	     7 7 !  
h hr   