Author: dmeyer
Date: Mon Apr  9 18:09:56 2007
New Revision: 9423

Added:
   trunk/core/src/resources.py
      - copied, changed from r9421, /trunk/ui/src/application/resources.py
Removed:
   trunk/ui/src/application/resources.py
Modified:
   trunk/ui/src/application/__init__.py
   trunk/ui/src/application/base.py

Log:
Move resource handling to Freevo core and make it a class
Patch by Mathias Weber


Copied: trunk/core/src/resources.py (from r9421, 
/trunk/ui/src/application/resources.py)
==============================================================================
--- /trunk/ui/src/application/resources.py      (original)
+++ trunk/core/src/resources.py Mon Apr  9 18:09:56 2007
@@ -29,32 +29,104 @@
 #
 # -----------------------------------------------------------------------------
 
+# kaa imports
+from kaa.notifier import InProgress, Callback, yield_execution
+
 _resources = {}
 
-def get_resources(instance, *resources):
+class ResourceHandler(object):
     """
-    Reserve a list of resources. If one or more resources can not be
-    reserved, the whole operation fails. The function will return the
-    list of failed resources with the instance having this resource.
+    The ResourceHandler inherit from it if you want to use resources that need
+    a specific access rights.
     """
-    blocked = {}
-    for r in resources:
-        if r in _resources:
-            blocked[r] = _resources[r]
-    if blocked:
-        # failed to reserve
-        return blocked
-    # reserve all resources
-    for r in resources:
-        _resources[r] = instance
-    return {}
+    suspended_handler = []
 
+    def get_resources(self, *resources):
+        """
+        Reserve a list of resources. If one or more resources can not be
+        reserved, the whole operation fails. The function will return the
+        list of failed resources with this instance having this resource.
+        """
+        blocked = {}
+        for r in resources:
+            if r in _resources:
+                if _resources[r].can_suspend():
+                    blocked[r] = _resources[r]
+                else:
+                    return False
+        if blocked:
+            # failed to reserve
+            return blocked
+        # reserve all resources
+        for r in resources:
+            _resources[r] = self
+        return {}
+
+
+    def free_resources(self, *resources):
+        """
+        Free all resources blocked by this instance. If no resources are
+        provided, free all resources.
+        """
+        for res, app in _resources.items()[:]:
+            if app == self and (not resources or res in resources):
+                del _resources[res]
+
+
+    def _handler_suspended(self, t, blocked, callback):
+        """
+        Callback for handler that need some more time to release a resource.
+        """
+        for resource in blocked.keys():
+            if resource in _resources:
+                # not yet all resources are free yet
+                return
+
+        # all resource are free now call the handler
+        callback()
+
+
+    @yield_execution()
+    def suspend_all(self, blocked):
+        """
+        Suspend all handler that block a resource if done call the callback
+        method.
+        """
+        for handler in blocked:
+            self.suspended_handler.append(blocked[handler])
+            answer = blocked[handler].suspend()
+            if isinstance(answer, InProgress):
+                yield answer
+
+
+    def resume_all(self):   
+        """
+        Resume all handler suspended by this handler. Clear list at the end.
+        """
+        # resume suspended handlers
+        for handler in self.suspended_handler:
+            handler.resume()
+        # clear list with suspended handler since all resumed
+        self.suspended_handler = []
+
+
+    def can_suspend(self):
+        """
+        Return if it is possible for this handler to release its resource.
+        """
+        return False
+
+
+    def suspend(self):
+        """
+        Suspend this resource handler. Should free all the resources.
+        """
+        return False
+
+
+    def resume(self):
+        """
+        Resume the plugin acquire all the resources again.
+        """
+        pass
 
-def free_resources(instance, *resources):
-    """
-    Free all resources blocked by the instance. If not resources are
-    provided, free all resources.
-    """
-    for res, app in _resources.items()[:]:
-        if app == instance and (not resources or res in resources):
-            del _resources[res]

Modified: trunk/ui/src/application/__init__.py
==============================================================================
--- trunk/ui/src/application/__init__.py        (original)
+++ trunk/ui/src/application/__init__.py        Mon Apr  9 18:09:56 2007
@@ -29,7 +29,6 @@
 #
 # -----------------------------------------------------------------------------
 
-from resources import get_resources, free_resources
 from base import Application, STATUS_RUNNING, STATUS_STOPPING, \
      STATUS_STOPPED, STATUS_IDLE, CAPABILITY_TOGGLE, CAPABILITY_PAUSE, \
      CAPABILITY_FULLSCREEN

Modified: trunk/ui/src/application/base.py
==============================================================================
--- trunk/ui/src/application/base.py    (original)
+++ trunk/ui/src/application/base.py    Mon Apr  9 18:09:56 2007
@@ -38,11 +38,11 @@
 from kaa.notifier import Signal
 
 # freevo imports
+from freevo.resources import ResourceHandler
 from freevo.ui import gui
 
 # application imports
 from handler import handler
-from resources import get_resources, free_resources
 
 # get logging object
 log = logging.getLogger()
@@ -56,7 +56,7 @@
 CAPABILITY_PAUSE      = 2
 CAPABILITY_FULLSCREEN = 4
 
-class Application(object):
+class Application(ResourceHandler):
     """
     A basic application
     """
@@ -104,6 +104,7 @@
             self.signals['start'].emit()
         elif status == STATUS_IDLE:
             handler.hide_application(self)
+            self.resume_all()
             self._status = status
             self.signals['stop'].emit()
         else:
@@ -178,23 +179,6 @@
         return self.__name
 
 
-    def get_resources(self, *resources):
-        """
-        Reserve a list of resources. If one or more resources can not be
-        reserved, the whole operation fails. The function will return the
-        list of failed resources with the application having this resource.
-        """
-        return get_resources(self, *resources)
-
-
-    def free_resources(self, *resources):
-        """
-        Free all resources blocked by this application. If not resources are
-        provided, free all resources.
-        """
-        return free_resources(self, *resources)
-
-
     def __repr__(self):
         """
         String for debugging.

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Freevo-cvslog mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/freevo-cvslog

Reply via email to