From 5bcafb5beee2fef0e44243f8053ce90bc511a029 Mon Sep 17 00:00:00 2001 From: Peter Hutterer Date: Fri, 28 Jul 2017 17:46:35 +1000 Subject: [PATCH] Reduce to one special->special string mapping dictionary No need to have two copies here, simply have the dict and then flip it so it works with the strings as keys too. The slightly odd list comprehension is because we can't add to a dictionary while iterating through it, so we need to get everything out first. Signed-off-by: Peter Hutterer --- tools/ratbagctl.in | 70 +++++++++++++++++----------------------------- 1 file changed, 26 insertions(+), 44 deletions(-) diff --git a/tools/ratbagctl.in b/tools/ratbagctl.in index 466286e3..0c720e8f 100755 --- a/tools/ratbagctl.in +++ b/tools/ratbagctl.in @@ -29,6 +29,30 @@ import argparse from ratbagd import Ratbagd, RatbagdDevice, RatbagdProfile, RatbagdResolution, RatbagdButton, RatbagdLed, RatbagdDBusUnavailable # NOQA +button_specials_strmap = { + RatbagdButton.ACTION_SPECIAL_UNKNOWN: "unknown", + RatbagdButton.ACTION_SPECIAL_DOUBLECLICK: "doubleclick", + RatbagdButton.ACTION_SPECIAL_WHEEL_LEFT: "wheel-left", + RatbagdButton.ACTION_SPECIAL_WHEEL_RIGHT: "wheel-right", + RatbagdButton.ACTION_SPECIAL_WHEEL_UP: "wheel-up", + RatbagdButton.ACTION_SPECIAL_WHEEL_DOWN: "wheel-down", + RatbagdButton.ACTION_SPECIAL_RATCHET_MODE_SWITCH: "ratchet-mode-switch", + RatbagdButton.ACTION_SPECIAL_RESOLUTION_CYCLE_UP: "resolution-cycle-up", + RatbagdButton.ACTION_SPECIAL_RESOLUTION_CYCLE_DOWN: "resolution-cycle-down", + RatbagdButton.ACTION_SPECIAL_RESOLUTION_UP: "resolution-up", + RatbagdButton.ACTION_SPECIAL_RESOLUTION_DOWN: "resolution-down", + RatbagdButton.ACTION_SPECIAL_RESOLUTION_ALTERNATE: "resolution-alternate", + RatbagdButton.ACTION_SPECIAL_RESOLUTION_DEFAULT: "resolution-default", + RatbagdButton.ACTION_SPECIAL_PROFILE_CYCLE_UP: "profile-cycle-up", + RatbagdButton.ACTION_SPECIAL_PROFILE_CYCLE_DOWN: "profile-cycle-down", + RatbagdButton.ACTION_SPECIAL_PROFILE_UP: "profile-up", + RatbagdButton.ACTION_SPECIAL_PROFILE_DOWN: "profile-down", + RatbagdButton.ACTION_SPECIAL_SECOND_MODE: "second-mode", + RatbagdButton.ACTION_SPECIAL_BATTERY_LEVEL: "battery-level", +} +for k, v in [(v, k) for k, v in button_specials_strmap.items()]: + button_specials_strmap[k] = v + def list_devices(r, args): for d in r.devices: @@ -166,27 +190,6 @@ def print_button(d, p, b, level): RatbagdButton.TYPE_PROFILE_UP: "profile-up", RatbagdButton.TYPE_PROFILE_DOWN: "profile-down", } - specials = { - RatbagdButton.ACTION_SPECIAL_UNKNOWN: "unknown", - RatbagdButton.ACTION_SPECIAL_DOUBLECLICK: "doubleclick", - RatbagdButton.ACTION_SPECIAL_WHEEL_LEFT: "wheel-left", - RatbagdButton.ACTION_SPECIAL_WHEEL_RIGHT: "wheel-right", - RatbagdButton.ACTION_SPECIAL_WHEEL_UP: "wheel-up", - RatbagdButton.ACTION_SPECIAL_WHEEL_DOWN: "wheel-down", - RatbagdButton.ACTION_SPECIAL_RATCHET_MODE_SWITCH: "ratchet-mode-switch", - RatbagdButton.ACTION_SPECIAL_RESOLUTION_CYCLE_UP: "resolution-cycle-up", - RatbagdButton.ACTION_SPECIAL_RESOLUTION_CYCLE_DOWN: "resolution-cycle-down", - RatbagdButton.ACTION_SPECIAL_RESOLUTION_UP: "resolution-up", - RatbagdButton.ACTION_SPECIAL_RESOLUTION_DOWN: "resolution-down", - RatbagdButton.ACTION_SPECIAL_RESOLUTION_ALTERNATE: "resolution-alternate", - RatbagdButton.ACTION_SPECIAL_RESOLUTION_DEFAULT: "resolution-default", - RatbagdButton.ACTION_SPECIAL_PROFILE_CYCLE_UP: "profile-cycle-up", - RatbagdButton.ACTION_SPECIAL_PROFILE_CYCLE_DOWN: "profile-cycle-down", - RatbagdButton.ACTION_SPECIAL_PROFILE_UP: "profile-up", - RatbagdButton.ACTION_SPECIAL_PROFILE_DOWN: "profile-down", - RatbagdButton.ACTION_SPECIAL_SECOND_MODE: "second-mode", - RatbagdButton.ACTION_SPECIAL_BATTERY_LEVEL: "battery-level", - } t = types[b.type] header = " " * level + "Button: {} type {} is mapped to ".format(b.index, t) @@ -200,7 +203,7 @@ def print_button(d, p, b, level): print("{}'{}'".format(header, bmap)) elif b.action_type == RatbagdButton.ACTION_TYPE_SPECIAL: - print("{}'{}'".format(header, specials[b.special])) + print("{}'{}'".format(header, button_specials_strmap[b.special])) elif b.action_type == RatbagdButton.ACTION_TYPE_MACRO: bmap = "" for t, v in b.macro: @@ -377,28 +380,7 @@ def func_button_action_set_special(r, args): except AttributeError: pass else: - specials = { - "unknown": RatbagdButton.ACTION_SPECIAL_UNKNOWN, - "doubleclick": RatbagdButton.ACTION_SPECIAL_DOUBLECLICK, - "wheel-left": RatbagdButton.ACTION_SPECIAL_WHEEL_LEFT, - "wheel-right": RatbagdButton.ACTION_SPECIAL_WHEEL_RIGHT, - "wheel-up": RatbagdButton.ACTION_SPECIAL_WHEEL_UP, - "wheel-down": RatbagdButton.ACTION_SPECIAL_WHEEL_DOWN, - "ratchet-mode-switch": RatbagdButton.ACTION_SPECIAL_RATCHET_MODE_SWITCH, - "resolution-cycle-up": RatbagdButton.ACTION_SPECIAL_RESOLUTION_CYCLE_UP, - "resolution-cycle-down": RatbagdButton.ACTION_SPECIAL_RESOLUTION_CYCLE_DOWN, - "resolution-up": RatbagdButton.ACTION_SPECIAL_RESOLUTION_UP, - "resolution-down": RatbagdButton.ACTION_SPECIAL_RESOLUTION_DOWN, - "resolution-alternate": RatbagdButton.ACTION_SPECIAL_RESOLUTION_ALTERNATE, - "resolution-default": RatbagdButton.ACTION_SPECIAL_RESOLUTION_DEFAULT, - "profile-cycle-up": RatbagdButton.ACTION_SPECIAL_PROFILE_CYCLE_UP, - "profile-cycle-down": RatbagdButton.ACTION_SPECIAL_PROFILE_CYCLE_DOWN, - "profile-up": RatbagdButton.ACTION_SPECIAL_PROFILE_UP, - "profile-down": RatbagdButton.ACTION_SPECIAL_PROFILE_DOWN, - "second-mode": RatbagdButton.ACTION_SPECIAL_SECOND_MODE, - "battery-level": RatbagdButton.ACTION_SPECIAL_BATTERY_LEVEL, - } - b.special = specials[special] + b.special = button_specials_strmap[special] d.commit()