Remove duplicate code
This commit is contained in:
parent
c57307c585
commit
bc0f8b0a47
@ -411,11 +411,12 @@ class Collection(BaseCollection):
|
|||||||
raise
|
raise
|
||||||
self._sync_directory(directory)
|
self._sync_directory(directory)
|
||||||
|
|
||||||
def _find_available_file_name(self):
|
@staticmethod
|
||||||
|
def _find_available_file_name(exists_fn):
|
||||||
# Prevent infinite loop
|
# Prevent infinite loop
|
||||||
for _ in range(10000):
|
for _ in range(10000):
|
||||||
file_name = hex(getrandbits(32))[2:]
|
file_name = hex(getrandbits(32))[2:]
|
||||||
if not self.has(file_name):
|
if not exists_fn(file_name):
|
||||||
return file_name
|
return file_name
|
||||||
raise FileExistsError(errno.EEXIST, "No usable file name found")
|
raise FileExistsError(errno.EEXIST, "No usable file name found")
|
||||||
|
|
||||||
@ -557,21 +558,12 @@ class Collection(BaseCollection):
|
|||||||
new_collection = vobject.iCalendar()
|
new_collection = vobject.iCalendar()
|
||||||
for item in items:
|
for item in items:
|
||||||
new_collection.add(item)
|
new_collection.add(item)
|
||||||
|
href = self._find_available_file_name(vobject_items.get)
|
||||||
# Prevent infinite loop
|
|
||||||
for _ in range(10000):
|
|
||||||
href = self._find_available_file_name()
|
|
||||||
if href not in vobject_items:
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
raise FileExistsError(
|
|
||||||
errno.EEXIST, "No usable file name found")
|
|
||||||
vobject_items[href] = new_collection
|
vobject_items[href] = new_collection
|
||||||
|
|
||||||
self.upload_all(vobject_items)
|
self.upload_all(vobject_items)
|
||||||
elif props.get("tag") == "VCARD":
|
elif props.get("tag") == "VCARD":
|
||||||
for card in collection:
|
for card in collection:
|
||||||
self.upload(self._find_available_file_name(), card)
|
self.upload(self._find_available_file_name(self.has), card)
|
||||||
|
|
||||||
# This operation is not atomic on the filesystem level but it's
|
# This operation is not atomic on the filesystem level but it's
|
||||||
# very unlikely that one rename operations succeeds while the
|
# very unlikely that one rename operations succeeds while the
|
||||||
|
Loading…
x
Reference in New Issue
Block a user