[PATCH 1/2] Replace system-config-keyboard with our methods using ksdata.keyboard

Vratislav Podzimek vpodzime at redhat.com
Mon Jul 23 12:47:05 UTC 2012


The only thing we were using from the system-config-keyboard was the
activate method that basically just calls loadkeys command. This can
be easily replaced with our own method using data from ksdata.keyboard
allowing us to remove anaconda.keyboard object.
---
 anaconda                               |    6 ++-
 anaconda.spec.in                       |    2 -
 pyanaconda/__init__.py                 |    2 -
 pyanaconda/keyboard.py                 |   67 ++++++++++++++++++++++++++++----
 pyanaconda/language.py                 |   12 ------
 tests/pyanaconda_test/language_test.py |   20 ----------
 6 files changed, 63 insertions(+), 46 deletions(-)

diff --git a/anaconda b/anaconda
index 256e7cd..2c17b58 100755
--- a/anaconda
+++ b/anaconda
@@ -725,6 +725,7 @@ if __name__ == "__main__":
     from pyanaconda import vnc
     from pyanaconda import kickstart
     from pyanaconda import ntp
+    from pyanaconda import keyboard
 
     # to set UTF8 mode on the terminal, we need LANG to be set usefully
     if os.environ.get("LANG", "C") == "C":
@@ -901,8 +902,9 @@ if __name__ == "__main__":
     if opts.keymap:
         if not ksdata.keyboard.keyboard:
             ksdata.keyboard.keyboard = opts.keymap
-        anaconda.keyboard.set(opts.keymap)
-        anaconda.keyboard.activate()
+
+    if ksdata.keyboard.keyboard:
+        keyboard.activate_console_keymap(ksdata.keyboard.keyboard)
 
     # now start the interface
     setupDisplay(anaconda, opts)
diff --git a/anaconda.spec.in b/anaconda.spec.in
index 2c5b246..3eb99a6 100644
--- a/anaconda.spec.in
+++ b/anaconda.spec.in
@@ -85,7 +85,6 @@ BuildRequires: zlib-devel
 BuildRequires: NetworkManager-devel >= %{nmver}
 BuildRequires: NetworkManager-glib-devel >= %{nmver}
 BuildRequires: dbus-devel >= %{dbusver}, dbus-python
-BuildRequires: system-config-keyboard >= %{sckeyboardver}
 %ifarch %livearches
 BuildRequires: desktop-file-utils
 %endif
@@ -126,7 +125,6 @@ Requires: python-cryptsetup >= %{pythoncryptsetupver}
 Requires: mdadm
 Requires: lvm2
 Requires: util-linux >= 2.15.1
-Requires: system-config-keyboard >= %{sckeyboardver}
 Requires: dbus-python
 Requires: python-pwquality
 Requires: python-bugzilla
diff --git a/pyanaconda/__init__.py b/pyanaconda/__init__.py
index eb44ca9..4eb3179 100644
--- a/pyanaconda/__init__.py
+++ b/pyanaconda/__init__.py
@@ -44,7 +44,6 @@ _ = lambda x: gettext.ldgettext("anaconda", x)
 class Anaconda(object):
     def __init__(self):
         import desktop, firewall, security
-        import system_config_keyboard.keyboard as keyboard
         from flags import flags
 
         self._backend = None
@@ -60,7 +59,6 @@ class Anaconda(object):
         self._instLanguage = None
         self._intf = None
         self.isHeadless = False
-        self.keyboard = keyboard.Keyboard()
         self.ksdata = None
         self.mediaDevice = None
         self.methodstr = None
diff --git a/pyanaconda/keyboard.py b/pyanaconda/keyboard.py
index d0497d6..9a6c21f 100755
--- a/pyanaconda/keyboard.py
+++ b/pyanaconda/keyboard.py
@@ -31,6 +31,11 @@ for listing and various modifications of keyboard layouts settings.
 """
 
 import os
+import re
+from pyanaconda import iutil
+
+import logging
+log = logging.getLogger("anaconda")
 
 class KeyboardConfigError(Exception):
     """Exception class for keyboard configuration related problems"""
@@ -100,30 +105,76 @@ def get_layouts_xorg_conf(keyboard):
 
 def write_layouts_config(keyboard, root):
     """
-    Function that writes a file with layouts configuration to
-    $root/etc/X11/xorg.conf.d/01-anaconda-layouts.conf
+    Function that writes files with layouts configuration to
+    $root/etc/X11/xorg.conf.d/01-anaconda-layouts.conf and
+    $root/etc/sysconfig/keyboard.
 
     @param keyboard: ksdata.keyboard object
     @param root: path to the root of the installed system
 
     """
 
-    conf_dir = os.path.normpath(root + "/etc/X11/xorg.conf.d")
-    conf_file = "01-anaconda-keyboard.conf"
+    xconf_dir = os.path.normpath(root + "/etc/X11/xorg.conf.d")
+    xconf_file = "01-anaconda-keyboard.conf"
+
+    sysconf_file = "/etc/sysconfig/keyboard"
 
     try:
-        if not os.path.isdir(conf_dir):
-            os.makedirs(conf_dir)
+        if not os.path.isdir(xconf_dir):
+            os.makedirs(xconf_dir)
 
     except OSError as oserr:
         raise KeyboardConfigError("Cannot create directory xorg.conf.d")
 
     try:
-        with open(os.path.join(conf_dir, conf_file), "w") as f:
+        with open(os.path.join(xconf_dir, xconf_file), "w") as f:
             f.write(get_layouts_xorg_conf(keyboard))
 
+        with open(sysconf_file, "w") as f:
+            f.write('KEYTABLE="%s"\n' % keyboard.keyboard)
+
     except IOError as ioerr:
-        raise KeyboardConfigError("Cannot write keyboard configuration file")
+        raise KeyboardConfigError("Cannot write keyboard configuration files")
+
+def activate_console_keymap(keymap):
+    """
+    Try to setup a given keymap as a console keymap. If there is no such
+    keymap, try to setup a basic variant (e.g. 'cz' instead of 'cz (qwerty)').
+
+    @param keymap: a keymap
+    @type keymap: string
+    @raise KeyboardConfigError: if loadkeys command is not available
+    @return: False if failed to activate both the given keymap and its basic
+             variant, True otherwise
+
+    """
+
+    try:
+        #TODO: replace with calling systemd-localed methods once it can load
+        #      X layouts
+        ret = iutil.execWithRedirect("loadkeys", [keymap], stdout="/dev/tty5",
+                                     stderr="/dev/tty5")
+    except OSError as oserr:
+        msg = "'loadkeys' command not available (%s)" % oserr.strerror
+        raise KeyboardConfigError(msg)
+
+    if ret != 0:
+        log.error("Failed to activate keymap %s" % keymap)
+
+        #failed to activate the given keymap, extract and try
+        #the basic keymap -- e.g. 'cz-cp1250' -> 'cz'
+        parts = re.split(r'[- _(]', keymap, 1)
+        keymap = parts[0]
+
+        ret = iutil.execWithRedirect("loadkeys", [keymap], stdout="/dev/tty5",
+                                     stderr="/dev/tty5")
+
+        if ret != 0:
+            log.error("Failed to activate basic variant %s" % keymap)
+        else:
+            log.error("Activated basic variant %s, instead" % keymap)
+
+    return ret == 0
 
 def item_str(s):
     """Convert a zero-terminated byte array to a proper str"""
diff --git a/pyanaconda/language.py b/pyanaconda/language.py
index f1663d7..fb8d35e 100644
--- a/pyanaconda/language.py
+++ b/pyanaconda/language.py
@@ -28,7 +28,6 @@ import gettext
 from pyanaconda.constants import ROOT_PATH, DEFAULT_LANG
 import localeinfo
 from simpleconfig import SimpleConfigFile
-import system_config_keyboard.keyboard as keyboard
 
 import logging
 log = logging.getLogger("anaconda")
@@ -165,17 +164,6 @@ class Language(object):
     def getCurrentLangSearchList(self):
         return localeinfo.expandLangs(self.systemLang) + ['C']
 
-    def getDefaultKeyboard(self):
-        try:
-            return self.localeInfo[self.systemLang][3]
-        except KeyError:
-            try:
-                kbd = keyboard.Keyboard()
-                kbd.read(ROOT_PATH)
-                return kbd.get()
-            except:
-                return self.localeInfo[self._default][3]
-
     def getDefaultTimeZone(self):
         try:
             return self.localeInfo[self.systemLang][4]
diff --git a/tests/pyanaconda_test/language_test.py b/tests/pyanaconda_test/language_test.py
index 5c895b2..2325a73 100644
--- a/tests/pyanaconda_test/language_test.py
+++ b/tests/pyanaconda_test/language_test.py
@@ -142,26 +142,6 @@ class LanguageTest(mock.TestCase):
         ret = lang.getCurrentLangSearchList()
         self.assertEqual(set(ret), set(['cs_CZ.UTF-8', 'cs_CZ', 'cs', 'C']))
 
-    def get_default_keyboard_default_test(self):
-        import pyanaconda.language
-        lang = pyanaconda.language.Language()
-        ret = lang.getDefaultKeyboard()
-        self.assertEqual(ret, 'us')
-
-    def get_default_keyboard_after_set_test(self):
-        import pyanaconda.language
-        lang = pyanaconda.language.Language()
-        lang.systemLang = 'cs'
-        ret = lang.getDefaultKeyboard()
-        self.assertEqual(ret, 'cz-lat2')
-
-    def get_default_keyboard_with_cs_CZ_locale_test(self):
-        import pyanaconda.language
-        pyanaconda.language.os.environ = {'LANG': 'cs'}
-        lang = pyanaconda.language.Language()
-        ret = lang.getDefaultKeyboard()
-        self.assertEqual(ret, 'cz-lat2')
-
     def get_default_time_zone_default_test(self):
         import pyanaconda.language
         pyanaconda.language.os.path.exists = mock.Mock(return_value=False)
-- 
1.7.10.4



More information about the anaconda-patches mailing list