From 72f9cb0e0a4fcbd6ff46cb5be5bedccb6dc1bcc6 Mon Sep 17 00:00:00 2001 From: Alex Martens Date: Mon, 26 May 2025 10:02:19 -0700 Subject: [PATCH] ruff: add flake8-logging-format --- docs/conf.py | 2 +- monitorcontrol/vcp/vcp_codes.py | 22 ++++++++++---------- monitorcontrol/vcp/vcp_linux.py | 34 +++++++++++++++++++++++-------- monitorcontrol/vcp/vcp_windows.py | 12 +++++++---- pyproject.toml | 4 ++++ 5 files changed, 50 insertions(+), 24 deletions(-) diff --git a/docs/conf.py b/docs/conf.py index 66eb637..989bea6 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -30,7 +30,7 @@ project = pyproject["project"]["name"] year = datetime.datetime.now().year author = pyproject["project"]["authors"][0]["name"] -copyright = f"2019 - {year}, {author}" +copyright = f"2019 - {year}, {author}" # noqa: A001 version = pyproject["project"]["version"] release = pyproject["project"]["version"] language = "en" diff --git a/monitorcontrol/vcp/vcp_codes.py b/monitorcontrol/vcp/vcp_codes.py index ca8d640..e659997 100644 --- a/monitorcontrol/vcp/vcp_codes.py +++ b/monitorcontrol/vcp/vcp_codes.py @@ -6,17 +6,17 @@ class VCPCode: Args: name: VCP code name. value: VCP code value. - type: VCP code type. + code_type: VCP code type. function: VCP code function. Raises: KeyError: VCP code not found. """ - def __init__(self, name: str, value: int, type: str, function: str): + def __init__(self, name: str, value: int, code_type: str, function: str): self.name = name self.value = value - self.type = type + self.type = code_type self.function = function def __str__(self) -> str: @@ -47,48 +47,48 @@ def writeable(self) -> bool: image_factory_default = VCPCode( name="restore factory default image", value=0x04, - type="wo", + code_type="wo", function="nc", ) image_luminance = VCPCode( name="image luminance", value=0x10, - type="rw", + code_type="rw", function="c", ) image_contrast = VCPCode( name="image contrast", value=0x12, - type="rw", + code_type="rw", function="c", ) image_color_preset = VCPCode( name="image color preset", value=0x14, - type="rw", + code_type="rw", function="nc", ) active_control = VCPCode( name="active control", value=0x52, - type="ro", + code_type="ro", function="nc", ) input_select = VCPCode( name="input select", value=0x60, - type="rw", + code_type="rw", function="nc", ) image_orientation = VCPCode( name="image orientation", value=0xAA, - type="ro", + code_type="ro", function="nc", ) display_power_mode = VCPCode( name="display power mode", value=0xD6, - type="rw", + code_type="rw", function="nc", ) diff --git a/monitorcontrol/vcp/vcp_linux.py b/monitorcontrol/vcp/vcp_linux.py index 49521a1..c649e47 100644 --- a/monitorcontrol/vcp/vcp_linux.py +++ b/monitorcontrol/vcp/vcp_linux.py @@ -124,7 +124,10 @@ def set_vcp_feature(self, code: int, value: int): data.append(self.get_checksum(bytearray([self.DDCCI_ADDR << 1]) + data)) # write data - self.logger.debug("data=" + " ".join([f"{x:02X}" for x in data])) + self.logger.debug( + "data=", + extra=dict(data=" ".join([f"{x:02X}" for x in data])), + ) self.write_bytes(data) # store time of last set VCP @@ -156,18 +159,27 @@ def get_vcp_feature(self, code: int) -> Tuple[int, int]: data.append(self.get_checksum(bytearray([self.DDCCI_ADDR << 1]) + data)) # write data - self.logger.debug("data=" + " ".join([f"{x:02X}" for x in data])) + self.logger.debug( + "data=", + extra=dict(data=" ".join([f"{x:02X}" for x in data])), + ) self.write_bytes(data) time.sleep(self.GET_VCP_TIMEOUT) # read the data header = self.read_bytes(self.GET_VCP_HEADER_LENGTH) - self.logger.debug("header=" + " ".join([f"{x:02X}" for x in header])) + self.logger.debug( + "header={header}", + extra=dict(header=" ".join([f"{x:02X}" for x in header])), + ) source, length = struct.unpack("=BB", header) length &= ~self.PROTOCOL_FLAG # clear protocol flag payload = self.read_bytes(length + 1) - self.logger.debug("payload=" + " ".join([f"{x:02X}" for x in payload])) + self.logger.debug( + "payload={payload}", + extra=dict(payload=" ".join([f"{x:02X}" for x in payload])), + ) # check checksum payload, checksum = struct.unpack(f"={length}sB", payload) @@ -206,7 +218,7 @@ def get_vcp_feature(self, code: int) -> Tuple[int, int]: return feature_current, feature_max - def get_vcp_capabilities(self): + def get_vcp_capabilities(self) -> str: """ Gets capabilities string from the virtual control panel. @@ -255,11 +267,17 @@ def get_vcp_capabilities(self): # read the data header = self.read_bytes(self.GET_VCP_HEADER_LENGTH) - self.logger.debug("header=" + " ".join([f"{x:02X}" for x in header])) + self.logger.debug( + "header={header}", + extra=dict(header=" ".join([f"{x:02X}" for x in header])), + ) source, length = struct.unpack("BB", header) length &= ~self.PROTOCOL_FLAG # clear protocol flag payload = self.read_bytes(length + 1) - self.logger.debug("payload=" + " ".join([f"{x:02X}" for x in payload])) + self.logger.debug( + "payload={payload}", + extra=dict(payload=" ".join([f"{x:02X}" for x in payload])), + ) # check if length is valid if length < 3 or length > 35: @@ -297,7 +315,7 @@ def get_vcp_capabilities(self): # update the offset and go again offset += length - self.logger.debug(f"caps str={caps_str}") + self.logger.debug("caps str={caps_str}", extra=dict(caps_str=caps_str)) if loop_count >= loop_count_limit: raise VCPIOError("Capabilities string incomplete or too long") diff --git a/monitorcontrol/vcp/vcp_windows.py b/monitorcontrol/vcp/vcp_windows.py index 3185d6b..fb37e2a 100644 --- a/monitorcontrol/vcp/vcp_windows.py +++ b/monitorcontrol/vcp/vcp_windows.py @@ -106,7 +106,10 @@ def set_vcp_feature(self, code: int, value: int): Raises: VCPError: Failed to set VCP feature. """ - self.logger.debug(f"SetVCPFeature(_, {code=}, {value=})") + self.logger.debug( + "SetVCPFeature(_, {code=}, {value=})", + extra=dict(code=code, value=value), + ) try: if not ctypes.windll.dxva2.SetVCPFeature( HANDLE(self.handle), BYTE(code), DWORD(value) @@ -131,7 +134,8 @@ def get_vcp_feature(self, code: int) -> Tuple[int, int]: feature_current = DWORD() feature_max = DWORD() self.logger.debug( - f"GetVCPFeatureAndVCPFeatureReply(_, {code=}, None, _, _)" + "GetVCPFeatureAndVCPFeatureReply(_, {code=}, None, _, _)", + extra=dict(code=code), ) try: if not ctypes.windll.dxva2.GetVCPFeatureAndVCPFeatureReply( @@ -145,8 +149,8 @@ def get_vcp_feature(self, code: int) -> Tuple[int, int]: except OSError as e: raise VCPError("failed to get VCP feature") from e self.logger.debug( - "GetVCPFeatureAndVCPFeatureReply -> " - f"({feature_current.value}, {feature_max.value})" + "GetVCPFeatureAndVCPFeatureReply -> ({feat_cur}, {feat_max})", + extra=dict(feat_cur=feature_current.value, feat_max=feature_max.value), ) return feature_current.value, feature_max.value diff --git a/pyproject.toml b/pyproject.toml index b460778..59620cd 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -39,10 +39,14 @@ packages = ["monitorcontrol"] [tool.ruff.lint] extend-select = [ + # flake8-builtins + "A", # flake8-unused-arguments "ARG", # flake8-bugbear "B", + # flake8-logging-format + "G", # pep8-naming "N", ]