[Sugar-devel] [PATCH sugar 03/20] Split keyboard setup out of main.py

Daniel Narvaez dwnarvaez at gmail.com
Mon Dec 10 14:11:29 EST 2012


From: Daniel Narvaez <dwnarvaez at gmail.com>

main.py is big and messy and the setup method is complex enough
to be worth it's own module.
---
 src/jarabe/main.py           |   68 +--------------------------------
 src/jarabe/model/Makefile.am |    1 +
 src/jarabe/model/keyboard.py |   85 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 88 insertions(+), 66 deletions(-)
 create mode 100755 src/jarabe/model/keyboard.py

diff --git a/src/jarabe/main.py b/src/jarabe/main.py
index 77b8841..55d0496 100755
--- a/src/jarabe/main.py
+++ b/src/jarabe/main.py
@@ -38,19 +38,11 @@ from gi.repository import GLib
 from gi.repository import GConf
 from gi.repository import Gtk
 from gi.repository import Gdk
-from gi.repository import GdkX11
 from gi.repository import GObject
 from gi.repository import Gst
 import dbus.glib
 from gi.repository import Wnck
 
-_USE_XKL = False
-try:
-    from gi.repository import Xkl
-    _USE_XKL = True
-except ImportError:
-    logging.debug('Could not load xklavier for keyboard configuration')
-
 GLib.threads_init()
 Gdk.threads_init()
 dbus.glib.threads_init()
@@ -116,62 +108,6 @@ def setup_file_transfer_cb():
     from jarabe.model import filetransfer
     filetransfer.init()
 
-def setup_keyboard_cb():
-    logging.debug('STARTUP: setup_keyboard_cb')
-
-    gconf_client = GConf.Client.get_default()
-    have_config = False
-
-    try:
-        display = GdkX11.x11_get_default_xdisplay()
-        if display is not None:
-            engine = Xkl.Engine.get_instance(display)
-        else:
-            logging.debug('setup_keyboard_cb: Could not get default display.')
-            return
-
-        configrec = Xkl.ConfigRec()
-        configrec.get_from_server(engine)
-
-        # FIXME, gconf_client_get_list not introspectable #681433
-        layouts_from_gconf = gconf_client.get(
-            '/desktop/sugar/peripherals/keyboard/layouts')
-        layouts_list = []
-        variants_list = []
-        if layouts_from_gconf:
-            for gval in layouts_from_gconf.get_list():
-                layout = gval.get_string()
-                layouts_list.append(layout.split('(')[0])
-                variants_list.append(layout.split('(')[1][:-1])
-
-            if layouts_list and variants_list:
-                have_config = True
-                configrec.set_layouts(layouts_list)
-                configrec.set_variants(variants_list)
-
-        model = gconf_client.get_string(\
-            '/desktop/sugar/peripherals/keyboard/model')
-        if model:
-            have_config = True
-            configrec.set_model(model)
-
-        options = []
-        # FIXME, gconf_client_get_list not introspectable #681433
-        options_from_gconf = gconf_client.get(\
-            '/desktop/sugar/peripherals/keyboard/options')
-        if options_from_gconf:
-            for gval in options_from_gconf.get_list():
-                option = gval.get_string()
-                options.append(option)
-            if options:
-                have_config = True
-                configrec.set_options(options)
-
-        if have_config:
-            configrec.activate(engine)
-    except Exception:
-        logging.exception('Error during keyboard configuration')
-
 def setup_window_manager():
     logging.debug('STARTUP: window_manager')
 
@@ -201,8 +137,8 @@ def bootstrap():
     GObject.idle_add(setup_file_transfer_cb)
     GObject.idle_add(show_software_updates_cb)
 
-    if _USE_XKL:
-        GObject.idle_add(setup_keyboard_cb)
+    from jarabe.model import keyboard
+    keyboard.setup()
 
 def set_fonts():
     client = GConf.Client.get_default()
diff --git a/src/jarabe/model/Makefile.am b/src/jarabe/model/Makefile.am
index 2fc6b1c..8e2da6a 100644
--- a/src/jarabe/model/Makefile.am
+++ b/src/jarabe/model/Makefile.am
@@ -7,6 +7,7 @@ sugar_PYTHON =			\
 	filetransfer.py		\
 	friends.py		\
 	invites.py		\
+	keyboard.py		\
 	olpcmesh.py		\
 	mimeregistry.py		\
 	neighborhood.py		\
diff --git a/src/jarabe/model/keyboard.py b/src/jarabe/model/keyboard.py
new file mode 100755
index 0000000..43b16c2
--- /dev/null
+++ b/src/jarabe/model/keyboard.py
@@ -0,0 +1,85 @@
+# Copyright (C) 2006, Red Hat, Inc.
+# Copyright (C) 2009, One Laptop Per Child Association Inc
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+
+import logging
+
+from gi.repository import GConf
+from gi.repository import GdkX11
+
+_USE_XKL = False
+try:
+    from gi.repository import Xkl
+    _USE_XKL = True
+except ImportError:
+    logging.debug('Could not load xklavier for keyboard configuration')
+
+def setup():
+    if not _USE_XKL:
+        return
+
+    gconf_client = GConf.Client.get_default()
+    have_config = False
+
+    try:
+        display = GdkX11.x11_get_default_xdisplay()
+        if display is not None:
+            engine = Xkl.Engine.get_instance(display)
+        else:
+            logging.debug('setup_keyboard_cb: Could not get default display.')
+            return
+
+        configrec = Xkl.ConfigRec()
+        configrec.get_from_server(engine)
+
+        # FIXME, gconf_client_get_list not introspectable #681433
+        layouts_from_gconf = gconf_client.get(
+            '/desktop/sugar/peripherals/keyboard/layouts')
+        layouts_list = []
+        variants_list = []
+        if layouts_from_gconf:
+            for gval in layouts_from_gconf.get_list():
+                layout = gval.get_string()
+                layouts_list.append(layout.split('(')[0])
+                variants_list.append(layout.split('(')[1][:-1])
+
+            if layouts_list and variants_list:
+                have_config = True
+                configrec.set_layouts(layouts_list)
+                configrec.set_variants(variants_list)
+
+        model = gconf_client.get_string(\
+            '/desktop/sugar/peripherals/keyboard/model')
+        if model:
+            have_config = True
+            configrec.set_model(model)
+
+        options = []
+        # FIXME, gconf_client_get_list not introspectable #681433
+        options_from_gconf = gconf_client.get(\
+            '/desktop/sugar/peripherals/keyboard/options')
+        if options_from_gconf:
+            for gval in options_from_gconf.get_list():
+                option = gval.get_string()
+                options.append(option)
+            if options:
+                have_config = True
+                configrec.set_options(options)
+
+        if have_config:
+            configrec.activate(engine)
+    except Exception:
+        logging.exception('Error during keyboard configuration')
-- 
1.7.10.4



More information about the Sugar-devel mailing list