from pathlib import Path import json import math class Keymap: keymap_dir = Path.home() / '.config/openrazer_scripter/' def __init__(self): self.keys = dict() pass @classmethod def get_keymap_path(cls, keyboard_name): return cls.keymap_dir / f"keymap_{keyboard_name.replace(' ', '_')}.json" def load_from_file(self, keyboard_name): path = self.get_keymap_path(keyboard_name) try: with open(path,'r') as keymap_file: self.keys = json.load(keymap_file) except FileNotFoundError: print(f"No such file: '{path}'. Run 'create_keyboard_map.py' to generate a keymap, or move an existing one to the directory.") return False return True def write_keymap_file(self, keyboard_name): keymap_json = json.dumps(self.keys) path = self.get_keymap_path(keyboard_name) Path(path.parent).mkdir(parents=True, exist_ok=True) with open(path,'w') as keymap_file: keymap_file.write(keymap_json) def set_keys(self, keys): self.keys = keys def get_row_column(self, index): row = math.floor(index / 22) column = index % 22 return (row, column) def get_location(self, key): if key in self.keys.keys(): value = self.keys[key] return self.get_row_column(value)