some pylint and tests

This commit is contained in:
sezanzeb 2020-11-27 00:34:45 +01:00 committed by sezanzeb
parent ccf1451acf
commit d3b971414a
7 changed files with 41 additions and 25 deletions

View File

@ -15,7 +15,7 @@
<g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11">
<text x="31.5" y="15" fill="#010101" fill-opacity=".3">coverage</text>
<text x="31.5" y="14">coverage</text>
<text x="80" y="15" fill="#010101" fill-opacity=".3">82%</text>
<text x="80" y="14">82%</text>
<text x="80" y="15" fill="#010101" fill-opacity=".3">83%</text>
<text x="80" y="14">83%</text>
</g>
</svg>

Before

Width:  |  Height:  |  Size: 904 B

After

Width:  |  Height:  |  Size: 904 B

View File

@ -17,7 +17,7 @@
<text x="22.0" y="14">pylint</text>
</g>
<g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11">
<text x="63.0" y="15" fill="#010101" fill-opacity=".3">9.66</text>
<text x="62.0" y="14">9.66</text>
<text x="63.0" y="15" fill="#010101" fill-opacity=".3">9.82</text>
<text x="62.0" y="14">9.82</text>
</g>
</svg>

Before

Width:  |  Height:  |  Size: 1.0 KiB

After

Width:  |  Height:  |  Size: 1.0 KiB

View File

@ -24,6 +24,7 @@
import subprocess
import dbus
from dbus import service
import dbus.mainloop.glib
@ -57,7 +58,7 @@ def get_dbus_interface():
remote_object = bus.get_object('keymapper.Control', '/')
interface = dbus.Interface(remote_object, 'keymapper.Interface')
logger.debug('Connected to dbus')
except Exception as error:
except dbus.exceptions.DBusException as error:
logger.error(
'Could not connect to the dbus of "key-mapper-service", mapping '
'keys only works as long as the window is open.'
@ -86,7 +87,11 @@ class Daemon(service.Object):
print(device, preset)
mapping = Mapping()
mapping.load(device, preset)
self.injectors[device] = KeycodeInjector(device, mapping)
try:
injector = KeycodeInjector(device, mapping)
self.injectors[device] = injector
except OSError as error:
logger.error(error)
super().__init__(*args, **kwargs)
@dbus.service.method(

View File

@ -40,8 +40,10 @@ def can_read_devices():
def warn(group):
logger.warning(
'Some devices may not be visible without being in the '
f'"{group}" user group. Try `sudo usermod -a -G {group} $USER` '
'and log out and back in.'
f'"%s" user group. Try `sudo usermod -a -G %s $USER` '
'and log out and back in.',
group,
group
)
if not is_root and not is_test:

View File

@ -109,6 +109,7 @@ class Row(Gtk.ListBoxRow):
self.get_style_context().remove_class('changed')
def on_character_input_change(self, _):
"""When the output character for that keycode is typed in."""
keycode = self.get_keycode()
character = self.get_character()

View File

@ -27,13 +27,17 @@ import logging
import pkg_resources
SPAM = 5
def spam(self, message, *args, **kws):
"""Log a more-verbose message than debug."""
# pylint: disable=protected-access
if self.isEnabledFor(SPAM):
# https://stackoverflow.com/a/13638084
self._log(SPAM, message, args, **kws)
SPAM = 5
logging.addLevelName(SPAM, "SPAM")
logging.Logger.spam = spam
@ -42,10 +46,11 @@ class Formatter(logging.Formatter):
"""Overwritten Formatter to print nicer logs."""
def format(self, record):
"""Overwritten format function."""
# pylint: disable=protected-access
debug = logger.level <= logging.DEBUG
if record.levelno == logging.INFO and not debug:
# if not launched with --debug, then don't print "INFO:"
self._style._fmt = '%(message)s' # pylint: disable=line-too-long
self._style._fmt = '%(message)s'
else:
# see https://en.wikipedia.org/wiki/ANSI_escape_code#3/4_bit
# for those numbers

View File

@ -30,19 +30,11 @@ from keymapper.logger import logger
from keymapper.paths import get_config_path
def update_reverse_mapping(func):
"""Generate a reverse mapping to optimize reverse lookups.
If _mapping contains `20: "a, A"` (the xkb syntax for modified keys),
reverse mapping will contain `"a": 20, "A": 20`.
"""
def keep_reverse_mapping_intact(func):
"""Decorator for Mapping.update_reverse_mapping."""
def wrapper(self, *args, **kwargs):
func(self, *args, **kwargs)
self._reverse_mapping = {}
for key, value in self._mapping.items():
for character in value.split(','):
self._reverse_mapping[character.strip()] = key
self.update_reverse_mapping()
return wrapper
@ -71,7 +63,18 @@ class Mapping:
def __len__(self):
return len(self._mapping)
@update_reverse_mapping
def update_reverse_mapping(self):
"""Generate a reverse mapping to optimize reverse lookups.
If _mapping contains `20: "a, A"` (the xkb syntax for modified keys),
reverse mapping will contain `"a": 20, "A": 20`.
"""
self._reverse_mapping = {}
for key, value in self._mapping.items():
for character in value.split(','):
self._reverse_mapping[character.strip()] = key
@keep_reverse_mapping_intact
def change(self, new_keycode, character, previous_keycode=None):
"""Replace the mapping of a keycode with a different one.
@ -110,7 +113,7 @@ class Mapping:
return False
@update_reverse_mapping
@keep_reverse_mapping_intact
def clear(self, keycode):
"""Remove a keycode from the mapping.
@ -122,13 +125,13 @@ class Mapping:
del self._mapping[keycode]
self.changed = True
@update_reverse_mapping
@keep_reverse_mapping_intact
def empty(self):
"""Remove all mappings."""
self._mapping = {}
self.changed = True
@update_reverse_mapping
@keep_reverse_mapping_intact
def load(self, device, preset):
"""Load a dumped JSON from home to overwrite the mappings."""
path = get_config_path(device, preset)