Python: make ruff & black happy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594
diff --git a/scripts/ensure-stable-doc-urls.py b/scripts/ensure-stable-doc-urls.py
index 9111e49..f40356b 100755
--- a/scripts/ensure-stable-doc-urls.py
+++ b/scripts/ensure-stable-doc-urls.py
@@ -140,12 +140,12 @@ def update_registry(registry_path: Path, doc_dir: Path, updates: Sequence[str]):
print(f"[ERROR] “{old}” not found and has no update.")
exit_code |= ExitCode.MISSING_UPDATES
if exit_code:
- print(f"[ERROR] Processing interrupted: please fix the errors above.")
+ print("[ERROR] Processing interrupted: please fix the errors above.")
exit(exit_code.value)
# Write changes
with registry_path.open("wt", encoding="utf-8") as fd:
fd.write(f"# WARNING: This file is autogenerated by: {RELATIVE_SCRIPT_PATH}\n")
- fd.write(f"# Do not edit manually.\n")
+ fd.write("# Do not edit manually.\n")
yaml.dump(
registry,
fd,
diff --git a/scripts/makekeys b/scripts/makekeys
index fe30067..f22277b 100755
--- a/scripts/makekeys
+++ b/scripts/makekeys
@@ -1,47 +1,55 @@
#!/usr/bin/env python
-import re, sys, itertools
+import re
+import sys
+import itertools
import perfect_hash
-pattern = re.compile(r'^#define\s+XKB_KEY_(?P<name>\w+)\s+(?P<value>0x[0-9a-fA-F]+)\s')
+pattern = re.compile(r"^#define\s+XKB_KEY_(?P<name>\w+)\s+(?P<value>0x[0-9a-fA-F]+)\s")
matches = [pattern.match(line) for line in open(sys.argv[1])]
entries = [(m.group("name"), int(m.group("value"), 16)) for m in matches if m]
entries_isorted = sorted(entries, key=lambda e: e[0].lower())
entries_kssorted = sorted(entries, key=lambda e: e[1])
-print('''
+print(
+ """
/**
* This file comes from libxkbcommon and was generated by makekeys.py
* You can always fetch the latest version from:
* https://raw.github.com/xkbcommon/libxkbcommon/master/src/ks_tables.h
*/
-''')
+"""
+)
entry_offsets = {}
-print('''
+print(
+ """
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Woverlength-strings"
#endif
static const char *keysym_names =
-'''.strip())
+""".strip()
+)
offs = 0
-for (name, _) in entries_isorted:
+for name, _ in entries_isorted:
entry_offsets[name] = offs
print(' "{name}\\0"'.format(name=name))
offs += len(name) + 1
-print('''
+print(
+ """
;
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
-'''.strip())
+""".strip()
+)
-template = r'''
+template = r"""
static const uint16_t keysym_name_G[] = {
$G
};
@@ -63,27 +71,39 @@ keysym_name_perfect_hash(const char *key)
keysym_name_G[keysym_name_hash_f(key, "$S2")]
) % $NG;
}
-'''
-print(perfect_hash.generate_code(
- keys=[name for name, value in entries_isorted],
- template=template,
-))
+"""
+print(
+ perfect_hash.generate_code(
+ keys=[name for name, value in entries_isorted],
+ template=template,
+ )
+)
-print('''
+print(
+ """
struct name_keysym {
xkb_keysym_t keysym;
uint32_t offset;
-};\n''')
+};\n"""
+)
+
def print_entries(x):
- for (name, value) in x:
- print(' {{ 0x{value:08x}, {offs} }}, /* {name} */'.format(offs=entry_offsets[name], value=value, name=name))
+ for name, value in x:
+ print(
+ " {{ 0x{value:08x}, {offs} }}, /* {name} */".format(
+ offs=entry_offsets[name], value=value, name=name
+ )
+ )
+
-print('static const struct name_keysym name_to_keysym[] = {')
+print("static const struct name_keysym name_to_keysym[] = {")
print_entries(entries_isorted)
-print('};\n')
+print("};\n")
# *.sort() is stable so we always get the first keysym for duplicate
-print('static const struct name_keysym keysym_to_name[] = {')
-print_entries(next(g[1]) for g in itertools.groupby(entries_kssorted, key=lambda e: e[1]))
-print('};')
+print("static const struct name_keysym keysym_to_name[] = {")
+print_entries(
+ next(g[1]) for g in itertools.groupby(entries_kssorted, key=lambda e: e[1])
+)
+print("};")
diff --git a/scripts/map-to-def b/scripts/map-to-def
index 63b566e..788e336 100755
--- a/scripts/map-to-def
+++ b/scripts/map-to-def
@@ -9,7 +9,7 @@ import pathlib
def symbols_from_map(path):
- return re.findall(r'^\s+(r?xkb_.*);', path.read_text('utf-8'), re.MULTILINE)
+ return re.findall(r"^\s+(r?xkb_.*);", path.read_text("utf-8"), re.MULTILINE)
if 2 > len(sys.argv) > 3:
diff --git a/scripts/perfect_hash.py b/scripts/perfect_hash.py
index 95c6156..a017bb6 100644
--- a/scripts/perfect_hash.py
+++ b/scripts/perfect_hash.py
@@ -92,7 +92,7 @@ else:
from io import StringIO
-__version__ = '0.4.2'
+__version__ = "0.4.2"
verbose = False
@@ -107,8 +107,9 @@ class Graph(object):
are assigned such that the two values corresponding to an edge add up to
the desired edge value (mod N).
"""
+
def __init__(self, N):
- self.N = N # number of vertices
+ self.N = N # number of vertices
# maps a vertex number to the list of tuples (vertex, edge value)
# to which it is connected by edges.
@@ -145,7 +146,7 @@ class Graph(object):
continue
# explore tree starting at 'root'
- self.vertex_values[root] = 0 # set arbitrarily to zero
+ self.vertex_values[root] = 0 # set arbitrarily to zero
# Stack of vertices to visit, a list of tuples (parent, vertex)
tovisit = [(None, root)]
@@ -170,7 +171,8 @@ class Graph(object):
# Set new vertex's value to the desired edge value,
# minus the value of the vertex we came here from.
self.vertex_values[neighbor] = (
- edge_value - self.vertex_values[vertex]) % self.N
+ edge_value - self.vertex_values[vertex]
+ ) % self.N
# check if all vertices have a valid value
for vertex in range(self.N):
@@ -188,20 +190,20 @@ class StrSaltHash(object):
a random string of characters, summed up, and finally modulo NG is
taken.
"""
+
chars = string.ascii_letters + string.digits
def __init__(self, N):
self.N = N
- self.salt = ''
+ self.salt = ""
def __call__(self, key):
# XXX: xkbcommon modification: make the salt length a power of 2
# so that the % operation in the hash is fast.
- while len(self.salt) < max(len(key), 32): # add more salt as necessary
+ while len(self.salt) < max(len(key), 32): # add more salt as necessary
self.salt += random.choice(self.chars)
- return sum(ord(self.salt[i]) * ord(c)
- for i, c in enumerate(key)) % self.N
+ return sum(ord(self.salt[i]) * ord(c) for i, c in enumerate(key)) % self.N
template = """
def hash_f(key, T):
@@ -212,22 +214,23 @@ def perfect_hash(key):
G[hash_f(key, "$S2")]) % $NG
"""
+
class IntSaltHash(object):
"""
Random hash function generator.
Simple byte level hashing, each byte is multiplied in sequence to a table
containing random numbers, summed tp, and finally modulo NG is taken.
"""
+
def __init__(self, N):
self.N = N
self.salt = []
def __call__(self, key):
- while len(self.salt) < len(key): # add more salt as necessary
+ while len(self.salt) < len(key): # add more salt as necessary
self.salt.append(random.randint(1, self.N - 1))
- return sum(self.salt[i] * ord(c)
- for i, c in enumerate(key)) % self.N
+ return sum(self.salt[i] * ord(c) for i, c in enumerate(key)) % self.N
template = """
S1 = [$S1]
@@ -241,14 +244,18 @@ def perfect_hash(key):
return (G[hash_f(key, S1)] + G[hash_f(key, S2)]) % $NG
"""
+
def builtin_template(Hash):
- return """\
+ return (
+ """\
# =======================================================================
# ================= Python code for perfect hash function ===============
# =======================================================================
G = [$G]
-""" + Hash.template + """
+"""
+ + Hash.template
+ + """
# ============================ Sanity check =============================
K = [$K]
@@ -257,6 +264,7 @@ assert len(K) == $NK
for h, k in enumerate(K):
assert perfect_hash(k) == h
"""
+ )
class TooManyInterationsError(Exception):
@@ -279,35 +287,38 @@ def generate_hash(keys, Hash=StrSaltHash):
if not isinstance(key, str):
raise TypeError("key a not string: %r" % key)
if NK > 10000 and Hash == StrSaltHash:
- print("""\
+ print(
+ """\
WARNING: You have %d keys.
Using --hft=1 is likely to fail for so many keys.
Please use --hft=2 instead.
-""" % NK)
+"""
+ % NK
+ )
# the number of vertices in the graph G
NG = NK + 1
if verbose:
- print('NG = %d' % NG)
+ print("NG = %d" % NG)
trial = 0 # Number of trial graphs so far
while True:
- if (trial % trials) == 0: # trials failures, increase NG slightly
+ if (trial % trials) == 0: # trials failures, increase NG slightly
if trial > 0:
NG = max(NG + 1, int(1.05 * NG))
if verbose:
- sys.stdout.write('\nGenerating graphs NG = %d ' % NG)
+ sys.stdout.write("\nGenerating graphs NG = %d " % NG)
trial += 1
if NG > 100 * (NK + 1):
raise TooManyInterationsError("%d keys" % NK)
if verbose:
- sys.stdout.write('.')
+ sys.stdout.write(".")
sys.stdout.flush()
- G = Graph(NG) # Create graph with NG vertices
- f1 = Hash(NG) # Create 2 random hash functions
+ G = Graph(NG) # Create graph with NG vertices
+ f1 = Hash(NG) # Create 2 random hash functions
f2 = Hash(NG)
# Connect vertices given by the values of the two hash functions
@@ -322,33 +333,30 @@ WARNING: You have %d keys.
break
if verbose:
- print('\nAcyclic graph found after %d trials.' % trial)
- print('NG = %d' % NG)
+ print("\nAcyclic graph found after %d trials." % trial)
+ print("NG = %d" % NG)
# Sanity check the result by actually verifying that all the keys
# hash to the right value.
for hashval, key in enumerate(keys):
- assert hashval == (
- G.vertex_values[f1(key)] + G.vertex_values[f2(key)]
- ) % NG
+ assert hashval == (G.vertex_values[f1(key)] + G.vertex_values[f2(key)]) % NG
if verbose:
- print('OK')
+ print("OK")
return f1, f2, G.vertex_values
class Format(object):
-
- def __init__(self, width=76, indent=4, delimiter=', '):
+ def __init__(self, width=76, indent=4, delimiter=", "):
self.width = width
self.indent = indent
self.delimiter = delimiter
def print_format(self):
print("Format options:")
- for name in 'width', 'indent', 'delimiter':
- print(' %s: %r' % (name, getattr(self, name)))
+ for name in "width", "indent", "delimiter":
+ print(" %s: %r" % (name, getattr(self, name)))
def __call__(self, data, quote=False):
if not isinstance(data, (list, tuple)):
@@ -360,10 +368,10 @@ class Format(object):
for i, elt in enumerate(data):
last = bool(i == len(data) - 1)
- s = ('"%s"' if quote else '%s') % elt
+ s = ('"%s"' if quote else "%s") % elt
if pos + len(s) + lendel > self.width:
- aux.write('\n' + (self.indent * ' '))
+ aux.write("\n" + (self.indent * " "))
pos = self.indent
aux.write(s)
@@ -372,7 +380,7 @@ class Format(object):
aux.write(self.delimiter)
pos += lendel
- return '\n'.join(l.rstrip() for l in aux.getvalue().split('\n'))
+ return "\n".join(l.rstrip() for l in aux.getvalue().split("\n"))
def generate_code(keys, Hash=StrSaltHash, template=None, options=None):
@@ -397,20 +405,22 @@ def generate_code(keys, Hash=StrSaltHash, template=None, options=None):
if options is None:
fmt = Format()
else:
- fmt = Format(width=options.width, indent=options.indent,
- delimiter=options.delimiter)
+ fmt = Format(
+ width=options.width, indent=options.indent, delimiter=options.delimiter
+ )
if verbose:
fmt.print_format()
return string.Template(template).substitute(
- NS = salt_len,
- S1 = fmt(f1.salt),
- S2 = fmt(f2.salt),
- NG = len(G),
- G = fmt(G),
- NK = len(keys),
- K = fmt(list(keys), quote=True))
+ NS=salt_len,
+ S1=fmt(f1.salt),
+ S2=fmt(f2.salt),
+ NG=len(G),
+ G=fmt(G),
+ NK=len(keys),
+ K=fmt(list(keys), quote=True),
+ )
def read_table(filename, options):
@@ -430,15 +440,15 @@ def read_table(filename, options):
if verbose:
print("Reader options:")
- for name in 'comment', 'splitby', 'keycol':
- print(' %s: %r' % (name, getattr(options, name)))
+ for name in "comment", "splitby", "keycol":
+ print(" %s: %r" % (name, getattr(options, name)))
for n, line in enumerate(fi):
line = line.strip()
if not line or line.startswith(options.comment):
continue
- if line.count(options.comment): # strip content after comment
+ if line.count(options.comment): # strip content after comment
line = line.split(options.comment)[0].strip()
row = [col.strip() for col in line.split(options.splitby)]
@@ -446,8 +456,9 @@ def read_table(filename, options):
try:
key = row[options.keycol - 1]
except IndexError:
- sys.exit("%s:%d: Error: Cannot read key, not enough columns." %
- (filename, n + 1))
+ sys.exit(
+ "%s:%d: Error: Cannot read key, not enough columns." % (filename, n + 1)
+ )
keys.append(key)
@@ -463,7 +474,7 @@ def read_template(filename):
if verbose:
print("Reading template from file `%s'" % filename)
try:
- with open(filename, 'r') as fi:
+ with open(filename, "r") as fi:
return fi.read()
except IOError:
sys.exit("Error: Could not open `%s' for reading." % filename)
@@ -471,8 +482,8 @@ def read_template(filename):
def run_code(code):
tmpdir = tempfile.mkdtemp()
- path = join(tmpdir, 't.py')
- with open(path, 'w') as fo:
+ path = join(tmpdir, "t.py")
+ with open(path, "w") as fo:
fo.write(code)
try:
subprocess.check_call([sys.executable, path])
@@ -494,102 +505,123 @@ If no template file is provided, a small built-in Python template
is processed and the output code is written to stdout.
"""
- parser = OptionParser(usage = usage,
- description = description,
- prog = sys.argv[0],
- version = "%prog: " + __version__)
-
- parser.add_option("--delimiter",
- action = "store",
- default = ", ",
- help = "Delimiter for list items used in output, "
- "the default delimiter is '%default'",
- metavar = "STR")
-
- parser.add_option("--indent",
- action = "store",
- default = 4,
- type = "int",
- help = "Make INT spaces at the beginning of a "
- "new line when generated list is wrapped. "
- "Default is %default",
- metavar = "INT")
-
- parser.add_option("--width",
- action = "store",
- default = 76,
- type = "int",
- help = "Maximal width of generated list when "
- "wrapped. Default width is %default",
- metavar = "INT")
-
- parser.add_option("--comment",
- action = "store",
- default = "#",
- help = "STR is the character, or sequence of "
- "characters, which marks the beginning "
- "of a comment (which runs till "
- "the end of the line), in the input "
- "KEYS_FILE. "
- "Default is '%default'",
- metavar = "STR")
-
- parser.add_option("--splitby",
- action = "store",
- default = ",",
- help = "STR is the character by which the columns "
- "in the input KEYS_FILE are split. "
- "Default is '%default'",
- metavar = "STR")
-
- parser.add_option("--keycol",
- action = "store",
- default = 1,
- type = "int",
- help = "Specifies the column INT in the input "
- "KEYS_FILE which contains the keys. "
- "Default is %default, i.e. the first column.",
- metavar = "INT")
-
- parser.add_option("--trials",
- action = "store",
- default = 5,
- type = "int",
- help = "Specifies the number of trials before "
- "NG is increased. A small INT will give "
- "compute faster, but the array G will be "
- "large. A large INT will take longer to "
- "compute but G will be smaller. "
- "Default is %default",
- metavar = "INT")
-
- parser.add_option("--hft",
- action = "store",
- default = 1,
- type = "int",
- help = "Hash function type INT. Possible values "
- "are 1 (StrSaltHash) and 2 (IntSaltHash). "
- "The default is %default",
- metavar = "INT")
-
- parser.add_option("-e", "--execute",
- action = "store_true",
- help = "Execute the generated code within "
- "the Python interpreter.")
-
- parser.add_option("-o", "--output",
- action = "store",
- help = "Specify output FILE explicitly. "
- "`-o std' means standard output. "
- "`-o no' means no output. "
- "By default, the file name is obtained "
- "from the name of the template file by "
- "substituting `tmpl' to `code'.",
- metavar = "FILE")
-
- parser.add_option("-v", "--verbose",
- action = "store_true",
- help = "verbosity")
+ parser = OptionParser(
+ usage=usage,
+ description=description,
+ prog=sys.argv[0],
+ version="%prog: " + __version__,
+ )
+
+ parser.add_option(
+ "--delimiter",
+ action="store",
+ default=", ",
+ help="Delimiter for list items used in output, "
+ "the default delimiter is '%default'",
+ metavar="STR",
+ )
+
+ parser.add_option(
+ "--indent",
+ action="store",
+ default=4,
+ type="int",
+ help="Make INT spaces at the beginning of a "
+ "new line when generated list is wrapped. "
+ "Default is %default",
+ metavar="INT",
+ )
+
+ parser.add_option(
+ "--width",
+ action="store",
+ default=76,
+ type="int",
+ help="Maximal width of generated list when "
+ "wrapped. Default width is %default",
+ metavar="INT",
+ )
+
+ parser.add_option(
+ "--comment",
+ action="store",
+ default="#",
+ help="STR is the character, or sequence of "
+ "characters, which marks the beginning "
+ "of a comment (which runs till "
+ "the end of the line), in the input "
+ "KEYS_FILE. "
+ "Default is '%default'",
+ metavar="STR",
+ )
+
+ parser.add_option(
+ "--splitby",
+ action="store",
+ default=",",
+ help="STR is the character by which the columns "
+ "in the input KEYS_FILE are split. "
+ "Default is '%default'",
+ metavar="STR",
+ )
+
+ parser.add_option(
+ "--keycol",
+ action="store",
+ default=1,
+ type="int",
+ help="Specifies the column INT in the input "
+ "KEYS_FILE which contains the keys. "
+ "Default is %default, i.e. the first column.",
+ metavar="INT",
+ )
+
+ parser.add_option(
+ "--trials",
+ action="store",
+ default=5,
+ type="int",
+ help="Specifies the number of trials before "
+ "NG is increased. A small INT will give "
+ "compute faster, but the array G will be "
+ "large. A large INT will take longer to "
+ "compute but G will be smaller. "
+ "Default is %default",
+ metavar="INT",
+ )
+
+ parser.add_option(
+ "--hft",
+ action="store",
+ default=1,
+ type="int",
+ help="Hash function type INT. Possible values "
+ "are 1 (StrSaltHash) and 2 (IntSaltHash). "
+ "The default is %default",
+ metavar="INT",
+ )
+
+ parser.add_option(
+ "-e",
+ "--execute",
+ action="store_true",
+ help="Execute the generated code within " "the Python interpreter.",
+ )
+
+ parser.add_option(
+ "-o",
+ "--output",
+ action="store",
+ help="Specify output FILE explicitly. "
+ "`-o std' means standard output. "
+ "`-o no' means no output. "
+ "By default, the file name is obtained "
+ "from the name of the template file by "
+ "substituting `tmpl' to `code'.",
+ metavar="FILE",
+ )
+
+ parser.add_option("-v", "--verbose", action="store_true", help="verbosity")
options, args = parser.parse_args()
@@ -605,7 +637,7 @@ is processed and the output code is written to stdout.
if len(args) not in (1, 2):
parser.error("incorrect number of arguments")
- if len(args) == 2 and not args[1].count('tmpl'):
+ if len(args) == 2 and not args[1].count("tmpl"):
parser.error("template filename does not contain 'tmpl'")
if options.hft == 1:
@@ -638,22 +670,22 @@ is processed and the output code is written to stdout.
outname = options.output
else:
if tmpl_file:
- if 'tmpl' not in tmpl_file:
+ if "tmpl" not in tmpl_file:
sys.exit("Hmm, template filename does not contain 'tmpl'")
- outname = tmpl_file.replace('tmpl', 'code')
+ outname = tmpl_file.replace("tmpl", "code")
else:
- outname = 'std'
+ outname = "std"
if verbose:
print("outname = %r\n" % outname)
- if outname == 'std':
+ if outname == "std":
outstream = sys.stdout
- elif outname == 'no':
+ elif outname == "no":
outstream = None
else:
try:
- outstream = open(outname, 'w')
+ outstream = open(outname, "w")
except IOError:
sys.exit("Error: Could not open `%s' for writing." % outname)
@@ -661,14 +693,14 @@ is processed and the output code is written to stdout.
if options.execute or template == builtin_template(Hash):
if verbose:
- print('Executing code...\n')
+ print("Executing code...\n")
run_code(code)
if outstream:
outstream.write(code)
- if not outname == 'std':
+ if not outname == "std":
outstream.close()
-if __name__ == '__main__':
+if __name__ == "__main__":
main()
diff --git a/test/symbols-leak-test.py b/test/symbols-leak-test.py
index a395079..4fee747 100755
--- a/test/symbols-leak-test.py
+++ b/test/symbols-leak-test.py
@@ -10,15 +10,15 @@ import re
import sys
-top_srcdir = pathlib.Path(os.environ['top_srcdir'])
+top_srcdir = pathlib.Path(os.environ["top_srcdir"])
def symbols_from_map(path):
- return re.findall(r'^\s+(xkb_.*);', path.read_text('utf-8'), re.MULTILINE)
+ return re.findall(r"^\s+(xkb_.*);", path.read_text("utf-8"), re.MULTILINE)
def symbols_from_src(path):
- return re.findall(r'XKB_EXPORT.*\n(xkb_.*)\(', path.read_text('utf-8'))
+ return re.findall(r"XKB_EXPORT.*\n(xkb_.*)\(", path.read_text("utf-8"))
def diff(map_path, src_paths):
@@ -31,32 +31,32 @@ exit = 0
# xkbcommon symbols
left, right = diff(
- top_srcdir/'xkbcommon.map',
+ top_srcdir / "xkbcommon.map",
[
- *(top_srcdir/'src').glob('*.c'),
- *(top_srcdir/'src'/'xkbcomp').glob('*.c'),
- *(top_srcdir/'src'/'compose').glob('*.c'),
+ *(top_srcdir / "src").glob("*.c"),
+ *(top_srcdir / "src" / "xkbcomp").glob("*.c"),
+ *(top_srcdir / "src" / "compose").glob("*.c"),
],
)
if left:
- print('xkbcommon map has extra symbols:', ' '.join(left))
+ print("xkbcommon map has extra symbols:", " ".join(left))
exit = 1
if right:
- print('xkbcommon src has extra symbols:', ' '.join(right))
+ print("xkbcommon src has extra symbols:", " ".join(right))
exit = 1
# xkbcommon-x11 symbols
left, right = diff(
- top_srcdir/'xkbcommon-x11.map',
+ top_srcdir / "xkbcommon-x11.map",
[
- *(top_srcdir/'src'/'x11').glob('*.c'),
+ *(top_srcdir / "src" / "x11").glob("*.c"),
],
)
if left:
- print('xkbcommon-x11 map has extra symbols:', ' '.join(left))
+ print("xkbcommon-x11 map has extra symbols:", " ".join(left))
exit = 1
if right:
- print('xkbcommon-x11 src has extra symbols:', ' '.join(right))
+ print("xkbcommon-x11 src has extra symbols:", " ".join(right))
exit = 1
sys.exit(exit)
diff --git a/test/test-keysym.py b/test/test-keysym.py
index d237f31..18bdc5f 100755
--- a/test/test-keysym.py
+++ b/test/test-keysym.py
@@ -21,51 +21,57 @@ xkb_symbols "basic" {{
"""
parser = argparse.ArgumentParser(
- description='Tool to verify whether a keysym is resolved'
+ description="Tool to verify whether a keysym is resolved"
+)
+parser.add_argument("keysym", type=str, help="XKB keysym")
+parser.add_argument(
+ "--tool",
+ type=str,
+ nargs=1,
+ default=["xkbcli", "compile-keymap"],
+ help="Full path to the xkbcli-compile-keymap tool",
)
-parser.add_argument('keysym', type=str, help='XKB keysym')
-parser.add_argument('--tool', type=str, nargs=1,
- default=['xkbcli', 'compile-keymap'],
- help='Full path to the xkbcli-compile-keymap tool')
args = parser.parse_args()
with tempfile.TemporaryDirectory() as tmpdir:
symfile = Path(tmpdir) / "symbols" / "keytest"
symfile.parent.mkdir()
- with symfile.open(mode='w') as f:
+ with symfile.open(mode="w") as f:
f.write(template.format(args.keysym))
try:
cmd = [
*args.tool,
- '--layout', 'keytest',
+ "--layout",
+ "keytest",
]
env = os.environ.copy()
- env['XKB_CONFIG_EXTRA_PATH'] = tmpdir
+ env["XKB_CONFIG_EXTRA_PATH"] = tmpdir
- result = subprocess.run(cmd, env=env, capture_output=True,
- universal_newlines=True)
+ result = subprocess.run(
+ cmd, env=env, capture_output=True, universal_newlines=True
+ )
if result.returncode != 0:
- print('ERROR: Failed to compile:')
+ print("ERROR: Failed to compile:")
print(result.stderr)
sys.exit(1)
# grep for TLDE actually being remapped
- for l in result.stdout.split('\n'):
- match = re.match(r'\s+key \<TLDE\>\s+{\s+\[\s+(?P<keysym>\w+)\s+\]\s+}', l)
+ for l in result.stdout.split("\n"):
+ match = re.match(r"\s+key \<TLDE\>\s+{\s+\[\s+(?P<keysym>\w+)\s+\]\s+}", l)
if match:
- if args.keysym == match.group('keysym'):
+ if args.keysym == match.group("keysym"):
sys.exit(0)
- elif match.group('keysym') == 'NoSymbol':
- print('ERROR: key {} not resolved:'.format(args.keysym), l)
+ elif match.group("keysym") == "NoSymbol":
+ print("ERROR: key {} not resolved:".format(args.keysym), l)
else:
- print('ERROR: key {} mapped to wrong key:'.format(args.keysym), l)
+ print("ERROR: key {} mapped to wrong key:".format(args.keysym), l)
sys.exit(1)
print(result.stdout)
- print('ERROR: above keymap is missing key mapping for {}'.format(args.keysym))
+ print("ERROR: above keymap is missing key mapping for {}".format(args.keysym))
sys.exit(1)
except FileNotFoundError as err:
- print('ERROR: invalid or missing tool: {}'.format(err))
+ print("ERROR: invalid or missing tool: {}".format(err))
sys.exit(1)
diff --git a/test/tool-option-parsing.py b/test/tool-option-parsing.py
index 6692d58..4d07661 100755
--- a/test/tool-option-parsing.py
+++ b/test/tool-option-parsing.py
@@ -32,28 +32,37 @@ import unittest
try:
- top_builddir = os.environ['top_builddir']
- top_srcdir = os.environ['top_srcdir']
+ top_builddir = os.environ["top_builddir"]
+ top_srcdir = os.environ["top_srcdir"]
except KeyError:
- print('Required environment variables not found: top_srcdir/top_builddir', file=sys.stderr)
+ print(
+ "Required environment variables not found: top_srcdir/top_builddir",
+ file=sys.stderr,
+ )
from pathlib import Path
- top_srcdir = '.'
+
+ top_srcdir = "."
try:
- top_builddir = next(Path('.').glob('**/meson-logs/')).parent
+ top_builddir = next(Path(".").glob("**/meson-logs/")).parent
except StopIteration:
sys.exit(1)
- print('Using srcdir "{}", builddir "{}"'.format(top_srcdir, top_builddir), file=sys.stderr)
+ print(
+ 'Using srcdir "{}", builddir "{}"'.format(top_srcdir, top_builddir),
+ file=sys.stderr,
+ )
logging.basicConfig(level=logging.DEBUG)
-logger = logging.getLogger('test')
+logger = logging.getLogger("test")
logger.setLevel(logging.DEBUG)
# Permutation of RMLVO that we use in multiple tests
-rmlvos = [list(x) for x in itertools.permutations(
- ['--rules=evdev', '--model=pc104',
- '--layout=ch', '--options=eurosign:5']
-)]
+rmlvos = [
+ list(x)
+ for x in itertools.permutations(
+ ["--rules=evdev", "--model=pc104", "--layout=ch", "--options=eurosign:5"]
+ )
+]
def _disable_coredump():
@@ -61,19 +70,23 @@ def _disable_coredump():
def run_command(args):
- logger.debug('run command: {}'.format(' '.join(args)))
+ logger.debug("run command: {}".format(" ".join(args)))
try:
- p = subprocess.run(args, preexec_fn=_disable_coredump,
- capture_output=True, text=True,
- timeout=0.7)
+ p = subprocess.run(
+ args,
+ preexec_fn=_disable_coredump,
+ capture_output=True,
+ text=True,
+ timeout=0.7,
+ )
return p.returncode, p.stdout, p.stderr
except subprocess.TimeoutExpired as e:
return 0, e.stdout, e.stderr
class XkbcliTool:
- xkbcli_tool = 'xkbcli'
+ xkbcli_tool = "xkbcli"
subtool = None
def __init__(self, subtool=None, skipIf=(), skipError=()):
@@ -87,7 +100,7 @@ class XkbcliTool:
if condition:
raise unittest.SkipTest(reason)
if self.subtool is not None:
- tool = '{}-{}'.format(self.xkbcli_tool, self.subtool)
+ tool = "{}-{}".format(self.xkbcli_tool, self.subtool)
else:
tool = self.xkbcli_tool
args = [os.path.join(self.tool_path, tool)] + args
@@ -111,14 +124,14 @@ class XkbcliTool:
def run_command_unrecognized_option(self, args):
rc, stdout, stderr = self.run_command(args)
assert rc == 2, (rc, stdout, stderr)
- assert stdout.startswith('Usage') or stdout == ''
- assert 'unrecognized option' in stderr
+ assert stdout.startswith("Usage") or stdout == ""
+ assert "unrecognized option" in stderr
def run_command_missing_arg(self, args):
rc, stdout, stderr = self.run_command(args)
assert rc == 2, (rc, stdout, stderr)
- assert stdout.startswith('Usage') or stdout == ''
- assert 'requires an argument' in stderr
+ assert stdout.startswith("Usage") or stdout == ""
+ assert "requires an argument" in stderr
def __str__(self):
return str(self.subtool)
@@ -128,28 +141,57 @@ class TestXkbcli(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.xkbcli = XkbcliTool()
- cls.xkbcli_list = XkbcliTool('list', skipIf=(
- (not int(os.getenv('HAVE_XKBCLI_LIST', '1')), 'xkbregistory not enabled'),
- ))
- cls.xkbcli_how_to_type = XkbcliTool('how-to-type')
- cls.xkbcli_compile_keymap = XkbcliTool('compile-keymap')
- cls.xkbcli_interactive_evdev = XkbcliTool('interactive-evdev', skipIf=(
- (not int(os.getenv('HAVE_XKBCLI_INTERACTIVE_EVDEV', '1')), 'evdev not enabled'),
- (not os.path.exists('/dev/input/event0'), 'event node required'),
- (not os.access('/dev/input/event0', os.R_OK), 'insufficient permissions'),
- ), skipError=(
- (lambda rc, stdout, stderr: 'Couldn\'t find any keyboards' in stderr,
- 'No keyboards available'),
- ),
+ cls.xkbcli_list = XkbcliTool(
+ "list",
+ skipIf=(
+ (
+ not int(os.getenv("HAVE_XKBCLI_LIST", "1")),
+ "xkbregistory not enabled",
+ ),
+ ),
+ )
+ cls.xkbcli_how_to_type = XkbcliTool("how-to-type")
+ cls.xkbcli_compile_keymap = XkbcliTool("compile-keymap")
+ cls.xkbcli_interactive_evdev = XkbcliTool(
+ "interactive-evdev",
+ skipIf=(
+ (
+ not int(os.getenv("HAVE_XKBCLI_INTERACTIVE_EVDEV", "1")),
+ "evdev not enabled",
+ ),
+ (not os.path.exists("/dev/input/event0"), "event node required"),
+ (
+ not os.access("/dev/input/event0", os.R_OK),
+ "insufficient permissions",
+ ),
+ ),
+ skipError=(
+ (
+ lambda rc, stdout, stderr: "Couldn't find any keyboards" in stderr,
+ "No keyboards available",
+ ),
+ ),
+ )
+ cls.xkbcli_interactive_x11 = XkbcliTool(
+ "interactive-x11",
+ skipIf=(
+ (
+ not int(os.getenv("HAVE_XKBCLI_INTERACTIVE_X11", "1")),
+ "x11 not enabled",
+ ),
+ (not os.getenv("DISPLAY"), "DISPLAY not set"),
+ ),
+ )
+ cls.xkbcli_interactive_wayland = XkbcliTool(
+ "interactive-wayland",
+ skipIf=(
+ (
+ not int(os.getenv("HAVE_XKBCLI_INTERACTIVE_WAYLAND", "1")),
+ "wayland not enabled",
+ ),
+ (not os.getenv("WAYLAND_DISPLAY"), "WAYLAND_DISPLAY not set"),
+ ),
)
- cls.xkbcli_interactive_x11 = XkbcliTool('interactive-x11', skipIf=(
- (not int(os.getenv('HAVE_XKBCLI_INTERACTIVE_X11', '1')), 'x11 not enabled'),
- (not os.getenv('DISPLAY'), 'DISPLAY not set'),
- ))
- cls.xkbcli_interactive_wayland = XkbcliTool('interactive-wayland', skipIf=(
- (not int(os.getenv('HAVE_XKBCLI_INTERACTIVE_WAYLAND', '1')), 'wayland not enabled'),
- (not os.getenv('WAYLAND_DISPLAY'), 'WAYLAND_DISPLAY not set'),
- ))
cls.all_tools = [
cls.xkbcli,
cls.xkbcli_list,
@@ -164,31 +206,31 @@ class TestXkbcli(unittest.TestCase):
# --help is supported by all tools
for tool in self.all_tools:
with self.subTest(tool=tool):
- stdout, stderr = tool.run_command_success(['--help'])
- assert stdout.startswith('Usage:')
- assert stderr == ''
+ stdout, stderr = tool.run_command_success(["--help"])
+ assert stdout.startswith("Usage:")
+ assert stderr == ""
def test_invalid_option(self):
# --foobar generates "Usage:" for all tools
for tool in self.all_tools:
with self.subTest(tool=tool):
- tool.run_command_unrecognized_option(['--foobar'])
+ tool.run_command_unrecognized_option(["--foobar"])
def test_xkbcli_version(self):
# xkbcli --version
- stdout, stderr = self.xkbcli.run_command_success(['--version'])
- assert stdout.startswith('1')
- assert stderr == ''
+ stdout, stderr = self.xkbcli.run_command_success(["--version"])
+ assert stdout.startswith("1")
+ assert stderr == ""
def test_xkbcli_too_many_args(self):
- self.xkbcli.run_command_invalid(['a'] * 64)
+ self.xkbcli.run_command_invalid(["a"] * 64)
def test_compile_keymap_args(self):
for args in (
- ['--verbose'],
- ['--rmlvo'],
+ ["--verbose"],
+ ["--rmlvo"],
# ['--kccgst'],
- ['--verbose', '--rmlvo'],
+ ["--verbose", "--rmlvo"],
# ['--verbose', '--kccgst'],
):
with self.subTest(args=args):
@@ -201,8 +243,8 @@ class TestXkbcli(unittest.TestCase):
def test_compile_keymap_include(self):
for args in (
- ['--include', '.', '--include-defaults'],
- ['--include', '/tmp', '--include-defaults'],
+ ["--include", ".", "--include-defaults"],
+ ["--include", "/tmp", "--include-defaults"],
):
with self.subTest(args=args):
# Succeeds thanks to include-defaults
@@ -210,59 +252,59 @@ class TestXkbcli(unittest.TestCase):
def test_compile_keymap_include_invalid(self):
# A non-directory is rejected by default
- args = ['--include', '/proc/version']
+ args = ["--include", "/proc/version"]
rc, stdout, stderr = self.xkbcli_compile_keymap.run_command(args)
assert rc == 1, (stdout, stderr)
assert "There are no include paths to search" in stderr
# A non-existing directory is rejected by default
- args = ['--include', '/tmp/does/not/exist']
+ args = ["--include", "/tmp/does/not/exist"]
rc, stdout, stderr = self.xkbcli_compile_keymap.run_command(args)
assert rc == 1, (stdout, stderr)
assert "There are no include paths to search" in stderr
# Valid dir, but missing files
- args = ['--include', '/tmp']
+ args = ["--include", "/tmp"]
rc, stdout, stderr = self.xkbcli_compile_keymap.run_command(args)
assert rc == 1, (stdout, stderr)
assert "Couldn't look up rules" in stderr
def test_how_to_type(self):
# Unicode codepoint conversions, we support whatever strtol does
- for args in (['123'], ['0x123'], ['0123']):
+ for args in (["123"], ["0x123"], ["0123"]):
with self.subTest(args=args):
self.xkbcli_how_to_type.run_command_success(args)
def test_how_to_type_rmlvo(self):
for rmlvo in rmlvos:
with self.subTest(rmlvo=rmlvo):
- args = rmlvo + ['0x1234']
+ args = rmlvo + ["0x1234"]
self.xkbcli_how_to_type.run_command_success(args)
def test_list_rmlvo(self):
for args in (
- ['--verbose'],
- ['-v'],
- ['--verbose', '--load-exotic'],
- ['--load-exotic'],
- ['--ruleset=evdev'],
- ['--ruleset=base'],
+ ["--verbose"],
+ ["-v"],
+ ["--verbose", "--load-exotic"],
+ ["--load-exotic"],
+ ["--ruleset=evdev"],
+ ["--ruleset=base"],
):
with self.subTest(args=args):
self.xkbcli_list.run_command_success(args)
def test_list_rmlvo_includes(self):
- args = ['/tmp/']
+ args = ["/tmp/"]
self.xkbcli_list.run_command_success(args)
def test_list_rmlvo_includes_invalid(self):
- args = ['/proc/version']
+ args = ["/proc/version"]
rc, stdout, stderr = self.xkbcli_list.run_command(args)
assert rc == 1
assert "Failed to append include path" in stderr
def test_list_rmlvo_includes_no_defaults(self):
- args = ['--skip-default-paths', '/tmp']
+ args = ["--skip-default-paths", "/tmp"]
rc, stdout, stderr = self.xkbcli_list.run_command(args)
assert rc == 1
assert "Failed to parse XKB description" in stderr
@@ -276,11 +318,11 @@ class TestXkbcli(unittest.TestCase):
# Note: --enable-compose fails if $prefix doesn't have the compose tables
# installed
for args in (
- ['--report-state-changes'],
- ['--enable-compose'],
- ['--consumed-mode=xkb'],
- ['--consumed-mode=gtk'],
- ['--without-x11-offset'],
+ ["--report-state-changes"],
+ ["--enable-compose"],
+ ["--consumed-mode=xkb"],
+ ["--consumed-mode=gtk"],
+ ["--without-x11-offset"],
):
with self.subTest(args=args):
self.xkbcli_interactive_evdev.run_command_success(args)
@@ -294,21 +336,21 @@ class TestXkbcli(unittest.TestCase):
pass
-if __name__ == '__main__':
+if __name__ == "__main__":
with tempfile.TemporaryDirectory() as tmpdir:
# Use our own test xkeyboard-config copy.
- os.environ['XKB_CONFIG_ROOT'] = top_srcdir + '/test/data'
+ os.environ["XKB_CONFIG_ROOT"] = top_srcdir + "/test/data"
# Use our own X11 locale copy.
- os.environ['XLOCALEDIR'] = top_srcdir + '/test/data/locale'
+ os.environ["XLOCALEDIR"] = top_srcdir + "/test/data/locale"
# Use our own locale.
- os.environ['LC_CTYPE'] = 'en_US.UTF-8'
+ os.environ["LC_CTYPE"] = "en_US.UTF-8"
# libxkbcommon has fallbacks when XDG_CONFIG_HOME isn't set so we need
# to override it with a known (empty) directory. Otherwise our test
# behavior depends on the system the test is run on.
- os.environ['XDG_CONFIG_HOME'] = tmpdir
+ os.environ["XDG_CONFIG_HOME"] = tmpdir
# Prevent the legacy $HOME/.xkb from kicking in.
- del os.environ['HOME']
+ del os.environ["HOME"]
# This needs to be separated if we do specific extra path testing
- os.environ['XKB_CONFIG_EXTRA_PATH'] = tmpdir
+ os.environ["XKB_CONFIG_EXTRA_PATH"] = tmpdir
unittest.main()
diff --git a/test/xkeyboard-config-test.py.in b/test/xkeyboard-config-test.py.in
index 77cff1f..c33e707 100755
--- a/test/xkeyboard-config-test.py.in
+++ b/test/xkeyboard-config-test.py.in
@@ -10,11 +10,11 @@ from pathlib import Path
verbose = False
-DEFAULT_RULES_XML = '@XKB_CONFIG_ROOT@/rules/evdev.xml'
+DEFAULT_RULES_XML = "@XKB_CONFIG_ROOT@/rules/evdev.xml"
# Meson needs to fill this in so we can call the tool in the buildir.
-EXTRA_PATH = '@MESON_BUILD_ROOT@'
-os.environ['PATH'] = ':'.join([EXTRA_PATH, os.getenv('PATH')])
+EXTRA_PATH = "@MESON_BUILD_ROOT@"
+os.environ["PATH"] = ":".join([EXTRA_PATH, os.getenv("PATH")])
def escape(s):
@@ -30,6 +30,7 @@ def create_progress_bar(verbose):
if not verbose and os.isatty(sys.stdout.fileno()):
try:
from tqdm import tqdm
+
progress_bar = tqdm
except ImportError:
pass
@@ -56,13 +57,13 @@ class Invocation:
def __str__(self):
s = []
rmlvo = [x or "" for x in self.rmlvo]
- rmlvo = ', '.join([f'"{x}"' for x in rmlvo])
- s.append(f'- rmlvo: [{rmlvo}]')
+ rmlvo = ", ".join([f'"{x}"' for x in rmlvo])
+ s.append(f"- rmlvo: [{rmlvo}]")
s.append(f' cmd: "{escape(self.command)}"')
- s.append(f' status: {self.exitstatus}')
+ s.append(f" status: {self.exitstatus}")
if self.error:
s.append(f' error: "{escape(self.error.strip())}"')
- return '\n'.join(s)
+ return "\n".join(s)
def run(self):
raise NotImplementedError
@@ -71,37 +72,45 @@ class Invocation:
class XkbCompInvocation(Invocation):
def run(self):
r, m, l, v, o = self.rmlvo
- args = ['setxkbmap', '-print']
+ args = ["setxkbmap", "-print"]
if r is not None:
- args.append('-rules')
- args.append('{}'.format(r))
+ args.append("-rules")
+ args.append("{}".format(r))
if m is not None:
- args.append('-model')
- args.append('{}'.format(m))
+ args.append("-model")
+ args.append("{}".format(m))
if l is not None:
- args.append('-layout')
- args.append('{}'.format(l))
+ args.append("-layout")
+ args.append("{}".format(l))
if v is not None:
- args.append('-variant')
- args.append('{}'.format(v))
+ args.append("-variant")
+ args.append("{}".format(v))
if o is not None:
- args.append('-option')
- args.append('{}'.format(o))
+ args.append("-option")
+ args.append("{}".format(o))
- xkbcomp_args = ['xkbcomp', '-xkb', '-', '-']
+ xkbcomp_args = ["xkbcomp", "-xkb", "-", "-"]
self.command = " ".join(args + ["|"] + xkbcomp_args)
- setxkbmap = subprocess.Popen(args, stdout=subprocess.PIPE,
- stderr=subprocess.PIPE, universal_newlines=True)
+ setxkbmap = subprocess.Popen(
+ args,
+ stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE,
+ universal_newlines=True,
+ )
stdout, stderr = setxkbmap.communicate()
if "Cannot open display" in stderr:
self.error = stderr
self.exitstatus = 90
else:
- xkbcomp = subprocess.Popen(xkbcomp_args, stdin=subprocess.PIPE,
- stdout=subprocess.PIPE, stderr=subprocess.PIPE,
- universal_newlines=True)
+ xkbcomp = subprocess.Popen(
+ xkbcomp_args,
+ stdin=subprocess.PIPE,
+ stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE,
+ universal_newlines=True,
+ )
stdout, stderr = xkbcomp.communicate(stdout)
if xkbcomp.returncode != 0:
self.error = "failed to compile keymap"
@@ -117,23 +126,27 @@ class XkbcommonInvocation(Invocation):
def run(self):
r, m, l, v, o = self.rmlvo
args = [
- 'xkbcli-compile-keymap', # this is run in the builddir
- '--verbose',
- '--rules', r,
- '--model', m,
- '--layout', l,
+ "xkbcli-compile-keymap", # this is run in the builddir
+ "--verbose",
+ "--rules",
+ r,
+ "--model",
+ m,
+ "--layout",
+ l,
]
if v is not None:
- args += ['--variant', v]
+ args += ["--variant", v]
if o is not None:
- args += ['--options', o]
+ args += ["--options", o]
self.command = " ".join(args)
try:
- output = subprocess.check_output(args, stderr=subprocess.STDOUT,
- universal_newlines=True)
+ output = subprocess.check_output(
+ args, stderr=subprocess.STDOUT, universal_newlines=True
+ )
if self.UNRECOGNIZED_KEYSYM_ERROR in output:
- for line in output.split('\n'):
+ for line in output.split("\n"):
if self.UNRECOGNIZED_KEYSYM_ERROR in line:
self.error = line
self.exitstatus = 99 # tool doesn't generate this one
@@ -147,11 +160,11 @@ class XkbcommonInvocation(Invocation):
def xkbcommontool(rmlvo):
try:
- r = rmlvo.get('r', 'evdev')
- m = rmlvo.get('m', 'pc105')
- l = rmlvo.get('l', 'us')
- v = rmlvo.get('v', None)
- o = rmlvo.get('o', None)
+ r = rmlvo.get("r", "evdev")
+ m = rmlvo.get("m", "pc105")
+ l = rmlvo.get("l", "us")
+ v = rmlvo.get("v", None)
+ o = rmlvo.get("o", None)
tool = XkbcommonInvocation(r, m, l, v, o)
tool.run()
return tool
@@ -161,11 +174,11 @@ def xkbcommontool(rmlvo):
def xkbcomp(rmlvo):
try:
- r = rmlvo.get('r', 'evdev')
- m = rmlvo.get('m', 'pc105')
- l = rmlvo.get('l', 'us')
- v = rmlvo.get('v', None)
- o = rmlvo.get('o', None)
+ r = rmlvo.get("r", "evdev")
+ m = rmlvo.get("m", "pc105")
+ l = rmlvo.get("l", "us")
+ v = rmlvo.get("v", None)
+ o = rmlvo.get("o", None)
tool = XkbCompInvocation(r, m, l, v, o)
tool.run()
return tool
@@ -175,25 +188,22 @@ def xkbcomp(rmlvo):
def parse(path):
root = ET.fromstring(open(path).read())
- layouts = root.findall('layoutList/layout')
+ layouts = root.findall("layoutList/layout")
- options = [
- e.text
- for e in root.findall('optionList/group/option/configItem/name')
- ]
+ options = [e.text for e in root.findall("optionList/group/option/configItem/name")]
combos = []
for l in layouts:
- layout = l.find('configItem/name').text
- combos.append({'l': layout})
+ layout = l.find("configItem/name").text
+ combos.append({"l": layout})
- variants = l.findall('variantList/variant')
+ variants = l.findall("variantList/variant")
for v in variants:
- variant = v.find('configItem/name').text
+ variant = v.find("configItem/name").text
- combos.append({'l': layout, 'v': variant})
+ combos.append({"l": layout, "v": variant})
for option in options:
- combos.append({'l': layout, 'v': variant, 'o': option})
+ combos.append({"l": layout, "v": variant, "o": option})
return combos
@@ -234,9 +244,9 @@ def run(combos, tool, njobs, keymap_output_dir):
keymap_file = fname
if keymap_file_fd:
keymap_file_fd.close()
- keymap_file_fd = open(keymap_file, 'a')
+ keymap_file_fd = open(keymap_file, "a")
- rmlvo = ', '.join([x or '' for x in invocation.rmlvo])
+ rmlvo = ", ".join([x or "" for x in invocation.rmlvo])
print(f"// {rmlvo}", file=keymap_file_fd)
print(invocation.keymap, file=keymap_file_fd)
keymap_file_fd.flush()
@@ -249,37 +259,56 @@ def main(args):
global verbose
tools = {
- 'libxkbcommon': xkbcommontool,
- 'xkbcomp': xkbcomp,
+ "libxkbcommon": xkbcommontool,
+ "xkbcomp": xkbcomp,
}
parser = argparse.ArgumentParser(
- description='''
+ description="""
This tool compiles a keymap for each layout, variant and
options combination in the given rules XML file. The output
of this tool is YAML, use your favorite YAML parser to
extract error messages. Errors are printed to stderr.
- '''
+ """
+ )
+ parser.add_argument(
+ "path",
+ metavar="/path/to/evdev.xml",
+ nargs="?",
+ type=str,
+ default=DEFAULT_RULES_XML,
+ help="Path to xkeyboard-config's evdev.xml",
+ )
+ parser.add_argument(
+ "--tool",
+ choices=tools.keys(),
+ type=str,
+ default="libxkbcommon",
+ help="parsing tool to use",
+ )
+ parser.add_argument(
+ "--jobs",
+ "-j",
+ type=int,
+ default=os.cpu_count() * 4,
+ help="number of processes to use",
+ )
+ parser.add_argument("--verbose", "-v", default=False, action="store_true")
+ parser.add_argument(
+ "--keymap-output-dir",
+ default=None,
+ type=str,
+ help="Directory to print compiled keymaps to",
+ )
+ parser.add_argument(
+ "--layout", default=None, type=str, help="Only test the given layout"
+ )
+ parser.add_argument(
+ "--variant", default=None, type=str, help="Only test the given variant"
+ )
+ parser.add_argument(
+ "--option", default=None, type=str, help="Only test the given option"
)
- parser.add_argument('path', metavar='/path/to/evdev.xml',
- nargs='?', type=str,
- default=DEFAULT_RULES_XML,
- help='Path to xkeyboard-config\'s evdev.xml')
- parser.add_argument('--tool', choices=tools.keys(),
- type=str, default='libxkbcommon',
- help='parsing tool to use')
- parser.add_argument('--jobs', '-j', type=int,
- default=os.cpu_count() * 4,
- help='number of processes to use')
- parser.add_argument('--verbose', '-v', default=False, action="store_true")
- parser.add_argument('--keymap-output-dir', default=None, type=str,
- help='Directory to print compiled keymaps to')
- parser.add_argument('--layout', default=None, type=str,
- help='Only test the given layout')
- parser.add_argument('--variant', default=None, type=str,
- help='Only test the given variant')
- parser.add_argument('--option', default=None, type=str,
- help='Only test the given option')
args = parser.parse_args()
@@ -290,19 +319,21 @@ def main(args):
tool = tools[args.tool]
if any([args.layout, args.variant, args.option]):
- combos = [{
- 'l': args.layout,
- 'v': args.variant,
- 'o': args.option,
- }]
+ combos = [
+ {
+ "l": args.layout,
+ "v": args.variant,
+ "o": args.option,
+ }
+ ]
else:
combos = parse(args.path)
failed = run(combos, tool, args.jobs, keymapdir)
sys.exit(failed)
-if __name__ == '__main__':
+if __name__ == "__main__":
try:
main(sys.argv)
except KeyboardInterrupt:
- print('# Exiting after Ctrl+C')
+ print("# Exiting after Ctrl+C")