<s> """<STR_LIT>""" <EOL> import binascii <EOL> import calendar <EOL> import datetime <EOL> try : <EOL> import hashlib <EOL> _md5func = hashlib . md5 <EOL> except ImportError : <EOL> import md5 <EOL> _md5func = md5 . new <EOL> import os <EOL> import random <EOL> import socket <EOL> import struct <EOL> import threading <EOL> import time <EOL> from openmdao . lib . casehandlers . pymongo_bson . errors import InvalidId <EOL> from openmdao . lib . casehandlers . pymongo_bson . py3compat import ( PY3 , b , binary_type , text_type , <EOL> bytes_from_hex , string_types ) <EOL> from openmdao . lib . casehandlers . pymongo_bson . tz_util import utc <EOL> EMPTY = b ( "<STR_LIT>" ) <EOL> ZERO = b ( "<STR_LIT:\x00>" ) <EOL> def _machine_bytes ( ) : <EOL> """<STR_LIT>""" <EOL> machine_hash = _md5func ( ) <EOL> if PY3 : <EOL> machine_hash . update ( socket . gethostname ( ) . encode ( ) ) <EOL> else : <EOL> machine_hash . update ( socket . gethostname ( ) ) <EOL> return machine_hash . digest ( ) [ <NUM_LIT:0> : <NUM_LIT:3> ] <EOL> class ObjectId ( object ) : <EOL> """<STR_LIT>""" <EOL> _inc = random . randint ( <NUM_LIT:0> , <NUM_LIT> ) <EOL> _inc_lock = threading . Lock ( ) <EOL> _machine_bytes = _machine_bytes ( ) <EOL> __slots__ = ( '<STR_LIT>' ) <EOL> _type_marker = <NUM_LIT:7> <EOL> def __init__ ( self , oid = None ) : <EOL> """<STR_LIT>""" <EOL> if oid is None : <EOL> self . __generate ( ) <EOL> else : <EOL> self . __validate ( oid ) <EOL> @ classmethod <EOL> def from_datetime ( cls , generation_time ) : <EOL> """<STR_LIT>""" <EOL> if generation_time . utcoffset ( ) is not None : <EOL> generation_time = generation_time - generation_time . utcoffset ( ) <EOL> ts = calendar . timegm ( generation_time . timetuple ( ) ) <EOL> oid = struct . pack ( "<STR_LIT>" , int ( ts ) ) + ZERO * <NUM_LIT:8> <EOL> return cls ( oid ) <EOL> @ classmethod <EOL> def is_valid ( cls , oid ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> ObjectId ( oid ) <EOL> return True <EOL> except ( InvalidId , TypeError ) : <EOL> return False <EOL> def __generate ( self ) : <EOL> """<STR_LIT>""" <EOL> oid = EMPTY <EOL> oid += struct . pack ( "<STR_LIT>" , int ( time . time ( ) ) ) <EOL> oid += ObjectId . _machine_bytes <EOL> oid += struct . pack ( "<STR_LIT>" , os . getpid ( ) % <NUM_LIT> ) <EOL> ObjectId . _inc_lock . acquire ( ) <EOL> oid += struct . pack ( "<STR_LIT>" , ObjectId . _inc ) [ <NUM_LIT:1> : <NUM_LIT:4> ] <EOL> ObjectId . _inc = ( ObjectId . _inc + <NUM_LIT:1> ) % <NUM_LIT> <EOL> ObjectId . _inc_lock . release ( ) <EOL> self . __id = oid <EOL> def __validate ( self , oid ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( oid , ObjectId ) : <EOL> self . __id = oid . __id <EOL> elif isinstance ( oid , string_types ) : <EOL> if len ( oid ) == <NUM_LIT:12> : <EOL> if isinstance ( oid , binary_type ) : <EOL> self . __id = oid <EOL> else : <EOL> raise InvalidId ( "<STR_LIT>" % oid ) <EOL> elif len ( oid ) == <NUM_LIT> : <EOL> try : <EOL> self . __id = bytes_from_hex ( oid ) <EOL> except ( TypeError , ValueError ) : <EOL> raise InvalidId ( "<STR_LIT>" % oid ) <EOL> else : <EOL> raise InvalidId ( "<STR_LIT>" % oid ) <EOL> else : <EOL> raise TypeError ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( binary_type . __name__ , <EOL> text_type . __name__ , type ( oid ) ) ) <EOL> @ property <EOL> def binary ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __id <EOL> @ property <EOL> def generation_time ( self ) : <EOL> """<STR_LIT>""" <EOL> t = struct . unpack ( "<STR_LIT>" , self . __id [ <NUM_LIT:0> : <NUM_LIT:4> ] ) [ <NUM_LIT:0> ] <EOL> return datetime . datetime . fromtimestamp ( t , utc ) <EOL> def __getstate__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __id <EOL> def __setstate__ ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( value , dict ) : <EOL> oid = value [ "<STR_LIT>" ] <EOL> else : <EOL> oid = value <EOL> if PY3 and isinstance ( oid , text_type ) : <EOL> self . __id = oid . encode ( '<STR_LIT>' ) <EOL> else : <EOL> self . __id = oid <EOL> def __str__ ( self ) : <EOL> if PY3 : <EOL> return binascii . hexlify ( self . __id ) . decode ( ) <EOL> return binascii . hexlify ( self . __id ) <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( str ( self ) , ) <EOL> def __eq__ ( self , other ) : <EOL> if isinstance ( other , ObjectId ) : <EOL> return self . __id == other . __id <EOL> return NotImplemented <EOL> def __ne__ ( self , other ) : <EOL> if isinstance ( other , ObjectId ) : <EOL> return self . __id != other . __id <EOL> return NotImplemented <EOL> def __lt__ ( self , other ) : <EOL> if isinstance ( other , ObjectId ) : <EOL> return self . __id < other . __id <EOL> return NotImplemented <EOL> def __le__ ( self , other ) : <EOL> if isinstance ( other , ObjectId ) : <EOL> return self . __id <= other . __id <EOL> return NotImplemented <EOL> def __gt__ ( self , other ) : <EOL> if isinstance ( other , ObjectId ) : <EOL> return self . __id > other . __id <EOL> return NotImplemented <EOL> def __ge__ ( self , other ) : <EOL> if isinstance ( other , ObjectId ) : <EOL> return self . __id >= other . __id <EOL> return NotImplemented <EOL> def __hash__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return hash ( self . __id ) </s>
