+class Shortcuts:
+
+ def __init__(self):
+ self.shortcut_entry = re.compile(r'^\s*"([^"]+)"\s*\"([^"]+)\"')
+ self.bindings = {}
+ self.bind = re.compile(r'^\s*\\bind\s+"([^"]+)"')
+ if lyx_userdir_ver is None:
+ self.dir = lyx_userdir
+ else:
+ self.dir = lyx_userdir_ver
+
+ def __UseShortcut(self, c):
+ m = self.shortcut_entry.match(c)
+ if m:
+ sh = m.group(1)
+ fkt = m.group(2)
+ self.bindings[sh] = fkt
+ else:
+ die(1, "cad shortcut spec(" + c + ")")
+
+ def __PrepareShortcuts(self):
+ if not self.dir is None:
+ tmp = tempfile.NamedTemporaryFile(suffix='.bind', delete=False)
+ try:
+ old = open(self.dir + '/bind/user.bind', 'r')
+ except IOError as e:
+ old = None
+ if not old is None:
+ lines = old.read().split("\n")
+ old.close()
+ bindfound = False
+ for line in lines:
+ m = self.bind.match(line)
+ if m:
+ bindfound = True
+ val = m.group(1)
+ if val in self.bindings:
+ if self.bindings[val] != "":
+ tmp.write("\\bind \"" + val + "\" \"" + self.bindings[val] + "\"\n")
+ self.bindings[val] = ""
+ else:
+ tmp.write(line + '\n')
+ elif not bindfound:
+ tmp.write(line + '\n')
+ else:
+ tmp.writelines(
+ '## This file is used for keytests only\n\n' +
+ 'Format 4\n\n'
+ )
+ for val in self.bindings:
+ if not self.bindings[val] is None:
+ if self.bindings[val] != "":
+ tmp.write("\\bind \"" + val + "\" \"" + self.bindings[val] + "\"\n")
+ self.bindings[val] = ""
+ tmp.close()
+ shutil.move(tmp.name, self.dir + '/bind/user.bind')
+ else:
+ print("User dir not specified")
+
+ def dispatch(self, c):
+ if c[0:12] == 'UseShortcut ':
+ self.__UseShortcut(c[12:])
+ elif c == 'PrepareShortcuts':
+ print('Preparing usefull sortcuts for tests')
+ self.__PrepareShortcuts()
+ else:
+ return False
+ return True