Use UUID format for random UIDs

This commit is contained in:
Unrud 2017-07-01 00:11:44 +02:00
parent 5b3fbb79a1
commit 019dc7a39c

View File

@ -137,6 +137,12 @@ def check_item(vobject_item):
raise ValueError("Unknown item type: %r" % vobject_item.name) raise ValueError("Unknown item type: %r" % vobject_item.name)
def random_uuid4():
"""Generate a pseudo-random UUID"""
r = "%016x" % getrandbits(128)
return "%s-%s-%s-%s-%s" % (r[:8], r[8:12], r[12:16], r[16:20], r[20:])
def scandir(path, only_dirs=False, only_files=False): def scandir(path, only_dirs=False, only_files=False):
"""Iterator for directory elements. (For compatibility with Python < 3.5) """Iterator for directory elements. (For compatibility with Python < 3.5)
@ -600,7 +606,7 @@ class Collection(BaseCollection):
def _find_available_file_name(exists_fn, suffix=""): def _find_available_file_name(exists_fn, suffix=""):
# Prevent infinite loop # Prevent infinite loop
for _ in range(1000): for _ in range(1000):
file_name = "%016x" % getrandbits(64) + suffix file_name = random_uuid4() + suffix
if not exists_fn(file_name): if not exists_fn(file_name):
return file_name return file_name
# something is wrong with the PRNG # something is wrong with the PRNG