Parcourir la source

Change find_available_file_name into a method

Guillaume Ayoub il y a 9 ans
Parent
commit
a7f078eb54
1 fichiers modifiés avec 11 ajouts et 11 suppressions
  1. 11 11
      radicale/storage.py

+ 11 - 11
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