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: @@ -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

8
create_keyboard_map.py

@ -1,4 +1,4 @@ @@ -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 @@ -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=""): @@ -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(): @@ -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)')

14
keyboard.py

@ -13,15 +13,11 @@ class Keyboard: @@ -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: @@ -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.")

15
keymap.py

@ -3,14 +3,17 @@ import json @@ -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: @@ -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)

7
main.py

@ -1,8 +1,7 @@ @@ -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(): @@ -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()

2
test_keyboard_map.py

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

Loading…
Cancel
Save