Author: remi
Date: 2009-04-07 11:54:40 +0200 (Tue, 07 Apr 2009)
New Revision: 4380

Modified:
   software_suite_v2/tuxware/tuxdroidserver/trunk/util/scheduler/Scheduler.py
   software_suite_v2/tuxware/tuxdroidserver/trunk/util/scheduler/Task.py
Log:
* added methods to load and store task configurations

Modified: 
software_suite_v2/tuxware/tuxdroidserver/trunk/util/scheduler/Scheduler.py
===================================================================
--- software_suite_v2/tuxware/tuxdroidserver/trunk/util/scheduler/Scheduler.py  
2009-04-07 08:32:50 UTC (rev 4379)
+++ software_suite_v2/tuxware/tuxdroidserver/trunk/util/scheduler/Scheduler.py  
2009-04-07 09:54:40 UTC (rev 4380)
@@ -3,6 +3,7 @@
 #    Distributed under the terms of the GNU General Public License
 #    http://www.gnu.org/copyleft/gpl.html
 
+import os
 import datetime
 import threading
 import time
@@ -51,14 +52,32 @@
     # 
--------------------------------------------------------------------------
     def loadTasks(self, directory):
         """Load tasks from a directory.
-        @param directory: Directory how to search the task configurations.
+        @param directory: Directory where to search the task configurations.
         """
+        if not os.path.isdir(directory):
+            return
         self.__tasksDirectory = directory
         self.__tasksToExecuteStackMutex.acquire()
         self.__tasksToExecuteStack = []
         self.__tasksToExecuteStackMutex.release()
         self.__setTasksLoaded(False)
-        # TODO : Complete this method ...
+        files = os.listdir(directory)
+        for file in files:
+            filePath = os.path.join(directory, file)
+            task = Task.load(filePath)
+            if task == None:
+                try:
+                    os.remove(filePath)
+                except:
+                    pass
+            else:
+                id, name = self.insertTask(task, 
task.getDescription().getName(),
+                    task.getDescription().getId())
+                if id == None:
+                    try:
+                        os.remove(filePath)
+                    except:
+                        pass
         self.__setTasksLoaded(True)
 
     # 
--------------------------------------------------------------------------
@@ -69,8 +88,36 @@
         """
         if self.__tasksDirectory == None:
             return
-        # TODO : Complete this method ...
-        print "Store tasks ..."
+        # Get stored task fileNames list
+        files = os.listdir(self.__tasksDirectory)
+        for i, file in enumerate(files):
+            if file.lower().rfind(".tcf") != len(file) - 4:
+                files.remove(file)
+            else:
+                files[i] = file[:-4]
+        self.__tasksToExecuteStackMutex.acquire()
+        # Remove orphan configurations
+        for file in files:
+            isFound = False
+            for taskInfo in self.__tasksToExecuteStack:
+                if taskInfo[TASK_OBJECT].getDescription().getId() == file:
+                    isFound = True
+                    break
+            if not isFound:
+                try:
+                    os.remove(os.path.join(self.__tasksDirectory, file + 
".tcf"))
+                except:
+                    pass
+        # Store task configurations
+        for taskInfo in self.__tasksToExecuteStack:
+            isFound = False
+            for file in files:
+                if taskInfo[TASK_OBJECT].getDescription().getId() == file:
+                    isFound = True
+                    break
+            if not isFound:
+                Task.store(taskInfo[TASK_OBJECT], self.__tasksDirectory)
+        self.__tasksToExecuteStackMutex.release()
 
     # 
--------------------------------------------------------------------------
     # Start the scheduler.
@@ -138,7 +185,6 @@
             if self.__onTasksLoadedCallback != None:
                 self.__onTasksLoadedCallback()
         else:
-            self.storeTasks()
             if self.__onTasksUnloadedCallback != None:
                 self.__onTasksUnloadedCallback()
 
@@ -190,13 +236,14 @@
     # 
--------------------------------------------------------------------------
     # Insert a task in the scheduler.
     # 
--------------------------------------------------------------------------
-    def insertTask(self, task, name):
+    def insertTask(self, task, name, id = None):
         """Insert a task in the scheduler.
         @param task: Task object.
         @return: The task identifier and the task name or None None if fail.
         """
         nextTime = task.getNextExecuteTime()
-        id = self.__generateSingleId()
+        if id == None:
+            id = self.__generateSingleId()
         name = self.__generateSingleName(name)
         self.__tasksToExecuteStackMutex.acquire()
         if nextTime != None:
@@ -309,6 +356,7 @@
         self.__tasksToExecuteStack = []
         self.__tasksToExecuteStackMutex.release()
         self.__setTasksLoaded(False)
+        self.storeTasks()
         self.__setTasksLoaded(True)
 
     # 
--------------------------------------------------------------------------

Modified: software_suite_v2/tuxware/tuxdroidserver/trunk/util/scheduler/Task.py
===================================================================
--- software_suite_v2/tuxware/tuxdroidserver/trunk/util/scheduler/Task.py       
2009-04-07 08:32:50 UTC (rev 4379)
+++ software_suite_v2/tuxware/tuxdroidserver/trunk/util/scheduler/Task.py       
2009-04-07 09:54:40 UTC (rev 4380)
@@ -3,6 +3,7 @@
 #    Distributed under the terms of the GNU General Public License
 #    http://www.gnu.org/copyleft/gpl.html
 
+import os
 import datetime
 import threading
 
@@ -203,3 +204,84 @@
                 pass
         t = threading.Thread(target = async)
         t.start()
+
+    # 
--------------------------------------------------------------------------
+    # Load a task.
+    # 
--------------------------------------------------------------------------
+    def load(filePath):
+        """Load a task.
+        @param filePath: Fle path of the task.
+        @return: The Task object or None.
+        """
+        if not os.path.isfile(filePath):
+            return None
+        if filePath.lower().rfind(".tcf") != len(filePath) - 4:
+            return None
+        try:
+            f = open(filePath, "rb")
+            try:
+                dictionary = eval(f.read())
+            except:
+                f.close()
+                return None
+        finally:
+            f.close()
+        try:
+            id = dictionary['id']
+            name = dictionary['name']
+            ruleType = dictionary['type']
+            weekMask = [True, True, True, True, True, True, True]
+            splitedStr = dictionary['weekMaskString'].split(",")
+            if len(splitedStr) == 7:
+                for i, value in enumerate(splitedStr):
+                    if value == "1":
+                        weekMask[i] = True
+                    else:
+                        weekMask[i] = False
+            hour = dictionary['hour']
+            minute = dictionary['minute']
+            second = dictionary['second']
+            year = dictionary['year']
+            month = dictionary['month']
+            day = dictionary['day']
+            command = dictionary['command']
+            arguments = dictionary['arguments']
+            data = dictionary['userData']
+        except:
+            return None
+        task = Task(ruleType, weekMask, hour, minute, second, year, month, day,
+            command, arguments, data)
+        task.getDescription().setId(id)
+        task.getDescription().setName(name)
+        return task
+
+    # 
--------------------------------------------------------------------------
+    # Store a task.
+    # 
--------------------------------------------------------------------------
+    def store(task, directory):
+        """Store a task.
+        @param task: Task object to store.
+        @param directory: Directory where to store the task.
+        @return: The success of the storing.
+        """
+        if not os.path.isdir(directory):
+            return False
+        taskInfos = task.getDictionary()
+        taskInfos['command'] = task.getDescription().getCommand()
+        taskInfos['arguments'] = task.getDescription().getArguments()
+        fileName = taskInfos['id'] + ".tcf"
+        filePath = os.path.join(directory, fileName)
+        result = False
+        try:
+            f = open(filePath, "w")
+            try:
+                f.write(str(taskInfos))
+                result = True
+            except:
+                pass
+        finally:
+            f.close()
+        return result
+
+    load = staticmethod(load)
+    store = staticmethod(store)


------------------------------------------------------------------------------
This SF.net email is sponsored by:
High Quality Requirements in a Collaborative Environment.
Download a free trial of Rational Requirements Composer Now!
http://p.sf.net/sfu/www-ibm-com
_______________________________________________
Tux-droid-svn mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tux-droid-svn

Reply via email to