o
    'h#                     @  s   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	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d
jZedjZed
jZdddZdddZG dd dZ dS )z)Tools for working with MongoDB ObjectIds.    )annotationsN)SystemRandom)AnyNoReturnOptionalTypeUnion)_datetime_to_millis	InvalidId)utci z>Iz>I5soidstrreturnr   c                 C  s   t d|  )NzS%r is not a valid ObjectId, it must be a 12-byte input or a 24-character hex stringr
   )r    r   I/var/www/html/olx_land/venv/lib/python3.10/site-packages/bson/objectid.py_raise_invalid_id%   s
   r   bytesc                   C  s
   t dS )z+Get the 5-byte random field of an ObjectId.   )osurandomr   r   r   r   _random_bytes,      
r   c                   @  s  e Zd ZdZe Ze de	Z
e Ze ZdZdZd;d<d
dZed=ddZed>ddZed?ddZd@ddZdAddZed?ddZedBdd Zd?d!d"ZdCd$d%ZdDd'd(ZdDd)d*ZdEd,d-Z dEd.d/Z!dEd0d1Z"dEd2d3Z#dEd4d5Z$dEd6d7Z%dFd9d:Z&dS )GObjectIdzA MongoDB ObjectId.r   )__id   Nr   %Optional[Union[str, ObjectId, bytes]]r   Nonec                 C  sB   |du r
|    dS t|trt|dkr|| _dS | | dS )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
isinstancer   len_ObjectId__id_ObjectId__validateselfr   r   r   r   __init__?   s
   %
zObjectId.__init__clsType[ObjectId]generation_timedatetime.datetimec                 C  s   t t|d d }| |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           )	_PACK_INTr	   )r'   r)   r   r   r   r   from_datetimek   s
   zObjectId.from_datetimer   boolc              	   C  s0   |sdS zt | W dS  ttfy   Y dS w )zChecks if a `oid` string is valid or not.

        :param oid: the object id to validate

        .. versionadded:: 2.3
        FT)r   r   	TypeError)r'   r   r   r   r   is_valid   s   zObjectId.is_validr   c                 C  s&   t  }|| jkr|| _t | _| jS )z1Generate a 5-byte random number once per process.)r   getpid_pidr   _ObjectId__random)r'   pidr   r   r   _random   s
   
zObjectId._randomc                 C  sj   t j t j}|d td  t _W d   n1 sw   Y  ttt t  t|dd  | _	dS )z'Generate a new value for this ObjectId.   N   )
r   	_inc_lock_inc_MAX_COUNTER_VALUE_PACK_INT_RANDOMinttimer4   r+   r"   )r%   incr   r   r   
__generate   s
   ,zObjectId.__generatec              	   C  s~   t |tr|j| _dS t |tr6t|dkr0z	t|| _W dS  tt	fy/   t
| Y dS w t
| dS tdt| )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"   r   r!   r   fromhexr.   
ValueErrorr   typer$   r   r   r   
__validate   s   
	
zObjectId.__validatec                 C     | j S )z/12-byte binary representation of this ObjectId.r"   r%   r   r   r   r@      s   zObjectId.binaryc                 C  s$   t | jdd d }t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   r6   )_UNPACK_INTr"   datetimefromtimestampr   )r%   	timestampr   r   r   r)      s   	zObjectId.generation_timec                 C  rE   )zdReturn value of object for pickling.
        needed explicitly because __slots__() defined.
        rF   rG   r   r   r   __getstate__   s   zObjectId.__getstate__valuec                 C  s<   t |tr
|d }n|}t |tr|d| _dS || _dS )z Explicit state set from picklingr"   zlatin-1N)r    dictr   encoder"   )r%   rM   r   r   r   r   __setstate__   s   



zObjectId.__setstate__r   c                 C  s   t | j S N)binasciihexlifyr"   decoderG   r   r   r   __str__   s   zObjectId.__str__c                 C  s   d| dS )Nz
ObjectId('z')r   rG   r   r   r   __repr__   s   zObjectId.__repr__otherc                 C  s   t |tr| j|jkS tS rQ   r    r   r"   r@   NotImplementedr%   rW   r   r   r   __eq__      
zObjectId.__eq__c                 C  s   t |tr| j|jkS tS rQ   rX   rZ   r   r   r   __ne__   r\   zObjectId.__ne__c                 C  s   t |tr| j|jk S tS rQ   rX   rZ   r   r   r   __lt__   r\   zObjectId.__lt__c                 C  s   t |tr| j|jkS tS rQ   rX   rZ   r   r   r   __le__  r\   zObjectId.__le__c                 C  s   t |tr| j|jkS tS rQ   rX   rZ   r   r   r   __gt__  r\   zObjectId.__gt__c                 C  s   t |tr| j|jkS tS rQ   rX   rZ   r   r   r   __ge__  r\   zObjectId.__ge__r;   c                 C  s
   t | jS )z,Get a hash value for this :class:`ObjectId`.)hashr"   rG   r   r   r   __hash__  r   zObjectId.__hash__rQ   )r   r   r   r   )r'   r(   r)   r*   r   r   )r'   r(   r   r   r   r-   r   r   )r   r   )r   r   r   r   )r   r*   )rM   r   r   r   )r   r   )rW   r   r   r-   )r   r;   )'__name__
__module____qualname____doc__r   r0   r1   r   randintr9   r8   	threadingLockr7   r   r2   	__slots___type_markerr&   classmethodr,   r/   r4   r   r#   propertyr@   r)   rL   rP   rU   rV   r[   r]   r^   r_   r`   ra   rc   r   r   r   r   r   1   s@    , 

	









r   )r   r   r   r   rd   )!rh   
__future__r   rR   rI   r   structrj   r<   randomr   typingr   r   r   r   r   bson.datetime_msr	   bson.errorsr   bson.tz_utilr   r9   Structpackr+   r:   unpackrH   r   r   r   r   r   r   r   <module>   s(   

