From 916ec6278a33e68e800bb3524c82f69e686e2ce9 Mon Sep 17 00:00:00 2001 From: sezanzeb Date: Sat, 31 Oct 2020 18:48:03 +0100 Subject: [PATCH] bunch of stuff --- bin/key-mapper-gtk | 35 ++++++++++++++++++++++----- data/key-mapper.glade | 10 +++++--- keymapper/config.py | 21 ++++++++++++++-- keymapper/{profiles.py => presets.py} | 27 ++++++++++++++++++++- tests/testcases/integration.py | 5 ++++ 5 files changed, 85 insertions(+), 13 deletions(-) rename keymapper/{profiles.py => presets.py} (75%) diff --git a/bin/key-mapper-gtk b/bin/key-mapper-gtk index a73e06d5..56ec8467 100755 --- a/bin/key-mapper-gtk +++ b/bin/key-mapper-gtk @@ -32,7 +32,8 @@ gi.require_version('GLib', '2.0') from gi.repository import Gtk from keymapper.data import get_data_path -from keymapper.profiles import find_devices, get_presets, get_mappings +from keymapper.presets import find_devices, get_presets, get_mappings, \ + find_newest_preset, create_preset from keymapper.logger import logger, update_verbosity, log_info @@ -91,7 +92,10 @@ class Window: self.window = window self.populate_devices() - self.on_select_profile('asdf') + + # find an select the newest preset based on file modification dates + self.on_select_preset(find_newest_preset()) + def get(self, name): """Get a widget from the window""" @@ -108,11 +112,29 @@ class Window: for (id, device) in devices: device_selection.append(device, device) - def populate_profiles(self): - """Show the available profiles for the selected device.""" + def populate_presets(self, device): + """Show the available presets for the selected device.""" + presets = get_presets(device) + preset_selection = self.get('preset_selection') + for preset in presets: + preset_selection.append(preset) + + def on_select_device(self, dropdown): + """List all presets, create one if none exist yet. + """ + device = dropdown.get_active_text() + presets = get_presets(device) + if len(presets) == 0: + create_preset(device) + self.populate_presets(device) + + def on_select_preset(self, preset): + """Show the mappings of the preset - def on_select_profile(self, profile): - """Show the mappings of the profile""" + Parameters + ---------- + preset : string + """ # prepare one empty input to add stuff, and to get the grid to # the correct column width, otherwise it may jump if the user adds # the first row. @@ -149,6 +171,7 @@ class Window: # add back an empty row self.on_add_key_clicked() + if __name__ == '__main__': parser = ArgumentParser() parser.add_argument( diff --git a/data/key-mapper.glade b/data/key-mapper.glade index 67165c30..55126c2e 100644 --- a/data/key-mapper.glade +++ b/data/key-mapper.glade @@ -36,6 +36,7 @@ 400 True False + True @@ -62,7 +63,7 @@ - + True False 10 @@ -78,7 +79,7 @@ 50 True False - Profile + preset 10 0 @@ -89,10 +90,11 @@ - + 200 True False + True @@ -122,7 +124,7 @@ - + True False 10 diff --git a/keymapper/config.py b/keymapper/config.py index fae3a5fd..991f4785 100644 --- a/keymapper/config.py +++ b/keymapper/config.py @@ -34,6 +34,9 @@ _configs = {} _defaults = {} +# TODO this works on xmodmaps instead of config files + + def _modify_config(config_contents, key, value): """Return a string representing the modified contents of the config file. @@ -173,8 +176,22 @@ class Config: return True -def get_config(device, preset, path): - """Ask for the config. Initialize it if not yet done so. +def get_config(device, preset, path=None): + """Ask for a config object. + + There should not be multiple Config objects for the same preset, so make + sure to use this function insted of the Config constructor. + + Creates a config file if it doesn't exist yet. + + Parameters + ---------- + device : string + preset : string + path : string or None + If none, will default to '~/.config/key-mapper/'. + In that directory, a folder for the device and a file for + the preset will be created. """ # don't initialize it right away in the global scope, to avoid having # the wrong logging verbosity. diff --git a/keymapper/profiles.py b/keymapper/presets.py similarity index 75% rename from keymapper/profiles.py rename to keymapper/presets.py index dac798a9..b92616e5 100644 --- a/keymapper/profiles.py +++ b/keymapper/presets.py @@ -24,7 +24,9 @@ import os import subprocess + from keymapper.logger import logger +from keymapper.config import get_config def get_xinput_list(type): @@ -62,7 +64,24 @@ def get_presets(device): ---------- device : string """ - pass + print('get_presets not yet implemented') + return [] + + +def create_preset(device, name=None): + """Create an empty preset.""" + existing_names = get_presets(device) + if name is None: + name = 'new preset' + + # find a name that is not already taken + i = 1 + while name in existing_names: + i += 1 + name = f'new preset {i}' + + # trigger the creation of a new config file: + get_config(device, name) def get_mappings(device, preset): @@ -74,3 +93,9 @@ def get_mappings(device, preset): preset : string """ pass + + +def find_newest_preset(): + """Get the device and present that was most recently modified.""" + print('find_newest_preset not yet implemented') + return None, None diff --git a/tests/testcases/integration.py b/tests/testcases/integration.py index 1c0815fe..b01af4d8 100644 --- a/tests/testcases/integration.py +++ b/tests/testcases/integration.py @@ -90,6 +90,11 @@ class Integration(unittest.TestCase): self.assertIsNotNone(self.window) self.assertTrue(self.window.window.get_visible()) + def test_select_device_and_preset(self): + self.window.on_select_device('fakeDevice1') + self.window.on_select_preset('fakePreset1') + # TODO test meaningful stuff here + if __name__ == "__main__": unittest.main()