summaryrefslogtreecommitdiff
path: root/.gitlab-ci
diff options
context:
space:
mode:
authorPeter Hutterer <peter.hutterer@who-t.net>2021-01-22 12:55:39 +1000
committerSergey Udaltsov <sergey.udaltsov@gmail.com>2021-02-16 16:16:32 +0000
commitd576583732dd5147f23c652975f42436588b061a (patch)
tree24a2015a67a8ce44a2e34081c4cb969ed23f4682 /.gitlab-ci
parent4265882fd2293cebea4980f7df0f2c53357bf78b (diff)
downloadxkeyboard-config-d576583732dd5147f23c652975f42436588b061a.tar.gz
gitlab CI: format the python script with black
While I'm not a huge fan of the code style it enforces, at least it's consistent this way. Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
Diffstat (limited to '.gitlab-ci')
-rwxr-xr-x.gitlab-ci/generate-evdev-keycodes.py110
1 files changed, 67 insertions, 43 deletions
diff --git a/.gitlab-ci/generate-evdev-keycodes.py b/.gitlab-ci/generate-evdev-keycodes.py
index fd8b0af..f6e7a18 100755
--- a/.gitlab-ci/generate-evdev-keycodes.py
+++ b/.gitlab-ci/generate-evdev-keycodes.py
@@ -12,30 +12,34 @@ import argparse
import contextlib
import re
import sys
+
try:
import libevdev
except ImportError:
- print('WARNING: python-libevdev not available, cannot check for new evdev keycodes', file=sys.stderr)
+ print(
+ "WARNING: python-libevdev not available, cannot check for new evdev keycodes",
+ file=sys.stderr,
+ )
sys.exit(77)
# The marker to search for in the template file, replaced with our generated
# codes.
-replacement_marker = '@evdevkeys@'
+replacement_marker = "@evdevkeys@"
# These markers are put into the result file and are used to detect
# the section that we added when parsing an existing file.
-section_header = 'Key codes below are autogenerated'
-section_footer = 'End of autogenerated key codes'
+section_header = "Key codes below are autogenerated"
+section_footer = "End of autogenerated key codes"
def evdev_codes():
- '''
+ """
Return the dict {code, name} for all known evdev codes.
The list of names is compiled into libevdev.so, use a recent libevdev
release to get the most up-to-date list.
- '''
+ """
codes = {}
for c in libevdev.EV_KEY.codes:
# 112 because that's where our 1:1 keycode entries historically
@@ -45,7 +49,7 @@ def evdev_codes():
if c.value < 112 or not c.is_defined:
continue
- if c.name.startswith('BTN_') or c.name == 'KEY_MAX':
+ if c.name.startswith("BTN_") or c.name == "KEY_MAX":
continue
codes[c.value] = c.name
@@ -54,14 +58,14 @@ def evdev_codes():
def existing_keys(lines):
- '''
+ """
Return the dict {code, name} for all existing keycodes in the templates
file.
This is a very simple parser, good enough for the keycodes/evdev file
but that's about it.
- '''
- pattern = re.compile(r'\s+\<([^>]+)\>\s+=\s+(\d+);')
+ """
+ pattern = re.compile(r"\s+\<([^>]+)\>\s+=\s+(\d+);")
keys = {}
for line in lines:
match = re.match(pattern, line)
@@ -73,7 +77,7 @@ def existing_keys(lines):
def generate_keycodes_file(template, codes):
- '''
+ """
Generate a new keycodes/evdev file with line containing @evdevkeys@
replaced by the full list of known evdev key codes, including our
section_header/footer. Expected output:
@@ -85,7 +89,7 @@ def generate_keycodes_file(template, codes):
...
// $section_footer
- '''
+ """
lines = template.readlines()
existing = existing_keys(lines)
@@ -95,7 +99,7 @@ def generate_keycodes_file(template, codes):
output.append(line)
continue
- output.append(f'\t// {section_header}\n')
+ output.append(f"\t// {section_header}\n")
warned = False
for code, name in codes.items():
@@ -103,25 +107,29 @@ def generate_keycodes_file(template, codes):
if xkeycode > 255 and not warned:
warned = True
- output.append('\n')
- output.append('\t// Key codes below cannot be used in X\n')
- output.append('\n')
+ output.append("\n")
+ output.append("\t// Key codes below cannot be used in X\n")
+ output.append("\n")
if xkeycode in existing:
- output.append(f'\talias <I{xkeycode}> = <{existing[xkeycode]}>; // #define {name:23s} {code}\n')
+ output.append(
+ f"\talias <I{xkeycode}> = <{existing[xkeycode]}>; // #define {name:23s} {code}\n"
+ )
continue
# Special keys that need a comment
special_keys = {
- 211: 'conflicts with AB11',
+ 211: "conflicts with AB11",
}
- comment = special_keys.get(xkeycode, '')
+ comment = special_keys.get(xkeycode, "")
if comment:
- comment = f' {comment}'
+ comment = f" {comment}"
- output.append(f'\t<I{xkeycode}> = {xkeycode};\t\t// #define {name:23s} {code}{comment}\n')
- output.append(f'\t// {section_footer}\n')
+ output.append(
+ f"\t<I{xkeycode}> = {xkeycode};\t\t// #define {name:23s} {code}{comment}\n"
+ )
+ output.append(f"\t// {section_footer}\n")
return output
@@ -132,7 +140,7 @@ def extract_generated_keycodes(fp):
header and footer.
"""
in_generated_section = False
- pattern = re.compile('.*<I([0-9]*)>.*')
+ pattern = re.compile(".*<I([0-9]*)>.*")
for line in fp:
if section_header in line:
@@ -147,11 +155,11 @@ def extract_generated_keycodes(fp):
def compare_with(codes, oldfile):
- '''
+ """
Extract the <I123> keycodes from between the section_header/footer of
oldfile and return a list of keycodes that are in codes but not in
oldfile.
- '''
+ """
old_keycodes = extract_generated_keycodes(oldfile)
keycodes = [c + 8 for c in codes] # X keycode offset
@@ -165,24 +173,38 @@ def log_msg(msg):
def main():
- parser = argparse.ArgumentParser(description='Generate the evdev keycode lists.')
- parser.add_argument('--template', type=argparse.FileType('r'),
- default=open('keycodes/evdev.in'),
- help='The template file (default: keycodes/evdev.in)')
- parser.add_argument('--output', type=str, default='keycodes/evdev', required=False,
- help='The file to be written to (default: keycodes/evdev)')
- parser.add_argument('--compare-with', type=argparse.FileType('r'),
- default=open('keycodes/evdev'),
- help='Compare generated output with the given file (default: keycodes/evdev)')
- parser.add_argument('--verbose', action=argparse.BooleanOptionalAction,
- help='Print verbose output to stderr')
+ parser = argparse.ArgumentParser(description="Generate the evdev keycode lists.")
+ parser.add_argument(
+ "--template",
+ type=argparse.FileType("r"),
+ default=open("keycodes/evdev.in"),
+ help="The template file (default: keycodes/evdev.in)",
+ )
+ parser.add_argument(
+ "--output",
+ type=str,
+ default="keycodes/evdev",
+ required=False,
+ help="The file to be written to (default: keycodes/evdev)",
+ )
+ parser.add_argument(
+ "--compare-with",
+ type=argparse.FileType("r"),
+ default=open("keycodes/evdev"),
+ help="Compare generated output with the given file (default: keycodes/evdev)",
+ )
+ parser.add_argument(
+ "--verbose",
+ action=argparse.BooleanOptionalAction,
+ help="Print verbose output to stderr",
+ )
ns = parser.parse_args()
codes = evdev_codes()
rc = 0
if ns.verbose:
kmin, kmax = min(codes.keys()), max(codes.keys())
- log_msg(f'evdev keycode range: {kmin} ({kmin:#x}) → {kmax} ({kmax:#x})')
+ log_msg(f"evdev keycode range: {kmin} ({kmin:#x}) → {kmax} ({kmax:#x})")
# We compare before writing so we can use the same filename for
# --compare-with and --output. That's also why --output has to be type
@@ -192,21 +214,23 @@ def main():
if diff:
rc = 1
if ns.verbose:
- log_msg(f'File {ns.compare_with.name} is out of date, missing keycodes:')
+ log_msg(
+ f"File {ns.compare_with.name} is out of date, missing keycodes:"
+ )
for k in diff:
name = codes[k - 8] # remove the X offset
- log_msg(f' <I{k}> // #define {name}')
+ log_msg(f" <I{k}> // #define {name}")
with contextlib.ExitStack() as stack:
- if ns.output == '-':
+ if ns.output == "-":
fd = sys.stdout
else:
- fd = stack.enter_context(open(ns.output, 'w'))
+ fd = stack.enter_context(open(ns.output, "w"))
output = generate_keycodes_file(ns.template, codes)
- fd.write(''.join(output))
+ fd.write("".join(output))
sys.exit(rc)
-if __name__ == '__main__':
+if __name__ == "__main__":
main()