def populate_presets(self): """Show the available presets for the selected device. This will destroy unsaved changes in the custom_mapping. """ device = self.selected_device presets = get_presets(device) if len(presets) == 0: new_preset = get_available_preset_name(self.selected_device) custom_mapping.empty() path = get_preset_path(self.selected_device, new_preset) custom_mapping.save(path) presets = [new_preset] else: logger.debug('"%s" presets: "%s"', device, '", "'.join(presets)) preset_selection = self.get('preset_selection') with HandlerDisabled(preset_selection, self.on_select_preset): # otherwise the handler is called with None for each preset preset_selection.remove_all() for preset in presets: preset_selection.append(preset, preset) # and select the newest one (on the top). triggers on_select_preset preset_selection.set_active(0)
def create_preset(self, copy=False): """Create a new preset and select it.""" self.save_preset() try: if copy: new_preset = get_available_preset_name(self.selected_device, self.selected_preset, copy) else: new_preset = get_available_preset_name(self.selected_device) custom_mapping.empty() path = get_preset_path(self.selected_device, new_preset) custom_mapping.save(path) self.get('preset_selection').append(new_preset, new_preset) self.get('preset_selection').set_active_id(new_preset) except PermissionError as error: error = str(error) self.show_status(CTX_ERROR, 'Permission denied!', error) logger.error(error)
def on_create_preset_clicked(self, _): """Create a new preset and select it.""" if custom_mapping.changed and unsaved_changes_dialog() == GO_BACK: return try: new_preset = get_available_preset_name(self.selected_device) custom_mapping.empty() path = get_preset_path(self.selected_device, new_preset) custom_mapping.save(path) self.get('preset_selection').append(new_preset, new_preset) self.get('preset_selection').set_active_id(new_preset) except PermissionError as error: error = str(error) self.show_status(CTX_ERROR, 'Error: Permission denied!', error) logger.error(error)
def test_get_available_preset_name(self): # no filename conflict self.assertEqual(get_available_preset_name('_', 'qux 2'), 'qux 2') touch(get_preset_path('_', 'qux 5')) self.assertEqual(get_available_preset_name('_', 'qux 5'), 'qux 6') touch(get_preset_path('_', 'qux')) self.assertEqual(get_available_preset_name('_', 'qux'), 'qux 2') touch(get_preset_path('_', 'qux1')) self.assertEqual(get_available_preset_name('_', 'qux1'), 'qux1 2') touch(get_preset_path('_', 'qux 2 3')) self.assertEqual(get_available_preset_name('_', 'qux 2 3'), 'qux 2 4') touch(get_preset_path('_', 'qux 5')) self.assertEqual(get_available_preset_name('_', 'qux 5', True), 'qux 5 copy') touch(get_preset_path('_', 'qux 5 copy')) self.assertEqual(get_available_preset_name('_', 'qux 5', True), 'qux 5 copy 2') touch(get_preset_path('_', 'qux 5 copy 2')) self.assertEqual(get_available_preset_name('_', 'qux 5', True), 'qux 5 copy 3') touch(get_preset_path('_', 'qux 5copy')) self.assertEqual(get_available_preset_name('_', 'qux 5copy', True), 'qux 5copy copy') touch(get_preset_path('_', 'qux 5copy 2')) self.assertEqual(get_available_preset_name('_', 'qux 5copy 2', True), 'qux 5copy 2 copy') touch(get_preset_path('_', 'qux 5copy 2 copy')) self.assertEqual( get_available_preset_name('_', 'qux 5copy 2 copy', True), 'qux 5copy 2 copy 2')
def create_preset(group_name, name='new preset'): name = get_available_preset_name(group_name, name) custom_mapping.empty() custom_mapping.save(get_preset_path(group_name, name))