def load_aliases(filename): """ Loads aliases.txt-formatted file and returns a dict. Accepted formats: aliasname,keystrokes (QF1.x style) aliasname:keystrokes (QF2.x style) """ aliases = {} # load the file contents try: with open(filename) as f: data = f.read() except: raise FileError("Could not open aliases file " + filename) data = util.convert_line_endings(data) lines = data.split('\n') # strip out comment and empty lines lines = [line for line in lines if line != '' and line[0] != '#'] # break into {aliasname:keystrokes} pairs for line in lines: match = re.match(r'([\w\d]+)(,|:) *(.+)\s*\n*', line) if match is not None: aliases[match.group(1)] = match.group(3) return aliases
def parse_interface_txt(path): """ Parse DF-syntax interface.txt. Returns a dictionary with keycodes as keys, whose values are lists of DF macro commands bound to each keycode in interface.txt. """ with open(path) as f: data = f.read() data = util.convert_line_endings(data) groups = [re.split('\n', kb) for kb in re.split(r'\[BIND:', data)] keybinds = copy(KEY_LIST) for kb in groups: if kb == ['']: continue bind = re.sub(r'(\w+):.+', r'\1', kb[0]) keys = [re.sub(r'\[(KEY:|SYM:)(.+?)\]', r'\2', k) for k in kb[1:]] for k in keys: if k == '': continue if keybinds.get(k) is None: keybinds[k] = [] keybinds[k].append('\t\t' + bind) return keybinds