<s> r"""<STR_LIT>""" <EOL> __author__ = '<STR_LIT>' <EOL> RESERVED_NCS , RFC_4122 , RESERVED_MICROSOFT , RESERVED_FUTURE = [ <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] <EOL> int_ = int <EOL> bytes_ = bytes <EOL> class UUID ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , hex = None , bytes = None , bytes_le = None , fields = None , <EOL> int = None , version = None ) : <EOL> r"""<STR_LIT>""" <EOL> if [ hex , bytes , bytes_le , fields , int ] . count ( None ) != <NUM_LIT:4> : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> if hex is not None : <EOL> hex = hex . replace ( '<STR_LIT>' , '<STR_LIT>' ) . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> hex = hex . strip ( '<STR_LIT:{}>' ) . replace ( '<STR_LIT:->' , '<STR_LIT>' ) <EOL> if len ( hex ) != <NUM_LIT:32> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> int = int_ ( hex , <NUM_LIT:16> ) <EOL> if bytes_le is not None : <EOL> if len ( bytes_le ) != <NUM_LIT:16> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> bytes = ( bytes_ ( reversed ( bytes_le [ <NUM_LIT:0> : <NUM_LIT:4> ] ) ) + <EOL> bytes_ ( reversed ( bytes_le [ <NUM_LIT:4> : <NUM_LIT:6> ] ) ) + <EOL> bytes_ ( reversed ( bytes_le [ <NUM_LIT:6> : <NUM_LIT:8> ] ) ) + <EOL> bytes_le [ <NUM_LIT:8> : ] ) <EOL> if bytes is not None : <EOL> if len ( bytes ) != <NUM_LIT:16> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> assert isinstance ( bytes , bytes_ ) , repr ( bytes ) <EOL> int = int_ . from_bytes ( bytes , byteorder = '<STR_LIT>' ) <EOL> if fields is not None : <EOL> if len ( fields ) != <NUM_LIT:6> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> ( time_low , time_mid , time_hi_version , <EOL> clock_seq_hi_variant , clock_seq_low , node ) = fields <EOL> if not <NUM_LIT:0> <= time_low < <NUM_LIT:1> << <NUM_LIT:32> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if not <NUM_LIT:0> <= time_mid < <NUM_LIT:1> << <NUM_LIT:16> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if not <NUM_LIT:0> <= time_hi_version < <NUM_LIT:1> << <NUM_LIT:16> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if not <NUM_LIT:0> <= clock_seq_hi_variant < <NUM_LIT:1> << <NUM_LIT:8> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if not <NUM_LIT:0> <= clock_seq_low < <NUM_LIT:1> << <NUM_LIT:8> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if not <NUM_LIT:0> <= node < <NUM_LIT:1> << <NUM_LIT> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> clock_seq = ( clock_seq_hi_variant << <NUM_LIT:8> ) | clock_seq_low <EOL> int = ( ( time_low << <NUM_LIT> ) | ( time_mid << <NUM_LIT> ) | <EOL> ( time_hi_version << <NUM_LIT:64> ) | ( clock_seq << <NUM_LIT> ) | node ) <EOL> if int is not None : <EOL> if not <NUM_LIT:0> <= int < <NUM_LIT:1> << <NUM_LIT> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if version is not None : <EOL> if not <NUM_LIT:1> <= version <= <NUM_LIT:5> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> int &= ~ ( <NUM_LIT> << <NUM_LIT> ) <EOL> int |= <NUM_LIT> << <NUM_LIT> <EOL> int &= ~ ( <NUM_LIT> << <NUM_LIT:64> ) <EOL> int |= version << <NUM_LIT> <EOL> self . __dict__ [ '<STR_LIT:int>' ] = int <EOL> def __eq__ ( self , other ) : <EOL> if isinstance ( other , UUID ) : <EOL> return self . int == other . int <EOL> return NotImplemented <EOL> def __ne__ ( self , other ) : <EOL> if isinstance ( other , UUID ) : <EOL> return self . int != other . int <EOL> return NotImplemented <EOL> def __lt__ ( self , other ) : <EOL> if isinstance ( other , UUID ) : <EOL> return self . int < other . int <EOL> return NotImplemented <EOL> def __gt__ ( self , other ) : <EOL> if isinstance ( other , UUID ) : <EOL> return self . int > other . int <EOL> return NotImplemented <EOL> def __le__ ( self , other ) : <EOL> if isinstance ( other , UUID ) : <EOL> return self . int <= other . int <EOL> return NotImplemented <EOL> def __ge__ ( self , other ) : <EOL> if isinstance ( other , UUID ) : <EOL> return self . int >= other . int <EOL> return NotImplemented <EOL> def __hash__ ( self ) : <EOL> return hash ( self . int ) <EOL> def __int__ ( self ) : <EOL> return self . int <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % str ( self ) <EOL> def __setattr__ ( self , name , value ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> def __str__ ( self ) : <EOL> hex = '<STR_LIT>' % self . int <EOL> return '<STR_LIT>' % ( <EOL> hex [ : <NUM_LIT:8> ] , hex [ <NUM_LIT:8> : <NUM_LIT:12> ] , hex [ <NUM_LIT:12> : <NUM_LIT:16> ] , hex [ <NUM_LIT:16> : <NUM_LIT:20> ] , hex [ <NUM_LIT:20> : ] ) <EOL> @ property <EOL> def bytes ( self ) : <EOL> bytes = bytearray ( ) <EOL> for shift in range ( <NUM_LIT:0> , <NUM_LIT> , <NUM_LIT:8> ) : <EOL> bytes . insert ( <NUM_LIT:0> , ( self . int >> shift ) & <NUM_LIT> ) <EOL> return bytes_ ( bytes ) <EOL> @ property <EOL> def bytes_le ( self ) : <EOL> bytes = self . bytes <EOL> return ( bytes_ ( reversed ( bytes [ <NUM_LIT:0> : <NUM_LIT:4> ] ) ) + <EOL> bytes_ ( reversed ( bytes [ <NUM_LIT:4> : <NUM_LIT:6> ] ) ) + <EOL> bytes_ ( reversed ( bytes [ <NUM_LIT:6> : <NUM_LIT:8> ] ) ) + <EOL> bytes [ <NUM_LIT:8> : ] ) <EOL> @ property <EOL> def fields ( self ) : <EOL> return ( self . time_low , self . time_mid , self . time_hi_version , <EOL> self . clock_seq_hi_variant , self . clock_seq_low , self . node ) <EOL> @ property <EOL> def time_low ( self ) : <EOL> return self . int >> <NUM_LIT> <EOL> @ property <EOL> def time_mid ( self ) : <EOL> return ( self . int >> <NUM_LIT> ) & <NUM_LIT> <EOL> @ property <EOL> def time_hi_version ( self ) : <EOL> return ( self . int >> <NUM_LIT:64> ) & <NUM_LIT> <EOL> @ property <EOL> def clock_seq_hi_variant ( self ) : <EOL> return ( self . int >> <NUM_LIT> ) & <NUM_LIT> <EOL> @ property <EOL> def clock_seq_low ( self ) : <EOL> return ( self . int >> <NUM_LIT> ) & <NUM_LIT> <EOL> @ property <EOL> def time ( self ) : <EOL> return ( ( ( self . time_hi_version & <NUM_LIT> ) << <NUM_LIT> ) | <EOL> ( self . time_mid << <NUM_LIT:32> ) | self . time_low ) <EOL> @ property <EOL> def clock_seq ( self ) : <EOL> return ( ( ( self . clock_seq_hi_variant & <NUM_LIT> ) << <NUM_LIT:8> ) | <EOL> self . clock_seq_low ) <EOL> @ property <EOL> def node ( self ) : <EOL> return self . int & <NUM_LIT> <EOL> @ property <EOL> def hex ( self ) : <EOL> return '<STR_LIT>' % self . int <EOL> @ property <EOL> def urn ( self ) : <EOL> return '<STR_LIT>' + str ( self ) <EOL> @ property <EOL> def variant ( self ) : <EOL> if not self . int & ( <NUM_LIT> << <NUM_LIT> ) : <EOL> return RESERVED_NCS <EOL> elif not self . int & ( <NUM_LIT> << <NUM_LIT> ) : <EOL> return RFC_4122 <EOL> elif not self . int & ( <NUM_LIT> << <NUM_LIT> ) : <EOL> return RESERVED_MICROSOFT <EOL> else : <EOL> return RESERVED_FUTURE <EOL> @ property <EOL> def version ( self ) : <EOL> if self . variant == RFC_4122 : <EOL> return int ( ( self . int >> <NUM_LIT> ) & <NUM_LIT> ) <EOL> def _find_mac ( command , args , hw_identifiers , get_index ) : <EOL> import os , shutil <EOL> executable = shutil . which ( command ) <EOL> if executable is None : <EOL> path = os . pathsep . join ( ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> executable = shutil . which ( command , path = path ) <EOL> if executable is None : <EOL> return None <EOL> try : <EOL> cmd = '<STR_LIT>' % ( executable , args ) <EOL> with os . popen ( cmd ) as pipe : <EOL> for line in pipe : <EOL> words = line . lower ( ) . split ( ) <EOL> for i in range ( len ( words ) ) : <EOL> if words [ i ] in hw_identifiers : <EOL> try : <EOL> return int ( <EOL> words [ get_index ( i ) ] . replace ( '<STR_LIT::>' , '<STR_LIT>' ) , <NUM_LIT:16> ) <EOL> except ( ValueError , IndexError ) : <EOL> pass <EOL> except OSError : <EOL> pass <EOL> def _ifconfig_getnode ( ) : <EOL> """<STR_LIT>""" <EOL> for args in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> mac = _find_mac ( '<STR_LIT>' , args , [ '<STR_LIT>' , '<STR_LIT>' ] , lambda i : i + <NUM_LIT:1> ) <EOL> if mac : <EOL> return mac <EOL> import socket <EOL> ip_addr = socket . gethostbyname ( socket . gethostname ( ) ) <EOL> mac = _find_mac ( '<STR_LIT>' , '<STR_LIT>' , [ ip_addr ] , lambda i : - <NUM_LIT:1> ) <EOL> if mac : <EOL> return mac <EOL> mac = _find_mac ( '<STR_LIT>' , '<STR_LIT>' , [ '<STR_LIT>' ] , lambda i : <NUM_LIT:0> ) <EOL> if mac : <EOL> return mac <EOL> return None <EOL> def _ipconfig_getnode ( ) : <EOL> """<STR_LIT>""" <EOL> import os , re <EOL> dirs = [ '<STR_LIT>' , r'<STR_LIT>' , r'<STR_LIT>' ] <EOL> try : <EOL> import ctypes <EOL> buffer = ctypes . create_string_buffer ( <NUM_LIT> ) <EOL> ctypes . windll . kernel32 . GetSystemDirectoryA ( buffer , <NUM_LIT> ) <EOL> dirs . insert ( <NUM_LIT:0> , buffer . value . decode ( '<STR_LIT>' ) ) <EOL> except : <EOL> pass <EOL> def _netbios_getnode ( ) : <EOL> """<STR_LIT>""" <EOL> import win32wnet , netbios <EOL> ncb = netbios . NCB ( ) <EOL> ncb . Command = netbios . NCBENUM <EOL> ncb . Buffer = adapters = netbios . LANA_ENUM ( ) <EOL> adapters . _pack ( ) <EOL> if win32wnet . Netbios ( ncb ) != <NUM_LIT:0> : <EOL> return <EOL> adapters . _unpack ( ) <EOL> for i in range ( adapters . length ) : <EOL> ncb . Reset ( ) <EOL> ncb . Command = netbios . NCBRESET <EOL> ncb . Lana_num = ord ( adapters . lana [ i ] ) <EOL> if win32wnet . Netbios ( ncb ) != <NUM_LIT:0> : <EOL> continue <EOL> ncb . Reset ( ) <EOL> ncb . Command = netbios . NCBASTAT <EOL> ncb . Lana_num = ord ( adapters . lana [ i ] ) <EOL> ncb . Callname = '<STR_LIT:*>' . ljust ( <NUM_LIT:16> ) <EOL> ncb . Buffer = status = netbios . ADAPTER_STATUS ( ) <EOL> if win32wnet . Netbios ( ncb ) != <NUM_LIT:0> : <EOL> continue <EOL> status . _unpack ( ) <EOL> bytes = status . adapter_address <EOL> return ( ( bytes [ <NUM_LIT:0> ] << <NUM_LIT> ) + ( bytes [ <NUM_LIT:1> ] << <NUM_LIT:32> ) + ( bytes [ <NUM_LIT:2> ] << <NUM_LIT> ) + <EOL> ( bytes [ <NUM_LIT:3> ] << <NUM_LIT:16> ) + ( bytes [ <NUM_LIT:4> ] << <NUM_LIT:8> ) + bytes [ <NUM_LIT:5> ] ) <EOL> _uuid_generate_random = _uuid_generate_time = _UuidCreate = None <EOL> try : <EOL> import ctypes , ctypes . util <EOL> for libname in [ '<STR_LIT>' , '<STR_LIT:c>' ] : <EOL> try : <EOL> lib = ctypes . CDLL ( ctypes . util . find_library ( libname ) ) <EOL> except : <EOL> continue <EOL> if hasattr ( lib , '<STR_LIT>' ) : <EOL> _uuid_generate_random = lib . uuid_generate_random <EOL> if hasattr ( lib , '<STR_LIT>' ) : <EOL> _uuid_generate_time = lib . uuid_generate_time <EOL> if _uuid_generate_random is not None : <EOL> break <EOL> import sys <EOL> if sys . platform == '<STR_LIT>' : <EOL> import os <EOL> if int ( os . uname ( ) . release . split ( '<STR_LIT:.>' ) [ <NUM_LIT:0> ] ) >= <NUM_LIT:9> : <EOL> _uuid_generate_random = _uuid_generate_time = None <EOL> try : <EOL> lib = ctypes . windll . rpcrt4 <EOL> except : <EOL> lib = None <EOL> _UuidCreate = getattr ( lib , '<STR_LIT>' , <EOL> getattr ( lib , '<STR_LIT>' , None ) ) <EOL> except : <EOL> pass <EOL> def _unixdll_getnode ( ) : <EOL> """<STR_LIT>""" <EOL> _buffer = ctypes . create_string_buffer ( <NUM_LIT:16> ) <EOL> _uuid_generate_time ( _buffer ) <EOL> return UUID ( bytes = bytes_ ( _buffer . raw ) ) . node <EOL> def _windll_getnode ( ) : <EOL> """<STR_LIT>""" <EOL> _buffer = ctypes . create_string_buffer ( <NUM_LIT:16> ) <EOL> if _UuidCreate ( _buffer ) == <NUM_LIT:0> : <EOL> return UUID ( bytes = bytes_ ( _buffer . raw ) ) . node <EOL> def _random_getnode ( ) : <EOL> """<STR_LIT>""" <EOL> import random <EOL> return random . randrange ( <NUM_LIT:0> , <NUM_LIT:1> << <NUM_LIT> ) | <NUM_LIT> <EOL> _node = None <EOL> def getnode ( ) : <EOL> """<STR_LIT>""" <EOL> global _node <EOL> if _node is not None : <EOL> return _node <EOL> import sys <EOL> if sys . platform == '<STR_LIT:win32>' : <EOL> getters = [ _windll_getnode , _netbios_getnode , _ipconfig_getnode ] <EOL> else : <EOL> getters = [ _unixdll_getnode , _ifconfig_getnode ] <EOL> for getter in getters + [ _random_getnode ] : <EOL> try : <EOL> _node = getter ( ) <EOL> except : <EOL> continue <EOL> if _node is not None : <EOL> return _node <EOL> _last_timestamp = None <EOL> def uuid1 ( node = None , clock_seq = None ) : <EOL> """<STR_LIT>""" <EOL> if _uuid_generate_time and node is clock_seq is None : <EOL> _buffer = ctypes . create_string_buffer ( <NUM_LIT:16> ) <EOL> _uuid_generate_time ( _buffer ) <EOL> return UUID ( bytes = bytes_ ( _buffer . raw ) ) <EOL> global _last_timestamp <EOL> import time <EOL> nanoseconds = int ( time . time ( ) * <NUM_LIT> ) <EOL> timestamp = int ( nanoseconds / <NUM_LIT:100> ) + <NUM_LIT> <EOL> if _last_timestamp is not None and timestamp <= _last_timestamp : <EOL> timestamp = _last_timestamp + <NUM_LIT:1> <EOL> _last_timestamp = timestamp <EOL> if clock_seq is None : <EOL> import random <EOL> clock_seq = random . randrange ( <NUM_LIT:1> << <NUM_LIT> ) <EOL> time_low = timestamp & <NUM_LIT> <EOL> time_mid = ( timestamp >> <NUM_LIT:32> ) & <NUM_LIT> <EOL> time_hi_version = ( timestamp >> <NUM_LIT> ) & <NUM_LIT> <EOL> clock_seq_low = clock_seq & <NUM_LIT> <EOL> clock_seq_hi_variant = ( clock_seq >> <NUM_LIT:8> ) & <NUM_LIT> <EOL> if node is None : <EOL> node = getnode ( ) <EOL> return UUID ( fields = ( time_low , time_mid , time_hi_version , <EOL> clock_seq_hi_variant , clock_seq_low , node ) , version = <NUM_LIT:1> ) <EOL> def uuid3 ( namespace , name ) : <EOL> """<STR_LIT>""" <EOL> from hashlib import md5 <EOL> hash = md5 ( namespace . bytes + bytes ( name , "<STR_LIT:utf-8>" ) ) . digest ( ) <EOL> return UUID ( bytes = hash [ : <NUM_LIT:16> ] , version = <NUM_LIT:3> ) <EOL> def uuid4 ( ) : <EOL> """<STR_LIT>""" <EOL> if _uuid_generate_random : <EOL> _buffer = ctypes . create_string_buffer ( <NUM_LIT:16> ) <EOL> _uuid_generate_random ( _buffer ) <EOL> return UUID ( bytes = bytes_ ( _buffer . raw ) ) <EOL> try : <EOL> import os <EOL> return UUID ( bytes = os . urandom ( <NUM_LIT:16> ) , version = <NUM_LIT:4> ) <EOL> except : <EOL> import random <EOL> bytes = bytes_ ( random . randrange ( <NUM_LIT> ) for i in range ( <NUM_LIT:16> ) ) <EOL> return UUID ( bytes = bytes , version = <NUM_LIT:4> ) <EOL> def uuid5 ( namespace , name ) : <EOL> """<STR_LIT>""" <EOL> from hashlib import sha1 <EOL> hash = sha1 ( namespace . bytes + bytes ( name , "<STR_LIT:utf-8>" ) ) . digest ( ) <EOL> return UUID ( bytes = hash [ : <NUM_LIT:16> ] , version = <NUM_LIT:5> ) <EOL> NAMESPACE_DNS = UUID ( '<STR_LIT>' ) <EOL> NAMESPACE_URL = UUID ( '<STR_LIT>' ) <EOL> NAMESPACE_OID = UUID ( '<STR_LIT>' ) <EOL> NAMESPACE_X500 = UUID ( '<STR_LIT>' ) </s>
