Author: remi
Date: 2009-03-27 12:12:56 +0100 (Fri, 27 Mar 2009)
New Revision: 4216

Added:
   software_suite_v2/tuxware/tuxdroidserver/trunk/util/attitunes/Attitune.py
   
software_suite_v2/tuxware/tuxdroidserver/trunk/util/attitunes/AttituneDescription.py
   
software_suite_v2/tuxware/tuxdroidserver/trunk/util/attitunes/AttitunesContainer.py
Log:
* added attitunes manager

Added: software_suite_v2/tuxware/tuxdroidserver/trunk/util/attitunes/Attitune.py
===================================================================
--- software_suite_v2/tuxware/tuxdroidserver/trunk/util/attitunes/Attitune.py   
                        (rev 0)
+++ software_suite_v2/tuxware/tuxdroidserver/trunk/util/attitunes/Attitune.py   
2009-03-27 11:12:56 UTC (rev 4216)
@@ -0,0 +1,157 @@
+#    Copyright (C) 2009 C2ME Sa
+#    Remi Jocaille <[email protected]>
+#    Distributed under the terms of the GNU General Public License
+#    http://www.gnu.org/copyleft/gpl.html
+
+import os
+import threading
+
+from AttituneDescription import AttituneDescription
+from AttituneToMacroDecl import attCmdToMacroCmd
+
+# 
------------------------------------------------------------------------------
+# Attitune class.
+# 
------------------------------------------------------------------------------
+class Attitune(object):
+    """Attitune class.
+    """
+
+    # 
--------------------------------------------------------------------------
+    # Constructor of the class.
+    # 
--------------------------------------------------------------------------
+    def __init__(self, parent, dictionary, attFile, workingPath):
+        """Constructor of the class.
+        @param parent: Parent Gadgets container.
+        @param dictionary: Gadget structure as dictionary.
+        @param attFile: ATT file name of the attitune.
+        @param workingPath: Working path of the attitune.
+        """
+        self.__parent = parent
+        # Save the dictionary
+        self.__dictionary = dictionary
+        # Save the working path
+        self.__workingPath = workingPath
+        # Save the att file name
+        self.__attFile = attFile
+        # Create descriptor
+        self.__description = AttituneDescription(self,
+            dictionary['scene']['header'], self.__workingPath)
+        # Get wav files
+        self.__wavs = {}
+        wavsPath = os.path.join(workingPath, "wavs")
+        if os.path.isdir(wavsPath):
+            files = os.listdir(wavsPath)
+            for file in files:
+                if file.lower().find(".wav") != -1:
+                    self.__wavs[file] = os.path.join(wavsPath, file)
+        # Timeline
+        self.__blocksStruct = dictionary['scene']['body']['script']['timeline']
+        self.__blocksKeys = self.__blocksStruct.keys()
+        self.__blocksKeys.sort()
+        # Macro
+        self.__macroStruct = []
+        self.__buildPreMacro()
+
+    # 
--------------------------------------------------------------------------
+    # Convert the attitunes timeline to macro.
+    # 
--------------------------------------------------------------------------
+    def __buildPreMacro(self):
+        """Convert the attitunes timeline to macro.
+        """
+        self.__macroStruct = []
+        for block in self.__blocksKeys:
+            blockStruct = self.__blocksStruct[block]
+            mcs = attCmdToMacroCmd(blockStruct)
+            for mc in mcs:
+                cmdStruct = {
+                    'delay' : blockStruct['start_time'],
+                    'cmd' : mc,
+                }
+                if blockStruct['cmd'] == 'wav_play':
+                    cmdStruct['delay'] = cmdStruct['delay'] - 0.3
+                    if self.__wavs.has_key(blockStruct['wav_name']):
+                        cmdStruct['wav_path'] = \
+                            self.__wavs[blockStruct['wav_name']]
+                elif blockStruct['cmd'] == 'tts_play':
+                    cmdStruct['delay'] = cmdStruct['delay'] - 0.3
+                self.__macroStruct.append(cmdStruct)
+
+    # 
--------------------------------------------------------------------------
+    # Get the directory path where this attitune is uncompressed.
+    # 
--------------------------------------------------------------------------
+    def getWorkingPath(self):
+        """Get the directory path where this attitune is uncompressed.
+        @return: A directory path as string.
+        """
+        return self.__workingPath
+
+    # 
--------------------------------------------------------------------------
+    # Get the ATT file of the gadget.
+    # 
--------------------------------------------------------------------------
+    def getAttFile(self):
+        """Get the ATT file of the gadget.
+        @return: A string.
+        """
+        return self.__attFile
+
+    # 
--------------------------------------------------------------------------
+    # Get the dictionary of the attitune.
+    # 
--------------------------------------------------------------------------
+    def getDictionary(self):
+        """Get the dictionary of the attitune.
+        @return: A dictionary.
+        """
+        return self.__dictionary
+
+    # 
--------------------------------------------------------------------------
+    # Get the parent attitunes container.
+    # 
--------------------------------------------------------------------------
+    def getContainer(self):
+        """Get the parent attitunes container.
+        @return: The parent attitunes container.
+        """
+        return self.__parent
+
+    # 
--------------------------------------------------------------------------
+    # Get the attitune description object.
+    # 
--------------------------------------------------------------------------
+    def getDescription(self):
+        """Get the attitune description object.
+        @return: The attitune description object.
+        """
+        return self.__description
+
+    # 
--------------------------------------------------------------------------
+    # Get a macro from the attitunes.
+    # 
--------------------------------------------------------------------------
+    def getMacro(self, begin = 0.0):
+        """Get a macro from the attitunes.
+        @param begin: The time begin index in seconds.
+        @return: The macro as string.
+        """
+        result = ""
+        if not self.__isValid:
+            return result
+        macroLst = []
+        for cmd in self.__macroStruct:
+            newCmd = copy.deepcopy(cmd)
+            newCmd['delay'] += 0.5
+            if newCmd['delay'] >= begin:
+                if newCmd.has_key('wav_path'):
+                    cmdStr = "OSL_CMD:WAV:PLAY:0.0,0.0,%s" % newCmd['wav_path']
+                    newCmd['cmd'] = cmdStr
+                newCmd['delay'] -= begin
+                macroLst.append(newCmd)
+            else:
+                if newCmd.has_key('wav_path'):
+                    idxb = begin - newCmd['delay']
+                    if idxb < os.path.getsize(cmd['wav_path']):
+                        cmdStr = "OSL_CMD:WAV:PLAY:%f,0.0,%s" % (idxb,
+                            newCmd['wav_path'])
+                        newCmd['cmd'] = cmdStr
+                        newCmd['delay'] -= begin
+                        macroLst.append(newCmd)
+        for cmd in macroLst:
+            tmpStr = "%f:%s\n" % (cmd['delay'], cmd['cmd'])
+            result = result + tmpStr
+        return result


Property changes on: 
software_suite_v2/tuxware/tuxdroidserver/trunk/util/attitunes/Attitune.py
___________________________________________________________________
Name: svn:keywords
   + Id

Added: 
software_suite_v2/tuxware/tuxdroidserver/trunk/util/attitunes/AttituneDescription.py
===================================================================
--- 
software_suite_v2/tuxware/tuxdroidserver/trunk/util/attitunes/AttituneDescription.py
                                (rev 0)
+++ 
software_suite_v2/tuxware/tuxdroidserver/trunk/util/attitunes/AttituneDescription.py
        2009-03-27 11:12:56 UTC (rev 4216)
@@ -0,0 +1,150 @@
+#    Copyright (C) 2009 C2ME Sa
+#    Remi Jocaille <[email protected]>
+#    Distributed under the terms of the GNU General Public License
+#    http://www.gnu.org/copyleft/gpl.html
+
+# 
------------------------------------------------------------------------------
+# Attitune description.
+# 
------------------------------------------------------------------------------
+class AttituneDescription(object):
+    """Attitune description.
+    """
+
+    # 
--------------------------------------------------------------------------
+    # Constructor of the class.
+    # 
--------------------------------------------------------------------------
+    def __init__(self, parent, dictionary, workingPath):
+        """Constructor of the class.
+        @param parent: Parent Attitune.
+        @param dictionary: Description as dictionary.
+        @param workingPath: Working path of the attitune.
+        """
+        self.__parent = parent
+        self.__dictionary = dictionary
+        self.__name = None
+        self.__description = None
+        self.__author = None
+        self.__version = None
+        self.__category = None
+        self.__subCategory = None
+        self.__duration = None
+        self.__language = None
+        self.__keywords = None
+        self.__update(dictionary, workingPath)
+
+    # 
--------------------------------------------------------------------------
+    # Get the parent attitune.
+    # 
--------------------------------------------------------------------------
+    def getParent(self):
+        """Get the parent attitune.
+        @return: A Gadget object.
+        """
+        return self.__parent
+
+    # 
--------------------------------------------------------------------------
+    # Get the dictionary.
+    # 
--------------------------------------------------------------------------
+    def getDictionary(self):
+        """Get the dictionary.
+        @return: A dictionary.
+        """
+        return self.__dictionary
+
+    # 
--------------------------------------------------------------------------
+    # Update the description.
+    # 
--------------------------------------------------------------------------
+    def __update(self, dictionary, workingPath):
+        """Update the description.
+        """
+        # Save the dictionary
+        self.__dictionary = dictionary
+        self.__name = dictionary['name']
+        self.__description = dictionary['description']
+        self.__author = dictionary['author']
+        self.__version = dictionary['version']
+        self.__category = dictionary['category']
+        self.__subCategory = dictionary['sub_category']
+        self.__duration = dictionary['length']
+        self.__language = dictionary['language'] #convert language format ...
+        self.__keywords = dictionary['keywords']
+
+    # 
--------------------------------------------------------------------------
+    # Get the attitune name.
+    # 
--------------------------------------------------------------------------
+    def getName(self):
+        """Get the attitune name.
+        @return: A string.
+        """
+        return self.__name
+
+    # 
--------------------------------------------------------------------------
+    # Get the author of the attitune.
+    # 
--------------------------------------------------------------------------
+    def getAuthor(self):
+        """Get the author of the attitune.
+        @return: A string.
+        """
+        return self.__author
+
+    # 
--------------------------------------------------------------------------
+    # Get the version of the attitune.
+    # 
--------------------------------------------------------------------------
+    def getVersion(self):
+        """Get the version of the attitune.
+        @return: A string.
+        """
+        return self.__version
+
+    # 
--------------------------------------------------------------------------
+    # Get the description of the attitune.
+    # 
--------------------------------------------------------------------------
+    def getDescription(self):
+        """Get the description of the attitune.
+        @return: A string.
+        """
+        return self.__description
+
+    # 
--------------------------------------------------------------------------
+    # Get the category of the attitune.
+    # 
--------------------------------------------------------------------------
+    def getCategory(self):
+        """Get the category of the attitune.
+        @return: A string.
+        """
+        return self.__category
+
+    # 
--------------------------------------------------------------------------
+    # Get the sub-category of the attitune.
+    # 
--------------------------------------------------------------------------
+    def getDescription(self):
+        """Get the sub-category of the attitune.
+        @return: A string.
+        """
+        return self.__subCategory
+
+    # 
--------------------------------------------------------------------------
+    # Get the language of the attitune.
+    # 
--------------------------------------------------------------------------
+    def getLanguage(self):
+        """Get the language of the attitune.
+        @return: A string.
+        """
+        return self.__language
+
+    # 
--------------------------------------------------------------------------
+    # Get the duration of the attitune.
+    # 
--------------------------------------------------------------------------
+    def getDuration(self):
+        """Get the duration of the attitune.
+        @return: A string.
+        """
+        return float(self.__duration)
+
+    # 
--------------------------------------------------------------------------
+    # Get the keywords of the attitune.
+    # 
--------------------------------------------------------------------------
+    def getKeywords(self):
+        """Get the keywords of the attitune.
+        @return: A string.
+        """
+        return self.__keywords


Property changes on: 
software_suite_v2/tuxware/tuxdroidserver/trunk/util/attitunes/AttituneDescription.py
___________________________________________________________________
Name: svn:keywords
   + Id

Added: 
software_suite_v2/tuxware/tuxdroidserver/trunk/util/attitunes/AttitunesContainer.py
===================================================================
--- 
software_suite_v2/tuxware/tuxdroidserver/trunk/util/attitunes/AttitunesContainer.py
                         (rev 0)
+++ 
software_suite_v2/tuxware/tuxdroidserver/trunk/util/attitunes/AttitunesContainer.py
 2009-03-27 11:12:56 UTC (rev 4216)
@@ -0,0 +1,294 @@
+#    Copyright (C) 2009 C2ME Sa
+#    Remi Jocaille <[email protected]>
+#    Distributed under the terms of the GNU General Public License
+#    http://www.gnu.org/copyleft/gpl.html
+
+import os
+import threading
+
+from util.filesystem.AutoDeployer import AutoDeployer
+from util.misc.XMLSerializer import fromXML
+from Attitune import Attitune
+
+# 
------------------------------------------------------------------------------
+# Attitunes container class.
+# 
------------------------------------------------------------------------------
+class AttitunesContainer(object):
+    """Attitunes container class.
+    """
+
+    # 
--------------------------------------------------------------------------
+    # Constructor of the class.
+    # 
--------------------------------------------------------------------------
+    def __init__(self):
+        """Constructor of the class.
+        """
+        self.__attitunes = []
+        self.__mutex = threading.Lock()
+        self.__autoDeployer = AutoDeployer("workForAttitunes")
+        
self.__autoDeployer.setOnPluginDeployedCallback(self.__onPluginDeployed)
+        
self.__autoDeployer.setOnPluginUndeployedCallback(self.__onPluginUndeployed)
+        
self.__autoDeployer.setOnPluginDeploymentErrorCallback(self.__onPluginDeploymentError)
+        self.__onAttituneDeployedCallback = None
+        self.__onAttituneDeploymentErrorCallback = None
+        self.__onAttituneUndeployedCallback = None
+
+    # 
==========================================================================
+    # AUTO-DEPLOYER
+    # 
==========================================================================
+
+    # 
--------------------------------------------------------------------------
+    # Add an attitunes directory to deploy.
+    # 
--------------------------------------------------------------------------
+    def addDirectory(self, directory):
+        """Add an attitunes directory to deploy.
+        @param directory: Directory path.
+        """
+        self.__autoDeployer.addDirectory(directory, [".att",])
+
+    # 
--------------------------------------------------------------------------
+    # Remove an attitunes directory to deploy.
+    # 
--------------------------------------------------------------------------
+    def removeDirectory(self, directory):
+        """Remove an attitunes directory to deploy.
+        @param directory: Directory path.
+        """
+        self.__autoDeployer.removeDirectory(directory)
+
+    # 
--------------------------------------------------------------------------
+    # Get the list of the attitunes directories.
+    # 
--------------------------------------------------------------------------
+    def getDirectories(self):
+        """Get the list of the attitunes directories.
+        @return: A list of string.
+        """
+        return self.__autoDeployer.getDirectories()
+
+    # 
--------------------------------------------------------------------------
+    # Deploy the setted attitunes directories.
+    # 
--------------------------------------------------------------------------
+    def deploy(self):
+        """Deploy the setted attitunes directories.
+        """
+        self.__autoDeployer.deploy()
+
+    # 
--------------------------------------------------------------------------
+    # Undeploy the setted attitunes directories.
+    # 
--------------------------------------------------------------------------
+    def undeploy(self):
+        """Undeploy the setted attitunes directories.
+        """
+        self.__autoDeployer.undeploy()
+
+    # 
--------------------------------------------------------------------------
+    # Get if the container is deployed or not.
+    # 
--------------------------------------------------------------------------
+    def isDeployed(self):
+        """Get if the container is deployed or not.
+        @return: A boolean.
+        """
+        return self.__autoDeployer.isDeployed
+
+    # 
--------------------------------------------------------------------------
+    # Set the directory deployed event callback.
+    # 
--------------------------------------------------------------------------
+    def setOnDirectoryDeployedCallback(self, funct):
+        """Set the directory deployed event callback.
+        @param funct: Function pointer.
+        Function prototype:
+        def onDirectoryDeployed(observerName):
+            pass
+        """
+        self.__autoDeployer.setOnDirectoryDeployedCallback(funct)
+
+    # 
--------------------------------------------------------------------------
+    # Set the directory undeployed event callback.
+    # 
--------------------------------------------------------------------------
+    def setOnDirectoryUndeployedCallback(self, funct):
+        """Set the directory undeployed event callback.
+        @param funct: Function pointer.
+        Function prototype:
+        def onDirectoryUndeployed(observerName):
+            pass
+        """
+        self.__autoDeployer.setOnDirectoryUndeployedCallback(funct)
+
+    # 
--------------------------------------------------------------------------
+    # Set the attitune deployed event callback.
+    # 
--------------------------------------------------------------------------
+    def setOnAttituneDeployedCallback(self, funct):
+        """Set the attitune deployed event callback.
+        @param funct: Function pointer.
+        Function prototype:
+        def onAttituneDeployed(attitune, attituneWorkingPath):
+            pass
+        """
+        self.__onAttituneDeployedCallback = funct
+
+    # 
--------------------------------------------------------------------------
+    # Set the attitune deployment error event callback.
+    # 
--------------------------------------------------------------------------
+    def setOnAttituneDeploymentErrorCallback(self, funct):
+        """Set the attitune deployment error event callback.
+        @param funct: Function pointer.
+        Function prototype:
+        def onAttituneDeploymentError(observerName, attituneFileName, message):
+            pass
+        """
+        self.__onAttituneDeploymentErrorCallback = funct
+
+    # 
--------------------------------------------------------------------------
+    # Set the attitune undeployed event callback.
+    # 
--------------------------------------------------------------------------
+    def setOnAttituneUndeployedCallback(self, funct):
+        """Set the attitune undeployed event callback.
+        @param funct: Function pointer.
+        Function prototype:
+        def onAttituneUndeployed(attitune, attituneWorkingPath):
+            pass
+        """
+        self.__onAttituneUndeployedCallback = funct
+
+    # 
--------------------------------------------------------------------------
+    # Event on plugin deployed.
+    # 
--------------------------------------------------------------------------
+    def __onPluginDeployed(self, observerName, fileName, pluginPath, 
pluginName):
+        """Event on plugin deployed.
+        @param observerName: Directory observer name.
+        @param fileName: Plugin file name.
+        @param pluginPath: Path of the uncompressed plugin.
+        @param pluginName: Name of the plugin.
+        @return: If the plugin structure is correct or not.
+        """
+        attitune, msg = self.__buildAttitune(observerName, fileName, 
pluginPath,
+            pluginName)
+        if attitune != None:
+            if self.__onAttituneDeployedCallback != None:
+                self.__onAttituneDeployedCallback(attitune,
+                    attitune.getWorkingPath())
+            result = True
+        else:
+            if self.__onAttituneDeploymentErrorCallback != None:
+                self.__onAttituneDeploymentErrorCallback(observerName,
+                    pluginName, msg)
+            result = False
+        return result
+
+    # 
--------------------------------------------------------------------------
+    # Event on plugin undeployed.
+    # 
--------------------------------------------------------------------------
+    def __onPluginUndeployed(self, observerName, fileName, pluginPath, 
pluginName):
+        """Event on plugin undeployed.
+        @param observerName: Directory observer name.
+        @param fileName: Plugin file name.
+        @param pluginPath: Path of the uncompressed plugin.
+        @param pluginName: Name of the plugin.
+        """
+        # Get the attitune object
+        for attitune in self.__attitunes:
+            if attitune.getWorkingPath() == os.path.join(pluginPath, 
pluginName):
+                if self.__onAttituneUndeployedCallback != None:
+                    self.__onAttituneUndeployedCallback(attitune,
+                        attitune.getWorkingPath())
+                # Destroy the attitune
+                self.__destroyAttitune(attitune)
+                break
+
+    # 
--------------------------------------------------------------------------
+    # Event on plugin deployment error.
+    # 
--------------------------------------------------------------------------
+    def __onPluginDeploymentError(self, observerName, pluginName):
+        """Event on plugin deployment error.
+        @param observerName: Directory observer name.
+        @param pluginPath: Path of the uncompressed plugin.
+        """
+        msg = "Invalid attitune file"
+        if self.__onAttituneDeploymentErrorCallback != None:
+            self.__onAttituneDeploymentErrorCallback(observerName, pluginName,
+                msg)
+
+    # 
==========================================================================
+    # ATTITUNES
+    # 
==========================================================================
+
+    # 
--------------------------------------------------------------------------
+    # Build an attitune object from a deployed plugin.
+    # 
--------------------------------------------------------------------------
+    def __buildAttitune(self, observerName, fileName, pluginPath, pluginName):
+        """Build an attitune object from a deployed plugin.
+        @param observerName: Directory observer name.
+        @param fileName: Plugin file name.
+        @param pluginPath: Path of the uncompressed plugin.
+        @param pluginName: Name of the plugin.
+        @return: A tuple (<attitune>, "<Message>")
+        If the attitune can't be created, the "attitune" object will be None 
and
+        the message explains the reason.
+        """
+        # Check for "scene.xml"
+        sceneXmlFile = os.path.join(pluginPath, pluginName, "scene.xml")
+        sceneXmlDict = fromXML(sceneXmlFile)
+        if sceneXmlDict == None:
+            return None, "'scene.xml' not found"
+        # Create the attitune
+        try:
+            attitune = Attitune(self, sceneXmlDict, fileName,
+                os.path.join(pluginPath, pluginName))
+        except:
+            return None, "Error in 'scene.xml'"
+        # Add this attitune to the container
+        self.__mutex.acquire()
+        self.__attitunes.append(attitune)
+        self.__mutex.release()
+        return attitune, "Ok"
+
+    # 
--------------------------------------------------------------------------
+    # Destroy an attitune.
+    # 
--------------------------------------------------------------------------
+    def __destroyAttitune(self, attitune):
+        """Destroy an attitune.
+        @param attitune: Attitune object to destroy.
+        """
+        # Remove this attitune from the container
+        self.__mutex.acquire()
+        self.__attitunes.remove(attitune)
+        self.__mutex.release()
+
+    # 
--------------------------------------------------------------------------
+    # Get the attitunes objects list.
+    # 
--------------------------------------------------------------------------
+    def getAttitunes(self):
+        """Get the attitune objects list.
+        @return: A list of Attitune objects.
+        """
+        self.__mutex.acquire()
+        result = self.__attitunes
+        self.__mutex.release()
+        return result
+
+    # 
--------------------------------------------------------------------------
+    # Get the number of attitunes contains in the container.
+    # 
--------------------------------------------------------------------------
+    def getCount(self):
+        """Get the number of attitunes contains in the container.
+        @return: An integer.
+        """
+        self.__mutex.acquire()
+        result = len(self.__attitunes)
+        self.__mutex.release()
+        return result
+
+    # 
--------------------------------------------------------------------------
+    # Get an attitune object by it name.
+    # 
--------------------------------------------------------------------------
+    def getAttitune(self, attituneName):
+        """Get an attitune object by it name.
+        @param gadgetName: The name of the attitune.
+        @return: An attitune object or None.
+        """
+        self.__mutex.acquire()
+        for attitune in self.__attitunes:
+            if attitune.getDescription().getName() == attituneName:
+                self.__mutex.release()
+                return attitune
+        self.__mutex.release()
+        return None


Property changes on: 
software_suite_v2/tuxware/tuxdroidserver/trunk/util/attitunes/AttitunesContainer.py
___________________________________________________________________
Name: svn:keywords
   + Id


------------------------------------------------------------------------------
_______________________________________________
Tux-droid-svn mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tux-droid-svn

Reply via email to