From 0e6e7dc97f39eb5b1ba6cf34acaa57efa83fc215 Mon Sep 17 00:00:00 2001 From: Burathar Date: Wed, 17 Jun 2020 20:34:15 +0200 Subject: [PATCH] Remove code errors --- color.py | 2 +- create_keyboard_map.py | 8 +++----- keyboard.py | 14 ++++++++------ keymap.py | 15 +++++++++------ main.py | 7 +++---- test_keyboard_map.py | 2 +- 6 files changed, 25 insertions(+), 23 deletions(-) diff --git a/color.py b/color.py index 61a8906..aec37f4 100644 --- a/color.py +++ b/color.py @@ -15,7 +15,7 @@ class Color: if hexstring[0] == '#': hexstring = hexstring[1:] if len(hexstring) == 6: - color = tuple(int(color[i:i+2], 16) for i in (0, 2, 4)) + color = tuple(int(hexstring[i:i+2], 16) for i in (0, 2, 4)) return cls(color[0], color[1], color[2]) @classmethod diff --git a/create_keyboard_map.py b/create_keyboard_map.py index 444c7b3..0d852be 100755 --- a/create_keyboard_map.py +++ b/create_keyboard_map.py @@ -1,4 +1,4 @@ -#! /bin/python3 +#!/usr/bin/env python3 from .keyboard import Keyboard from .keymap import Keymap @@ -7,8 +7,6 @@ import openrazer.client from pynput import keyboard as keyboardnput - - keyboard = Keyboard(no_keymap=True) if not keyboard.found_keyboard(): @@ -38,7 +36,7 @@ def set_key_columns(keymap, column_range, prefix=""): def iter_keys(): keymap = dict() print("Make sure caps lock and numpad are disabled, if not, do it now and restart the script") - print("This script will iterate over all keys, row by row. When a key lights up white, press it, and use the mouse to cancel any F-key effects. First, we'll skip the numpad. If no key lights up, press the bottom-right most key, excluding the numpad().") + print("This script will iterate over all keys, row by row. When a key lights up white, press it, and use the mouse to cancel any F-key effects. First, we'll skip the numpad. If no key lights up, press the bottom-right most key, excluding the numpad(right-arrow).") set_key_columns(keymap, range(0, 18)) print("Now we'll iterate over the numpad, if no key lights up, press the bottom right key(enter)") set_key_columns(keymap, range(18, 22), "num_") @@ -47,5 +45,5 @@ def iter_keys(): keymap = Keymap() keymap.set_keys(iter_keys()) -keymap.write_keymap_file() +keymap.write_keymap_file(keyboard.keyboard_name) print('Saved keymap to file (.config/openrazer_scripter/keymap.json)') diff --git a/keyboard.py b/keyboard.py index 9b1fe9a..937aa1d 100644 --- a/keyboard.py +++ b/keyboard.py @@ -13,15 +13,11 @@ class Keyboard: def __init__(self, no_keymap = False): self.kbd = None - self.find_keyboard() + self.find_keyboard(no_keymap) if self.kbd is None: warnings.warn('Compatible keyboard was not detected', RuntimeWarning) - if not no_keymap: - self.keymap = Keymap() - self.keymap.load_from_file() - - def find_keyboard(self): + def find_keyboard(self, no_keymap): if self.kbd is not None: print('keyboard was already initiated') return True @@ -31,12 +27,18 @@ class Keyboard: for device in devman.devices: if (device.name == "Razer BlackWidow Chroma V2"): self.kbd = device + if not no_keymap: + self.keymap = Keymap() + self.keymap.load_from_file(self.kbd.name) return True return False def found_keyboard(self): return self.kbd is not None + def keyboard_name(self): + return self.kbd.name + def set_keys(self, color, *keys): if not isinstance(color, Color): print(f"Color {color} for key(s) '{keys}' is no instance of Color, skipping.") diff --git a/keymap.py b/keymap.py index 4683d59..261ba1d 100644 --- a/keymap.py +++ b/keymap.py @@ -3,14 +3,17 @@ import json import math class Keymap: - + keymap_dir = Path.home() / '.config/openrazer_scripter/' def __init__(self): self.keys = dict() pass - def load_from_file(self, path = ''): - if path == '': - path = Path.home() / '.config/openrazer_scripter/keymap.json' + @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) @@ -27,9 +30,9 @@ class Keymap: value = self.keys[key] return self.get_row_column(value) - def write_keymap_file(self): + def write_keymap_file(self, keyboard_name): keymap_json = json.dumps(self.keys) - path = Path.home() / '.config/openrazer_scripter/keymap.json' + 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) \ No newline at end of file diff --git a/main.py b/main.py index f2f0480..fd93b12 100755 --- a/main.py +++ b/main.py @@ -1,8 +1,7 @@ +#! /bin/python3 + from .keyboard import Keyboard from .color import Color -import json -from pathlib import Path -import math keyboard = Keyboard() @@ -11,5 +10,5 @@ if not keyboard.found_keyboard(): exit() keyboard.set_static(Color(255,255,255)) -keyboard.set_keys('ff0000', 'enter') +keyboard.set_keys(Color.from_hex('ff0000'), 'enter') keyboard.draw() \ No newline at end of file diff --git a/test_keyboard_map.py b/test_keyboard_map.py index 5faa427..b4c025f 100755 --- a/test_keyboard_map.py +++ b/test_keyboard_map.py @@ -1,4 +1,4 @@ -#! /bin/python3 +#!/usr/bin/env python3 from .keyboard import Keyboard from .color import Color