From a7f078eb54bdd87a0c8e8e3cb1e1ba42670673d6 Mon Sep 17 00:00:00 2001 From: Guillaume Ayoub Date: Tue, 2 Aug 2016 11:16:24 +0200 Subject: [PATCH] Change find_available_file_name into a method --- radicale/storage.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/radicale/storage.py b/radicale/storage.py index 24e0732..a425a28 100644 --- a/radicale/storage.py +++ b/radicale/storage.py @@ -347,6 +347,14 @@ class Collection(BaseCollection): path, self.storage_encoding, mode).open() as fd: yield fd + def find_available_file_name(self): + # Prevent infinite loop + for _ in range(10000): + file_name = hex(getrandbits(32))[2:] + if not self.has(file_name): + return file_name + raise FileExistsError(errno.EEXIST, "No usable file name found") + @classmethod def discover(cls, path, depth="1"): # path == None means wrong URL @@ -394,14 +402,6 @@ class Collection(BaseCollection): @classmethod def create_collection(cls, href, collection=None, tag=None): - def find_available_file_name(exists_fn): - # Prevent infinite loop - for _ in range(10000): - file_name = hex(getrandbits(32))[2:] - if not exists_fn(file_name): - return file_name - raise FileExistsError(errno.EEXIST, "No usable file name found") - folder = os.path.expanduser( cls.configuration.get("storage", "filesystem_folder")) path = path_to_filesystem(folder, href) @@ -429,14 +429,14 @@ class Collection(BaseCollection): new_collection = vobject.iCalendar() for item in items: new_collection.add(item) - self.upload(find_available_file_name(self.has), - new_collection) + self.upload( + self.find_available_file_name(), new_collection) elif tag == "VCARD": self.set_meta("tag", "VADDRESSBOOK") if collection: for card in collection: - self.upload(find_available_file_name(self.has), card) + self.upload(self.find_available_file_name(), card) return self