Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrote string concatenation/format with f strings #2597

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/hidapi/hidconsole.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def strhex(d):
def _print(marker, data, scroll=False):
t = time.time() - start_time
if isinstance(data, str):
s = marker + " " + data
s = f"{marker} {data}"
else:
hexs = strhex(data)
s = "%s (% 8.3f) [%s %s %s %s] %s" % (marker, t, hexs[0:2], hexs[2:4], hexs[4:8], hexs[8:], repr(data))
Expand Down Expand Up @@ -86,7 +86,7 @@ def _continuous_read(handle, timeout=2000):
try:
reply = hidapi.read(handle, 128, timeout)
except OSError as e:
_error("Read failed, aborting: " + str(e), True)
_error(f"Read failed, aborting: {str(e)}", True)
break
assert reply is not None
if reply:
Expand All @@ -97,7 +97,7 @@ def _validate_input(line, hidpp=False):
try:
data = unhexlify(line.encode("ascii"))
except Exception as e:
_error("Invalid input: " + str(e))
_error(f"Invalid input: {str(e)}")
return None

if hidpp:
Expand Down
6 changes: 3 additions & 3 deletions lib/keysyms/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ def main():
for name, sym, uni in findall(xf86pattern, text):
sym = int(sym, 16)
uni = int(uni, 16) if uni else None
if keysymdef.get("XF86_" + name, None):
print("KEY DUP", "XF86_" + name)
keysymdef["XF86_" + name] = sym
if keysymdef.get(f"XF86_{name}", None):
print("KEY DUP", f"XF86_{name}")
keysymdef[f"XF86_{name}"] = sym

with open("keysymdef.py", "w") as f:
f.write("# flake8: noqa\nkeysymdef = \\\n")
Expand Down
4 changes: 2 additions & 2 deletions lib/logitech_receiver/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ def __eq__(self, other):
return self.name.lower() == other.lower()
# this should catch comparisons with bytes in Py3
if other is not None:
raise TypeError("Unsupported type " + str(type(other)))
raise TypeError(f"Unsupported type {str(type(other))}")

def __ne__(self, other):
return not self.__eq__(other)
Expand Down Expand Up @@ -460,7 +460,7 @@ def __getitem__(self, index):
def __setitem__(self, index, name):
assert isinstance(index, int), type(index)
if isinstance(name, NamedInt):
assert int(index) == int(name), repr(index) + " " + repr(name)
assert int(index) == int(name), f"{repr(index)} {repr(name)}"
value = name
elif isinstance(name, str):
value = NamedInt(index, name)
Expand Down
4 changes: 2 additions & 2 deletions lib/logitech_receiver/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,14 +225,14 @@ def codename(self):
self._codename = codename
elif self.protocol < 2.0:
self._codename = "? (%s)" % (self.wpid or self.product_id)
return self._codename or "?? (%s)" % (self.wpid or self.product_id)
return self._codename or f"?? ({self.wpid or self.product_id})"

@property
def name(self):
if not self._name:
if self.online and self.protocol >= 2.0:
self._name = _hidpp20.get_name(self)
return self._name or self._codename or "Unknown device %s" % (self.wpid or self.product_id)
return self._name or self._codename or f"Unknown device {self.wpid or self.product_id}"

def get_ids(self):
ids = _hidpp20.get_ids(self)
Expand Down
34 changes: 17 additions & 17 deletions lib/logitech_receiver/diversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ def __init__(self, args, source=None, warn=True):
self.source = source

def __str__(self):
source = "(" + self.source + ")" if self.source else ""
source = f"({self.source})" if self.source else ""
return f"Rule{source}[{', '.join([c.__str__() for c in self.components])}]"

def evaluate(self, feature, notification, device, last_result):
Expand Down Expand Up @@ -553,7 +553,7 @@ def __init__(self, op, warn=True):
self.component = self.compile(op)

def __str__(self):
return "Not: " + str(self.component)
return f"Not: {str(self.component)}"

def evaluate(self, feature, notification, device, last_result):
if logger.isEnabledFor(logging.DEBUG):
Expand Down Expand Up @@ -672,7 +672,7 @@ def __init__(self, process, warn=True):
self.process = str(process)

def __str__(self):
return "Process: " + str(self.process)
return f"Process: {str(self.process)}"

def evaluate(self, feature, notification, device, last_result):
if logger.isEnabledFor(logging.DEBUG):
Expand Down Expand Up @@ -703,7 +703,7 @@ def __init__(self, process, warn=True):
self.process = str(process)

def __str__(self):
return "MouseProcess: " + str(self.process)
return f"MouseProcess: {str(self.process)}"

def evaluate(self, feature, notification, device, last_result):
if logger.isEnabledFor(logging.DEBUG):
Expand All @@ -727,7 +727,7 @@ def __init__(self, feature, warn=True):
self.feature = FEATURE[feature]

def __str__(self):
return "Feature: " + str(self.feature)
return f"Feature: {str(self.feature)}"

def evaluate(self, feature, notification, device, last_result):
if logger.isEnabledFor(logging.DEBUG):
Expand All @@ -748,7 +748,7 @@ def __init__(self, report, warn=True):
self.report = report

def __str__(self):
return "Report: " + str(self.report)
return f"Report: {str(self.report)}"

def evaluate(self, report, notification, device, last_result):
if logger.isEnabledFor(logging.DEBUG):
Expand Down Expand Up @@ -821,7 +821,7 @@ def __init__(self, modifiers, warn=True):
logger.warning("unknown rule Modifier value: %s", k)

def __str__(self):
return "Modifiers: " + str(self.desired)
return f"Modifiers: {str(self.desired)}"

def evaluate(self, feature, notification, device, last_result):
if logger.isEnabledFor(logging.DEBUG):
Expand Down Expand Up @@ -967,7 +967,7 @@ def __init__(self, test, warn=True):
logger.warning("rule Test argument not valid %s", test)

def __str__(self):
return "Test: " + str(self.test)
return f"Test: {str(self.test)}"

def evaluate(self, feature, notification, device, last_result):
if logger.isEnabledFor(logging.DEBUG):
Expand Down Expand Up @@ -995,7 +995,7 @@ def __init__(self, test, warn=True):
logger.warning("rule TestBytes argument not valid %s", test)

def __str__(self):
return "TestBytes: " + str(self.test)
return f"TestBytes: {str(self.test)}"

def evaluate(self, feature, notification, device, last_result):
if logger.isEnabledFor(logging.DEBUG):
Expand Down Expand Up @@ -1070,7 +1070,7 @@ def __init__(self, devID, warn=True):
self.devID = devID

def __str__(self):
return "Active: " + str(self.devID)
return f"Active: {str(self.devID)}"

def evaluate(self, feature, notification, device, last_result):
if logger.isEnabledFor(logging.DEBUG):
Expand All @@ -1091,7 +1091,7 @@ def __init__(self, devID, warn=True):
self.devID = devID

def __str__(self):
return "Device: " + str(self.devID)
return f"Device: {str(self.devID)}"

def evaluate(self, feature, notification, device, last_result):
if logger.isEnabledFor(logging.DEBUG):
Expand All @@ -1111,7 +1111,7 @@ def __init__(self, host, warn=True):
self.host = host

def __str__(self):
return "Host: " + str(self.host)
return f"Host: {str(self.host)}"

def evaluate(self, feature, notification, device, last_result):
if logger.isEnabledFor(logging.DEBUG):
Expand Down Expand Up @@ -1389,7 +1389,7 @@ def __init__(self, args, warn=True):
self.components = self.rule.components

def __str__(self):
return "Later: [" + str(self.delay) + ", " + ", ".join(str(c) for c in self.components) + "]"
return f"Later: [{str(self.delay)}, " + ", ".join(str(c) for c in self.components) + "]"

def evaluate(self, feature, notification, device, last_result):
if self.delay and self.rule:
Expand Down Expand Up @@ -1482,18 +1482,18 @@ def process_notification(device, notification, feature):
new_g_keys_down = struct.unpack("<I", notification.data[:4])[0]
for i in range(32):
if new_g_keys_down & (0x01 << i) and not g_keys_down & (0x01 << i):
key_down = CONTROL["G" + str(i + 1)]
key_down = CONTROL[f"G{str(i + 1)}"]
if g_keys_down & (0x01 << i) and not new_g_keys_down & (0x01 << i):
key_up = CONTROL["G" + str(i + 1)]
key_up = CONTROL[f"G{str(i + 1)}"]
g_keys_down = new_g_keys_down
# and also M keys down
elif feature == FEATURE.MKEYS and notification.address == 0x00:
new_m_keys_down = struct.unpack("!1B", notification.data[:1])[0]
for i in range(1, 9):
if new_m_keys_down & (0x01 << (i - 1)) and not m_keys_down & (0x01 << (i - 1)):
key_down = CONTROL["M" + str(i)]
key_down = CONTROL[f"M{str(i)}"]
if m_keys_down & (0x01 << (i - 1)) and not new_m_keys_down & (0x01 << (i - 1)):
key_up = CONTROL["M" + str(i)]
key_up = CONTROL[f"M{str(i)}"]
m_keys_down = new_m_keys_down
# and also MR key
elif feature == FEATURE.MR and notification.address == 0x00:
Expand Down
2 changes: 1 addition & 1 deletion lib/logitech_receiver/hidpp10.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def get_firmware(self, device: Device):
fw_version = f"{fw_version[0:2]}.{fw_version[2:4]}"
reply = read_register(device, Registers.FIRMWARE, 0x02)
if reply:
fw_version += ".B" + common.strhex(reply[1:3])
fw_version += f".B{common.strhex(reply[1:3])}"
fw = common.FirmwareInfo(FIRMWARE_KIND.Firmware, "", fw_version, None)
firmware[0] = fw

Expand Down
22 changes: 11 additions & 11 deletions lib/logitech_receiver/hidpp20.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,23 +393,23 @@ def action(self):
if self.actionId == special_keys.ACTIONID.Empty:
return None
elif self.actionId == special_keys.ACTIONID.Key:
return "Key: " + str(self.modifiers) + str(self.remapped)
return f"Key: {str(self.modifiers)}{str(self.remapped)}"
elif self.actionId == special_keys.ACTIONID.Mouse:
return "Mouse Button: " + str(self.remapped)
return f"Mouse Button: {str(self.remapped)}"
elif self.actionId == special_keys.ACTIONID.Xdisp:
return "X Displacement " + str(self.remapped)
return f"X Displacement {str(self.remapped)}"
elif self.actionId == special_keys.ACTIONID.Ydisp:
return "Y Displacement " + str(self.remapped)
return f"Y Displacement {str(self.remapped)}"
elif self.actionId == special_keys.ACTIONID.Vscroll:
return "Vertical Scroll " + str(self.remapped)
return f"Vertical Scroll {str(self.remapped)}"
elif self.actionId == special_keys.ACTIONID.Hscroll:
return "Horizontal Scroll: " + str(self.remapped)
return f"Horizontal Scroll: {str(self.remapped)}"
elif self.actionId == special_keys.ACTIONID.Consumer:
return "Consumer: " + str(self.remapped)
return f"Consumer: {str(self.remapped)}"
elif self.actionId == special_keys.ACTIONID.Internal:
return "Internal Action " + str(self.remapped)
return f"Internal Action {str(self.remapped)}"
elif self.actionId == special_keys.ACTIONID.Internal:
return "Power " + str(self.remapped)
return f"Power {str(self.remapped)}"
else:
return "Unknown"

Expand Down Expand Up @@ -1158,7 +1158,7 @@ def to_bytes(self):
def __repr__(self):
return "%s{%s}" % (
self.__class__.__name__,
", ".join([str(key) + ":" + str(val) for key, val in self.__dict__.items()]),
", ".join([f"{str(key)}:{str(val)}" for key, val in self.__dict__.items()]),
)


Expand Down Expand Up @@ -1724,7 +1724,7 @@ def get_polling_rate(self, device: Device):
state = device.feature_request(FEATURE.REPORT_RATE, 0x10)
if state:
rate = struct.unpack("!B", state[:1])[0]
return str(rate) + "ms"
return f"{str(rate)}ms"
else:
rates = ["8ms", "4ms", "2ms", "1ms", "500us", "250us", "125us"]
state = device.feature_request(FEATURE.EXTENDED_ADJUSTABLE_REPORT_RATE, 0x20)
Expand Down
2 changes: 1 addition & 1 deletion lib/logitech_receiver/listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def __init__(self, receiver, notifications_callback):
path_name = receiver.path.split("/")[2]
except IndexError:
path_name = receiver.path
super().__init__(name=self.__class__.__name__ + ":" + path_name)
super().__init__(name=f"{self.__class__.__name__}:{path_name}")
self.daemon = True
self._active = False
self.receiver = receiver
Expand Down
2 changes: 1 addition & 1 deletion lib/logitech_receiver/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ def _process_hidpp10_notification(device, n):
if n.sub_id == Notification.DJ_PAIRING: # device connection (and disconnection)
flags = ord(n.data[:1]) & 0xF0
if n.address == 0x02: # very old 27 MHz protocol
wpid = "00" + common.strhex(n.data[2:3])
wpid = f"00{common.strhex(n.data[2:3])}"
link_established = True
link_encrypted = bool(flags & 0x80)
elif n.address > 0x00: # all other protocols are supposed to be almost the same
Expand Down
2 changes: 1 addition & 1 deletion lib/logitech_receiver/notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def device_icon_list(name="_", kind=None):
icon_list += ("input-mouse",)
elif str(kind) == "headset":
icon_list += ("audio-headphones", "audio-headset")
icon_list += ("input-" + str(kind),)
icon_list += (f"input-{str(kind)}",)
_ICON_LISTS[name] = icon_list
return icon_list

Expand Down
2 changes: 1 addition & 1 deletion lib/logitech_receiver/receiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ def notification_information(self, number, notification):
online = True
encrypted = bool(notification.data[0] & 0x80)
kind = hidpp10_constants.DEVICE_KIND[_get_kind_from_index(self, number)]
wpid = "00" + notification.data[2:3].hex().upper()
wpid = f"00{notification.data[2:3].hex().upper()}"
return online, encrypted, wpid, kind

def device_pairing_information(self, number: int) -> dict:
Expand Down
4 changes: 2 additions & 2 deletions lib/logitech_receiver/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -842,7 +842,7 @@ def __init__(self, options, byte_count=None):
def to_string(self, value):
def element_to_string(key, val):
k = next((k for k in self.options if int(key) == k), None)
return str(k) + ":" + str(val) if k is not None else "?"
return f"{str(k)}:{str(val)}" if k is not None else "?"

return "{" + ", ".join([element_to_string(k, value[k]) for k in value]) + "}"

Expand Down Expand Up @@ -1107,7 +1107,7 @@ def __init__(
def to_string(self, value):
def element_to_string(key, val):
k, c = next(((k, c) for k, c in self.choices.items() if int(key) == k), (None, None))
return str(k) + ":" + str(c[val]) if k is not None else "?"
return f"{str(k)}:{str(c[val])}" if k is not None else "?"

return "{" + ", ".join([element_to_string(k, value[k]) for k in sorted(value)]) + "}"

Expand Down
8 changes: 4 additions & 4 deletions lib/logitech_receiver/settings_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -1140,11 +1140,11 @@ def _str_os_version(version):
if version == 0:
return ""
elif version & 0xFF:
return str(version >> 8) + "." + str(version & 0xFF)
return f"{str(version >> 8)}.{str(version & 0xFF)}"
else:
return str(version >> 8)

return "" if low == 0 and high == 0 else " " + _str_os_version(low) + "-" + _str_os_version(high)
return "" if low == 0 and high == 0 else f" {_str_os_version(low)}-{_str_os_version(high)}"

infos = device.feature_request(_F.MULTIPLATFORM)
assert infos, "Oops, multiplatform count cannot be retrieved!"
Expand Down Expand Up @@ -1200,7 +1200,7 @@ def build(cls, setting_class, device):
choices = common.NamedInts()
for host in range(0, numHosts):
paired, hostName = hostNames.get(host, (True, ""))
choices[host] = str(host + 1) + ":" + hostName if hostName else str(host + 1)
choices[host] = f"{str(host + 1)}:{hostName}" if hostName else str(host + 1)
return cls(choices=choices, read_skip_byte_count=1) if choices and len(choices) > 1 else None


Expand Down Expand Up @@ -1709,7 +1709,7 @@ def build(cls, setting_class, device):
key = (
setting_class.keys_universe[i]
if i in setting_class.keys_universe
else common.NamedInt(i, "KEY " + str(i))
else common.NamedInt(i, f"KEY {str(i)}")
)
choices_map[key] = setting_class.choices_universe
result = cls(choices_map) if choices_map else None
Expand Down
4 changes: 2 additions & 2 deletions lib/logitech_receiver/special_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,9 +314,9 @@
)

for i in range(1, 33): # add in G keys - these are not really Logitech Controls
CONTROL[0x1000 + i] = "G" + str(i)
CONTROL[0x1000 + i] = f"G{str(i)}"
for i in range(1, 9): # add in M keys - these are not really Logitech Controls
CONTROL[0x1100 + i] = "M" + str(i)
CONTROL[0x1100 + i] = f"M{str(i)}"
CONTROL[0x1200] = "MR" # add in MR key - this is not really a Logitech Control

CONTROL._fallback = lambda x: f"unknown:{x:04X}"
Expand Down
Loading
Loading