input-remapper/keymapper/presets.py

159 lines
4.7 KiB
Python
Raw Normal View History

2020-10-26 22:45:22 +00:00
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# key-mapper - GUI for device specific keyboard mappings
2020-10-26 22:45:22 +00:00
# Copyright (C) 2020 sezanzeb <proxima@hip70890b.de>
#
2020-10-31 13:02:59 +00:00
# This file is part of key-mapper.
2020-10-26 22:45:22 +00:00
#
2020-10-31 13:02:59 +00:00
# key-mapper is free software: you can redistribute it and/or modify
2020-10-26 22:45:22 +00:00
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
2020-10-31 13:02:59 +00:00
# key-mapper is distributed in the hope that it will be useful,
2020-10-26 22:45:22 +00:00
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
2020-10-31 13:02:59 +00:00
# along with key-mapper. If not, see <https://www.gnu.org/licenses/>.
2020-10-26 22:45:22 +00:00
2020-11-09 22:16:30 +00:00
"""Helperfunctions to find device ids, names, and to load presets."""
2020-10-26 22:45:22 +00:00
2020-10-31 16:00:02 +00:00
import os
2020-11-07 23:54:19 +00:00
import time
import glob
2020-10-31 17:48:03 +00:00
2020-11-18 09:33:59 +00:00
from keymapper.paths import get_config_path
from keymapper.logger import logger
from keymapper.getdevices import get_devices
2020-10-31 16:00:02 +00:00
2020-11-18 09:33:59 +00:00
def get_available_preset_name(device, preset='new preset'):
"""Increment the preset name until it is available."""
preset = preset.strip()
# find a name that is not already taken
if os.path.exists(get_config_path(device, preset)):
i = 2
while os.path.exists(get_config_path(device, f'{preset} {i}')):
i += 1
return f'{preset} {i}'
return preset
2020-10-31 14:22:58 +00:00
def get_presets(device):
2020-10-31 19:19:46 +00:00
"""Get all configured presets for the device, sorted by modification date.
2020-10-31 16:00:02 +00:00
Parameters
----------
device : string
"""
2020-11-18 09:33:59 +00:00
device_folder = get_config_path(device)
2020-10-31 19:19:46 +00:00
if not os.path.exists(device_folder):
os.makedirs(device_folder)
presets = [
os.path.basename(path)
for path in sorted(
glob.glob(os.path.join(device_folder, '*')),
key=os.path.getmtime
)
]
# the highest timestamp to the front
presets.reverse()
return presets
2020-10-31 17:48:03 +00:00
def get_any_preset():
"""Return the first found tuple of (device, preset)."""
2020-11-02 22:52:58 +00:00
devices = get_devices().keys()
if len(devices) == 0:
return None, None
any_device = list(devices)[0]
any_preset = (get_presets(any_device) or [None])[0]
2020-11-02 22:52:58 +00:00
if any_preset is not None:
any_preset = any_preset
return any_device, any_preset
2020-11-02 22:52:58 +00:00
def find_newest_preset(device=None):
"""Get a tuple of (device, preset) that was most recently modified.
2020-11-02 22:52:58 +00:00
If no device has been configured yet, return an arbitrary device.
Parameters
----------
device : string
If set, will return the newest preset for the device or None
"""
2020-11-02 22:14:11 +00:00
# sort the oldest files to the front in order to use pop to get the newest
2020-11-02 22:52:58 +00:00
if device is None:
paths = sorted(
2020-11-18 09:33:59 +00:00
glob.glob(os.path.join(get_config_path(), '*/*')),
2020-11-02 22:52:58 +00:00
key=os.path.getmtime
)
else:
paths = sorted(
2020-11-18 09:33:59 +00:00
glob.glob(os.path.join(get_config_path(device), '*')),
2020-11-02 22:52:58 +00:00
key=os.path.getmtime
)
if len(paths) == 0:
2020-11-08 15:27:26 +00:00
logger.debug('No presets found')
return get_any_preset()
online_devices = get_devices().keys()
newest_path = None
while len(paths) > 0:
# take the newest path
path = paths.pop()
preset = os.path.split(path)[1]
device = os.path.split(os.path.split(path)[0])[1]
if device in online_devices:
newest_path = path
break
if newest_path is None:
2020-11-08 15:27:26 +00:00
logger.debug('None of the configured devices is currently online')
return get_any_preset()
logger.debug('The newest preset is "%s", "%s"', device, preset)
return device, preset
2020-11-02 22:52:58 +00:00
def delete_preset(device, preset):
"""Delete a preset from the file system."""
2020-11-18 09:33:59 +00:00
preset_path = get_config_path(device, preset)
2020-11-02 22:52:58 +00:00
if not os.path.exists(preset_path):
logger.debug('Cannot remove non existing path "%s"', preset_path)
return
logger.info('Removing "%s"', preset_path)
os.remove(preset_path)
2020-11-18 09:33:59 +00:00
device_path = get_config_path(device)
2020-11-09 22:16:30 +00:00
if os.path.exists(device_path) and len(os.listdir(device_path)) == 0:
2020-11-02 22:52:58 +00:00
logger.debug('Removing empty dir "%s"', device_path)
os.rmdir(device_path)
2020-11-02 22:52:58 +00:00
def rename_preset(device, old_preset_name, new_preset_name):
"""Rename a preset while avoiding name conflicts."""
2020-11-18 09:33:59 +00:00
if new_preset_name == old_preset_name:
return
new_preset_name = get_available_preset_name(device, new_preset_name)
2020-11-02 22:52:58 +00:00
logger.info('Moving "%s" to "%s"', old_preset_name, new_preset_name)
os.rename(
2020-11-18 09:33:59 +00:00
get_config_path(device, old_preset_name),
get_config_path(device, new_preset_name)
2020-11-02 22:52:58 +00:00
)
2020-11-07 23:54:19 +00:00
# set the modification date to now
now = time.time()
2020-11-18 09:33:59 +00:00
os.utime(get_config_path(device, new_preset_name), (now, now))