diff src/core/sat_main.py @ 1522:7d7e57a84792

core: progression handling improvments: - getProgress has been renamed to progressGet to follown new naming convention - new signals: progressStarted, progressFinished and progressError to indicate state of progressing events - new method progressGetAll to get all progressing events of all profile (or only one profile)
author Goffi <goffi@goffi.org>
date Fri, 25 Sep 2015 19:19:12 +0200
parents f681788097ba
children d749922300d0
line wrap: on
line diff
--- a/src/core/sat_main.py	Fri Sep 25 19:19:12 2015 +0200
+++ b/src/core/sat_main.py	Fri Sep 25 19:19:12 2015 +0200
@@ -121,7 +121,8 @@
         self.bridge.register("isConnected", self.isConnected)
         self.bridge.register("launchAction", self.launchCallback)
         self.bridge.register("confirmationAnswer", self.confirmationAnswer)
-        self.bridge.register("getProgress", self.getProgress)
+        self.bridge.register("progressGet", self._progressGet)
+        self.bridge.register("progressGetAll", self._progressGetAll)
         self.bridge.register("getMenus", self.getMenus)
         self.bridge.register("getMenuHelp", self.getMenuHelp)
         self.bridge.register("discoInfos", self.memory.disco._discoInfos)
@@ -449,15 +450,18 @@
         return self.profiles[profile]
 
     def getClients(self, profile_key):
-        """Convenient method to get list of clients from profile key (manage list through profile_key like @ALL@)
+        """Convenient method to get list of clients from profile key (manage list through profile_key like C.PROF_KEY_ALL)
+
         @param profile_key: %(doc_profile_key)s
-        @return: list of clients"""
-        profile = self.memory.getProfileName(profile_key, True)
-        if not profile:
+        @return: list of clients
+        """
+        try:
+            profile = self.memory.getProfileName(profile_key, True)
+        except exceptions.ProfileUnknownError:
             return []
-        if profile == "@ALL@":
+        if profile == C.PROF_KEY_ALL:
             return self.profiles.values()
-        if profile.count('@') > 1:
+        elif profile.count('@') > 1:
             raise exceptions.ProfileKeyUnknownError
         return [self.profiles[profile]]
 
@@ -779,33 +783,73 @@
         id_ = unicode(uuid.uuid4())
         self.bridge.actionNew(action_data, id_, profile)
 
-    def registerProgressCB(self, progress_id, CB, profile):
+    def registerProgressCb(self, progress_id, callback, profile):
         """Register a callback called when progress is requested for id"""
         client = self.getClient(profile)
-        client._progress_cb_map[progress_id] = CB
+        if progress_id in client._progress_cb:
+            raise exceptions.ConflictError(u"Progress ID is not unique !")
+        client._progress_cb[progress_id] = callback
 
-    def removeProgressCB(self, progress_id, profile):
+    def removeProgressCb(self, progress_id, profile):
         """Remove a progress callback"""
         client = self.getClient(profile)
-        if progress_id not in client._progress_cb_map:
+        try:
+            del client._progress_cb[progress_id]
+        except KeyError:
             log.error(_("Trying to remove an unknow progress callback"))
-        else:
-            del client._progress_cb_map[progress_id]
+            import ipdb; ipdb.set_trace()
+
+    def _progressGet(self, progress_id, profile):
+        data = self.progressGet(progress_id, profile)
+        return {k: unicode(v) for k,v in data}
 
-    def getProgress(self, progress_id, profile):
+    def progressGet(self, progress_id, profile):
         """Return a dict with progress information
-        data['position'] : current possition
-        data['size'] : end_position
+
+        @param progress_id(unicode): unique id of the progressing element
+        @param profile: %(doc_profile)s
+        @return (dict): data with the following keys:
+            'position' (int): current possition
+            'size' (int): end_position
+            if id doesn't exists (may be a finished progression), and empty dict is returned
         """
         client = self.getClient(profile)
         data = {}
         try:
-            client._progress_cb_map[progress_id](progress_id, data, profile)
+            client._progress_cb[progress_id](progress_id, data, profile)
         except KeyError:
-            pass
-            #log.debug("Requested progress for unknown progress_id")
+            log.debug("Requested progress for unknown progress_id")
         return data
 
+    def _progressGetAll(self, profile_key):
+        progress_all = self.progressGetAll(profile_key)
+        for profile, progress_dict in progress_all.iteritems():
+            for progress_id, data in progress_dict.iteritems():
+                for key, value in data.iteritems():
+                    data[key] = unicode(value)
+        return progress_all
+
+    def progressGetAll(self, profile_key):
+        """Return all progress informations
+
+        @param profile_key: %(doc_profile)s get all progress from this profile
+            if C.PROF_KEY_ALL is used, all progress from all profiles are returned
+        @return (dict[dict]): key=progress id, value=dict of data with the following keys:
+            'position' (int): current possition
+            'size' (int): end_position
+        """
+        clients = self.getClients(profile_key)
+        progress_all = {}
+        for client in clients:
+            profile = client.profile
+            progress_dict = {}
+            progress_all[profile] = progress_dict
+            for progress_id, progress_cb in client._progress_cb.iteritems():
+                data = {}
+                progress_dict[progress_id] = data
+                progress_dict[progress_id] = progress_cb(progress_id, data, profile)
+        return progress_all
+
     def registerCallback(self, callback, *args, **kwargs):
         """ Register a callback.
         Use with_data=True in kwargs if the callback use the optional data dict