f#dZddlmZddlZddlZddlZddlZddlZddlZddl Z ddl m Z ddl m Z mZmZmZmZddlmZddlmZdZd d Zdd ZGd d Zy)z)Tools for working with MongoDB ObjectIds.) annotationsN) SystemRandom)AnyNoReturnOptionalTypeUnion InvalidId)utcictd|z)NzS%r is not a valid ObjectId, it must be a 12-byte input or a 24-character hex stringr )oids V/var/lib/jenkins/workspace/mettalog/venv/lib/python3.12/site-packages/bson/objectid.py_raise_invalid_idr"s  (*- . c,tjdS)z+Get the 5-byte random field of an ObjectId.)osurandomrr _random_bytesr)s ::a=rcdeZdZdZej Zejde Z e jZ eZdZdZdddZeddZeddZedd Zdd Zdd Zedd Zed d ZddZd!dZd"dZd"dZd#dZ d#dZ!d#dZ"d#dZ#d#dZ$d#dZ%d$dZ&y)%ObjectIdzA MongoDB ObjectId.r)__idNc||jyt|trt|dk(r||_y|j |y)aZInitialize 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 `_. .. versionchanged:: 3.8 :class:`~bson.objectid.ObjectId` now implements the `ObjectID specification version 0.2 `_. N )_ObjectId__generate isinstancebyteslen _ObjectId__id_ObjectId__validateselfrs r__init__zObjectId.__init__<s;J ; OO  U #CBDI OOC rc|j}|||z }tj|j}t j dt |dz}||S)aJCreate 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. >Is) utcoffsetcalendartimegm timetuplestructpackint)clsgeneration_timeoffset timestamprs r from_datetimezObjectId.from_datetimehs\6!**,  -6OOOO$=$=$?@ kk$I/2UU3xrcL|sy t|y#ttf$rYywxYw)zChecks if a `oid` string is valid or not. :param oid: the object id to validate .. versionadded:: 2.3 FT)rr TypeError)r0rs ris_validzObjectId.is_valids0  SM9%  s ##ctj}||jk7r||_t|_|jS)z1Generate a 5-byte random number once per process.)rgetpid_pidr_ObjectId__random)r0pids r_randomzObjectId._randoms4iik #((?CH(?CL||rctjdttj}|tj z }tj 5|tjdtjddz }tjdztdzzt_ddd||_ y#1swY||_ yxYw)z'Generate a new value for this ObjectId.r(N) r-r.r/timerr= _inc_lock_inc_MAX_COUNTER_VALUEr"r$s r __generatezObjectId.__generateskk$DIIK 01 x!!   K 6;;tX]]3Aa8 8C%]]Q.3E3IJHM K  K s AB::C c@t|tr|j|_yt|tr6t |dk(r t j||_yt|ytdt|#ttf$rt|YywxYw)aValidate 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) rrbinaryr"strr!r fromhexr6 ValueErrorrtyper$s r __validatezObjectId.__validates c8 $ DI S !3x2~+ % c 2DI"#&TUYZ]U^T_`a a ":.+%c*+sBBBc|jS)z/12-byte binary representation of this ObjectId.r"r%s rrHzObjectId.binarysyyrctjd|jddd}tjj |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(rr@)r-unpackr"datetime fromtimestampr )r%r3s rr1zObjectId.generation_times<MM$ !A7:   ..y#>>rc|jS)zdReturn value of object for pickling. needed explicitly because __slots__() defined. rOrPs r __getstate__zObjectId.__getstate__syyrct|tr|d}n|}t|tr|jd|_y||_y)z Explicit state set from picklingr"zlatin-1N)rdictrIencoder")r%valuers r __setstate__zObjectId.__setstate__s@ eT "(CC c3  9-DIDIrc\tj|jjSN)binasciihexlifyr"decoderPs r__str__zObjectId.__str__s  *1133rcd|dS)Nz ObjectId('z')rrPs r__repr__zObjectId.__repr__sD82&&rc`t|tr|j|jk(StSr]rrr"rHNotImplementedr%others r__eq__zObjectId.__eq__% eX &99 , ,rc`t|tr|j|jk7StSr]rergs r__ne__zObjectId.__ne__rjrc`t|tr|j|jkStSr]rergs r__lt__zObjectId.__lt__% eX &99u||+ +rc`t|tr|j|jkStSr]rergs r__le__zObjectId.__le__rjrc`t|tr|j|jkDStSr]rergs r__gt__zObjectId.__gt__ rorc`t|tr|j|jk\StSr]rergs r__ge__zObjectId.__ge__rjrc,t|jS)z,Get a hash value for this :class:`ObjectId`.)hashr"rPs r__hash__zObjectId.__hash__sDIIrr])rz%Optional[Union[str, ObjectId, bytes]]returnNone)r0Type[ObjectId]r1datetime.datetimeryr)r0r{rrryboolryr )ryrz)rrryrz)ryr|)rZrryrz)ryrI)rhrryr})ryr/)'__name__ __module__ __qualname____doc__rr9r:rrandintrDrC threadingLockrBrr; __slots__ _type_markerr& classmethodr4r7r=rr#propertyrHr1rVr[rarcrirlrnrqrsrurxrrrrr.s 299;D > ! !!%7 8D  IHIL*!XB  b, ? ?  4'      rr)rrIryrr~)r __future__rr^r*rSrr-rrArandomrtypingrrrrr bson.errorsr bson.tz_utilr rDrrrrrrrsM0"  77! hhr