o
    Åîï]¡$  ã                   @   s    d 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 ddlmZmZmZmZ ddlmZ dZdd	„ Zd
d„ ZG dd„ deƒZdS )zXTools for working with MongoDB `ObjectIds
<http://dochub.mongodb.org/core/objectids>`_.
é    N)ÚSystemRandom©Ú	InvalidId)ÚPY3Úbytes_from_hexÚstring_typeÚ	text_type)Úutciÿÿÿ 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   )Úoid© r   ú?/var/www/html/env/lib/python3.10/site-packages/bson/objectid.pyÚ_raise_invalid_id%   s
   ÿÿr   c                   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„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&dS )-ÚObjectIdzA MongoDB ObjectId.
    r   )Ú__idé   Nc                 C   sB   |du r
|   ¡  dS t|tƒrt|ƒdkr|| _dS |  |¡ dS )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` or, in Python 2, any 12-character :class:`str`.

        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:`unicode` or :class:`str` of 24 hex digits::

          >>> ObjectId('0123456789ab0123456789ab')
          ObjectId('0123456789ab0123456789ab')
          >>>
          >>> # A u-prefixed unicode literal:
          >>> ObjectId(u'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.

        :Parameters:
          - `oid` (optional): a valid ObjectId.

        .. mongodoc:: 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
   r   r   r   Ú__init__?   s
   *
zObjectId.__init__c                 C   sB   |  ¡ dur||  ¡  }t | ¡ ¡}t dt|ƒ¡d }| |ƒS )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}})

        :Parameters:
          - `generation_time`: :class:`~datetime.datetime` to be used
            as the generation time for the resulting ObjectId.
        Nú>Is           )Ú	utcoffsetÚcalendarÚtimegmÚ	timetupleÚstructÚpackÚint)ÚclsÚgeneration_timeÚ	timestampr
   r   r   r   Úfrom_datetimep   s   ÿÿzObjectId.from_datetimec              	   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.

        :Parameters:
          - `oid`: the object id to validate

        .. versionadded:: 2.3
        FT)r   r   Ú	TypeError)r(   r
   r   r   r   Úis_valid“   s   	ÿzObjectId.is_validc                 C   s&   t  ¡ }|| jkr|| _tƒ | _| jS )z:Generate a 5-byte random number once per process.
        )r   ÚgetpidÚ_pidr   Ú_ObjectId__random)r(   Úpidr   r   r   Ú_random¥   s
   
zObjectId._randomc                 C   s~   t  dtt ¡ ƒ¡}|t ¡ 7 }tj� |t  dtj¡dd… 7 }tjd td  t_W d  ƒ n1 s5w   Y  || _	dS )z0Generate a new value for this ObjectId.
        r    é   é   N)
r%   r&   r'   Útimer   r2   Ú	_inc_lockÚ_incÚ_MAX_COUNTER_VALUEr   r   r   r   r   Ú
__generate¯   s   þ
zObjectId.__generatec              	   C   s€   t |tƒr|j| _dS t |tƒr5t|ƒdkr/zt|ƒ| _W dS  ttfy.   t	|ƒ Y dS w t	|ƒ dS tdt
jt|ƒf ƒ‚)a;  Validate and use the given id for this ObjectId.

        Raises TypeError if id is not an instance of
        (:class:`basestring` (:class:`str` or :class:`bytes`
        in python 3), ObjectId) and InvalidId if it is not a
        valid ObjectId.

        :Parameters:
          - `oid`: a valid ObjectId
        é   z7id must be an instance of (bytes, %s, ObjectId), not %sN)r   r   Úbinaryr   r   r   r   r,   Ú
ValueErrorr   r   Ú__name__Útyper   r   r   r   Ú
__validateÀ   s   

ÿÿzObjectId.__validatec                 C   ó   | j S )z812-byte binary representation of this ObjectId.
        ©r   ©r   r   r   r   r;   Ú   ó   zObjectId.binaryc                 C   s(   t  d| 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   r4   )r%   Úunpackr   ÚdatetimeÚfromtimestampr	   )r   r*   r   r   r   r)   à   s   	zObjectId.generation_timec                 C   r@   )zdreturn value of object for pickling.
        needed explicitly because __slots__() defined.
        rA   rB   r   r   r   Ú__getstate__ì   rC   zObjectId.__getstate__c                 C   s@   t |tƒr
|d }n|}trt |tƒr| d¡| _dS || _dS )z)explicit state set from pickling
        r   zlatin-1N)r   Údictr   r   Úencoder   )r   Úvaluer
   r   r   r   Ú__setstate__ò   s   


zObjectId.__setstate__c                 C   s    t r
t | j¡ ¡ S t | j¡S ©N)r   ÚbinasciiÚhexlifyr   ÚdecoderB   r   r   r   Ú__str__  s   zObjectId.__str__c                 C   s   dt | ƒf S )NzObjectId('%s'))ÚstrrB   r   r   r   Ú__repr__  s   zObjectId.__repr__c                 C   s   t |tƒr| j|jkS tS rL   ©r   r   r   r;   ÚNotImplemented©r   Úotherr   r   r   Ú__eq__  ó   
zObjectId.__eq__c                 C   s   t |tƒr| j|jkS tS rL   rS   rU   r   r   r   Ú__ne__  rX   zObjectId.__ne__c                 C   s   t |tƒr| j|jk S tS rL   rS   rU   r   r   r   Ú__lt__  rX   zObjectId.__lt__c                 C   s   t |tƒr| j|jkS tS rL   rS   rU   r   r   r   Ú__le__  rX   zObjectId.__le__c                 C   s   t |tƒr| j|jkS tS rL   rS   rU   r   r   r   Ú__gt__  rX   zObjectId.__gt__c                 C   s   t |tƒr| j|jkS tS rL   rS   rU   r   r   r   Ú__ge__$  rX   zObjectId.__ge__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=   Ú
__module__Ú__qualname__Ú__doc__r   r.   r/   r   Úrandintr8   r7   Ú	threadingÚLockr6   r   r0   Ú	__slots__Ú_type_markerr   Úclassmethodr+   r-   r2   r   r   Úpropertyr;   r)   rG   rK   rP   rR   rW   rY   rZ   r[   r\   r]   r_   r   r   r   r   r   0   s@    
1
"

	

r   )rb   rM   r"   rE   r   r%   rd   r5   Úrandomr   Úbson.errorsr   Úbson.py3compatr   r   r   r   Úbson.tz_utilr	   r8   r   r   Úobjectr   r   r   r   r   Ú<module>   s    