input validation for mapping change

xkb
sezanzeb 4 years ago committed by sezanzeb
parent 4e01e43d0c
commit 7064be391e

@ -438,6 +438,8 @@ if __name__ == '__main__':
log_info()
if getpass.getuser() != 'root' and 'unittest' not in sys.modules.keys():
# TODO
# add a polkit thing like https://gitlab.manjaro.org/applications/pamac/-/tree/master/data/polkit
logger.error('Needs to run with sudo')
ErrorDialog(
'Error',

@ -99,6 +99,19 @@ class Mapping:
new_keycode : int
character : string
"""
try:
new_keycode = int(new_keycode)
except ValueError:
logger.error('Cannot use %s as keycode', new_keycode)
return False
if previous_keycode is not None:
try:
previous_keycode = int(previous_keycode)
except ValueError:
logger.error('Cannot use %s as keycode', previous_keycode)
return False
if new_keycode and character:
self._mapping[new_keycode] = str(character)
if new_keycode != previous_keycode:
@ -106,7 +119,9 @@ class Mapping:
# representing that one will now represent a different one.
self.clear(previous_keycode)
self.changed = True
return self.changed
return True
return False
def clear(self, keycode):
"""Remove a keycode from the mapping.

@ -28,7 +28,8 @@ Is a module so that tests can modify them.
import os
# the path in home, is symlinked with SYMBOLS_PATH
# the path in home, is symlinked with SYMBOLS_PATH.
# getlogin gets the user who ran sudo
CONFIG_PATH = os.path.join('/home', os.getlogin(), '.config/key-mapper')
# should not contain spaces

@ -59,6 +59,15 @@ class TestMapping(unittest.TestCase):
self.assertEqual(self.mapping.get(4), 'e')
self.assertEqual(len(self.mapping), 2)
# and this
self.mapping.change('4', '4', 'f')
self.assertEqual(self.mapping.get(4), 'f')
self.assertEqual(len(self.mapping), 2)
# non-ints are ignored
self.mapping.change('a', 'b', 'c')
self.assertEqual(len(self.mapping), 2)
def test_clear(self):
# does nothing
self.mapping.clear(40)

Loading…
Cancel
Save