Skip to content
This repository has been archived by the owner on Oct 31, 2021. It is now read-only.

Commit

Permalink
Corrections in keyboard handling and Unit Tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyrille Pontvieux committed Feb 1, 2013
1 parent 99f1bcc commit edcb6e6
Showing 1 changed file with 42 additions and 8 deletions.
50 changes: 42 additions & 8 deletions src/salix-live-installer/keyboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@
- listAvailableKeymaps
- isNumLockEnabledByDefault
- isIbusEnabledByDefault
- setDefaultKeymap
- setNumLockDefault
- setIbusDefault
"""
import os
import re
import glob
from kernel import *
from chroot import *
from execute import checkRoot

_keymapsLocation = ['/usr/share/salixtools/keymaps', '/mnt/salt/lib/keymaps', 'keymaps']

Expand All @@ -21,9 +25,9 @@ def findCurrentKeymap(mountPoint = None):
Find the currently used console keymaps (as loaded by 'loadkeys') by looking in:
- /etc/rc.d/rc.keymap, or
- in the 'keyb=' kernel parameter
The detected keymap is then check against the first column of one of the files: {kml}
The detected keymap is then check against the first column of one of the files: {0}
Returns None if not found
""".format({kml: ' '.join(_keymapsLocation)})
""".format(' '.join(_keymapsLocation))
if mountPoint and not os.path.isdir(mountPoint):
raise IOError("'{0}' does not exist or is not a directory.".format(mountPoint))
if mountPoint == None:
Expand All @@ -39,7 +43,7 @@ def findCurrentKeymap(mountPoint = None):
if kmFile:
# first, try parsing /etc/rc.d/rc.keymap
try:
for line in open('{0}/etc/rc.d/rc.keymap'.format(mountPoint), 'rb').read().decode('utf8').readlines():
for line in open('{0}/etc/rc.d/rc.keymap'.format(mountPoint), 'rb').read().decode('utf8').splitlines():
if '.map' in line:
keymap = re.sub(r'^.* ([^ ]+)\.map$', r'\1', line)
break
Expand All @@ -52,7 +56,7 @@ def findCurrentKeymap(mountPoint = None):
keymap = keybParam
if keymap:
# verify that the detected keymap actually exists
if not keymap in [line.split('|', 1)[0] for line in open(kmFile, 'r').read().readlines() if line and line[0] != '#']:
if not keymap in [line.split('|', 1)[0] for line in open(kmFile, 'r').read().splitlines() if line and line[0] != '#']:
keymap = None
return keymap

Expand All @@ -61,8 +65,8 @@ def listAvailableKeymaps(mountPoint = None):
Returns a list of couple (keymap, keyboardType).
'keymap' is a Console keymap as found in /usr/share/kbd/
'keyboardType' is either 'azerty', 'qwerty', 'qwertz', etc and is there only for information
The keymaps are extracted from one of the files: {kml}
""".format({kml: ' '.join(_keymapsLocation)})
The keymaps are extracted from one of the files: {0}
""".format(' '.join(_keymapsLocation))
if mountPoint and not os.path.isdir(mountPoint):
raise IOError("'{0}' does not exist or is not a directory.".format(mountPoint))
if mountPoint == None:
Expand All @@ -76,7 +80,7 @@ def listAvailableKeymaps(mountPoint = None):
kmFile = kml
break
if kmFile:
if keymap in [line.split('|', 1)[0] for line in open(kmFile, 'r').read().readlines() if line and line[0] != '#']:
for keymap in [line.split('|', 1)[0] for line in open(kmFile, 'r').read().splitlines() if line and line[0] != '#']:
keyboardType = '-'
typePosition = 6 # usr/share/kbd/keymaps/i386/azerty => 6
if mountPoint:
Expand Down Expand Up @@ -114,7 +118,10 @@ def isIbusEnabledByDefault(mountPoint = None):

def setDefaultKeymap(keymap, mountPoint = None):
"""
Fix the configuration in /etc/rc.d/rc.keymap to use the specified 'keymap'.
This use 'keyboardsetup' Salix tool.
"""
checkRoot()
if mountPoint and not os.path.isdir(mountPoint):
raise IOError("'{0}' does not exist or is not a directory.".format(mountPoint))
if mountPoint == None:
Expand All @@ -126,7 +133,10 @@ def setDefaultKeymap(keymap, mountPoint = None):

def setNumLockDefault(enabled, mountPoint = None):
"""
Fix the configuration for default numlock activated on boot or not.
This use 'keyboardsetup' Salix tool.
"""
checkRoot()
if mountPoint and not os.path.isdir(mountPoint):
raise IOError("'{0}' does not exist or is not a directory.".format(mountPoint))
if mountPoint == None:
Expand All @@ -141,7 +151,10 @@ def setNumLockDefault(enabled, mountPoint = None):

def setIbusDefault(enabled, mountPoint = None):
"""
Fix the configuration for default Ibus activated on boot or not.
This use 'keyboardsetup' Salix tool.
"""
checkRoot()
if mountPoint and not os.path.isdir(mountPoint):
raise IOError("'{0}' does not exist or is not a directory.".format(mountPoint))
if mountPoint == None:
Expand All @@ -157,4 +170,25 @@ def setIbusDefault(enabled, mountPoint = None):
# Unit test
if __name__ == '__main__':
from assertPlus import *
# TODO ?
checkRoot()
keymaps = listAvailableKeymaps()
assertTrue(type(keymaps) == list)
assertTrue(len(keymaps) > 0)
keymaps = dict(keymaps) # change it to dictionnary
assertEquals('azerty', keymaps['fr-latin9'])
keymap = findCurrentKeymap()
assertTrue(keymap)
numlock = isNumLockEnabledByDefault()
assertTrue(type(numlock) == bool)
ibus = isIbusEnabledByDefault()
assertTrue(type(ibus) == bool)
assertEquals(0, setDefaultKeymap('fr-latin1'))
assertEquals('fr-latin1', findCurrentKeymap())
assertEquals(0, setNumLockDefault(True))
assertTrue(isNumLockEnabledByDefault())
assertEquals(0, setIbusDefault(True))
assertTrue(isIbusEnabledByDefault())
# restore actual keybaord parameters
setDefaultKeymap(keymap)
setNumLockDefault(numlock)
setIbusDefault(ibus)

0 comments on commit edcb6e6

Please sign in to comment.