o
    sDi!                     @  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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   K/home/alumno/antzosa8961/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d7d8d
dZed9ddZed:ddZed;ddZed;ddZed<ddZd;ddZd=d d!Zd>d#d$Zd>d%d&Zd?d(d)Zd?d*d+Zd?d,d-Z d?d.d/Z!d?d0d1Z"d?d2d3Z#d@d5d6Z$dS )AObjectIdzA MongoDB ObjectId.r   )__id   Nr   %Optional[Union[str, ObjectId, bytes]]r   Nonec              	   C  s  |du r9t j t j}|d td  t _W d   n1 sw   Y  ttt t  t|dd  | _	dS t
|trIt|dkrI|| _	dS t
|trtt|dkrnz	t|| _	W dS  ttfym   t| Y dS w t| dS t
|t r|j| _	dS tdt| )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
isinstancer   lenr   fromhex	TypeError
ValueErrorr   binarytype)selfr   incr   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           )r)   r	   )r5   r7   r   r   r   r   from_datetimez   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   r.   )r5   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)r5   pidr   r   r   r(      s
   
zObjectId._randomc                 C     | j S )z/12-byte binary representation of this ObjectId.r*   r2   r   r   r   r0      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   r   )_UNPACK_INTr*   datetimefromtimestampr   )r2   	timestampr   r   r   r7      s   	zObjectId.generation_timec                 C  r@   )zdReturn value of object for pickling.
        needed explicitly because __slots__() defined.
        rA   rB   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*   )r2   rH   r   r   r   r   __setstate__   s   



zObjectId.__setstate__r   c                 C  s
   | j  S N)r*   hexrB   r   r   r   __str__   s   
zObjectId.__str__c                 C  s   d| dS )Nz
ObjectId('z')r   rB   r   r   r   __repr__   s   zObjectId.__repr__otherc                 C  s   t |tr| j|jkS tS rL   r+   r   r*   r0   NotImplementedr2   rP   r   r   r   __eq__      
zObjectId.__eq__c                 C  s   t |tr| j|jkS tS rL   rQ   rS   r   r   r   __ne__   rU   zObjectId.__ne__c                 C  s   t |tr| j|jk S tS rL   rQ   rS   r   r   r   __lt__   rU   zObjectId.__lt__c                 C  s   t |tr| j|jkS tS rL   rQ   rS   r   r   r   __le__   rU   zObjectId.__le__c                 C  s   t |tr| j|jkS tS rL   rQ   rS   r   r   r   __gt__   rU   zObjectId.__gt__c                 C  s   t |tr| j|jkS tS rL   rQ   rS   r   r   r   __ge__   rU   zObjectId.__ge__r&   c                 C  s
   t | jS )z,Get a hash value for this :class:`ObjectId`.)hashr*   rB   r   r   r   __hash__   r   zObjectId.__hash__rL   )r   r   r   r   )r5   r6   r7   r8   r   r   )r5   r6   r   r   r   r:   r   r   )r   r8   )rH   r   r   r   )r   r   )rP   r   r   r:   )r   r&   )%__name__
__module____qualname____doc__r   r<   r=   r   randintr$   r#   	threadingLockr"   r   r>   	__slots___type_markerr4   classmethodr9   r;   r(   propertyr0   r7   rG   rK   rN   rO   rT   rV   rW   rX   rY   rZ   r\   r   r   r   r   r   0   s<    < 









r   )r   r   r   r   r]   ) ra   
__future__r   rD   r   structrc   r'   randomr   typingr   r   r   r   r   bson.datetime_msr	   bson.errorsr   bson.tz_utilr   r$   Structpackr)   r%   unpackrC   r   r   r   r   r   r   r   <module>   s&   

