def test_import_keymap_with_comments__returns_expected_keyboard_layout_object(self): actual_keyboard_layout = parse_keymap_c('./assets/keymap_with_comments.c', two_layer_switch_layout) expected_keyboard_layout = KeyboardLayout(two_layer_switch_layout, [ KeyboardKeymap('_default', ['KC_1', 'KC_2', 'KC_3', 'MO(_special)']), KeyboardKeymap('_special', ['KC_A', 'KC_B', 'KC_C', 'KC_D']) ]) self.assertEqual(expected_keyboard_layout, actual_keyboard_layout)
def parse_keymap_c(filepath: str, switch_layout: KeyboardSwitchLayout, layout_type: str = 'LAYOUT'): keymap_layer_matcher_string = '\[\w+]\s*=\s*{0}\((\w|\s|,|\(|\))+\),*'.format(layout_type) keymap_layer_matcher = re.compile(keymap_layer_matcher_string) if does_file_exist(filepath): with open(abs_path(filepath), 'r') as my_file: raw_content = my_file.read() cleaned_content = remove_c_comments(raw_content) keymaps_match = KEYMAPS_START_MATCHER.search(cleaned_content) if keymaps_match is not None: end_of_keymaps_start = keymaps_match.end() cleaned_content = cleaned_content[end_of_keymaps_start:] cleaned_content = cleaned_content.replace('\\', '') layers = [layer.group() for layer in keymap_layer_matcher.finditer(cleaned_content)] keymap_layers = [] for layer_index in range(0, layers.__len__()): layer = layers[layer_index] layer_name_matcher = re.compile('\[(\w|\s)+]') layer_start_matcher = re.compile('\[(\w|\s)+]\s*=\s*\w+\(') layer_name = layer_name_matcher.search(layer) layer_name = layer_name.group().strip() layer_name = layer_name.replace('[', '').replace(']', '') layer_start = layer_start_matcher.search(layer) key_codes = layer[layer_start.end():] if key_codes[-1:] == ',': key_codes = key_codes[:-2] else: key_codes = key_codes[:-1] key_codes = remove_all_whitespace(key_codes) key_codes = key_codes.split(',') formatted_keycodes = format_keycodes_with_parentheses(key_codes) switch_layer = KeyboardKeymap(layer_name, formatted_keycodes) keymap_layers.append(switch_layer) return KeyboardLayout(switch_layout, keymap_layers, layout_type) else: raise FileNotFoundError('Could not find {0}'.format(filepath))
def test_init_with_invalid_input__raises_error(self): eight_key_layer_list = [ "KC_8", "KC_7", "KC_6", "KC_5", "KC_4", "KC_3", "KC_2", "KC_1" ] eight_key_layer = KeyboardKeymap("layer_1", eight_key_layer_list) with self.assertRaises(ValueError) as error: KeyboardLayout(nine_key_switch_layout, [eight_key_layer]) self.assertEqual( error.exception.__str__(), "Layer layer_1 has 8 keycodes on a board with 9 switches")
def test__str__returns_expected_string_for_simple_layer_list(self): keycodes_layer1_list = ["KC_1", "KC_2", "KC_3", "KC_4"] keycodes_layer2_list = ["KC_4", "KC_3", "KC_2", "KC_1"] layer_1 = KeyboardKeymap("layer1", keycodes_layer1_list) layer_2 = KeyboardKeymap("layer2", keycodes_layer2_list) two_switch_row = KeyboardSwitchRow(2) two_by_two_switch_layout = KeyboardSwitchLayout( [two_switch_row, two_switch_row]) simple_keyboard_layout = KeyboardLayout(two_by_two_switch_layout, [layer_1, layer_2]) simple_keyboard_layout_string = simple_keyboard_layout.__str__() expected_layout_string = "const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {\n\n" \ "\t[layer1] = LAYOUT( \\\n" \ "\t\tKC_1, KC_2, \\\n" \ "\t\tKC_3, KC_4),\n\n" \ "\t[layer2] = LAYOUT( \\\n" \ "\t\tKC_4, KC_3, \\\n" \ "\t\tKC_2, KC_1)\n\n" \ "};" self.assertEqual(expected_layout_string, simple_keyboard_layout_string)
KeyboardSwitchRow(2), KeyboardSwitchRow(3)]) nine_key_layer_list = [ "KC_8", "KC_7", "KC_6", "KC_5", "KC_4", "KC_3", "KC_2", "KC_1", "KC_0" ] long_keycode_list = [ "KC_8", "KC_7", "KC_6", "KC_5", "KC_4", "KC_3", "KC_2", "KC_1", "TO(DEFAULT)" ] uneven_rows_keycode_list = [ "KC_0", "KC_DOT", "KC_RGHT", "KC_1", "KC_2", "KC_3" ] nine_key_layer = KeyboardKeymap("layer_1", nine_key_layer_list) nine_key_keyboard_layout = KeyboardLayout(nine_key_switch_layout, [nine_key_layer]) long_keycode_layer = KeyboardKeymap("layer_with_one_long_keycode", long_keycode_list) long_keycode_keyboard_layout = KeyboardLayout(nine_key_switch_layout, [long_keycode_layer]) uneven_keycode_layer = KeyboardKeymap("uneven_layer", uneven_rows_keycode_list) uneven_keyboard_layout = KeyboardLayout(uneven_key_switch_layout, [uneven_keycode_layer]) class TestKeyboardLayout(unittest.TestCase): def test_init_with_invalid_input__raises_error(self): eight_key_layer_list = [ "KC_8", "KC_7", "KC_6", "KC_5", "KC_4", "KC_3", "KC_2", "KC_1"
def test_import_simple_keymap__returns_expected_keyboard_layout_object(self): actual_keyboard_layout = parse_keymap_c('./assets/simple_keymap.c', simple_switch_layout) expected_keyboard_layout = KeyboardLayout(simple_switch_layout, [ KeyboardKeymap('0', ['KC_1', 'KC_2', 'KC_3', 'KC_4']) ]) self.assertEqual(expected_keyboard_layout, actual_keyboard_layout)