comparison 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
comparison
equal deleted inserted replaced
1521:d2ab9c62ac3a 1522:7d7e57a84792
119 self.bridge.register("updateContact", self._updateContact) 119 self.bridge.register("updateContact", self._updateContact)
120 self.bridge.register("delContact", self._delContact) 120 self.bridge.register("delContact", self._delContact)
121 self.bridge.register("isConnected", self.isConnected) 121 self.bridge.register("isConnected", self.isConnected)
122 self.bridge.register("launchAction", self.launchCallback) 122 self.bridge.register("launchAction", self.launchCallback)
123 self.bridge.register("confirmationAnswer", self.confirmationAnswer) 123 self.bridge.register("confirmationAnswer", self.confirmationAnswer)
124 self.bridge.register("getProgress", self.getProgress) 124 self.bridge.register("progressGet", self._progressGet)
125 self.bridge.register("progressGetAll", self._progressGetAll)
125 self.bridge.register("getMenus", self.getMenus) 126 self.bridge.register("getMenus", self.getMenus)
126 self.bridge.register("getMenuHelp", self.getMenuHelp) 127 self.bridge.register("getMenuHelp", self.getMenuHelp)
127 self.bridge.register("discoInfos", self.memory.disco._discoInfos) 128 self.bridge.register("discoInfos", self.memory.disco._discoInfos)
128 self.bridge.register("discoItems", self.memory.disco._discoItems) 129 self.bridge.register("discoItems", self.memory.disco._discoItems)
129 self.bridge.register("saveParamsTemplate", self.memory.save_xml) 130 self.bridge.register("saveParamsTemplate", self.memory.save_xml)
447 if not profile: 448 if not profile:
448 raise exceptions.ProfileKeyUnknownError 449 raise exceptions.ProfileKeyUnknownError
449 return self.profiles[profile] 450 return self.profiles[profile]
450 451
451 def getClients(self, profile_key): 452 def getClients(self, profile_key):
452 """Convenient method to get list of clients from profile key (manage list through profile_key like @ALL@) 453 """Convenient method to get list of clients from profile key (manage list through profile_key like C.PROF_KEY_ALL)
454
453 @param profile_key: %(doc_profile_key)s 455 @param profile_key: %(doc_profile_key)s
454 @return: list of clients""" 456 @return: list of clients
455 profile = self.memory.getProfileName(profile_key, True) 457 """
456 if not profile: 458 try:
459 profile = self.memory.getProfileName(profile_key, True)
460 except exceptions.ProfileUnknownError:
457 return [] 461 return []
458 if profile == "@ALL@": 462 if profile == C.PROF_KEY_ALL:
459 return self.profiles.values() 463 return self.profiles.values()
460 if profile.count('@') > 1: 464 elif profile.count('@') > 1:
461 raise exceptions.ProfileKeyUnknownError 465 raise exceptions.ProfileKeyUnknownError
462 return [self.profiles[profile]] 466 return [self.profiles[profile]]
463 467
464 def _getConfig(self, section, name): 468 def _getConfig(self, section, name):
465 """Get the main configuration option 469 """Get the main configuration option
777 @param profile: %(doc_profile)s 781 @param profile: %(doc_profile)s
778 """ 782 """
779 id_ = unicode(uuid.uuid4()) 783 id_ = unicode(uuid.uuid4())
780 self.bridge.actionNew(action_data, id_, profile) 784 self.bridge.actionNew(action_data, id_, profile)
781 785
782 def registerProgressCB(self, progress_id, CB, profile): 786 def registerProgressCb(self, progress_id, callback, profile):
783 """Register a callback called when progress is requested for id""" 787 """Register a callback called when progress is requested for id"""
784 client = self.getClient(profile) 788 client = self.getClient(profile)
785 client._progress_cb_map[progress_id] = CB 789 if progress_id in client._progress_cb:
786 790 raise exceptions.ConflictError(u"Progress ID is not unique !")
787 def removeProgressCB(self, progress_id, profile): 791 client._progress_cb[progress_id] = callback
792
793 def removeProgressCb(self, progress_id, profile):
788 """Remove a progress callback""" 794 """Remove a progress callback"""
789 client = self.getClient(profile) 795 client = self.getClient(profile)
790 if progress_id not in client._progress_cb_map: 796 try:
797 del client._progress_cb[progress_id]
798 except KeyError:
791 log.error(_("Trying to remove an unknow progress callback")) 799 log.error(_("Trying to remove an unknow progress callback"))
792 else: 800 import ipdb; ipdb.set_trace()
793 del client._progress_cb_map[progress_id] 801
794 802 def _progressGet(self, progress_id, profile):
795 def getProgress(self, progress_id, profile): 803 data = self.progressGet(progress_id, profile)
804 return {k: unicode(v) for k,v in data}
805
806 def progressGet(self, progress_id, profile):
796 """Return a dict with progress information 807 """Return a dict with progress information
797 data['position'] : current possition 808
798 data['size'] : end_position 809 @param progress_id(unicode): unique id of the progressing element
810 @param profile: %(doc_profile)s
811 @return (dict): data with the following keys:
812 'position' (int): current possition
813 'size' (int): end_position
814 if id doesn't exists (may be a finished progression), and empty dict is returned
799 """ 815 """
800 client = self.getClient(profile) 816 client = self.getClient(profile)
801 data = {} 817 data = {}
802 try: 818 try:
803 client._progress_cb_map[progress_id](progress_id, data, profile) 819 client._progress_cb[progress_id](progress_id, data, profile)
804 except KeyError: 820 except KeyError:
805 pass 821 log.debug("Requested progress for unknown progress_id")
806 #log.debug("Requested progress for unknown progress_id")
807 return data 822 return data
823
824 def _progressGetAll(self, profile_key):
825 progress_all = self.progressGetAll(profile_key)
826 for profile, progress_dict in progress_all.iteritems():
827 for progress_id, data in progress_dict.iteritems():
828 for key, value in data.iteritems():
829 data[key] = unicode(value)
830 return progress_all
831
832 def progressGetAll(self, profile_key):
833 """Return all progress informations
834
835 @param profile_key: %(doc_profile)s get all progress from this profile
836 if C.PROF_KEY_ALL is used, all progress from all profiles are returned
837 @return (dict[dict]): key=progress id, value=dict of data with the following keys:
838 'position' (int): current possition
839 'size' (int): end_position
840 """
841 clients = self.getClients(profile_key)
842 progress_all = {}
843 for client in clients:
844 profile = client.profile
845 progress_dict = {}
846 progress_all[profile] = progress_dict
847 for progress_id, progress_cb in client._progress_cb.iteritems():
848 data = {}
849 progress_dict[progress_id] = data
850 progress_dict[progress_id] = progress_cb(progress_id, data, profile)
851 return progress_all
808 852
809 def registerCallback(self, callback, *args, **kwargs): 853 def registerCallback(self, callback, *args, **kwargs):
810 """ Register a callback. 854 """ Register a callback.
811 Use with_data=True in kwargs if the callback use the optional data dict 855 Use with_data=True in kwargs if the callback use the optional data dict
812 use force_id=id to avoid generated id. Can lead to name conflict, avoid if possible 856 use force_id=id to avoid generated id. Can lead to name conflict, avoid if possible