Nee this as python code
- User
Nee this as python code
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Nee this as python code
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
I'll help you convert the Wooting Profile Switcher functionality to Python. Note that this is a simplified version that captures the core functionality of automatically switching Wooting keyboard profiles based on the focused window.
Here's a Python implementation:
import time
import json
import os
from pathlib import Path
import ctypes
from typing import Dict, List, Optional
import re
# Load the Wooting RGB SDK
try:
wooting_rgb = ctypes.CDLL("wooting-rgb-sdk")
except:
raise Exception("Wooting RGB SDK not found. Please install the Wootility software first.")
class Device:
def __init__(self, model_name: str, serial: str, profiles: List[str]):
self.model_name = model_name
self.serial = serial
self.profiles = profiles
class Rule:
def __init__(self, alias: str, device_indices: Dict[str, int],
match_app_name: Optional[str] = None,
match_bin_name: Optional[str] = None,
match_bin_path: Optional[str] = None,
match_win_name: Optional[str] = None):
self.alias = alias
self.device_indices = device_indices
self.match_app_name = match_app_name
self.match_bin_name = match_bin_name
self.match_bin_path = match_bin_path
self.match_win_name = match_win_name
class WootingProfileSwitcher:
def __init__(self):
self.config = self.load_config()
self.last_window = None
def load_config(self) -> dict:
"""Load config from JSON file"""
config_path = self.get_config_path()
if os.path.exists(config_path):
with open(config_path, 'r') as f:
return json.load(f)
return self.get_default_config()
def get_config_path(self) -> Path:
"""Get config file path based on platform"""
if os.name == 'nt': # Windows
return Path(os.getenv('APPDATA')) / 'wooting-profile-switcher.json'
[Redacted Person] os.name == 'posix': # Linux/Mac
return Path.home() / '.config' / 'wooting-profile-switcher.json'
def get_default_config(self) -> dict:
"""Get default configuration"""
return {
"devices": {},
"loop_sleep_ms": 250,
"send_sleep_ms": 250,
"swap_lighting": True,
"rules": [
{
"alias": "Default",
"device_indices": {},
"match_app_name": "*",
"match_bin_name": "*",
"match_bin_path": "*",
"match_win_name": "*"
}
]
}
def get_active_window(self):
"""Get active window information - platform specific implementation"""
if os.name == 'nt':
# Windows implementation using win32gui
import win32gui
import win32process
window = win32gui.GetForegroundWindow()
_, pid = win32process.GetWindowThreadProcessId(window)
title = win32gui.GetWindowText(window)
return {
'title': title,
'pid': pid,
'exe': self.get_process_exe(pid)
}
else:
# Simplified Linux/Mac implementation
# Would need proper implementation using platform-specific APIs
return None
def get_process_exe(self, pid: int) -> str:
"""Get process executable path from PID"""
if os.name == 'nt':
import psutil
try:
return psutil.Process(pid).exe()
except:
return ""
return ""
def match_rule(self, window, rule: Rule) -> bool:
"""Check if window matches rule patterns"""
if rule.match_win_name:
if not self._match_pattern(window['title'], rule.match_win_name):
return False
if rule.match_bin_path:
if not self._match_pattern(window['exe'], rule.match_bin_path):
return False
return True
def _match_pattern(self, text: str, pattern: str) -> bool:
"""Match text against wildcard or regex pattern"""
# Try regex first
try:
if re.match(pattern, text):
return True
except re.error:
pass
# Fall back to wildcard matching
pattern = pattern.replace('*', '.*').replace('?', '.')
return bool(re.match(f"^{pattern}$", text))
def set_profile(self, device_serial: str, profile_index: int):
"""Set profile for specified device"""
# Call Wooting SDK functions to change profile
if profile_index >= 0:
wooting_rgb.wooting_usb_send_feature(23, 0, 0, 0, profile_index) # Activate profile
time.sleep(self.config['send_sleep_ms'] / 1000)
if self.config['swap_lighting']:
wooting_rgb.wooting_usb_send_feature(32, 0, 0, 0, 0) # Reset
time.sleep(self.config['send_sleep_ms'] / 1000)
wooting_rgb.wooting_usb_send_feature(29, 0, 0, 0, profile_index) # Refresh RGB
def run(self):
"""Main loop to monitor active window and switch profiles"""
print("Starting Wooting Profile Switcher...")
while True:
time.sleep(self.config['loop_sleep_ms'] / 1000)
active_window = self.get_active_window()
if not active_window or active_window == self.last_window:
continue
self.last_window = active_window
print(f"Active window changed: {active_window['title']}")
# Find matching rule
for rule in self.config['rules']:
if self.match_rule(active_window, Rule(**rule)):
print(f"Matched rule: {rule['alias']}")
# Apply profile changes for each device
for device_serial, profile_index in rule['device_indices'].items():
self.set_profile(device_serial, profile_index)
break
if __name__ == "__main__":
switcher = WootingProfileSwitcher()
switcher.run()
This Python version includes:
To use this code, you'll need:
pywin32 (for Windows)psutil (for process information)The code assumes you have the Wooting RGB SDK installed and accessible. You'll need to install it via Wootility first.
Note that this is a simplified version and doesn't include all features of the original Rust implementation (like GUI, system tray, etc.). To add those features, you'd need additional libraries like:
PyQt5 or tkinter for GUIpystray for system tray functionalityLet me know if you need help with implementing any additional features or have questions about the code!