Skip to content

Instantly share code, notes, and snippets.

@celediel
Created December 7, 2022 04:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save celediel/42e5370efa826128e5ab876a037db386 to your computer and use it in GitHub Desktop.
Save celediel/42e5370efa826128e5ab876a037db386 to your computer and use it in GitHub Desktop.
# @author : celediel (lilian.jonsdottir@gmail.com)
# @file : qw_to_dv
# @created : Tuesday Dec 06, 2022 12:53:00 Pacific Standard Time
# Qwerty -> Dvorak for Dwarf Fortress
from os import path, sep
from re import sub as resub, Match
from typing import AnyStr, Optional
q_to_d = {
"q": "'",
"w": ",",
"e": ".",
"r": "p",
"t": "y",
"y": "f",
"u": "g",
"i": "c",
"o": "r",
"p": "l",
"[": "/",
"]": "=",
"s": "o",
"d": "e",
"f": "u",
"g": "i",
"h": "d",
"j": "h",
"k": "t",
"l": "n",
";": "s",
"'": "-",
"z": ";",
"x": "q",
"c": "j",
"v": "k",
"b": "x",
"n": "b",
",": "w",
".": "v",
"/": "z",
"-": "[",
"=": "]",
"Q": '"',
"W": "<",
"E": ">",
"R": "P",
"T": "Y",
"Y": "F",
"U": "G",
"I": "C",
"O": "R",
"P": "L",
"{": "?",
"}": "+",
"S": "O",
"D": "E",
"F": "U",
"G": "I",
"H": "D",
"J": "H",
"K": "T",
"L": "N",
":": "S",
'"': "-",
"Z": ":",
"X": "Q",
"C": "J",
"V": "K",
"B": "X",
"N": "B",
"<": "W",
">": "V",
"?": "Z",
"_": "{",
"+": "}",
}
def key_replacer(m: Optional[Match[AnyStr]]):
if m:
# pyright is mad about these unless I cast them all as str
# because re.Match.group() returns AnyStr or Any
whole_thing = str(m.group(0))
starter = str(m.group(1))
thing = str(m.group(2))
ender = str(m.group(3))
try:
return f"{starter}{q_to_d[thing]}{ender}"
except KeyError:
return whole_thing
def check_ignored_line(line):
ignored_line_strings = [
"CUSTOM_",
"STRING_",
]
ignored = False
for ignored_line_string in ignored_line_strings:
if line.find(ignored_line_string) >= 0:
ignored = True
return ignored
def main():
in_file = f"data{sep}init{sep}interface.txt"
out_file = f"prefs{sep}interface.txt"
key_regex = r"(\[KEY:)(.)(\])"
with open(in_file, encoding="utf8") as file:
stuff = file.readlines()
new_stuff = []
last_line = ""
for line in stuff:
new_line = line
if not check_ignored_line(last_line):
new_line = resub(key_regex, key_replacer, new_line) # type: ignore
new_stuff.append(new_line)
last_line = line
with open(out_file, "w", encoding="utf8") as file:
file.writelines(new_stuff)
if __name__ == "__main__":
exe = "Dwarf Fortress.exe"
if not path.exists(f".{sep}{exe}"):
print(f"Couldn't find {exe}.")
print("Please run this script from wherever you installed Dwarf Fortress to.")
else:
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment