Skip to content

Instantly share code, notes, and snippets.

@r3n4ud
Forked from jrd/bépo-afnor-quote-invert
Created March 15, 2022 09:27
Show Gist options
  • Save r3n4ud/a3a05bec00751d1764f57afb4c97bfac to your computer and use it in GitHub Desktop.
Save r3n4ud/a3a05bec00751d1764f57afb4c97bfac to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
from os import getuid
from subprocess import run
from sys import exit, stderr
MAIN_PATH = '/usr/share/X11/xkb/symbols/fr'
BACKUP_PATH = MAIN_PATH + '.bck'
if getuid() != 0:
print(
"Vous devez lancer ce script avec les droits d’administration.",
file=stderr,
)
exit(0)
# création sauvegarde si inexistante ou fichier plus récent (après mise à jour)
run(['cp', '-u', MAIN_PATH, BACKUP_PATH], check=True)
with open(MAIN_PATH, 'r+') as f:
content = f.read()
# liste des modifications ('original': 'modification')
modifications = {
"""key <AC05> { type[group1] = "FOUR_LEVEL", [ comma, semicolon, apostrophe, dead_belowcomma ] }; // , ; ' ,""": # noqa: E501
"""key <AC05> { type[group1] = "FOUR_LEVEL", [ comma, semicolon, rightsinglequotemark, dead_belowcomma ] }; // , ; ’ ,""", # noqa: E501
"""key <AB06> { type[group1] = "FOUR_LEVEL", [ rightsinglequotemark, question, questiondown, dead_hook ] }; // ’ ? ¿ ̉ """: # noqa: E501
"""key <AB06> { type[group1] = "FOUR_LEVEL", [ apostrophe, question, questiondown, dead_hook ] }; // ' ? ¿ ̉ """, # noqa: E501
}
if all([orig.strip() in content for orig in modifications]):
f.seek(0)
for original, modification in modifications.items():
content = content.replace(original.strip(), modification.strip())
f.write(content)
xkbinfo = dict([
map(lambda x: x.strip(), line.split(':', 1))
for line in
run(['setxkbmap', '-query'], check=True, capture_output=True, text=True).stdout.strip().split('\n') # noqa: E501
])
run(['setxkbmap', xkbinfo['layout'], xkbinfo['variant']], check=True)
print("Modifications effectuées, veuillez redémarrer X dès que possible")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment