1#!/usr/local/bin/python3.8 2 3import sys 4import os 5import syslog 6import subprocess 7 8if __name__ == '__main__': 9 10 try: 11 12 # Exit if something is missing 13 for required_file in ["/usr/local/etc/default/keyboard", "/usr/local/bin/setxkbmap"]: 14 if not os.path.exists(required_file): 15 syslog.syslog("%s not found." % required_file) 16 sys.exit(0) 17 18 # Log current keyboard configuration 19 output = subprocess.check_output(["setxkbmap", "-query"]).decode("UTF-8") 20 syslog.syslog("Current keyboard configuration: %s" % output) 21 22 # Parse keyboard configuration file 23 XKBMODEL = "" 24 XKBLAYOUT = "" 25 XKBVARIANT = "" 26 XKBOPTIONS = "" 27 with open("/usr/local/etc/default/keyboard", "r") as keyboard: 28 for line in keyboard: 29 line = line.strip() 30 if "XKBMODEL=" in line: 31 XKBMODEL = line.split('=')[1].replace('"', '') 32 if "XKBLAYOUT=" in line: 33 XKBLAYOUT = line.split('=')[1].replace('"', '') 34 if "XKBVARIANT=" in line: 35 XKBVARIANT = line.split('=')[1].replace('"', '') 36 if "XKBOPTIONS=" in line: 37 XKBOPTIONS = line.split('=')[1].replace('"', '') 38 39 # Apply keyboard configuration 40 command = ["setxkbmap", "-model", XKBMODEL, "-layout", XKBLAYOUT, "-variant", XKBVARIANT, "-option", XKBOPTIONS, "-v"] 41 syslog.syslog("Applying keyboard configuration: %s" % command) 42 output = subprocess.check_output(command).decode("UTF-8") 43 syslog.syslog("Result: %s" % output) 44 45 # Log new keyboard configuration 46 output = subprocess.check_output(["setxkbmap", "-query"]).decode("UTF-8") 47 syslog.syslog("New keyboard configuration: %s" % output) 48 49 except Exception as e: 50 # best effort, syslog it and bail out 51 syslog.syslog("ERROR: %s" % e) 52 53 sys.exit(0) 54