[Sugar-devel] [sugar 0.98 PATCH v2] sl#3800: In "My Settings" -> "ModemConfiguration", convert the procedure to process the results of "connection.get_secrets('gsm', _secrets_cb, _secrets_err_cb)" from pseudo-asynchronous to asynchronous.

Ajay Garg ajay at activitycentral.com
Mon Aug 13 01:21:59 EDT 2012


Note that this patch is to be applied in full, and NOT over version-1
patch, present at http://patchwork.sugarlabs.org/patch/1643/


Changes of version-2 over version-1 ::::
=======================================

a)
Fixed a typo in the description.

Thanks James Cameron :)


b)
Removed extraneous comments.

Thanks James Cameron :)


========================================================================


In the method "get_modem_settings()", we call
"connection.get_secrets('gsm', _secrets_cb, _secrets_err_cb)". This is
an asynchronous call.

As of now, we busy wait, till the specified "result" callback is called
(which in this case happens to be "def _secrets_cb(secrets)". After this
is called, we return the modem-settings.

Instead of busy waiting, pass a callback from the callee function.
Thereafter, when "def _secrets_cb(secrets)" is hit, execute this
callback function, passing the modem-settings.


 extensions/cpsection/modemconfiguration/model.py |   10 +++-------
 extensions/cpsection/modemconfiguration/view.py  |    4 +++-
 2 files changed, 6 insertions(+), 8 deletions(-)

diff --git a/extensions/cpsection/modemconfiguration/model.py b/extensions/cpsection/modemconfiguration/model.py
index 969b5d9..7781165 100755
--- a/extensions/cpsection/modemconfiguration/model.py
+++ b/extensions/cpsection/modemconfiguration/model.py
@@ -26,7 +26,7 @@ def get_connection():
     return network.find_gsm_connection()
 
 
-def get_modem_settings():
+def get_modem_settings(callback):
     modem_settings = {}
     connection = get_connection()
     if not connection:
@@ -48,6 +48,8 @@ def get_modem_settings():
         modem_settings['password'] = gsm_secrets.get('password', '')
         modem_settings['pin'] = gsm_secrets.get('pin', '')
 
+        callback(modem_settings)
+
     def _secrets_err_cb(err):
         secrets_call_done[0] = True
         if isinstance(err, dbus.exceptions.DBusException) and \
@@ -59,12 +61,6 @@ def get_modem_settings():
     # must be called asynchronously as this re-enters the GTK main loop
     connection.get_secrets('gsm', _secrets_cb, _secrets_err_cb)
 
-    # wait til asynchronous execution completes
-    while not secrets_call_done[0]:
-        gtk.main_iteration()
-
-    return modem_settings
-
 
 def _set_or_clear(_dict, key, value):
     """Update a dictionary value for a specific key. If value is None or
diff --git a/extensions/cpsection/modemconfiguration/view.py b/extensions/cpsection/modemconfiguration/view.py
index 4ce6c0d..39a30c0 100644
--- a/extensions/cpsection/modemconfiguration/view.py
+++ b/extensions/cpsection/modemconfiguration/view.py
@@ -118,7 +118,9 @@ class ModemConfiguration(SectionView):
         entry.handler_unblock_by_func(self.__entry_changed_cb)
 
     def setup(self):
-        settings = self._model.get_modem_settings()
+        self._model.get_modem_settings(self.populate_entries)
+
+    def populate_entries(self, settings):
         self._populate_entry(self._username_entry,
             settings.get('username', ''))
         self._populate_entry(self._number_entry, settings.get('number', ''))
-- 
1.7.4.4



More information about the Sugar-devel mailing list