from pathlib import Path import json import math class Keymap: keymap_dir = Path.home() / '.config/openrazer_scripter/' def __init__(self): self.keys = dict() pass @staticmethod def get_keymap_path(keyboard_name): return '.config/openrazer_scripter/' / f"keymap_{keyboard_name.replace(' ', '_')}.json" def load_from_file(self, keyboard_name): path = self.get_keymap_path(keyboard_name) with open(path,'r') as keymap_file: self.keys = json.load(keymap_file) 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) 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)