codekingpro/portable-devtools
114k
1from uuid import (
2 NAMESPACE_DNS,
3 NAMESPACE_OID,
4 NAMESPACE_URL,
5 NAMESPACE_X500,
6 RESERVED_FUTURE,
7 RESERVED_MICROSOFT,
8 RESERVED_NCS,
9 RFC_4122,
10 UUID,
11 getnode,
12)
13
14import uuid_utils
15
16NIL = UUID("00000000-0000-0000-0000-000000000000")
17MAX = UUID("ffffffff-ffff-ffff-ffff-ffffffffffff")
18
19
20def uuid1(node=None, clock_seq=None):
21 """Generate a UUID from a host ID, sequence number, and the current time.
22 If 'node' is not given, getnode() is used to obtain the hardware
23 address. If 'clock_seq' is given, it is used as the sequence number;
24 otherwise a random 14-bit sequence number is chosen."""
25 return UUID(int=uuid_utils.uuid1(node, clock_seq).int)
26
27
28def uuid3(namespace, name):
29 """Generate a UUID from the MD5 hash of a namespace UUID and a name."""
30 namespace = uuid_utils.UUID(namespace.hex) if namespace else namespace
31 return UUID(int=uuid_utils.uuid3(namespace, name).int)
32
33
34def uuid4():
35 """Generate a random UUID."""
36 return UUID(int=uuid_utils.uuid4().int)
37
38
39def uuid5(namespace, name):
40 """Generate a UUID from the SHA-1 hash of a namespace UUID and a name."""
41 namespace = uuid_utils.UUID(namespace.hex) if namespace else namespace
42 return UUID(int=uuid_utils.uuid5(namespace, name).int)
43
44
45def uuid6(node=None, timestamp=None):
46 """Generate a version 6 UUID using the given timestamp and a host ID.
47 This is similar to version 1 UUIDs,
48 except that it is lexicographically sortable by timestamp.
49 """
50 return UUID(int=uuid_utils.uuid6(node, timestamp).int)
51
52
53def uuid7(timestamp=None, nanos=None):
54 """Generate a version 7 UUID using a time value and random bytes."""
55 return UUID(int=uuid_utils.uuid7(timestamp, nanos).int)
56
57
58def uuid8(bytes):
59 """Generate a custom UUID comprised almost entirely of user-supplied bytes."""
60 return UUID(bytes=uuid_utils.uuid8(bytes).bytes)
61
62
63__all__ = [
64 "MAX",
65 "NAMESPACE_DNS",
66 "NAMESPACE_OID",
67 "NAMESPACE_URL",
68 "NAMESPACE_X500",
69 "NIL",
70 "RESERVED_FUTURE",
71 "RESERVED_MICROSOFT",
72 "RESERVED_NCS",
73 "RFC_4122",
74 "UUID",
75 "getnode",
76 "uuid1",
77 "uuid3",
78 "uuid4",
79 "uuid5",
80 "uuid6",
81 "uuid7",
82 "uuid8",
83]
84 