Add add_swarm to TreeTextFileAccess.
authorRazvan Deaconescu <razvan.deaconescu@cs.pub.ro>
Sun, 21 Aug 2011 16:48:12 +0000 (19:48 +0300)
committerRazvan Deaconescu <razvan.deaconescu@cs.pub.ro>
Sun, 21 Aug 2011 17:08:14 +0000 (20:08 +0300)
ppf/new/storage.py

index 4cf9e43..92ab3a9 100644 (file)
@@ -20,7 +20,7 @@ logger.setLevel(logging.DEBUG)
 
 # Create console handler and set level to ERROR.
 ch = logging.StreamHandler()
-ch.setLevel(logging.DEBUG)
+ch.setLevel(logging.ERROR)
 
 # Create formatter.
 formatter = logging.Formatter('%(filename)s:%(lineno)s - %(levelname)s: %(message)s')
@@ -277,7 +277,6 @@ def find_last_numeric_subfolder(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))
 
@@ -292,8 +291,27 @@ class TreeTextFileAccess(FileAccess):
         super(TreeTextFileAccess, self).__init__(path)
 
     def add_swarm(self, swarm):
-        f = open(os.path.join(self.base_path, "swarm.conf"))
-        pass
+        """
+        Create a subfolder with an unique id. Add 1 to the last numeric
+        subfolder id. In case none exists, use 1 as id.
+        """
+        id = find_last_numeric_subfolder(self.base_path)
+        if id == None:
+            id = 1
+        else:
+            id = id+1
+
+        swarm_path = os.path.join(self.base_path, str(id))
+        os.mkdir(swarm_path)
+
+        swarm_config = os.path.join(swarm_path, "swarm.conf")
+        f = open(swarm_config, 'w')
+        f.write("""id = %s
+        self.torrent_filename = %s
+        self.data_size = %s
+        self.description = %s
+        """ %(id, swarm.torrent_filename, swarm.data_size, swarm.description))
+        f.close()
 
     def add_client_session(self, session):
         pass