o SbJ$@sdZddlZddlZddlZddlZddlZddlZddlZddlm Z ddl m Z m Z m Z mZmZddlmZddlmZdZded e fd d Zd efd d ZGdddeZdS)z*Tools for working with MongoDB ObjectIds. N) SystemRandom)AnyNoReturnOptionalTypeUnion InvalidId)utcioidreturncCs td|)NzS%r is not a valid ObjectId, it must be a 12-byte input or a 24-character hex stringr)r r 4/tmp/pip-target-onvjaxws/lib/python/bson/objectid.py_raise_invalid_id"s rcCs tdS)z+Get the 5-byte random field of an ObjectId.)osurandomr r r r _random_bytes) rc@seZdZdZeZede Z e Z eZdZdZd2deeedefddfdd Zed edd ejddfd d Zed eddedefddZedefddZd3ddZdeddfddZ e!defddZ"e!dejfddZ#defddZ$deddfddZ%defdd Z&d!d"Z'd#edefd$d%Z(d#edefd&d'Z)d#edefd(d)Z*d#edefd*d+Z+d#edefd,d-Z,d#edefd.d/Z-de.fd0d1Z/dS)4ObjectIdzA MongoDB ObjectId.r)Z__idNr r cCsB|dur |dSt|trt|dkr||_dS||dS)ayInitialize 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. :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 `_. N )_ObjectId__generate isinstancebyteslen _ObjectId__id_ObjectId__validateselfr r r r__init__<s &  zObjectId.__init__clsgeneration_timecCsB|}|dur ||}t|}tdt|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) utcoffsetcalendartimegm timetuplestructpackint)r!r"offset timestampr r r r from_datetimeis zObjectId.from_datetimec Cs0|sdSzt|WdSttfyYdSw)zChecks if a `oid` string is valid or not. :Parameters: - `oid`: the object id to validate .. versionadded:: 2.3 FT)rr TypeError)r!r r r ris_valids zObjectId.is_validcCs&t}||jkr||_t|_|jS)z1Generate a 5-byte random number once per process.)rgetpid_pidr_ObjectId__random)r!pidr r r_randoms  zObjectId._randomcCs~tdtt}|t7}tj|tdtjdd7}tjdtdt_Wdn1s5wY||_ dS)z'Generate a new value for this ObjectId.r#N) r(r)r*timerr4 _inc_lock_inc_MAX_COUNTER_VALUErrr r rZ __generates  zObjectId.__generatec Cs~t|tr |j|_dSt|tr6t|dkr0z t||_WdStt fy/t |YdSwt |dStdt |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 z8id must be an instance of (bytes, str, ObjectId), not %sN) rrbinaryrstrrrfromhexr. ValueErrorrtyperr r rZ __validates     zObjectId.__validatecC|jS)z/12-byte binary representation of this ObjectId.rrr r rr<szObjectId.binarycCs(td|jddd}tj|tS)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#rr6)r(unpackrdatetime fromtimestampr )rr,r r rr"s zObjectId.generation_timecCrA)zdreturn value of object for pickling. needed explicitly because __slots__() defined. rBrCr r r __getstate__szObjectId.__getstate__valuecCs<t|tr |d}n|}t|tr|d|_dS||_dS)z explicit state set from picklingrzlatin-1N)rdictr=encoder)rrHr r r r __setstate__s    zObjectId.__setstate__cCst|jSN)binasciihexlifyrdecoderCr r r__str__szObjectId.__str__cCsdt|fS)NzObjectId('%s'))r=rCr r r__repr__szObjectId.__repr__othercCst|tr |j|jkStSrLrrrr<NotImplementedrrRr r r__eq__  zObjectId.__eq__cCst|tr |j|jkStSrLrSrUr r r__ne__rWzObjectId.__ne__cCst|tr |j|jkStSrLrSrUr r r__lt__rWzObjectId.__lt__cCst|tr |j|jkStSrLrSrUr r r__le__ rWzObjectId.__le__cCst|tr |j|jkStSrLrSrUr r r__gt__rWzObjectId.__gt__cCst|tr |j|jkStSrLrSrUr r r__ge__rWzObjectId.__ge__cCs t|jS)z,Get a hash value for this :class:`ObjectId`.)hashrrCr r r__hash__rzObjectId.__hash__rL)r N)0__name__ __module__ __qualname____doc__rr0r1rrandintr:r9 threadingLockr8rr2 __slots__Z _type_markerrrr=rr classmethodrrEr-rboolr/r4rrpropertyr<r"rGrKrPrQrVrXrYrZr[r\r*r^r r r rr.s@"-"  r)rbrMr%rErr(rdr7randomrtypingrrrrrZ bson.errorsr Z bson.tz_utilr r:r=rrrobjectrr r r rs