3
O6b$                 @   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|  d S )NzS%r is not a valid ObjectId, it must be a 12-byte input or a 24-character hex string)r   )oid r
   L/var/www/html/sandeepIITI/myenv/lib/python3.6/site-packages/bson/objectid.py_raise_invalid_id%   s    r   c               C   s
   t jdS )z+Get the 5-byte random field of an ObjectId.   )osurandomr
   r
   r
   r   _random_bytes+   s    r   c               @   s   e Zd ZdZej Ze j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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   s>   |dkr| j   n(t|tr0t|dkr0|| _n
| j| 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.

        .. seealso:: The MongoDB documentation on `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   |j  dk	r||j   }tj|j }tj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.
        Nz>Is           )	utcoffsetcalendartimegm	timetuplestructpackint)clsgeneration_time	timestampr	   r
   r
   r   from_datetimep   s    zObjectId.from_datetimec             C   s2   |sdS yt | dS  ttfk
r,   dS X dS )zChecks if a `oid` string is valid or not.

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

        .. versionadded:: 2.3
        FTN)r   r   	TypeError)r$   r	   r
   r
   r   is_valid   s    	zObjectId.is_validc             C   s&   t j }|| 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   sj   t jdttj }|tj 7 }tj4 |t jdtjdd 7 }tjd td  t_W dQ R X || _	dS )z0Generate a new value for this ObjectId.
        z>I      N)
r!   r"   r#   timer   r.   	_inc_lock_inc_MAX_COUNTER_VALUEr   )r   r	   r
   r
   r   Z
__generate   s    zObjectId.__generatec             C   s   t |tr|j| _nht |trft|dkr\yt|| _W qd ttfk
rX   t	| Y qdX q|t	| ntdt
jt|f dS )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__type)r   r	   r
   r
   r   Z
__validate   s    



zObjectId.__validatec             C   s   | j S )z812-byte binary representation of this ObjectId.
        )r   )r   r
   r
   r   r6      s    zObjectId.binaryc             C   s(   t jd| jdd d }tj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.
        z>Ir   r0   )r!   unpackr   datetimefromtimestampr   )r   r&   r
   r
   r   r%      s    	zObjectId.generation_timec             C   s   | j S )zdreturn value of object for pickling.
        needed explicitly because __slots__() defined.
        )r   )r   r
   r
   r   __getstate__   s    zObjectId.__getstate__c             C   s>   t |tr|d }n|}tr4t |tr4|jd| _n|| _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| jj S tj| jS )N)r   binasciihexlifyr   decode)r   r
   r
   r   __str__  s    zObjectId.__str__c             C   s   dt | f S )NzObjectId('%s'))str)r   r
   r
   r   __repr__  s    zObjectId.__repr__c             C   s   t |tr| j|jkS tS )N)r   r   r   r6   NotImplemented)r   otherr
   r
   r   __eq__  s    
zObjectId.__eq__c             C   s   t |tr| j|jkS tS )N)r   r   r   r6   rH   )r   rI   r
   r
   r   __ne__  s    
zObjectId.__ne__c             C   s   t |tr| j|jk S tS )N)r   r   r   r6   rH   )r   rI   r
   r
   r   __lt__  s    
zObjectId.__lt__c             C   s   t |tr| j|jkS tS )N)r   r   r   r6   rH   )r   rI   r
   r
   r   __le__  s    
zObjectId.__le__c             C   s   t |tr| j|jkS tS )N)r   r   r   r6   rH   )r   rI   r
   r
   r   __gt__  s    
zObjectId.__gt__c             C   s   t |tr| j|jkS tS )N)r   r   r   r6   rH   )r   rI   r
   r
   r   __ge__$  s    
zObjectId.__ge__c             C   s
   t | jS )z,Get a hash value for this :class:`ObjectId`.)hashr   )r   r
   r
   r   __hash__)  s    zObjectId.__hash__)r   )N)'r8   
__module____qualname____doc__r   r*   r+   r   randintr4   r3   	threadingLockr2   r   r,   	__slots__Z_type_markerr   classmethodr'   r)   r.   r   r   propertyr6   r%   r=   rA   rE   rG   rJ   rK   rL   rM   rN   rO   rQ   r
   r
   r
   r   r   0   s4   
1#
r   )rT   rB   r   r;   r   r!   rV   r1   randomr   Zbson.errorsr   Zbson.py3compatr   r   r   r   Zbson.tz_utilr   r4   r   r   objectr   r
   r
   r
   r   <module>   s   