def uuid2slug(uuid_str): """ :param uuid_str : str(UUID) :returns: ``encoded string of the given UUID``` e.g : UUID('737d4ea0-28bf-11e6-b12e-68f7288f1809')-> c31OoCi_EeaxLmj3KI8YCQ """ encode = UUID(str(uuid_str)).bytes.encode('base64') # replace invalid char as vol name return encode.rstrip('=\n').replace('/', '_').replace('+', '-')
def uuid4_to_bytes(uuid4): """Convert a given uuid4 to the right number of bytes. - If it is a fully random uuid4, return the 16 bytes - If it looks like a manually build uuid4, remove the fake bytes """ bytes_array = UUID(uuid4).bytes is_built_uuid4 = bytes_array[6] in (b"\x40", ord(b"\x40")) and bytes_array[8] in (b"\x80", ord(b"\x80")) if is_built_uuid4: bytes_array = b"".join((bytes_array[:6], bytes_array[7:8], bytes_array[9:16])) return bytes_array.rstrip(b"\x00").ljust(9, b"\x00")