Example #1
0
    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)
Example #2
0
def import_control():
    """Import the core function of the key-mapper-control command."""
    custom_mapping.empty()

    bin_path = os.path.join(os.getcwd(), 'bin', 'key-mapper-control')

    loader = SourceFileLoader('__not_main_idk__', bin_path)
    spec = spec_from_loader('__not_main_idk__', loader)
    module = module_from_spec(spec)
    spec.loader.exec_module(module)

    return module.main, module.internals
Example #3
0
    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)
Example #4
0
def launch(argv=None):
    """Start key-mapper-gtk with the command line argument array argv."""
    custom_mapping.empty()

    bin_path = os.path.join(os.getcwd(), 'bin', 'key-mapper-gtk')

    if not argv:
        argv = ['-d']

    with patch.object(sys, 'argv', [''] + [str(arg) for arg in argv]):
        loader = SourceFileLoader('__main__', bin_path)
        spec = spec_from_loader('__main__', loader)
        module = module_from_spec(spec)
        spec.loader.exec_module(module)

    gtk_iteration()

    return module.window
Example #5
0
def cleanup():
    """Reset the applications state."""
    keycode_reader.stop_reading()
    keycode_reader.clear()
    keycode_reader.newest_event = None
    keycode_reader._unreleased = {}

    for task in asyncio.Task.all_tasks():
        task.cancel()

    os.system('pkill -f key-mapper-service')

    time.sleep(0.05)

    if os.path.exists(tmp):
        shutil.rmtree(tmp)

    config.clear_config()
    config.save_config()

    system_mapping.populate()
    custom_mapping.empty()
    custom_mapping.clear_config()

    clear_write_history()

    for key in list(active_macros.keys()):
        del active_macros[key]
    for key in list(unreleased.keys()):
        del unreleased[key]

    for key in list(pending_events.keys()):
        del pending_events[key]

    for path in list(fixtures.keys()):
        if path not in _fixture_copy:
            del fixtures[path]
    for path in list(_fixture_copy.keys()):
        if path not in fixtures:
            fixtures[path] = _fixture_copy[path]

    refresh_devices()
Example #6
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)
Example #7
0
def quick_cleanup(log=True):
    """Reset the applications state."""
    if log:
        print('quick cleanup')

    for device in list(pending_events.keys()):
        try:
            while pending_events[device][1].poll():
                pending_events[device][1].recv()
        except (UnpicklingError, EOFError):
            pass

        # setup new pipes for the next test
        pending_events[device] = None
        setup_pipe(device)

    try:
        reader.terminate()
    except (BrokenPipeError, OSError):
        pass

    if asyncio.get_event_loop().is_running():
        for task in asyncio.all_tasks():
            task.cancel()

    if not macro_variables.process.is_alive():
        raise AssertionError('the SharedDict manager is not running anymore')

    macro_variables._stop()

    join_children()

    macro_variables._start()

    if os.path.exists(tmp):
        shutil.rmtree(tmp)

    config.path = os.path.join(get_config_path(), 'config.json')
    config.clear_config()
    config.save_config()

    system_mapping.populate()
    custom_mapping.empty()
    custom_mapping.clear_config()
    custom_mapping.changed = False

    clear_write_history()

    for name in list(uinputs.keys()):
        del uinputs[name]

    for device in list(active_macros.keys()):
        del active_macros[device]
    for device in list(unreleased.keys()):
        del unreleased[device]

    for path in list(fixtures.keys()):
        if path not in _fixture_copy:
            del fixtures[path]
    for path in list(_fixture_copy.keys()):
        fixtures[path] = copy.deepcopy(_fixture_copy[path])

    os.environ.update(environ_copy)
    for device in list(os.environ.keys()):
        if device not in environ_copy:
            del os.environ[device]

    reader.clear()

    for _, pipe in pending_events.values():
        assert not pipe.poll()

    assert macro_variables.is_alive(1)
Example #8
0
 def clear_mapping_table(self):
     """Remove all rows from the mappings table."""
     key_list = self.get('key_list')
     key_list.forall(key_list.remove)
     custom_mapping.empty()
Example #9
0
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))