From ba7f282dffd41af830cdf3e1cd42333abfe348c4 Mon Sep 17 00:00:00 2001 From: sezanzeb Date: Sun, 29 Nov 2020 01:53:12 +0100 Subject: [PATCH] lint --- keymapper/daemon.py | 20 ++++++++++++- keymapper/dev/macros.py | 66 ++++++++++++++++++++++------------------- 2 files changed, 55 insertions(+), 31 deletions(-) diff --git a/keymapper/daemon.py b/keymapper/daemon.py index 5f7da07c..d06932bc 100644 --- a/keymapper/daemon.py +++ b/keymapper/daemon.py @@ -22,6 +22,8 @@ """Starts injecting keycodes based on the configuration.""" +import subprocess + import dbus from dbus import service import dbus.mainloop.glib @@ -32,8 +34,24 @@ from keymapper.mapping import Mapping from keymapper.config import config +def is_service_running(): + """Check if the daemon is running.""" + try: + subprocess.check_output(['pgrep', '-f', 'key-mapper-service']) + except subprocess.CalledProcessError: + return False + return True + + def get_dbus_interface(): """Get an interface to start and stop injecting keystrokes.""" + if not is_service_running(): + logger.warning( + 'The daemon "key-mapper-service" is not running, mapping keys ' + 'only works as long as the window is open.' + ) + return Daemon(autoload=False) + try: bus = dbus.SessionBus() remote_object = bus.get_object('keymapper.Control', '/') @@ -45,7 +63,7 @@ def get_dbus_interface(): 'keys only works as long as the window is open.' ) logger.debug(error) - return Daemon() + return Daemon(autoload=False) return interface diff --git a/keymapper/dev/macros.py b/keymapper/dev/macros.py index 233b6545..34663fe9 100644 --- a/keymapper/dev/macros.py +++ b/keymapper/dev/macros.py @@ -72,7 +72,7 @@ class _Macro: async def run(self): """Run the macro.""" - for i, (_, task) in enumerate(self.tasks): + for _, task in self.tasks: coroutine = task() if asyncio.iscoroutine(coroutine): await coroutine @@ -168,6 +168,30 @@ def _extract_params(inner): return params +def _count_brackets(macro): + """Find where the first opening bracket closes.""" + brackets = 0 + position = 0 + for char in macro: + position += 1 + if char == '(': + brackets += 1 + continue + + if char == ')': + brackets -= 1 + if brackets < 0: + raise Exception(f'There is one ")" too much at {position}') + if brackets == 0: + # the closing bracket of the call + break + + if brackets != 0: + raise Exception(f'There are {brackets} closing brackets missing') + + return brackets, position + + def _parse_recurse(macro, handler, macro_instance=None, depth=0): """Handle a subset of the macro, e.g. one parameter or function call. @@ -215,25 +239,7 @@ def _parse_recurse(macro, handler, macro_instance=None, depth=0): raise Exception(f'Unknown function {call}') # get all the stuff inbetween - brackets = 0 - position = 0 - for char in macro: - position += 1 - - if char == '(': - brackets += 1 - continue - - if char == ')': - brackets -= 1 - if brackets < 0: - raise Exception(f'There is one ")" too much at {position}') - if brackets == 0: - # the closing bracket of the call - break - - if brackets != 0: - raise Exception(f'There are {brackets} closing brackets missing') + brackets, position = _count_brackets(macro) inner = macro[2:position - 1] @@ -256,14 +262,14 @@ def _parse_recurse(macro, handler, macro_instance=None, depth=0): _parse_recurse(chain, handler, macro_instance, depth) return macro_instance - else: - # probably a parameter for an outer function - try: - macro = int(macro) - except ValueError: - pass - logger.spam('%s%s %s', space, type(macro), macro) - return macro + + # probably a parameter for an outer function + try: + macro = int(macro) + except ValueError: + pass + logger.spam('%s%s %s', space, type(macro), macro) + return macro def parse(macro, handler): @@ -286,6 +292,6 @@ def parse(macro, handler): logger.spam('preparing macro %s for later execution', macro) try: return _parse_recurse(macro, handler) - except Exception as e: - logger.error('Failed to parse macro "%s": %s', macro, e) + except Exception as error: + logger.error('Failed to parse macro "%s": %s', macro, error) return None