Browse Source

Remove code errors

master
Burathar 5 years ago
parent
commit
0e6e7dc97f
  1. 2
      color.py
  2. 8
      create_keyboard_map.py
  3. 14
      keyboard.py
  4. 15
      keymap.py
  5. 7
      main.py
  6. 2
      test_keyboard_map.py

2
color.py

@ -15,7 +15,7 @@ class Color:
if hexstring[0] == '#': if hexstring[0] == '#':
hexstring = hexstring[1:] hexstring = hexstring[1:]
if len(hexstring) == 6: 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]) return cls(color[0], color[1], color[2])
@classmethod @classmethod

8
create_keyboard_map.py

@ -1,4 +1,4 @@
#! /bin/python3 #!/usr/bin/env python3
from .keyboard import Keyboard from .keyboard import Keyboard
from .keymap import Keymap from .keymap import Keymap
@ -7,8 +7,6 @@ import openrazer.client
from pynput import keyboard as keyboardnput from pynput import keyboard as keyboardnput
keyboard = Keyboard(no_keymap=True) keyboard = Keyboard(no_keymap=True)
if not keyboard.found_keyboard(): if not keyboard.found_keyboard():
@ -38,7 +36,7 @@ def set_key_columns(keymap, column_range, prefix=""):
def iter_keys(): def iter_keys():
keymap = dict() keymap = dict()
print("Make sure caps lock and numpad are disabled, if not, do it now and restart the script") 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)) 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)") 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_") set_key_columns(keymap, range(18, 22), "num_")
@ -47,5 +45,5 @@ def iter_keys():
keymap = Keymap() keymap = Keymap()
keymap.set_keys(iter_keys()) 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)') print('Saved keymap to file (.config/openrazer_scripter/keymap.json)')

14
keyboard.py

@ -13,15 +13,11 @@ class Keyboard:
def __init__(self, no_keymap = False): def __init__(self, no_keymap = False):
self.kbd = None self.kbd = None
self.find_keyboard() self.find_keyboard(no_keymap)
if self.kbd is None: if self.kbd is None:
warnings.warn('Compatible keyboard was not detected', RuntimeWarning) warnings.warn('Compatible keyboard was not detected', RuntimeWarning)
if not no_keymap:
self.keymap = Keymap()
self.keymap.load_from_file()
def find_keyboard(self, no_keymap):
def find_keyboard(self):
if self.kbd is not None: if self.kbd is not None:
print('keyboard was already initiated') print('keyboard was already initiated')
return True return True
@ -31,12 +27,18 @@ class Keyboard:
for device in devman.devices: for device in devman.devices:
if (device.name == "Razer BlackWidow Chroma V2"): if (device.name == "Razer BlackWidow Chroma V2"):
self.kbd = device self.kbd = device
if not no_keymap:
self.keymap = Keymap()
self.keymap.load_from_file(self.kbd.name)
return True return True
return False return False
def found_keyboard(self): def found_keyboard(self):
return self.kbd is not None return self.kbd is not None
def keyboard_name(self):
return self.kbd.name
def set_keys(self, color, *keys): def set_keys(self, color, *keys):
if not isinstance(color, Color): if not isinstance(color, Color):
print(f"Color {color} for key(s) '{keys}' is no instance of Color, skipping.") print(f"Color {color} for key(s) '{keys}' is no instance of Color, skipping.")

15
keymap.py

@ -3,14 +3,17 @@ import json
import math import math
class Keymap: class Keymap:
keymap_dir = Path.home() / '.config/openrazer_scripter/'
def __init__(self): def __init__(self):
self.keys = dict() self.keys = dict()
pass pass
def load_from_file(self, path = ''): @staticmethod
if path == '': def get_keymap_path(keyboard_name):
path = Path.home() / '.config/openrazer_scripter/keymap.json' 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: with open(path,'r') as keymap_file:
self.keys = json.load(keymap_file) self.keys = json.load(keymap_file)
@ -27,9 +30,9 @@ class Keymap:
value = self.keys[key] value = self.keys[key]
return self.get_row_column(value) return self.get_row_column(value)
def write_keymap_file(self): def write_keymap_file(self, keyboard_name):
keymap_json = json.dumps(self.keys) 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) Path(path.parent).mkdir(parents=True, exist_ok=True)
with open(path,'w') as keymap_file: with open(path,'w') as keymap_file:
keymap_file.write(keymap_json) keymap_file.write(keymap_json)

7
main.py

@ -1,8 +1,7 @@
#! /bin/python3
from .keyboard import Keyboard from .keyboard import Keyboard
from .color import Color from .color import Color
import json
from pathlib import Path
import math
keyboard = Keyboard() keyboard = Keyboard()
@ -11,5 +10,5 @@ if not keyboard.found_keyboard():
exit() exit()
keyboard.set_static(Color(255,255,255)) keyboard.set_static(Color(255,255,255))
keyboard.set_keys('ff0000', 'enter') keyboard.set_keys(Color.from_hex('ff0000'), 'enter')
keyboard.draw() keyboard.draw()

2
test_keyboard_map.py

@ -1,4 +1,4 @@
#! /bin/python3 #!/usr/bin/env python3
from .keyboard import Keyboard from .keyboard import Keyboard
from .color import Color from .color import Color

Loading…
Cancel
Save