o
    h'                     @   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Zddl	Z	ddl
mZmZmZmZ ddlmZ er?dd ZneZefddZd	d
 ZG dd de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)PY3bytes_from_hexstring_type	text_type)utcc                 C   s   | S N )xr   r   @/var/www/html/venv/lib/python3.10/site-packages/bson/objectid.py<lambda>$   s    r   c                 C   s>   d}d}d}| D ]}|||A }|| | }q|d? |d@ A S )zFNV-1a 24 bit hashl        i l   9     r   )data_ordZ	hash_sizeZfnv_32_primeZfnv_1a_hasheltr   r   r
   
_fnv_1a_24(   s   r   c                   C   s    t dtt  dd S )z,Get the machine portion of an ObjectId.
    z<IN   )structpackr   socketgethostnameencoder   r   r   r
   _machine_bytes5   s    r   c                   @   s   e Zd ZdZdS )	InvalidIdz@Raised when trying to create an ObjectId from invalid data.
    N)__name__
__module____qualname____doc__r   r   r   r
   r   >   s    r   c                 C   s   t d|  )NzS%r is not a valid ObjectId, it must be a 12-byte input or a 24-character hex string)r   )oidr   r   r
   _raise_invalid_idB   s
   r   c                   @   s   e Zd ZdZeddZe Z	e
 Z
dZdZd+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   r   Z__id   Nc                 C   sB   |du r
|    dS t|trt|dkr|| _dS | | dS )aE  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 3-byte machine identifier,
          - a 2-byte process id, and
          - 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
        N   )_ObjectId__generate
isinstancebyteslen_ObjectId__id_ObjectId__validateselfr   r   r   r
   __init__U   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	timetupler   r   int)clsgeneration_time	timestampr   r   r   r
   from_datetime   s   zObjectId.from_datetimec              	   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.

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

        .. versionadded:: 2.3
        FT)r    r   	TypeError)r2   r   r   r   r
   is_valid   s   	zObjectId.is_validc                 C   s   t dtt }|tj7 }|t dt d 7 }tj |t dtj	dd 7 }tj	d d t_	W d   n1 s>w   Y  || _
dS )z0Generate a new value for this ObjectId.
        r,   z>Hi        r   N)r   r   r1   timer    r   osgetpid	_inc_lock_incr'   r)   r   r   r
   Z
__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
        r   z7id must be an instance of (bytes, %s, ObjectId), not %sN)r$   r    binaryr'   r   r&   r   r6   
ValueErrorr   r   r   typer)   r   r   r
   Z
__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   r9   )r   unpackr'   datetimefromtimestampr   )r*   r4   r   r   r
   r3      s   	zObjectId.generation_timec                 C   rB   )zdreturn value of object for pickling.
        needed explicitly because __slots__() defined.
        rC   rD   r   r   r
   __getstate__   rE   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   r   r'   )r*   valuer   r   r   r
   __setstate__   s   


zObjectId.__setstate__c                 C   s    t r
t| j S t| jS r   )r   binasciihexlifyr'   decoderD   r   r   r
   __str__  s   zObjectId.__str__c                 C   s   dt | f S )NzObjectId('%s'))strrD   r   r   r
   __repr__  s   zObjectId.__repr__c                 C   s   t |tr| j|jkS tS r   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 r   rS   rU   r   r   r
   __ne__  rX   zObjectId.__ne__c                 C   s   t |tr| j|jk S tS r   rS   rU   r   r   r
   __lt__  rX   zObjectId.__lt__c                 C   s   t |tr| j|jkS tS r   rS   rU   r   r   r
   __le__$  rX   zObjectId.__le__c                 C   s   t |tr| j|jkS tS r   rS   rU   r   r   r
   __gt__)  rX   zObjectId.__gt__c                 C   s   t |tr| j|jkS tS r   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'   rD   r   r   r
   __hash__3  s   
zObjectId.__hash__r   )!r   r   r   r   randomrandintr>   	threadingLockr=   r   	__slots__Z_type_markerr+   classmethodr5   r7   r#   r(   propertyr?   r3   rI   rL   rP   rR   rW   rY   rZ   r[   r\   r]   r_   r   r   r   r
   r    H   s:    
,
"


r    )r   rM   r.   rG   r;   r`   r   r   rb   r:   Zbson.py3compatr   r   r   r   Zbson.tz_utilr   r   ordr   r   r@   r   r   objectr    r   r   r   r
   <module>   s(   
	