o
    C©Ì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   ú\/var/www/html/Moveonwheels/move_on_wheels/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 )z€Checks 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(   

