some os.path.join refactoring

This commit is contained in:
sezanzeb 2020-11-02 22:06:05 +01:00 committed by sezanzeb
parent d211b39ad2
commit 2952fb47f0
4 changed files with 30 additions and 16 deletions

View File

@ -85,7 +85,7 @@ class Window:
self.selected_preset = None self.selected_preset = None
self.mappings = [] self.mappings = []
gladefile = os.path.join(get_data_path(), 'key-mapper.glade') gladefile = get_data_path('key-mapper.glade')
builder = Gtk.Builder() builder = Gtk.Builder()
builder.add_from_file(gladefile) builder.add_from_file(gladefile)
builder.connect_signals(self) builder.connect_signals(self)
@ -233,7 +233,7 @@ class Window:
) )
# TODO use user defined mapping # TODO use user defined mapping
self.mappings = [(10, 'f')] self.mappings = [(10, 'c')]
create_setxkbmap_config( create_setxkbmap_config(
self.selected_device, self.selected_device,

View File

@ -35,7 +35,7 @@ import os
import re import re
import subprocess import subprocess
from keymapper.paths import CONFIG_PATH, SYMBOLS_PATH, KEYCODES_PATH from keymapper.paths import get_home_path, get_usr_path, KEYCODES_PATH
from keymapper.logger import logger from keymapper.logger import logger
from keymapper.data import get_data_path from keymapper.data import get_data_path
from keymapper.presets import get_presets from keymapper.presets import get_presets
@ -78,21 +78,21 @@ def create_setxkbmap_config(device, preset, mappings):
""" """
create_identity_mapping() create_identity_mapping()
config_path = os.path.join(CONFIG_PATH, device, preset) home_path = get_home_path(device, preset)
# setxkbmap cannot handle spaces # setxkbmap cannot handle spaces
usr_path = os.path.join(SYMBOLS_PATH, device, preset).replace(' ', '_') usr_path = get_usr_path(device, preset)
if not os.path.exists(config_path): if not os.path.exists(home_path):
logger.info('Creating config file "%s"', config_path) logger.info('Creating config file "%s"', home_path)
os.makedirs(os.path.dirname(config_path), exist_ok=True) os.makedirs(os.path.dirname(home_path), exist_ok=True)
os.mknod(config_path) os.mknod(home_path)
if not os.path.exists(usr_path): if not os.path.exists(usr_path):
logger.info('Creating symlink in "%s"', usr_path) logger.info('Creating symlink in "%s"', usr_path)
os.makedirs(os.path.dirname(usr_path), exist_ok=True) os.makedirs(os.path.dirname(usr_path), exist_ok=True)
os.symlink(config_path, usr_path) os.symlink(home_path, usr_path)
logger.info('Writing key mappings') logger.info('Writing key mappings')
with open(config_path, 'w') as f: with open(home_path, 'w') as f:
f.write(generate_symbols_file_content(device, preset, mappings)) f.write(generate_symbols_file_content(device, preset, mappings))
@ -143,7 +143,7 @@ def create_identity_mapping():
for code in range(minimum, maximum + 1): for code in range(minimum, maximum + 1):
xkb_keycodes.append(f'<{code}> = {code};') xkb_keycodes.append(f'<{code}> = {code};')
template_path = os.path.join(get_data_path(), 'xkb_keycodes_template') template_path = get_data_path('xkb_keycodes_template')
with open(template_path, 'r') as template_file: with open(template_path, 'r') as template_file:
template = template_file.read() template = template_file.read()
@ -183,7 +183,7 @@ def generate_symbols_file_content(device, preset, mappings):
logger.error(f'Unknown keycode <{code}> for "{character}"') logger.error(f'Unknown keycode <{code}> for "{character}"')
xkb_symbols.append(f'key <{code}> {{ [ {character} ] }};') xkb_symbols.append(f'key <{code}> {{ [ {character} ] }};')
template_path = os.path.join(get_data_path(), 'xkb_symbols_template') template_path = get_data_path('xkb_symbols_template')
with open(template_path, 'r') as template_file: with open(template_path, 'r') as template_file:
template = template_file.read() template = template_file.read()

View File

@ -27,7 +27,7 @@ import site
import pkg_resources import pkg_resources
def get_data_path(): def get_data_path(filename=''):
"""Depending on the installation prefix, return the data dir.""" """Depending on the installation prefix, return the data dir."""
source_path = pkg_resources.require('keymapper')[0].location source_path = pkg_resources.require('keymapper')[0].location
@ -44,4 +44,4 @@ def get_data_path():
# installed with -e, running from the cloned git source # installed with -e, running from the cloned git source
data_path = os.path.join(source_path, 'data') data_path = os.path.join(source_path, 'data')
return data_path return os.path.join(data_path, filename)

View File

@ -29,11 +29,25 @@ import os
import subprocess import subprocess
# should not contain spaces
SYMBOLS_PATH = '/usr/share/X11/xkb/symbols/key-mapper' SYMBOLS_PATH = '/usr/share/X11/xkb/symbols/key-mapper'
KEYCODES_PATH = '/usr/share/X11/xkb/keycodes/key-mapper' KEYCODES_PATH = '/usr/share/X11/xkb/keycodes/key-mapper'
# since this needs to run as sudo, # since this needs to run as sudo,
# get the home dir of the user who called sudo. # get the home dir of the user who called sudo.
who = subprocess.check_output('who').decode().split()[0] who = subprocess.check_output('who').decode().split()[0]
CONFIG_PATH = os.path.join('/home', who, '.config/key-mapper') CONFIG_PATH = os.path.join('/home', who, '.config/key-mapper')
def get_home_path(device, preset=''):
"""Get the path to the config file in /usr."""
return os.path.join(CONFIG_PATH, device, preset)
def get_usr_path(device, preset=''):
"""Get the path to the config file in /usr."""
return os.path.join(
SYMBOLS_PATH,
device.replace(' ', '_'),
preset.replace(' ', '_')
)