2020-10-26 22:45:22 +00:00
|
|
|
#!/usr/bin/python3
|
|
|
|
# -*- coding: utf-8 -*-
|
2020-10-31 13:43:09 +00:00
|
|
|
# key-mapper - GUI for device specific keyboard mappings
|
2021-02-22 18:48:20 +00:00
|
|
|
# Copyright (C) 2021 sezanzeb <proxima@sezanzeb.de>
|
2020-10-26 22:45:22 +00:00
|
|
|
#
|
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
|
2020-11-02 00:25:43 +00:00
|
|
|
import glob
|
2021-03-17 20:08:15 +00:00
|
|
|
import re
|
2020-10-31 17:48:03 +00:00
|
|
|
|
2020-12-24 00:26:34 +00:00
|
|
|
from keymapper.paths import get_preset_path, mkdir, CONFIG_PATH
|
2020-10-31 13:43:09 +00:00
|
|
|
from keymapper.logger import logger
|
2021-04-23 09:51:21 +00:00
|
|
|
from keymapper.groups import groups
|
2020-10-31 16:00:02 +00:00
|
|
|
|
|
|
|
|
2020-12-24 00:26:34 +00:00
|
|
|
def migrate_path():
|
2021-01-01 13:09:28 +00:00
|
|
|
"""Migrate the folder structure from < 0.4.0.
|
2020-12-24 00:26:34 +00:00
|
|
|
|
|
|
|
Move existing presets into the new subfolder "presets"
|
|
|
|
"""
|
|
|
|
new_preset_folder = os.path.join(CONFIG_PATH, 'presets')
|
|
|
|
if not os.path.exists(get_preset_path()) and os.path.exists(CONFIG_PATH):
|
2021-01-01 13:09:28 +00:00
|
|
|
logger.info('Migrating presets from < 0.4.0...')
|
2020-12-24 00:26:34 +00:00
|
|
|
devices = os.listdir(CONFIG_PATH)
|
|
|
|
mkdir(get_preset_path())
|
|
|
|
for device in devices:
|
|
|
|
path = os.path.join(CONFIG_PATH, device)
|
|
|
|
if os.path.isdir(path):
|
|
|
|
target = path.replace(CONFIG_PATH, new_preset_folder)
|
|
|
|
logger.info('Moving "%s" to "%s"', path, target)
|
|
|
|
os.rename(path, target)
|
|
|
|
logger.info('done')
|
|
|
|
|
|
|
|
|
|
|
|
migrate_path()
|
|
|
|
|
|
|
|
|
2021-04-23 09:51:21 +00:00
|
|
|
def get_available_preset_name(group_name, preset='new preset', copy=False):
|
2020-11-18 09:33:59 +00:00
|
|
|
"""Increment the preset name until it is available."""
|
2021-04-23 09:51:21 +00:00
|
|
|
if group_name is None:
|
2021-03-21 18:15:20 +00:00
|
|
|
# endless loop otherwise
|
2021-04-23 09:51:21 +00:00
|
|
|
raise ValueError('group_name may not be None')
|
2021-03-21 18:15:20 +00:00
|
|
|
|
2020-11-18 09:33:59 +00:00
|
|
|
preset = preset.strip()
|
|
|
|
|
2021-03-17 20:08:15 +00:00
|
|
|
if copy and not re.match(r'^.+\scopy( \d+)?$', preset):
|
|
|
|
preset = f'{preset} copy'
|
|
|
|
|
2020-11-18 09:33:59 +00:00
|
|
|
# find a name that is not already taken
|
2021-04-23 09:51:21 +00:00
|
|
|
if os.path.exists(get_preset_path(group_name, preset)):
|
2021-03-17 20:08:15 +00:00
|
|
|
# if there already is a trailing number, increment it instead of
|
|
|
|
# adding another one
|
|
|
|
match = re.match(r'^(.+) (\d+)$', preset)
|
|
|
|
if match:
|
|
|
|
preset = match[1]
|
|
|
|
i = int(match[2]) + 1
|
|
|
|
else:
|
|
|
|
i = 2
|
|
|
|
|
2021-04-23 09:51:21 +00:00
|
|
|
while os.path.exists(get_preset_path(group_name, f'{preset} {i}')):
|
2020-11-18 09:33:59 +00:00
|
|
|
i += 1
|
2021-03-17 20:08:15 +00:00
|
|
|
|
2020-11-18 09:33:59 +00:00
|
|
|
return f'{preset} {i}'
|
|
|
|
|
|
|
|
return preset
|
|
|
|
|
|
|
|
|
2021-04-23 09:51:21 +00:00
|
|
|
def get_presets(group_name):
|
2020-11-24 23:23:34 +00:00
|
|
|
"""Get all presets for the device and user, starting with the newest.
|
2020-10-31 16:00:02 +00:00
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
2021-04-23 09:51:21 +00:00
|
|
|
group_name : string
|
2020-10-31 16:00:02 +00:00
|
|
|
"""
|
2021-04-23 09:51:21 +00:00
|
|
|
device_folder = get_preset_path(group_name)
|
2020-11-29 17:24:50 +00:00
|
|
|
mkdir(device_folder)
|
2020-11-25 22:36:03 +00:00
|
|
|
|
|
|
|
paths = glob.glob(os.path.join(device_folder, '*.json'))
|
2020-11-02 18:17:28 +00:00
|
|
|
presets = [
|
2020-11-24 23:23:34 +00:00
|
|
|
os.path.splitext(os.path.basename(path))[0]
|
2020-11-25 22:36:03 +00:00
|
|
|
for path in sorted(paths, key=os.path.getmtime)
|
2020-11-02 18:17:28 +00:00
|
|
|
]
|
|
|
|
# the highest timestamp to the front
|
|
|
|
presets.reverse()
|
2020-11-18 21:33:01 +00:00
|
|
|
return presets
|
2020-10-31 17:48:03 +00:00
|
|
|
|
|
|
|
|
2020-11-02 01:17:58 +00:00
|
|
|
def get_any_preset():
|
|
|
|
"""Return the first found tuple of (device, preset)."""
|
2021-04-23 09:51:21 +00:00
|
|
|
group_names = groups.list_group_names()
|
|
|
|
if len(group_names) == 0:
|
2020-11-02 22:52:58 +00:00
|
|
|
return None, None
|
2021-04-23 09:51:21 +00:00
|
|
|
any_device = list(group_names)[0]
|
2020-11-02 01:17:58 +00:00
|
|
|
any_preset = (get_presets(any_device) or [None])[0]
|
|
|
|
return any_device, any_preset
|
|
|
|
|
|
|
|
|
2021-04-23 09:51:21 +00:00
|
|
|
def find_newest_preset(group_name=None):
|
2020-11-24 23:23:34 +00:00
|
|
|
"""Get a tuple of (device, preset) that was most recently modified
|
|
|
|
in the users home directory.
|
2020-11-02 00:25:43 +00:00
|
|
|
|
2020-11-02 22:52:58 +00:00
|
|
|
If no device has been configured yet, return an arbitrary device.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
2021-04-23 09:51:21 +00:00
|
|
|
group_name : string
|
2020-11-02 22:52:58 +00:00
|
|
|
If set, will return the newest preset for the device or None
|
2020-11-02 00:25:43 +00:00
|
|
|
"""
|
2020-11-02 22:14:11 +00:00
|
|
|
# sort the oldest files to the front in order to use pop to get the newest
|
2021-04-23 09:51:21 +00:00
|
|
|
if group_name is None:
|
2020-11-02 22:52:58 +00:00
|
|
|
paths = sorted(
|
2020-12-24 00:26:34 +00:00
|
|
|
glob.glob(os.path.join(get_preset_path(), '*/*.json')),
|
2020-11-02 22:52:58 +00:00
|
|
|
key=os.path.getmtime
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
paths = sorted(
|
2021-04-23 09:51:21 +00:00
|
|
|
glob.glob(os.path.join(get_preset_path(group_name), '*.json')),
|
2020-11-02 22:52:58 +00:00
|
|
|
key=os.path.getmtime
|
|
|
|
)
|
2020-11-02 00:25:43 +00:00
|
|
|
|
2020-11-02 01:17:58 +00:00
|
|
|
if len(paths) == 0:
|
2020-11-08 15:27:26 +00:00
|
|
|
logger.debug('No presets found')
|
2020-11-02 01:17:58 +00:00
|
|
|
return get_any_preset()
|
|
|
|
|
2021-04-23 09:51:21 +00:00
|
|
|
group_names = groups.list_group_names()
|
2020-11-02 00:25:43 +00:00
|
|
|
|
|
|
|
newest_path = None
|
|
|
|
while len(paths) > 0:
|
|
|
|
# take the newest path
|
|
|
|
path = paths.pop()
|
|
|
|
preset = os.path.split(path)[1]
|
2021-04-23 09:51:21 +00:00
|
|
|
group_name = os.path.split(os.path.split(path)[0])[1]
|
|
|
|
if group_name in group_names:
|
2020-11-02 00:25:43 +00:00
|
|
|
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')
|
2020-11-02 01:17:58 +00:00
|
|
|
return get_any_preset()
|
2020-11-02 00:25:43 +00:00
|
|
|
|
2020-11-24 23:23:34 +00:00
|
|
|
preset = os.path.splitext(preset)[0]
|
2021-04-23 09:51:21 +00:00
|
|
|
logger.debug('The newest preset is "%s", "%s"', group_name, preset)
|
2020-11-02 18:17:28 +00:00
|
|
|
|
2021-04-23 09:51:21 +00:00
|
|
|
return group_name, preset
|
2020-11-02 22:52:58 +00:00
|
|
|
|
|
|
|
|
2021-04-23 09:51:21 +00:00
|
|
|
def delete_preset(group_name, preset):
|
2020-11-24 23:23:34 +00:00
|
|
|
"""Delete one of the users presets."""
|
2021-04-23 09:51:21 +00:00
|
|
|
preset_path = get_preset_path(group_name, 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)
|
|
|
|
|
2021-04-23 09:51:21 +00:00
|
|
|
device_path = get_preset_path(group_name)
|
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)
|
2020-11-10 22:14:28 +00:00
|
|
|
os.rmdir(device_path)
|
2020-11-02 22:52:58 +00:00
|
|
|
|
|
|
|
|
2021-04-23 09:51:21 +00:00
|
|
|
def rename_preset(group_name, old_preset_name, new_preset_name):
|
2020-11-24 23:23:34 +00:00
|
|
|
"""Rename one of the users presets while avoiding name conflicts."""
|
2020-11-18 09:33:59 +00:00
|
|
|
if new_preset_name == old_preset_name:
|
2021-03-21 13:17:34 +00:00
|
|
|
return None
|
2020-11-18 09:33:59 +00:00
|
|
|
|
2021-04-23 09:51:21 +00:00
|
|
|
new_preset_name = get_available_preset_name(group_name, 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(
|
2021-04-23 09:51:21 +00:00
|
|
|
get_preset_path(group_name, old_preset_name),
|
|
|
|
get_preset_path(group_name, 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()
|
2021-04-23 09:51:21 +00:00
|
|
|
os.utime(get_preset_path(group_name, new_preset_name), (now, now))
|
2021-03-21 13:17:34 +00:00
|
|
|
return new_preset_name
|