Added missing svn:eol-style property to text files.

This commit is contained in:
Tim Peters 2006-06-13 00:30:50 +00:00
parent edd66fa7e9
commit 2adc626bb5
2 changed files with 873 additions and 873 deletions

View File

@ -1,396 +1,396 @@
from unittest import TestCase, main from unittest import TestCase, main
import uuid import uuid
def importable(name): def importable(name):
try: try:
__import__(name) __import__(name)
return True return True
except: except:
return False return False
class TestUUID(TestCase): class TestUUID(TestCase):
last_node = None last_node = None
def test_UUID(self): def test_UUID(self):
equal = self.assertEqual equal = self.assertEqual
ascending = [] ascending = []
for (string, curly, hex, bytes, fields, integer, urn, for (string, curly, hex, bytes, fields, integer, urn,
time, clock_seq, variant, version) in [ time, clock_seq, variant, version) in [
('00000000-0000-0000-0000-000000000000', ('00000000-0000-0000-0000-000000000000',
'{00000000-0000-0000-0000-000000000000}', '{00000000-0000-0000-0000-000000000000}',
'00000000000000000000000000000000', '00000000000000000000000000000000',
'\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0', '\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0',
(0, 0, 0, 0, 0, 0), (0, 0, 0, 0, 0, 0),
0, 0,
'urn:uuid:00000000-0000-0000-0000-000000000000', 'urn:uuid:00000000-0000-0000-0000-000000000000',
0, 0, uuid.RESERVED_NCS, None), 0, 0, uuid.RESERVED_NCS, None),
('00010203-0405-0607-0809-0a0b0c0d0e0f', ('00010203-0405-0607-0809-0a0b0c0d0e0f',
'{00010203-0405-0607-0809-0a0b0c0d0e0f}', '{00010203-0405-0607-0809-0a0b0c0d0e0f}',
'000102030405060708090a0b0c0d0e0f', '000102030405060708090a0b0c0d0e0f',
'\0\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\x0d\x0e\x0f', '\0\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\x0d\x0e\x0f',
(0x00010203L, 0x0405, 0x0607, 8, 9, 0x0a0b0c0d0e0fL), (0x00010203L, 0x0405, 0x0607, 8, 9, 0x0a0b0c0d0e0fL),
0x000102030405060708090a0b0c0d0e0fL, 0x000102030405060708090a0b0c0d0e0fL,
'urn:uuid:00010203-0405-0607-0809-0a0b0c0d0e0f', 'urn:uuid:00010203-0405-0607-0809-0a0b0c0d0e0f',
0x607040500010203L, 0x809, uuid.RESERVED_NCS, None), 0x607040500010203L, 0x809, uuid.RESERVED_NCS, None),
('02d9e6d5-9467-382e-8f9b-9300a64ac3cd', ('02d9e6d5-9467-382e-8f9b-9300a64ac3cd',
'{02d9e6d5-9467-382e-8f9b-9300a64ac3cd}', '{02d9e6d5-9467-382e-8f9b-9300a64ac3cd}',
'02d9e6d59467382e8f9b9300a64ac3cd', '02d9e6d59467382e8f9b9300a64ac3cd',
'\x02\xd9\xe6\xd5\x94\x67\x38\x2e\x8f\x9b\x93\x00\xa6\x4a\xc3\xcd', '\x02\xd9\xe6\xd5\x94\x67\x38\x2e\x8f\x9b\x93\x00\xa6\x4a\xc3\xcd',
(0x02d9e6d5L, 0x9467, 0x382e, 0x8f, 0x9b, 0x9300a64ac3cdL), (0x02d9e6d5L, 0x9467, 0x382e, 0x8f, 0x9b, 0x9300a64ac3cdL),
0x02d9e6d59467382e8f9b9300a64ac3cdL, 0x02d9e6d59467382e8f9b9300a64ac3cdL,
'urn:uuid:02d9e6d5-9467-382e-8f9b-9300a64ac3cd', 'urn:uuid:02d9e6d5-9467-382e-8f9b-9300a64ac3cd',
0x82e946702d9e6d5L, 0xf9b, uuid.RFC_4122, 3), 0x82e946702d9e6d5L, 0xf9b, uuid.RFC_4122, 3),
('12345678-1234-5678-1234-567812345678', ('12345678-1234-5678-1234-567812345678',
'{12345678-1234-5678-1234-567812345678}', '{12345678-1234-5678-1234-567812345678}',
'12345678123456781234567812345678', '12345678123456781234567812345678',
'\x12\x34\x56\x78'*4, '\x12\x34\x56\x78'*4,
(0x12345678, 0x1234, 0x5678, 0x12, 0x34, 0x567812345678), (0x12345678, 0x1234, 0x5678, 0x12, 0x34, 0x567812345678),
0x12345678123456781234567812345678, 0x12345678123456781234567812345678,
'urn:uuid:12345678-1234-5678-1234-567812345678', 'urn:uuid:12345678-1234-5678-1234-567812345678',
0x678123412345678L, 0x1234, uuid.RESERVED_NCS, None), 0x678123412345678L, 0x1234, uuid.RESERVED_NCS, None),
('6ba7b810-9dad-11d1-80b4-00c04fd430c8', ('6ba7b810-9dad-11d1-80b4-00c04fd430c8',
'{6ba7b810-9dad-11d1-80b4-00c04fd430c8}', '{6ba7b810-9dad-11d1-80b4-00c04fd430c8}',
'6ba7b8109dad11d180b400c04fd430c8', '6ba7b8109dad11d180b400c04fd430c8',
'\x6b\xa7\xb8\x10\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8', '\x6b\xa7\xb8\x10\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
(0x6ba7b810L, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8L), (0x6ba7b810L, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8L),
0x6ba7b8109dad11d180b400c04fd430c8L, 0x6ba7b8109dad11d180b400c04fd430c8L,
'urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8', 'urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8',
0x1d19dad6ba7b810L, 0xb4, uuid.RFC_4122, 1), 0x1d19dad6ba7b810L, 0xb4, uuid.RFC_4122, 1),
('6ba7b811-9dad-11d1-80b4-00c04fd430c8', ('6ba7b811-9dad-11d1-80b4-00c04fd430c8',
'{6ba7b811-9dad-11d1-80b4-00c04fd430c8}', '{6ba7b811-9dad-11d1-80b4-00c04fd430c8}',
'6ba7b8119dad11d180b400c04fd430c8', '6ba7b8119dad11d180b400c04fd430c8',
'\x6b\xa7\xb8\x11\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8', '\x6b\xa7\xb8\x11\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
(0x6ba7b811L, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8L), (0x6ba7b811L, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8L),
0x6ba7b8119dad11d180b400c04fd430c8L, 0x6ba7b8119dad11d180b400c04fd430c8L,
'urn:uuid:6ba7b811-9dad-11d1-80b4-00c04fd430c8', 'urn:uuid:6ba7b811-9dad-11d1-80b4-00c04fd430c8',
0x1d19dad6ba7b811L, 0xb4, uuid.RFC_4122, 1), 0x1d19dad6ba7b811L, 0xb4, uuid.RFC_4122, 1),
('6ba7b812-9dad-11d1-80b4-00c04fd430c8', ('6ba7b812-9dad-11d1-80b4-00c04fd430c8',
'{6ba7b812-9dad-11d1-80b4-00c04fd430c8}', '{6ba7b812-9dad-11d1-80b4-00c04fd430c8}',
'6ba7b8129dad11d180b400c04fd430c8', '6ba7b8129dad11d180b400c04fd430c8',
'\x6b\xa7\xb8\x12\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8', '\x6b\xa7\xb8\x12\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
(0x6ba7b812L, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8L), (0x6ba7b812L, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8L),
0x6ba7b8129dad11d180b400c04fd430c8L, 0x6ba7b8129dad11d180b400c04fd430c8L,
'urn:uuid:6ba7b812-9dad-11d1-80b4-00c04fd430c8', 'urn:uuid:6ba7b812-9dad-11d1-80b4-00c04fd430c8',
0x1d19dad6ba7b812L, 0xb4, uuid.RFC_4122, 1), 0x1d19dad6ba7b812L, 0xb4, uuid.RFC_4122, 1),
('6ba7b814-9dad-11d1-80b4-00c04fd430c8', ('6ba7b814-9dad-11d1-80b4-00c04fd430c8',
'{6ba7b814-9dad-11d1-80b4-00c04fd430c8}', '{6ba7b814-9dad-11d1-80b4-00c04fd430c8}',
'6ba7b8149dad11d180b400c04fd430c8', '6ba7b8149dad11d180b400c04fd430c8',
'\x6b\xa7\xb8\x14\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8', '\x6b\xa7\xb8\x14\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
(0x6ba7b814L, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8L), (0x6ba7b814L, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8L),
0x6ba7b8149dad11d180b400c04fd430c8L, 0x6ba7b8149dad11d180b400c04fd430c8L,
'urn:uuid:6ba7b814-9dad-11d1-80b4-00c04fd430c8', 'urn:uuid:6ba7b814-9dad-11d1-80b4-00c04fd430c8',
0x1d19dad6ba7b814L, 0xb4, uuid.RFC_4122, 1), 0x1d19dad6ba7b814L, 0xb4, uuid.RFC_4122, 1),
('7d444840-9dc0-11d1-b245-5ffdce74fad2', ('7d444840-9dc0-11d1-b245-5ffdce74fad2',
'{7d444840-9dc0-11d1-b245-5ffdce74fad2}', '{7d444840-9dc0-11d1-b245-5ffdce74fad2}',
'7d4448409dc011d1b2455ffdce74fad2', '7d4448409dc011d1b2455ffdce74fad2',
'\x7d\x44\x48\x40\x9d\xc0\x11\xd1\xb2\x45\x5f\xfd\xce\x74\xfa\xd2', '\x7d\x44\x48\x40\x9d\xc0\x11\xd1\xb2\x45\x5f\xfd\xce\x74\xfa\xd2',
(0x7d444840L, 0x9dc0, 0x11d1, 0xb2, 0x45, 0x5ffdce74fad2L), (0x7d444840L, 0x9dc0, 0x11d1, 0xb2, 0x45, 0x5ffdce74fad2L),
0x7d4448409dc011d1b2455ffdce74fad2L, 0x7d4448409dc011d1b2455ffdce74fad2L,
'urn:uuid:7d444840-9dc0-11d1-b245-5ffdce74fad2', 'urn:uuid:7d444840-9dc0-11d1-b245-5ffdce74fad2',
0x1d19dc07d444840L, 0x3245, uuid.RFC_4122, 1), 0x1d19dc07d444840L, 0x3245, uuid.RFC_4122, 1),
('e902893a-9d22-3c7e-a7b8-d6e313b71d9f', ('e902893a-9d22-3c7e-a7b8-d6e313b71d9f',
'{e902893a-9d22-3c7e-a7b8-d6e313b71d9f}', '{e902893a-9d22-3c7e-a7b8-d6e313b71d9f}',
'e902893a9d223c7ea7b8d6e313b71d9f', 'e902893a9d223c7ea7b8d6e313b71d9f',
'\xe9\x02\x89\x3a\x9d\x22\x3c\x7e\xa7\xb8\xd6\xe3\x13\xb7\x1d\x9f', '\xe9\x02\x89\x3a\x9d\x22\x3c\x7e\xa7\xb8\xd6\xe3\x13\xb7\x1d\x9f',
(0xe902893aL, 0x9d22, 0x3c7e, 0xa7, 0xb8, 0xd6e313b71d9fL), (0xe902893aL, 0x9d22, 0x3c7e, 0xa7, 0xb8, 0xd6e313b71d9fL),
0xe902893a9d223c7ea7b8d6e313b71d9fL, 0xe902893a9d223c7ea7b8d6e313b71d9fL,
'urn:uuid:e902893a-9d22-3c7e-a7b8-d6e313b71d9f', 'urn:uuid:e902893a-9d22-3c7e-a7b8-d6e313b71d9f',
0xc7e9d22e902893aL, 0x27b8, uuid.RFC_4122, 3), 0xc7e9d22e902893aL, 0x27b8, uuid.RFC_4122, 3),
('eb424026-6f54-4ef8-a4d0-bb658a1fc6cf', ('eb424026-6f54-4ef8-a4d0-bb658a1fc6cf',
'{eb424026-6f54-4ef8-a4d0-bb658a1fc6cf}', '{eb424026-6f54-4ef8-a4d0-bb658a1fc6cf}',
'eb4240266f544ef8a4d0bb658a1fc6cf', 'eb4240266f544ef8a4d0bb658a1fc6cf',
'\xeb\x42\x40\x26\x6f\x54\x4e\xf8\xa4\xd0\xbb\x65\x8a\x1f\xc6\xcf', '\xeb\x42\x40\x26\x6f\x54\x4e\xf8\xa4\xd0\xbb\x65\x8a\x1f\xc6\xcf',
(0xeb424026L, 0x6f54, 0x4ef8, 0xa4, 0xd0, 0xbb658a1fc6cfL), (0xeb424026L, 0x6f54, 0x4ef8, 0xa4, 0xd0, 0xbb658a1fc6cfL),
0xeb4240266f544ef8a4d0bb658a1fc6cfL, 0xeb4240266f544ef8a4d0bb658a1fc6cfL,
'urn:uuid:eb424026-6f54-4ef8-a4d0-bb658a1fc6cf', 'urn:uuid:eb424026-6f54-4ef8-a4d0-bb658a1fc6cf',
0xef86f54eb424026L, 0x24d0, uuid.RFC_4122, 4), 0xef86f54eb424026L, 0x24d0, uuid.RFC_4122, 4),
('f81d4fae-7dec-11d0-a765-00a0c91e6bf6', ('f81d4fae-7dec-11d0-a765-00a0c91e6bf6',
'{f81d4fae-7dec-11d0-a765-00a0c91e6bf6}', '{f81d4fae-7dec-11d0-a765-00a0c91e6bf6}',
'f81d4fae7dec11d0a76500a0c91e6bf6', 'f81d4fae7dec11d0a76500a0c91e6bf6',
'\xf8\x1d\x4f\xae\x7d\xec\x11\xd0\xa7\x65\x00\xa0\xc9\x1e\x6b\xf6', '\xf8\x1d\x4f\xae\x7d\xec\x11\xd0\xa7\x65\x00\xa0\xc9\x1e\x6b\xf6',
(0xf81d4faeL, 0x7dec, 0x11d0, 0xa7, 0x65, 0x00a0c91e6bf6L), (0xf81d4faeL, 0x7dec, 0x11d0, 0xa7, 0x65, 0x00a0c91e6bf6L),
0xf81d4fae7dec11d0a76500a0c91e6bf6L, 0xf81d4fae7dec11d0a76500a0c91e6bf6L,
'urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6', 'urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6',
0x1d07decf81d4faeL, 0x2765, uuid.RFC_4122, 1), 0x1d07decf81d4faeL, 0x2765, uuid.RFC_4122, 1),
('fffefdfc-fffe-fffe-fffe-fffefdfcfbfa', ('fffefdfc-fffe-fffe-fffe-fffefdfcfbfa',
'{fffefdfc-fffe-fffe-fffe-fffefdfcfbfa}', '{fffefdfc-fffe-fffe-fffe-fffefdfcfbfa}',
'fffefdfcfffefffefffefffefdfcfbfa', 'fffefdfcfffefffefffefffefdfcfbfa',
'\xff\xfe\xfd\xfc\xff\xfe\xff\xfe\xff\xfe\xff\xfe\xfd\xfc\xfb\xfa', '\xff\xfe\xfd\xfc\xff\xfe\xff\xfe\xff\xfe\xff\xfe\xfd\xfc\xfb\xfa',
(0xfffefdfcL, 0xfffe, 0xfffe, 0xff, 0xfe, 0xfffefdfcfbfaL), (0xfffefdfcL, 0xfffe, 0xfffe, 0xff, 0xfe, 0xfffefdfcfbfaL),
0xfffefdfcfffefffefffefffefdfcfbfaL, 0xfffefdfcfffefffefffefffefdfcfbfaL,
'urn:uuid:fffefdfc-fffe-fffe-fffe-fffefdfcfbfa', 'urn:uuid:fffefdfc-fffe-fffe-fffe-fffefdfcfbfa',
0xffefffefffefdfcL, 0x3ffe, uuid.RESERVED_FUTURE, None), 0xffefffefffefdfcL, 0x3ffe, uuid.RESERVED_FUTURE, None),
('ffffffff-ffff-ffff-ffff-ffffffffffff', ('ffffffff-ffff-ffff-ffff-ffffffffffff',
'{ffffffff-ffff-ffff-ffff-ffffffffffff}', '{ffffffff-ffff-ffff-ffff-ffffffffffff}',
'ffffffffffffffffffffffffffffffff', 'ffffffffffffffffffffffffffffffff',
'\xff'*16, '\xff'*16,
(0xffffffffL, 0xffffL, 0xffffL, 0xff, 0xff, 0xffffffffffffL), (0xffffffffL, 0xffffL, 0xffffL, 0xff, 0xff, 0xffffffffffffL),
0xffffffffffffffffffffffffffffffffL, 0xffffffffffffffffffffffffffffffffL,
'urn:uuid:ffffffff-ffff-ffff-ffff-ffffffffffff', 'urn:uuid:ffffffff-ffff-ffff-ffff-ffffffffffff',
0xfffffffffffffffL, 0x3fff, uuid.RESERVED_FUTURE, None), 0xfffffffffffffffL, 0x3fff, uuid.RESERVED_FUTURE, None),
]: ]:
equivalents = [] equivalents = []
# Construct each UUID in several different ways. # Construct each UUID in several different ways.
for u in [uuid.UUID(string), uuid.UUID(curly), uuid.UUID(hex), for u in [uuid.UUID(string), uuid.UUID(curly), uuid.UUID(hex),
uuid.UUID(bytes=bytes), uuid.UUID(fields=fields), uuid.UUID(bytes=bytes), uuid.UUID(fields=fields),
uuid.UUID(int=integer), uuid.UUID(urn)]: uuid.UUID(int=integer), uuid.UUID(urn)]:
# Test all conversions and properties of the UUID object. # Test all conversions and properties of the UUID object.
equal(str(u), string) equal(str(u), string)
equal(int(u), integer) equal(int(u), integer)
equal(u.bytes, bytes) equal(u.bytes, bytes)
equal(u.fields, fields) equal(u.fields, fields)
equal(u.time_low, fields[0]) equal(u.time_low, fields[0])
equal(u.time_mid, fields[1]) equal(u.time_mid, fields[1])
equal(u.time_hi_version, fields[2]) equal(u.time_hi_version, fields[2])
equal(u.clock_seq_hi_variant, fields[3]) equal(u.clock_seq_hi_variant, fields[3])
equal(u.clock_seq_low, fields[4]) equal(u.clock_seq_low, fields[4])
equal(u.node, fields[5]) equal(u.node, fields[5])
equal(u.hex, hex) equal(u.hex, hex)
equal(u.int, integer) equal(u.int, integer)
equal(u.urn, urn) equal(u.urn, urn)
equal(u.time, time) equal(u.time, time)
equal(u.clock_seq, clock_seq) equal(u.clock_seq, clock_seq)
equal(u.variant, variant) equal(u.variant, variant)
equal(u.version, version) equal(u.version, version)
equivalents.append(u) equivalents.append(u)
# Different construction methods should give the same UUID. # Different construction methods should give the same UUID.
for u in equivalents: for u in equivalents:
for v in equivalents: for v in equivalents:
equal(u, v) equal(u, v)
ascending.append(u) ascending.append(u)
# Test comparison of UUIDs. # Test comparison of UUIDs.
for i in range(len(ascending)): for i in range(len(ascending)):
for j in range(len(ascending)): for j in range(len(ascending)):
equal(cmp(i, j), cmp(ascending[i], ascending[j])) equal(cmp(i, j), cmp(ascending[i], ascending[j]))
# Test sorting of UUIDs (above list is in ascending order). # Test sorting of UUIDs (above list is in ascending order).
resorted = ascending[:] resorted = ascending[:]
resorted.reverse() resorted.reverse()
resorted.sort() resorted.sort()
equal(ascending, resorted) equal(ascending, resorted)
def test_exceptions(self): def test_exceptions(self):
badvalue = lambda f: self.assertRaises(ValueError, f) badvalue = lambda f: self.assertRaises(ValueError, f)
badtype = lambda f: self.assertRaises(TypeError, f) badtype = lambda f: self.assertRaises(TypeError, f)
# Badly formed hex strings. # Badly formed hex strings.
badvalue(lambda: uuid.UUID('')) badvalue(lambda: uuid.UUID(''))
badvalue(lambda: uuid.UUID('abc')) badvalue(lambda: uuid.UUID('abc'))
badvalue(lambda: uuid.UUID('1234567812345678123456781234567')) badvalue(lambda: uuid.UUID('1234567812345678123456781234567'))
badvalue(lambda: uuid.UUID('123456781234567812345678123456789')) badvalue(lambda: uuid.UUID('123456781234567812345678123456789'))
badvalue(lambda: uuid.UUID('123456781234567812345678z2345678')) badvalue(lambda: uuid.UUID('123456781234567812345678z2345678'))
# Badly formed bytes. # Badly formed bytes.
badvalue(lambda: uuid.UUID(bytes='abc')) badvalue(lambda: uuid.UUID(bytes='abc'))
badvalue(lambda: uuid.UUID(bytes='\0'*15)) badvalue(lambda: uuid.UUID(bytes='\0'*15))
badvalue(lambda: uuid.UUID(bytes='\0'*17)) badvalue(lambda: uuid.UUID(bytes='\0'*17))
# Badly formed fields. # Badly formed fields.
badvalue(lambda: uuid.UUID(fields=(1,))) badvalue(lambda: uuid.UUID(fields=(1,)))
badvalue(lambda: uuid.UUID(fields=(1, 2, 3, 4, 5))) badvalue(lambda: uuid.UUID(fields=(1, 2, 3, 4, 5)))
badvalue(lambda: uuid.UUID(fields=(1, 2, 3, 4, 5, 6, 7))) badvalue(lambda: uuid.UUID(fields=(1, 2, 3, 4, 5, 6, 7)))
# Field values out of range. # Field values out of range.
badvalue(lambda: uuid.UUID(fields=(-1, 0, 0, 0, 0, 0))) badvalue(lambda: uuid.UUID(fields=(-1, 0, 0, 0, 0, 0)))
badvalue(lambda: uuid.UUID(fields=(0x100000000L, 0, 0, 0, 0, 0))) badvalue(lambda: uuid.UUID(fields=(0x100000000L, 0, 0, 0, 0, 0)))
badvalue(lambda: uuid.UUID(fields=(0, -1, 0, 0, 0, 0))) badvalue(lambda: uuid.UUID(fields=(0, -1, 0, 0, 0, 0)))
badvalue(lambda: uuid.UUID(fields=(0, 0x10000L, 0, 0, 0, 0))) badvalue(lambda: uuid.UUID(fields=(0, 0x10000L, 0, 0, 0, 0)))
badvalue(lambda: uuid.UUID(fields=(0, 0, -1, 0, 0, 0))) badvalue(lambda: uuid.UUID(fields=(0, 0, -1, 0, 0, 0)))
badvalue(lambda: uuid.UUID(fields=(0, 0, 0x10000L, 0, 0, 0))) badvalue(lambda: uuid.UUID(fields=(0, 0, 0x10000L, 0, 0, 0)))
badvalue(lambda: uuid.UUID(fields=(0, 0, 0, -1, 0, 0))) badvalue(lambda: uuid.UUID(fields=(0, 0, 0, -1, 0, 0)))
badvalue(lambda: uuid.UUID(fields=(0, 0, 0, 0x100L, 0, 0))) badvalue(lambda: uuid.UUID(fields=(0, 0, 0, 0x100L, 0, 0)))
badvalue(lambda: uuid.UUID(fields=(0, 0, 0, 0, -1, 0))) badvalue(lambda: uuid.UUID(fields=(0, 0, 0, 0, -1, 0)))
badvalue(lambda: uuid.UUID(fields=(0, 0, 0, 0, 0x100L, 0))) badvalue(lambda: uuid.UUID(fields=(0, 0, 0, 0, 0x100L, 0)))
badvalue(lambda: uuid.UUID(fields=(0, 0, 0, 0, 0, -1))) badvalue(lambda: uuid.UUID(fields=(0, 0, 0, 0, 0, -1)))
badvalue(lambda: uuid.UUID(fields=(0, 0, 0, 0, 0, 0x1000000000000L))) badvalue(lambda: uuid.UUID(fields=(0, 0, 0, 0, 0, 0x1000000000000L)))
# Version number out of range. # Version number out of range.
badvalue(lambda: uuid.UUID('00'*16, version=0)) badvalue(lambda: uuid.UUID('00'*16, version=0))
badvalue(lambda: uuid.UUID('00'*16, version=6)) badvalue(lambda: uuid.UUID('00'*16, version=6))
# Integer value out of range. # Integer value out of range.
badvalue(lambda: uuid.UUID(int=-1)) badvalue(lambda: uuid.UUID(int=-1))
badvalue(lambda: uuid.UUID(int=1<<128L)) badvalue(lambda: uuid.UUID(int=1<<128L))
# Must supply exactly one of hex, bytes, fields, int. # Must supply exactly one of hex, bytes, fields, int.
h, b, f, i = '00'*16, '\0'*16, (0, 0, 0, 0, 0, 0), 0 h, b, f, i = '00'*16, '\0'*16, (0, 0, 0, 0, 0, 0), 0
uuid.UUID(h) uuid.UUID(h)
uuid.UUID(hex=h) uuid.UUID(hex=h)
uuid.UUID(bytes=b) uuid.UUID(bytes=b)
uuid.UUID(fields=f) uuid.UUID(fields=f)
uuid.UUID(int=i) uuid.UUID(int=i)
# Wrong number of arguments (positional). # Wrong number of arguments (positional).
badtype(lambda: uuid.UUID()) badtype(lambda: uuid.UUID())
badtype(lambda: uuid.UUID(h, b)) badtype(lambda: uuid.UUID(h, b))
badtype(lambda: uuid.UUID(h, b, f)) badtype(lambda: uuid.UUID(h, b, f))
badtype(lambda: uuid.UUID(h, b, f, i)) badtype(lambda: uuid.UUID(h, b, f, i))
# Duplicate arguments (named). # Duplicate arguments (named).
badtype(lambda: uuid.UUID(hex=h, bytes=b)) badtype(lambda: uuid.UUID(hex=h, bytes=b))
badtype(lambda: uuid.UUID(hex=h, fields=f)) badtype(lambda: uuid.UUID(hex=h, fields=f))
badtype(lambda: uuid.UUID(hex=h, int=i)) badtype(lambda: uuid.UUID(hex=h, int=i))
badtype(lambda: uuid.UUID(bytes=b, fields=f)) badtype(lambda: uuid.UUID(bytes=b, fields=f))
badtype(lambda: uuid.UUID(bytes=b, int=i)) badtype(lambda: uuid.UUID(bytes=b, int=i))
badtype(lambda: uuid.UUID(fields=f, int=i)) badtype(lambda: uuid.UUID(fields=f, int=i))
badtype(lambda: uuid.UUID(hex=h, bytes=b, fields=f)) badtype(lambda: uuid.UUID(hex=h, bytes=b, fields=f))
badtype(lambda: uuid.UUID(hex=h, bytes=b, int=i)) badtype(lambda: uuid.UUID(hex=h, bytes=b, int=i))
badtype(lambda: uuid.UUID(hex=h, fields=f, int=i)) badtype(lambda: uuid.UUID(hex=h, fields=f, int=i))
badtype(lambda: uuid.UUID(bytes=b, int=i, fields=f)) badtype(lambda: uuid.UUID(bytes=b, int=i, fields=f))
badtype(lambda: uuid.UUID(hex=h, bytes=b, int=i, fields=f)) badtype(lambda: uuid.UUID(hex=h, bytes=b, int=i, fields=f))
# Duplicate arguments (positional and named). # Duplicate arguments (positional and named).
badtype(lambda: uuid.UUID(h, hex=h)) badtype(lambda: uuid.UUID(h, hex=h))
badtype(lambda: uuid.UUID(h, bytes=b)) badtype(lambda: uuid.UUID(h, bytes=b))
badtype(lambda: uuid.UUID(h, fields=f)) badtype(lambda: uuid.UUID(h, fields=f))
badtype(lambda: uuid.UUID(h, int=i)) badtype(lambda: uuid.UUID(h, int=i))
badtype(lambda: uuid.UUID(h, hex=h, bytes=b)) badtype(lambda: uuid.UUID(h, hex=h, bytes=b))
badtype(lambda: uuid.UUID(h, hex=h, fields=f)) badtype(lambda: uuid.UUID(h, hex=h, fields=f))
badtype(lambda: uuid.UUID(h, hex=h, int=i)) badtype(lambda: uuid.UUID(h, hex=h, int=i))
badtype(lambda: uuid.UUID(h, bytes=b, fields=f)) badtype(lambda: uuid.UUID(h, bytes=b, fields=f))
badtype(lambda: uuid.UUID(h, bytes=b, int=i)) badtype(lambda: uuid.UUID(h, bytes=b, int=i))
badtype(lambda: uuid.UUID(h, fields=f, int=i)) badtype(lambda: uuid.UUID(h, fields=f, int=i))
badtype(lambda: uuid.UUID(h, hex=h, bytes=b, fields=f)) badtype(lambda: uuid.UUID(h, hex=h, bytes=b, fields=f))
badtype(lambda: uuid.UUID(h, hex=h, bytes=b, int=i)) badtype(lambda: uuid.UUID(h, hex=h, bytes=b, int=i))
badtype(lambda: uuid.UUID(h, hex=h, fields=f, int=i)) badtype(lambda: uuid.UUID(h, hex=h, fields=f, int=i))
badtype(lambda: uuid.UUID(h, bytes=b, int=i, fields=f)) badtype(lambda: uuid.UUID(h, bytes=b, int=i, fields=f))
badtype(lambda: uuid.UUID(h, hex=h, bytes=b, int=i, fields=f)) badtype(lambda: uuid.UUID(h, hex=h, bytes=b, int=i, fields=f))
# Immutability. # Immutability.
u = uuid.UUID(h) u = uuid.UUID(h)
badtype(lambda: setattr(u, 'hex', h)) badtype(lambda: setattr(u, 'hex', h))
badtype(lambda: setattr(u, 'bytes', b)) badtype(lambda: setattr(u, 'bytes', b))
badtype(lambda: setattr(u, 'fields', f)) badtype(lambda: setattr(u, 'fields', f))
badtype(lambda: setattr(u, 'int', i)) badtype(lambda: setattr(u, 'int', i))
def check_node(self, node, source=''): def check_node(self, node, source=''):
individual_group_bit = (node >> 40L) & 1 individual_group_bit = (node >> 40L) & 1
universal_local_bit = (node >> 40L) & 2 universal_local_bit = (node >> 40L) & 2
message = "%012x doesn't look like a real MAC address" % node message = "%012x doesn't look like a real MAC address" % node
self.assertEqual(individual_group_bit, 0, message) self.assertEqual(individual_group_bit, 0, message)
self.assertEqual(universal_local_bit, 0, message) self.assertEqual(universal_local_bit, 0, message)
self.assertNotEqual(node, 0, message) self.assertNotEqual(node, 0, message)
self.assertNotEqual(node, 0xffffffffffffL, message) self.assertNotEqual(node, 0xffffffffffffL, message)
self.assert_(0 <= node, message) self.assert_(0 <= node, message)
self.assert_(node < 1<<48L, message) self.assert_(node < 1<<48L, message)
import sys import sys
if source: if source:
sys.stderr.write('(%s: %012x)' % (source, node)) sys.stderr.write('(%s: %012x)' % (source, node))
if TestUUID.last_node: if TestUUID.last_node:
self.assertEqual(TestUUID.last_node, node, 'inconsistent node IDs') self.assertEqual(TestUUID.last_node, node, 'inconsistent node IDs')
else: else:
TestUUID.last_node = node TestUUID.last_node = node
def test_ifconfig_getnode(self): def test_ifconfig_getnode(self):
import os import os
if os.name == 'posix': if os.name == 'posix':
self.check_node(uuid._ifconfig_getnode(), 'ifconfig') self.check_node(uuid._ifconfig_getnode(), 'ifconfig')
def test_ipconfig_getnode(self): def test_ipconfig_getnode(self):
import os import os
if os.name == 'nt': if os.name == 'nt':
self.check_node(uuid._ipconfig_getnode(), 'ipconfig') self.check_node(uuid._ipconfig_getnode(), 'ipconfig')
def test_netbios_getnode(self): def test_netbios_getnode(self):
if importable('win32wnet') and importable('netbios'): if importable('win32wnet') and importable('netbios'):
self.check_node(uuid._netbios_getnode(), 'netbios') self.check_node(uuid._netbios_getnode(), 'netbios')
def test_random_getnode(self): def test_random_getnode(self):
node = uuid._random_getnode() node = uuid._random_getnode()
self.assert_(0 <= node) self.assert_(0 <= node)
self.assert_(node < 1<<48L) self.assert_(node < 1<<48L)
def test_unixdll_getnode(self): def test_unixdll_getnode(self):
import os import os
if importable('ctypes') and os.name == 'posix': if importable('ctypes') and os.name == 'posix':
self.check_node(uuid._unixdll_getnode(), 'unixdll') self.check_node(uuid._unixdll_getnode(), 'unixdll')
def test_windll_getnode(self): def test_windll_getnode(self):
import os import os
if importable('ctypes') and os.name == 'nt': if importable('ctypes') and os.name == 'nt':
self.check_node(uuid._windll_getnode(), 'windll') self.check_node(uuid._windll_getnode(), 'windll')
def test_getnode(self): def test_getnode(self):
self.check_node(uuid.getnode()) self.check_node(uuid.getnode())
# Test it again to ensure consistency. # Test it again to ensure consistency.
self.check_node(uuid.getnode()) self.check_node(uuid.getnode())
def test_uuid1(self): def test_uuid1(self):
equal = self.assertEqual equal = self.assertEqual
# Make sure uuid4() generates UUIDs that are actually version 1. # Make sure uuid4() generates UUIDs that are actually version 1.
for u in [uuid.uuid1() for i in range(10)]: for u in [uuid.uuid1() for i in range(10)]:
equal(u.variant, uuid.RFC_4122) equal(u.variant, uuid.RFC_4122)
equal(u.version, 1) equal(u.version, 1)
# Make sure the supplied node ID appears in the UUID. # Make sure the supplied node ID appears in the UUID.
u = uuid.uuid1(0) u = uuid.uuid1(0)
equal(u.node, 0) equal(u.node, 0)
u = uuid.uuid1(0x123456789abc) u = uuid.uuid1(0x123456789abc)
equal(u.node, 0x123456789abc) equal(u.node, 0x123456789abc)
u = uuid.uuid1(0xffffffffffff) u = uuid.uuid1(0xffffffffffff)
equal(u.node, 0xffffffffffff) equal(u.node, 0xffffffffffff)
# Make sure the supplied clock sequence appears in the UUID. # Make sure the supplied clock sequence appears in the UUID.
u = uuid.uuid1(0x123456789abc, 0) u = uuid.uuid1(0x123456789abc, 0)
equal(u.node, 0x123456789abc) equal(u.node, 0x123456789abc)
equal(((u.clock_seq_hi_variant & 0x3f) << 8) | u.clock_seq_low, 0) equal(((u.clock_seq_hi_variant & 0x3f) << 8) | u.clock_seq_low, 0)
u = uuid.uuid1(0x123456789abc, 0x1234) u = uuid.uuid1(0x123456789abc, 0x1234)
equal(u.node, 0x123456789abc) equal(u.node, 0x123456789abc)
equal(((u.clock_seq_hi_variant & 0x3f) << 8) | equal(((u.clock_seq_hi_variant & 0x3f) << 8) |
u.clock_seq_low, 0x1234) u.clock_seq_low, 0x1234)
u = uuid.uuid1(0x123456789abc, 0x3fff) u = uuid.uuid1(0x123456789abc, 0x3fff)
equal(u.node, 0x123456789abc) equal(u.node, 0x123456789abc)
equal(((u.clock_seq_hi_variant & 0x3f) << 8) | equal(((u.clock_seq_hi_variant & 0x3f) << 8) |
u.clock_seq_low, 0x3fff) u.clock_seq_low, 0x3fff)
def test_uuid3(self): def test_uuid3(self):
equal = self.assertEqual equal = self.assertEqual
# Test some known version-3 UUIDs. # Test some known version-3 UUIDs.
for u, v in [(uuid.uuid3(uuid.NAMESPACE_DNS, 'python.org'), for u, v in [(uuid.uuid3(uuid.NAMESPACE_DNS, 'python.org'),
'6fa459ea-ee8a-3ca4-894e-db77e160355e'), '6fa459ea-ee8a-3ca4-894e-db77e160355e'),
(uuid.uuid3(uuid.NAMESPACE_URL, 'http://python.org/'), (uuid.uuid3(uuid.NAMESPACE_URL, 'http://python.org/'),
'9fe8e8c4-aaa8-32a9-a55c-4535a88b748d'), '9fe8e8c4-aaa8-32a9-a55c-4535a88b748d'),
(uuid.uuid3(uuid.NAMESPACE_OID, '1.3.6.1'), (uuid.uuid3(uuid.NAMESPACE_OID, '1.3.6.1'),
'dd1a1cef-13d5-368a-ad82-eca71acd4cd1'), 'dd1a1cef-13d5-368a-ad82-eca71acd4cd1'),
(uuid.uuid3(uuid.NAMESPACE_X500, 'c=ca'), (uuid.uuid3(uuid.NAMESPACE_X500, 'c=ca'),
'658d3002-db6b-3040-a1d1-8ddd7d189a4d'), '658d3002-db6b-3040-a1d1-8ddd7d189a4d'),
]: ]:
equal(u.variant, uuid.RFC_4122) equal(u.variant, uuid.RFC_4122)
equal(u.version, 3) equal(u.version, 3)
equal(u, uuid.UUID(v)) equal(u, uuid.UUID(v))
equal(str(u), v) equal(str(u), v)
def test_uuid4(self): def test_uuid4(self):
equal = self.assertEqual equal = self.assertEqual
# Make sure uuid4() generates UUIDs that are actually version 4. # Make sure uuid4() generates UUIDs that are actually version 4.
for u in [uuid.uuid4() for i in range(10)]: for u in [uuid.uuid4() for i in range(10)]:
equal(u.variant, uuid.RFC_4122) equal(u.variant, uuid.RFC_4122)
equal(u.version, 4) equal(u.version, 4)
def test_uuid5(self): def test_uuid5(self):
equal = self.assertEqual equal = self.assertEqual
# Test some known version-5 UUIDs. # Test some known version-5 UUIDs.
for u, v in [(uuid.uuid5(uuid.NAMESPACE_DNS, 'python.org'), for u, v in [(uuid.uuid5(uuid.NAMESPACE_DNS, 'python.org'),
'886313e1-3b8a-5372-9b90-0c9aee199e5d'), '886313e1-3b8a-5372-9b90-0c9aee199e5d'),
(uuid.uuid5(uuid.NAMESPACE_URL, 'http://python.org/'), (uuid.uuid5(uuid.NAMESPACE_URL, 'http://python.org/'),
'4c565f0d-3f5a-5890-b41b-20cf47701c5e'), '4c565f0d-3f5a-5890-b41b-20cf47701c5e'),
(uuid.uuid5(uuid.NAMESPACE_OID, '1.3.6.1'), (uuid.uuid5(uuid.NAMESPACE_OID, '1.3.6.1'),
'1447fa61-5277-5fef-a9b3-fbc6e44f4af3'), '1447fa61-5277-5fef-a9b3-fbc6e44f4af3'),
(uuid.uuid5(uuid.NAMESPACE_X500, 'c=ca'), (uuid.uuid5(uuid.NAMESPACE_X500, 'c=ca'),
'cc957dd1-a972-5349-98cd-874190002798'), 'cc957dd1-a972-5349-98cd-874190002798'),
]: ]:
equal(u.variant, uuid.RFC_4122) equal(u.variant, uuid.RFC_4122)
equal(u.version, 5) equal(u.version, 5)
equal(u, uuid.UUID(v)) equal(u, uuid.UUID(v))
equal(str(u), v) equal(str(u), v)
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View File

@ -1,477 +1,477 @@
r"""UUID objects (universally unique identifiers) according to RFC 4122. r"""UUID objects (universally unique identifiers) according to RFC 4122.
This module provides immutable UUID objects (class UUID) and the functions This module provides immutable UUID objects (class UUID) and the functions
uuid1(), uuid3(), uuid4(), uuid5() for generating version 1, 3, 4, and 5 uuid1(), uuid3(), uuid4(), uuid5() for generating version 1, 3, 4, and 5
UUIDs as specified in RFC 4122. UUIDs as specified in RFC 4122.
If all you want is a unique ID, you should probably call uuid1() or uuid4(). If all you want is a unique ID, you should probably call uuid1() or uuid4().
Note that uuid1() may compromise privacy since it creates a UUID containing Note that uuid1() may compromise privacy since it creates a UUID containing
the computer's network address. uuid4() creates a random UUID. the computer's network address. uuid4() creates a random UUID.
Typical usage: Typical usage:
>>> import uuid >>> import uuid
# make a UUID based on the host ID and current time # make a UUID based on the host ID and current time
>>> uuid.uuid1() >>> uuid.uuid1()
UUID('a8098c1a-f86e-11da-bd1a-00112444be1e') UUID('a8098c1a-f86e-11da-bd1a-00112444be1e')
# make a UUID using an MD5 hash of a namespace UUID and a name # make a UUID using an MD5 hash of a namespace UUID and a name
>>> uuid.uuid3(uuid.NAMESPACE_DNS, 'python.org') >>> uuid.uuid3(uuid.NAMESPACE_DNS, 'python.org')
UUID('6fa459ea-ee8a-3ca4-894e-db77e160355e') UUID('6fa459ea-ee8a-3ca4-894e-db77e160355e')
# make a random UUID # make a random UUID
>>> uuid.uuid4() >>> uuid.uuid4()
UUID('16fd2706-8baf-433b-82eb-8c7fada847da') UUID('16fd2706-8baf-433b-82eb-8c7fada847da')
# make a UUID using a SHA-1 hash of a namespace UUID and a name # make a UUID using a SHA-1 hash of a namespace UUID and a name
>>> uuid.uuid5(uuid.NAMESPACE_DNS, 'python.org') >>> uuid.uuid5(uuid.NAMESPACE_DNS, 'python.org')
UUID('886313e1-3b8a-5372-9b90-0c9aee199e5d') UUID('886313e1-3b8a-5372-9b90-0c9aee199e5d')
# make a UUID from a string of hex digits (braces and hyphens ignored) # make a UUID from a string of hex digits (braces and hyphens ignored)
>>> x = uuid.UUID('{00010203-0405-0607-0809-0a0b0c0d0e0f}') >>> x = uuid.UUID('{00010203-0405-0607-0809-0a0b0c0d0e0f}')
# convert a UUID to a string of hex digits in standard form # convert a UUID to a string of hex digits in standard form
>>> str(x) >>> str(x)
'00010203-0405-0607-0809-0a0b0c0d0e0f' '00010203-0405-0607-0809-0a0b0c0d0e0f'
# get the raw 16 bytes of the UUID # get the raw 16 bytes of the UUID
>>> x.bytes >>> x.bytes
'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f' '\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f'
# make a UUID from a 16-byte string # make a UUID from a 16-byte string
>>> uuid.UUID(bytes=x.bytes) >>> uuid.UUID(bytes=x.bytes)
UUID('00010203-0405-0607-0809-0a0b0c0d0e0f') UUID('00010203-0405-0607-0809-0a0b0c0d0e0f')
This module works with Python 2.3 or higher.""" This module works with Python 2.3 or higher."""
__author__ = 'Ka-Ping Yee <ping@zesty.ca>' __author__ = 'Ka-Ping Yee <ping@zesty.ca>'
__date__ = '$Date: 2006/06/12 23:15:40 $'.split()[1].replace('/', '-') __date__ = '$Date: 2006/06/12 23:15:40 $'.split()[1].replace('/', '-')
__version__ = '$Revision: 1.30 $'.split()[1] __version__ = '$Revision: 1.30 $'.split()[1]
RESERVED_NCS, RFC_4122, RESERVED_MICROSOFT, RESERVED_FUTURE = [ RESERVED_NCS, RFC_4122, RESERVED_MICROSOFT, RESERVED_FUTURE = [
'reserved for NCS compatibility', 'specified in RFC 4122', 'reserved for NCS compatibility', 'specified in RFC 4122',
'reserved for Microsoft compatibility', 'reserved for future definition'] 'reserved for Microsoft compatibility', 'reserved for future definition']
class UUID(object): class UUID(object):
"""Instances of the UUID class represent UUIDs as specified in RFC 4122. """Instances of the UUID class represent UUIDs as specified in RFC 4122.
UUID objects are immutable, hashable, and usable as dictionary keys. UUID objects are immutable, hashable, and usable as dictionary keys.
Converting a UUID to a string with str() yields something in the form Converting a UUID to a string with str() yields something in the form
'12345678-1234-1234-1234-123456789abc'. The UUID constructor accepts '12345678-1234-1234-1234-123456789abc'. The UUID constructor accepts
four possible forms: a similar string of hexadecimal digits, or a four possible forms: a similar string of hexadecimal digits, or a
string of 16 raw bytes as an argument named 'bytes', or a tuple of string of 16 raw bytes as an argument named 'bytes', or a tuple of
six integer fields (with 32-bit, 16-bit, 16-bit, 8-bit, 8-bit, and six integer fields (with 32-bit, 16-bit, 16-bit, 8-bit, 8-bit, and
48-bit values respectively) as an argument named 'fields', or a single 48-bit values respectively) as an argument named 'fields', or a single
128-bit integer as an argument named 'int'. 128-bit integer as an argument named 'int'.
UUIDs have these read-only attributes: UUIDs have these read-only attributes:
bytes the UUID as a 16-byte string bytes the UUID as a 16-byte string
fields a tuple of the six integer fields of the UUID, fields a tuple of the six integer fields of the UUID,
which are also available as six individual attributes which are also available as six individual attributes
and two derived attributes: and two derived attributes:
time_low the first 32 bits of the UUID time_low the first 32 bits of the UUID
time_mid the next 16 bits of the UUID time_mid the next 16 bits of the UUID
time_hi_version the next 16 bits of the UUID time_hi_version the next 16 bits of the UUID
clock_seq_hi_variant the next 8 bits of the UUID clock_seq_hi_variant the next 8 bits of the UUID
clock_seq_low the next 8 bits of the UUID clock_seq_low the next 8 bits of the UUID
node the last 48 bits of the UUID node the last 48 bits of the UUID
time the 60-bit timestamp time the 60-bit timestamp
clock_seq the 14-bit sequence number clock_seq the 14-bit sequence number
hex the UUID as a 32-character hexadecimal string hex the UUID as a 32-character hexadecimal string
int the UUID as a 128-bit integer int the UUID as a 128-bit integer
urn the UUID as a URN as specified in RFC 4122 urn the UUID as a URN as specified in RFC 4122
variant the UUID variant (one of the constants RESERVED_NCS, variant the UUID variant (one of the constants RESERVED_NCS,
RFC_4122, RESERVED_MICROSOFT, or RESERVED_FUTURE) RFC_4122, RESERVED_MICROSOFT, or RESERVED_FUTURE)
version the UUID version number (1 through 5, meaningful only version the UUID version number (1 through 5, meaningful only
when the variant is RFC_4122) when the variant is RFC_4122)
""" """
def __init__(self, hex=None, bytes=None, fields=None, int=None, def __init__(self, hex=None, bytes=None, fields=None, int=None,
version=None): version=None):
r"""Create a UUID from either a string of 32 hexadecimal digits, r"""Create a UUID from either a string of 32 hexadecimal digits,
a string of 16 bytes as the 'bytes' argument, a tuple of six a string of 16 bytes as the 'bytes' argument, a tuple of six
integers (32-bit time_low, 16-bit time_mid, 16-bit time_hi_version, integers (32-bit time_low, 16-bit time_mid, 16-bit time_hi_version,
8-bit clock_seq_hi_variant, 8-bit clock_seq_low, 48-bit node) as 8-bit clock_seq_hi_variant, 8-bit clock_seq_low, 48-bit node) as
the 'fields' argument, or a single 128-bit integer as the 'int' the 'fields' argument, or a single 128-bit integer as the 'int'
argument. When a string of hex digits is given, curly braces, argument. When a string of hex digits is given, curly braces,
hyphens, and a URN prefix are all optional. For example, these hyphens, and a URN prefix are all optional. For example, these
expressions all yield the same UUID: expressions all yield the same UUID:
UUID('{12345678-1234-5678-1234-567812345678}') UUID('{12345678-1234-5678-1234-567812345678}')
UUID('12345678123456781234567812345678') UUID('12345678123456781234567812345678')
UUID('urn:uuid:12345678-1234-5678-1234-567812345678') UUID('urn:uuid:12345678-1234-5678-1234-567812345678')
UUID(bytes='\x12\x34\x56\x78'*4) UUID(bytes='\x12\x34\x56\x78'*4)
UUID(fields=(0x12345678, 0x1234, 0x5678, 0x12, 0x34, 0x567812345678)) UUID(fields=(0x12345678, 0x1234, 0x5678, 0x12, 0x34, 0x567812345678))
UUID(int=0x12345678123456781234567812345678) UUID(int=0x12345678123456781234567812345678)
Exactly one of 'hex', 'bytes', 'fields', or 'int' must be given. Exactly one of 'hex', 'bytes', 'fields', or 'int' must be given.
The 'version' argument is optional; if given, the resulting UUID The 'version' argument is optional; if given, the resulting UUID
will have its variant and version number set according to RFC 4122, will have its variant and version number set according to RFC 4122,
overriding bits in the given 'hex', 'bytes', 'fields', or 'int'. overriding bits in the given 'hex', 'bytes', 'fields', or 'int'.
""" """
if [hex, bytes, fields, int].count(None) != 3: if [hex, bytes, fields, int].count(None) != 3:
raise TypeError('need just one of hex, bytes, fields, or int') raise TypeError('need just one of hex, bytes, fields, or int')
if hex is not None: if hex is not None:
hex = hex.replace('urn:', '').replace('uuid:', '') hex = hex.replace('urn:', '').replace('uuid:', '')
hex = hex.strip('{}').replace('-', '') hex = hex.strip('{}').replace('-', '')
if len(hex) != 32: if len(hex) != 32:
raise ValueError('badly formed hexadecimal UUID string') raise ValueError('badly formed hexadecimal UUID string')
int = long(hex, 16) int = long(hex, 16)
if bytes is not None: if bytes is not None:
if len(bytes) != 16: if len(bytes) != 16:
raise ValueError('bytes is not a 16-char string') raise ValueError('bytes is not a 16-char string')
int = long(('%02x'*16) % tuple(map(ord, bytes)), 16) int = long(('%02x'*16) % tuple(map(ord, bytes)), 16)
if fields is not None: if fields is not None:
if len(fields) != 6: if len(fields) != 6:
raise ValueError('fields is not a 6-tuple') raise ValueError('fields is not a 6-tuple')
(time_low, time_mid, time_hi_version, (time_low, time_mid, time_hi_version,
clock_seq_hi_variant, clock_seq_low, node) = fields clock_seq_hi_variant, clock_seq_low, node) = fields
if not 0 <= time_low < 1<<32L: if not 0 <= time_low < 1<<32L:
raise ValueError('field 1 out of range (need a 32-bit value)') raise ValueError('field 1 out of range (need a 32-bit value)')
if not 0 <= time_mid < 1<<16L: if not 0 <= time_mid < 1<<16L:
raise ValueError('field 2 out of range (need a 16-bit value)') raise ValueError('field 2 out of range (need a 16-bit value)')
if not 0 <= time_hi_version < 1<<16L: if not 0 <= time_hi_version < 1<<16L:
raise ValueError('field 3 out of range (need a 16-bit value)') raise ValueError('field 3 out of range (need a 16-bit value)')
if not 0 <= clock_seq_hi_variant < 1<<8L: if not 0 <= clock_seq_hi_variant < 1<<8L:
raise ValueError('field 4 out of range (need an 8-bit value)') raise ValueError('field 4 out of range (need an 8-bit value)')
if not 0 <= clock_seq_low < 1<<8L: if not 0 <= clock_seq_low < 1<<8L:
raise ValueError('field 5 out of range (need an 8-bit value)') raise ValueError('field 5 out of range (need an 8-bit value)')
if not 0 <= node < 1<<48L: if not 0 <= node < 1<<48L:
raise ValueError('field 6 out of range (need a 48-bit value)') raise ValueError('field 6 out of range (need a 48-bit value)')
clock_seq = (clock_seq_hi_variant << 8L) | clock_seq_low clock_seq = (clock_seq_hi_variant << 8L) | clock_seq_low
int = ((time_low << 96L) | (time_mid << 80L) | int = ((time_low << 96L) | (time_mid << 80L) |
(time_hi_version << 64L) | (clock_seq << 48L) | node) (time_hi_version << 64L) | (clock_seq << 48L) | node)
if int is not None: if int is not None:
if not 0 <= int < 1<<128L: if not 0 <= int < 1<<128L:
raise ValueError('int is out of range (need a 128-bit value)') raise ValueError('int is out of range (need a 128-bit value)')
if version is not None: if version is not None:
if not 1 <= version <= 5: if not 1 <= version <= 5:
raise ValueError('illegal version number') raise ValueError('illegal version number')
# Set the variant to RFC 4122. # Set the variant to RFC 4122.
int &= ~(0xc000 << 48L) int &= ~(0xc000 << 48L)
int |= 0x8000 << 48L int |= 0x8000 << 48L
# Set the version number. # Set the version number.
int &= ~(0xf000 << 64L) int &= ~(0xf000 << 64L)
int |= version << 76L int |= version << 76L
self.__dict__['int'] = int self.__dict__['int'] = int
def __cmp__(self, other): def __cmp__(self, other):
if isinstance(other, UUID): if isinstance(other, UUID):
return cmp(self.int, other.int) return cmp(self.int, other.int)
return NotImplemented return NotImplemented
def __hash__(self): def __hash__(self):
return hash(self.int) return hash(self.int)
def __int__(self): def __int__(self):
return self.int return self.int
def __repr__(self): def __repr__(self):
return 'UUID(%r)' % str(self) return 'UUID(%r)' % str(self)
def __setattr__(self, name, value): def __setattr__(self, name, value):
raise TypeError('UUID objects are immutable') raise TypeError('UUID objects are immutable')
def __str__(self): def __str__(self):
hex = '%032x' % self.int hex = '%032x' % self.int
return '%s-%s-%s-%s-%s' % ( return '%s-%s-%s-%s-%s' % (
hex[:8], hex[8:12], hex[12:16], hex[16:20], hex[20:]) hex[:8], hex[8:12], hex[12:16], hex[16:20], hex[20:])
def get_bytes(self): def get_bytes(self):
bytes = '' bytes = ''
for shift in range(0, 128, 8): for shift in range(0, 128, 8):
bytes = chr((self.int >> shift) & 0xff) + bytes bytes = chr((self.int >> shift) & 0xff) + bytes
return bytes return bytes
bytes = property(get_bytes) bytes = property(get_bytes)
def get_fields(self): def get_fields(self):
return (self.time_low, self.time_mid, self.time_hi_version, return (self.time_low, self.time_mid, self.time_hi_version,
self.clock_seq_hi_variant, self.clock_seq_low, self.node) self.clock_seq_hi_variant, self.clock_seq_low, self.node)
fields = property(get_fields) fields = property(get_fields)
def get_time_low(self): def get_time_low(self):
return self.int >> 96L return self.int >> 96L
time_low = property(get_time_low) time_low = property(get_time_low)
def get_time_mid(self): def get_time_mid(self):
return (self.int >> 80L) & 0xffff return (self.int >> 80L) & 0xffff
time_mid = property(get_time_mid) time_mid = property(get_time_mid)
def get_time_hi_version(self): def get_time_hi_version(self):
return (self.int >> 64L) & 0xffff return (self.int >> 64L) & 0xffff
time_hi_version = property(get_time_hi_version) time_hi_version = property(get_time_hi_version)
def get_clock_seq_hi_variant(self): def get_clock_seq_hi_variant(self):
return (self.int >> 56L) & 0xff return (self.int >> 56L) & 0xff
clock_seq_hi_variant = property(get_clock_seq_hi_variant) clock_seq_hi_variant = property(get_clock_seq_hi_variant)
def get_clock_seq_low(self): def get_clock_seq_low(self):
return (self.int >> 48L) & 0xff return (self.int >> 48L) & 0xff
clock_seq_low = property(get_clock_seq_low) clock_seq_low = property(get_clock_seq_low)
def get_time(self): def get_time(self):
return (((self.time_hi_version & 0x0fffL) << 48L) | return (((self.time_hi_version & 0x0fffL) << 48L) |
(self.time_mid << 32L) | self.time_low) (self.time_mid << 32L) | self.time_low)
time = property(get_time) time = property(get_time)
def get_clock_seq(self): def get_clock_seq(self):
return (((self.clock_seq_hi_variant & 0x3fL) << 8L) | return (((self.clock_seq_hi_variant & 0x3fL) << 8L) |
self.clock_seq_low) self.clock_seq_low)
clock_seq = property(get_clock_seq) clock_seq = property(get_clock_seq)
def get_node(self): def get_node(self):
return self.int & 0xffffffffffff return self.int & 0xffffffffffff
node = property(get_node) node = property(get_node)
def get_hex(self): def get_hex(self):
return '%032x' % self.int return '%032x' % self.int
hex = property(get_hex) hex = property(get_hex)
def get_urn(self): def get_urn(self):
return 'urn:uuid:' + str(self) return 'urn:uuid:' + str(self)
urn = property(get_urn) urn = property(get_urn)
def get_variant(self): def get_variant(self):
if not self.int & (0x8000 << 48L): if not self.int & (0x8000 << 48L):
return RESERVED_NCS return RESERVED_NCS
elif not self.int & (0x4000 << 48L): elif not self.int & (0x4000 << 48L):
return RFC_4122 return RFC_4122
elif not self.int & (0x2000 << 48L): elif not self.int & (0x2000 << 48L):
return RESERVED_MICROSOFT return RESERVED_MICROSOFT
else: else:
return RESERVED_FUTURE return RESERVED_FUTURE
variant = property(get_variant) variant = property(get_variant)
def get_version(self): def get_version(self):
# The version bits are only meaningful for RFC 4122 UUIDs. # The version bits are only meaningful for RFC 4122 UUIDs.
if self.variant == RFC_4122: if self.variant == RFC_4122:
return int((self.int >> 76L) & 0xf) return int((self.int >> 76L) & 0xf)
version = property(get_version) version = property(get_version)
def _ifconfig_getnode(): def _ifconfig_getnode():
"""Get the hardware address on Unix by running ifconfig.""" """Get the hardware address on Unix by running ifconfig."""
import os import os
for dir in ['', '/sbin/', '/usr/sbin']: for dir in ['', '/sbin/', '/usr/sbin']:
try: try:
pipe = os.popen(os.path.join(dir, 'ifconfig')) pipe = os.popen(os.path.join(dir, 'ifconfig'))
except IOError: except IOError:
continue continue
for line in pipe: for line in pipe:
words = line.lower().split() words = line.lower().split()
for i in range(len(words)): for i in range(len(words)):
if words[i] in ['hwaddr', 'ether']: if words[i] in ['hwaddr', 'ether']:
return int(words[i + 1].replace(':', ''), 16) return int(words[i + 1].replace(':', ''), 16)
def _ipconfig_getnode(): def _ipconfig_getnode():
"""Get the hardware address on Windows by running ipconfig.exe.""" """Get the hardware address on Windows by running ipconfig.exe."""
import os, re import os, re
dirs = ['', r'c:\windows\system32', r'c:\winnt\system32'] dirs = ['', r'c:\windows\system32', r'c:\winnt\system32']
try: try:
import ctypes import ctypes
buffer = ctypes.create_string_buffer(300) buffer = ctypes.create_string_buffer(300)
ctypes.windll.kernel32.GetSystemDirectoryA(buffer, 300) ctypes.windll.kernel32.GetSystemDirectoryA(buffer, 300)
dirs.insert(0, buffer.value.decode('mbcs')) dirs.insert(0, buffer.value.decode('mbcs'))
except: except:
pass pass
for dir in dirs: for dir in dirs:
try: try:
pipe = os.popen(os.path.join(dir, 'ipconfig') + ' /all') pipe = os.popen(os.path.join(dir, 'ipconfig') + ' /all')
except IOError: except IOError:
continue continue
for line in pipe: for line in pipe:
value = line.split(':')[-1].strip().lower() value = line.split(':')[-1].strip().lower()
if re.match('([0-9a-f][0-9a-f]-){5}[0-9a-f][0-9a-f]', value): if re.match('([0-9a-f][0-9a-f]-){5}[0-9a-f][0-9a-f]', value):
return int(value.replace('-', ''), 16) return int(value.replace('-', ''), 16)
def _netbios_getnode(): def _netbios_getnode():
"""Get the hardware address on Windows using NetBIOS calls. """Get the hardware address on Windows using NetBIOS calls.
See http://support.microsoft.com/kb/118623 for details.""" See http://support.microsoft.com/kb/118623 for details."""
import win32wnet, netbios import win32wnet, netbios
ncb = netbios.NCB() ncb = netbios.NCB()
ncb.Command = netbios.NCBENUM ncb.Command = netbios.NCBENUM
ncb.Buffer = adapters = netbios.LANA_ENUM() ncb.Buffer = adapters = netbios.LANA_ENUM()
adapters._pack() adapters._pack()
if win32wnet.Netbios(ncb) != 0: if win32wnet.Netbios(ncb) != 0:
return return
adapters._unpack() adapters._unpack()
for i in range(adapters.length): for i in range(adapters.length):
ncb.Reset() ncb.Reset()
ncb.Command = netbios.NCBRESET ncb.Command = netbios.NCBRESET
ncb.Lana_num = ord(adapters.lana[i]) ncb.Lana_num = ord(adapters.lana[i])
if win32wnet.Netbios(ncb) != 0: if win32wnet.Netbios(ncb) != 0:
continue continue
ncb.Reset() ncb.Reset()
ncb.Command = netbios.NCBASTAT ncb.Command = netbios.NCBASTAT
ncb.Lana_num = ord(adapters.lana[i]) ncb.Lana_num = ord(adapters.lana[i])
ncb.Callname = '*'.ljust(16) ncb.Callname = '*'.ljust(16)
ncb.Buffer = status = netbios.ADAPTER_STATUS() ncb.Buffer = status = netbios.ADAPTER_STATUS()
if win32wnet.Netbios(ncb) != 0: if win32wnet.Netbios(ncb) != 0:
continue continue
status._unpack() status._unpack()
bytes = map(ord, status.adapter_address) bytes = map(ord, status.adapter_address)
return ((bytes[0]<<40L) + (bytes[1]<<32L) + (bytes[2]<<24L) + return ((bytes[0]<<40L) + (bytes[1]<<32L) + (bytes[2]<<24L) +
(bytes[3]<<16L) + (bytes[4]<<8L) + bytes[5]) (bytes[3]<<16L) + (bytes[4]<<8L) + bytes[5])
# Thanks to Thomas Heller for ctypes and for his help with its use here. # Thanks to Thomas Heller for ctypes and for his help with its use here.
# If ctypes is available, use it to find system routines for UUID generation. # If ctypes is available, use it to find system routines for UUID generation.
_uuid_generate_random = _uuid_generate_time = _UuidCreate = None _uuid_generate_random = _uuid_generate_time = _UuidCreate = None
try: try:
import ctypes, ctypes.util import ctypes, ctypes.util
_buffer = ctypes.create_string_buffer(16) _buffer = ctypes.create_string_buffer(16)
# The uuid_generate_* routines are provided by libuuid on at least # The uuid_generate_* routines are provided by libuuid on at least
# Linux and FreeBSD, and provided by libc on Mac OS X. # Linux and FreeBSD, and provided by libc on Mac OS X.
for libname in ['uuid', 'c']: for libname in ['uuid', 'c']:
try: try:
lib = ctypes.CDLL(ctypes.util.find_library(libname)) lib = ctypes.CDLL(ctypes.util.find_library(libname))
except: except:
continue continue
if hasattr(lib, 'uuid_generate_random'): if hasattr(lib, 'uuid_generate_random'):
_uuid_generate_random = lib.uuid_generate_random _uuid_generate_random = lib.uuid_generate_random
if hasattr(lib, 'uuid_generate_time'): if hasattr(lib, 'uuid_generate_time'):
_uuid_generate_time = lib.uuid_generate_time _uuid_generate_time = lib.uuid_generate_time
# On Windows prior to 2000, UuidCreate gives a UUID containing the # On Windows prior to 2000, UuidCreate gives a UUID containing the
# hardware address. On Windows 2000 and later, UuidCreate makes a # hardware address. On Windows 2000 and later, UuidCreate makes a
# random UUID and UuidCreateSequential gives a UUID containing the # random UUID and UuidCreateSequential gives a UUID containing the
# hardware address. These routines are provided by the RPC runtime. # hardware address. These routines are provided by the RPC runtime.
try: try:
lib = ctypes.windll.rpcrt4 lib = ctypes.windll.rpcrt4
except: except:
lib = None lib = None
_UuidCreate = getattr(lib, 'UuidCreateSequential', _UuidCreate = getattr(lib, 'UuidCreateSequential',
getattr(lib, 'UuidCreate', None)) getattr(lib, 'UuidCreate', None))
except: except:
pass pass
def _unixdll_getnode(): def _unixdll_getnode():
"""Get the hardware address on Unix using ctypes.""" """Get the hardware address on Unix using ctypes."""
_uuid_generate_time(_buffer) _uuid_generate_time(_buffer)
return UUID(bytes=_buffer.raw).node return UUID(bytes=_buffer.raw).node
def _windll_getnode(): def _windll_getnode():
"""Get the hardware address on Windows using ctypes.""" """Get the hardware address on Windows using ctypes."""
if _UuidCreate(_buffer) == 0: if _UuidCreate(_buffer) == 0:
return UUID(bytes=_buffer.raw).node return UUID(bytes=_buffer.raw).node
def _random_getnode(): def _random_getnode():
"""Get a random node ID, with eighth bit set as suggested by RFC 4122.""" """Get a random node ID, with eighth bit set as suggested by RFC 4122."""
import random import random
return random.randrange(0, 1<<48L) | 0x010000000000L return random.randrange(0, 1<<48L) | 0x010000000000L
_node = None _node = None
def getnode(): def getnode():
"""Get the hardware address as a 48-bit integer. The first time this """Get the hardware address as a 48-bit integer. The first time this
runs, it may launch a separate program, which could be quite slow. If runs, it may launch a separate program, which could be quite slow. If
all attempts to obtain the hardware address fail, we choose a random all attempts to obtain the hardware address fail, we choose a random
48-bit number with its eighth bit set to 1 as recommended in RFC 4122.""" 48-bit number with its eighth bit set to 1 as recommended in RFC 4122."""
global _node global _node
if _node is not None: if _node is not None:
return _node return _node
import sys import sys
if sys.platform == 'win32': if sys.platform == 'win32':
getters = [_windll_getnode, _netbios_getnode, _ipconfig_getnode] getters = [_windll_getnode, _netbios_getnode, _ipconfig_getnode]
else: else:
getters = [_unixdll_getnode, _ifconfig_getnode] getters = [_unixdll_getnode, _ifconfig_getnode]
for getter in getters + [_random_getnode]: for getter in getters + [_random_getnode]:
try: try:
_node = getter() _node = getter()
except: except:
continue continue
if _node is not None: if _node is not None:
return _node return _node
def uuid1(node=None, clock_seq=None): def uuid1(node=None, clock_seq=None):
"""Generate a UUID from a host ID, sequence number, and the current time. """Generate a UUID from a host ID, sequence number, and the current time.
If 'node' is not given, getnode() is used to obtain the hardware If 'node' is not given, getnode() is used to obtain the hardware
address. If 'clock_seq' is given, it is used as the sequence number; address. If 'clock_seq' is given, it is used as the sequence number;
otherwise a random 14-bit sequence number is chosen.""" otherwise a random 14-bit sequence number is chosen."""
# When the system provides a version-1 UUID generator, use it (but don't # When the system provides a version-1 UUID generator, use it (but don't
# use UuidCreate here because its UUIDs don't conform to RFC 4122). # use UuidCreate here because its UUIDs don't conform to RFC 4122).
if _uuid_generate_time and node is clock_seq is None: if _uuid_generate_time and node is clock_seq is None:
_uuid_generate_time(_buffer) _uuid_generate_time(_buffer)
return UUID(bytes=_buffer.raw) return UUID(bytes=_buffer.raw)
import time import time
nanoseconds = int(time.time() * 1e9) nanoseconds = int(time.time() * 1e9)
# 0x01b21dd213814000 is the number of 100-ns intervals between the # 0x01b21dd213814000 is the number of 100-ns intervals between the
# UUID epoch 1582-10-15 00:00:00 and the Unix epoch 1970-01-01 00:00:00. # UUID epoch 1582-10-15 00:00:00 and the Unix epoch 1970-01-01 00:00:00.
timestamp = int(nanoseconds/100) + 0x01b21dd213814000L timestamp = int(nanoseconds/100) + 0x01b21dd213814000L
if clock_seq is None: if clock_seq is None:
import random import random
clock_seq = random.randrange(1<<14L) # instead of stable storage clock_seq = random.randrange(1<<14L) # instead of stable storage
time_low = timestamp & 0xffffffffL time_low = timestamp & 0xffffffffL
time_mid = (timestamp >> 32L) & 0xffffL time_mid = (timestamp >> 32L) & 0xffffL
time_hi_version = (timestamp >> 48L) & 0x0fffL time_hi_version = (timestamp >> 48L) & 0x0fffL
clock_seq_low = clock_seq & 0xffL clock_seq_low = clock_seq & 0xffL
clock_seq_hi_variant = (clock_seq >> 8L) & 0x3fL clock_seq_hi_variant = (clock_seq >> 8L) & 0x3fL
if node is None: if node is None:
node = getnode() node = getnode()
return UUID(fields=(time_low, time_mid, time_hi_version, return UUID(fields=(time_low, time_mid, time_hi_version,
clock_seq_hi_variant, clock_seq_low, node), version=1) clock_seq_hi_variant, clock_seq_low, node), version=1)
def uuid3(namespace, name): def uuid3(namespace, name):
"""Generate a UUID from the MD5 hash of a namespace UUID and a name.""" """Generate a UUID from the MD5 hash of a namespace UUID and a name."""
import md5 import md5
hash = md5.md5(namespace.bytes + name).digest() hash = md5.md5(namespace.bytes + name).digest()
return UUID(bytes=hash[:16], version=3) return UUID(bytes=hash[:16], version=3)
def uuid4(): def uuid4():
"""Generate a random UUID.""" """Generate a random UUID."""
# When the system provides a version-4 UUID generator, use it. # When the system provides a version-4 UUID generator, use it.
if _uuid_generate_random: if _uuid_generate_random:
_uuid_generate_random(_buffer) _uuid_generate_random(_buffer)
return UUID(bytes=_buffer.raw) return UUID(bytes=_buffer.raw)
# Otherwise, get randomness from urandom or the 'random' module. # Otherwise, get randomness from urandom or the 'random' module.
try: try:
import os import os
return UUID(bytes=os.urandom(16), version=4) return UUID(bytes=os.urandom(16), version=4)
except: except:
import random import random
bytes = [chr(random.randrange(256)) for i in range(16)] bytes = [chr(random.randrange(256)) for i in range(16)]
return UUID(bytes=bytes, version=4) return UUID(bytes=bytes, version=4)
def uuid5(namespace, name): def uuid5(namespace, name):
"""Generate a UUID from the SHA-1 hash of a namespace UUID and a name.""" """Generate a UUID from the SHA-1 hash of a namespace UUID and a name."""
import sha import sha
hash = sha.sha(namespace.bytes + name).digest() hash = sha.sha(namespace.bytes + name).digest()
return UUID(bytes=hash[:16], version=5) return UUID(bytes=hash[:16], version=5)
# The following standard UUIDs are for use with uuid3() or uuid5(). # The following standard UUIDs are for use with uuid3() or uuid5().
NAMESPACE_DNS = UUID('6ba7b810-9dad-11d1-80b4-00c04fd430c8') NAMESPACE_DNS = UUID('6ba7b810-9dad-11d1-80b4-00c04fd430c8')
NAMESPACE_URL = UUID('6ba7b811-9dad-11d1-80b4-00c04fd430c8') NAMESPACE_URL = UUID('6ba7b811-9dad-11d1-80b4-00c04fd430c8')
NAMESPACE_OID = UUID('6ba7b812-9dad-11d1-80b4-00c04fd430c8') NAMESPACE_OID = UUID('6ba7b812-9dad-11d1-80b4-00c04fd430c8')
NAMESPACE_X500 = UUID('6ba7b814-9dad-11d1-80b4-00c04fd430c8') NAMESPACE_X500 = UUID('6ba7b814-9dad-11d1-80b4-00c04fd430c8')