Debian Codegraph @

arctica-greeter-set-keyboard-layout

raw symbols SCIP
1 #!/usr/bin/python3
2 # -*- coding: utf-8 -*-
3
4 # Copyright (C) 2017 Clement Lefebvre <[email protected]>
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License version 3 as
8 # published by the Free Software Foundation.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #
18 # Authors: Clement Lefebvre <[email protected]>
19
20 import sys
21 import os
22 import syslog
23 import subprocess
24
25 if __name__ == '__main__':
26
27 try:
28
29 # Exit if something is missing
30 for required_file in ["/etc/default/keyboard", "/usr/bin/setxkbmap"]:
31 if not os.path.exists(required_file):
32 syslog.syslog("%s not found." % required_file)
33 sys.exit(0)
34
35 # Log current keyboard configuration
36 output = subprocess.check_output(["setxkbmap", "-query"]).decode("UTF-8")
37 syslog.syslog("Current keyboard configuration: %s" % output)
38
39 # Parse keyboard configuration file
40 XKBMODEL = ""
41 XKBLAYOUT = ""
42 XKBVARIANT = ""
43 XKBOPTIONS = ""
44 with open("/etc/default/keyboard", "r") as keyboard:
45 for line in keyboard:
46 line = line.strip()
47 if "XKBMODEL=" in line:
48 XKBMODEL = line.split('=')[1].replace('"', '')
49 if "XKBLAYOUT=" in line:
50 XKBLAYOUT = line.split('=')[1].replace('"', '')
51 if "XKBVARIANT=" in line:
52 XKBVARIANT = line.split('=')[1].replace('"', '')
53 if "XKBOPTIONS=" in line:
54 XKBOPTIONS = line.split('=')[1].replace('"', '')
55
56 # Apply keyboard configuration
57 command = ["setxkbmap", "-model", XKBMODEL, "-layout", XKBLAYOUT, "-variant", XKBVARIANT, "-option", XKBOPTIONS, "-v"]
58 syslog.syslog("Applying keyboard configuration: %s" % command)
59 output = subprocess.check_output(command).decode("UTF-8")
60 syslog.syslog("Result: %s" % output)
61
62 # Log new keyboard configuration
63 output = subprocess.check_output(["setxkbmap", "-query"]).decode("UTF-8")
64 syslog.syslog("New keyboard configuration: %s" % output)
65
66 except Exception as e:
67 # best effort, syslog it and bail out
68 syslog.syslog("ERROR: %s" % e)
69
70 sys.exit(0)