
    _i$                    >   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j        d
          j        Z ej        d          j        Z ej        d
          j        ZddZddZ G 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                &    t          d| z            )NzS%r is not a valid ObjectId, it must be a 12-byte input or a 24-character hex stringr   )r   s    0D:\Nbitz\venv\Lib\site-packages\bson/objectid.py_raise_invalid_idr   %   s!    
	(*-	.      bytesc                 *    t          j        d          S )z+Get the 5-byte random field of an ObjectId.   )osurandom r   r   _random_bytesr   ,   s    :a==r   c                     e Zd ZdZ ej                    Z e                                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d0dZd1dZd1dZd2d Z d2d!Z!d2d"Z"d2d#Z#d2d$Z$d2d%Z%d3d'Z&dS )4ObjectIdzA MongoDB ObjectId.r   )__id   Nr   %Optional[Union[str, ObjectId, bytes]]r   Nonec                    ||                                   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   s     r   __init__zObjectId.__init__?   sc    J ;OOU## 	!CBDIIIOOC     r   clsType[ObjectId]generation_timedatetime.datetimec                \    t          t          |          dz            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.
        i  s           )	_PACK_INTr
   )r,   r.   r   s      r   from_datetimezObjectId.from_datetimek   s9    8 )/::dBCC12 	 s3xxr   r   boolc                ^    |sdS 	 t          |           dS # t          t          f$ r Y dS 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)r,   r   s     r   is_validzObjectId.is_valid   sM      	5	SMMM49% 	 	 	55	s    ,,r   c                    t          j                    }|| j        k    r|| _        t                      | _        | j        S )z1Generate a 5-byte random number once per process.)r   getpid_pidr   _ObjectId__random)r,   pids     r   _randomzObjectId._random   s4     ikk#(??CH(??CL|r   c                ^   t           j        5  t           j        }|dz   t          dz   z  t           _        ddd           n# 1 swxY w Y   t	          t          t          j                              t                                                     t          |          dd         z   | _	        dS )z'Generate a new value for this ObjectId.   N   )
r   	_inc_lock_inc_MAX_COUNTER_VALUE_PACK_INT_RANDOMinttimer<   r1   r'   )r*   incs     r   
__generatezObjectId.__generate   s     	A 	A-C 1W);a)?@HM	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A
 %S%5%5x7G7G7I7IJJYWZ^^\]^_\_M``			s   'A  AAc                   t          |t                    r|j        | _        dS t          |t                    rmt          |          dk    rI	 t                              |          | _        dS # t          t          f$ r t          |           Y dS w xY 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   fromhexr5   
ValueErrorr   typer)   s     r   
__validatezObjectId.__validate   s     c8$$ 	b
DIIIS!! 		b3xx2~~+ %c 2 2DIII!:. + + +%c******+ "#&&&&&`UYZ]U^U^``aaas   A. . BBc                    | j         S )z/12-byte binary representation of this ObjectId.r'   r*   s    r   rJ   zObjectId.binary   s     yr   c                    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   )r*   	timestamps     r   r.   zObjectId.generation_time   s9      	!A#//2	 ..y#>>>r   c                    | j         S )zdReturn value of object for pickling.
        needed explicitly because __slots__() defined.
        rP   rQ   s    r   __getstate__zObjectId.__getstate__   s     yr   valuec                    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*   rY   r   s      r   __setstate__zObjectId.__setstate__   s]     eT"" 	(CCC c3 	

9--DIIIDIIIr   r   c                X    t          j        | j                                                  S N)binasciihexlifyr'   decoderQ   s    r   __str__zObjectId.__str__   s!    	**11333r   c                    d| dS )Nz
ObjectId('z')r   rQ   s    r   __repr__zObjectId.__repr__   s    &D&&&&r   otherc                Z    t          |t                    r| j        |j        k    S t          S r_   r%   r   r'   rJ   NotImplementedr*   rf   s     r   __eq__zObjectId.__eq__   )    eX&& 	-9,,r   c                Z    t          |t                    r| j        |j        k    S t          S r_   rh   rj   s     r   __ne__zObjectId.__ne__   rl   r   c                Z    t          |t                    r| j        |j        k     S t          S r_   rh   rj   s     r   __lt__zObjectId.__lt__   )    eX&& 	,9u|++r   c                Z    t          |t                    r| j        |j        k    S t          S r_   rh   rj   s     r   __le__zObjectId.__le__  rl   r   c                Z    t          |t                    r| j        |j        k    S t          S r_   rh   rj   s     r   __gt__zObjectId.__gt__  rq   r   c                Z    t          |t                    r| j        |j        k    S t          S r_   rh   rj   s     r   __ge__zObjectId.__ge__  rl   r   rD   c                *    t          | j                  S )z,Get a hash value for this :class:`ObjectId`.)hashr'   rQ   s    r   __hash__zObjectId.__hash__  s    DIr   r_   )r   r    r   r!   )r,   r-   r.   r/   r   r   )r,   r-   r   r   r   r3   r   r   )r   r!   )r   r   r   r!   )r   r/   )rY   r   r   r!   )r   r   )rf   r   r   r3   )r   rD   )'__name__
__module____qualname____doc__r   r8   r9   r   randintrB   rA   	threadingLockr@   r   r:   	__slots___type_markerr+   classmethodr2   r6   r<   r$   r(   propertyrJ   r.   rX   r]   rc   re   rk   rn   rp   rs   ru   rw   rz   r   r   r   r   r   1   s.       29;;D<>>!!!%788D	  I}HIL*! *! *! *! *!X    [@    [     [a a a ab b b b,    X 	? 	? 	? X	?       4 4 4 4' ' ' '   
   
   
   
   
   
     r   r   )r   r   r   r   r{   )!r   
__future__r   r`   rT   r   structr   rE   randomr   typingr   r   r   r   r	   bson.datetime_msr
   bson.errorsr   bson.tz_utilr   rB   Structpackr1   rC   unpackrS   r   r   r   r   r   r   <module>r      sq   0 / " " " " " "   				             7 7 7 7 7 7 7 7 7 7 7 7 7 7 0 0 0 0 0 0 ! ! ! ! ! !       FM$$	 6=((- fmD!!(      
a a a a a a a a a ar   