ppf: Move find_numeric_subfolder function outside class.
authorRazvan Deaconescu <razvan.deaconescu@cs.pub.ro>
Sun, 21 Aug 2011 16:30:34 +0000 (19:30 +0300)
committerRazvan Deaconescu <razvan.deaconescu@cs.pub.ro>
Sun, 21 Aug 2011 16:30:34 +0000 (19:30 +0300)
ppf/new/storage.py

index 997db03..4cf9e43 100644 (file)
@@ -20,7 +20,7 @@ logger.setLevel(logging.DEBUG)
 
 # Create console handler and set level to ERROR.
 ch = logging.StreamHandler()
-ch.setLevel(logging.ERROR)
+ch.setLevel(logging.DEBUG)
 
 # Create formatter.
 formatter = logging.Formatter('%(filename)s:%(lineno)s - %(levelname)s: %(message)s')
@@ -196,7 +196,7 @@ class VerboseMessage(object):
         self.length = length
         self.listen_port = listen_port
 
-class SwarmDataAccess():
+class SwarmDataAccess(object):
     def __init__(self):
         pass
 
@@ -264,33 +264,32 @@ class FileAccess(SwarmDataAccess):
     def __init__(self, path):
         self.base_path = path
 
+def find_last_numeric_subfolder(path):
+    """
+    Find last numeric folder in base_path folder.
+    The last numeric folder is the last swarm_id.
+    """
+    dir_list = []
+    pattern = re.compile("[0-9]+")
+
+    # Browse entries in base_path folder.
+    listing = os.listdir(path)
+    for entry in listing:
+        # If directory name is a number (id) add it to the list.
+        if os.path.isdir(os.path.join(path, entry)):
+            logger.debug("folder entry: %s" %(entry))
+            if pattern.match(entry):
+                dir_list.append(int(entry))
+
+    if not dir_list:
+        return None
+    else:
+        dir_list.sort()
+        return dir_list[len(dir_list)-1]
+
 class TreeTextFileAccess(FileAccess):
     def __init__(self, path):
-        super(TextTreeFileAccess, self).__init(path)
-        # TODO: Check exception.
-        os.mkdir(self.base_path)
-
-    def find_last_folder_id(self):
-        """
-        Find last numeric folder in base_path folder.
-        The last numeric folder is the last swarm_id.
-        """
-        dir_list = []
-        pattern = re.compile("[0-9]+")
-
-        # Browse entries in base_path folder.
-        listing = os.listdir(self.base_path)
-        for entry in listing:
-            # If directory name is a number (id) add it to the list.
-            if os.path.isdir(entry):
-                if pattern.match(entry):
-                    dir_list.append(int(entry))
-
-        if dir_list.empty():
-            return None
-        else:
-            dir_list.sort()
-            return dir_list[len(dir_list)-1]
+        super(TreeTextFileAccess, self).__init__(path)
 
     def add_swarm(self, swarm):
         f = open(os.path.join(self.base_path, "swarm.conf"))