From e9dbb7de280904ffcb35907dedc4e4a310b861db Mon Sep 17 00:00:00 2001 From: Roald Ruiter Date: Fri, 28 Jun 2024 14:08:42 +0200 Subject: [PATCH] run ruff --- .pre-commit-hooks.yaml | 2 +- pre_commit_hooks/check_twincat_versions.py | 54 +++++++++++++------ .../tests/test_check_twincat_versions.py | 18 ++++--- 3 files changed, 50 insertions(+), 24 deletions(-) diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index 3fbcb99..7f37d01 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -63,6 +63,6 @@ description: Checks if TwinCAT versions match in different tsproj files, or if it matches the targeted one. entry: check-twincat-versions # All files need to be passed at once, else not all files are compared to eachother - require_serial: true + require_serial: true language: python files: .*\.tsproj$ diff --git a/pre_commit_hooks/check_twincat_versions.py b/pre_commit_hooks/check_twincat_versions.py index 7b2ca4f..9a274f4 100644 --- a/pre_commit_hooks/check_twincat_versions.py +++ b/pre_commit_hooks/check_twincat_versions.py @@ -1,38 +1,43 @@ import argparse import re import xml.etree.ElementTree as ET + from pre_commit_hooks.exceptions import PreCommitException def tc_version_pinned(xml_content: str) -> bool: root = ET.fromstring(xml_content) - return 'TcVersionFixed' in root.attrib and root.attrib.get('TcVersionFixed') == 'true' + return ( + "TcVersionFixed" in root.attrib and root.attrib.get("TcVersionFixed") == "true" + ) def get_tc_version(xml_content: str) -> str: root = ET.fromstring(xml_content) - return root.attrib.get('TcVersion') + return root.attrib.get("TcVersion") def fix_tc_version(xml_content: str, new_version: str) -> str: pattern = r'(TcVersion=")([^"]*)(")' - new_xml_content = re.sub(pattern, r'\g<1>' + new_version + r'\g<3>', xml_content) + new_xml_content = re.sub(pattern, r"\g<1>" + new_version + r"\g<3>", xml_content) return new_xml_content def fix_pinned_version(xml_content: str, pin_version: bool) -> str: - new_value = 'true' if pin_version else 'false' + new_value = "true" if pin_version else "false" pattern = r'(TcVersionFixed=")([^"]*)(")' if re.search(pattern, xml_content): - new_xml_content = re.sub(pattern, r'\g<1>' + new_value + r'\g<3>', xml_content) + new_xml_content = re.sub(pattern, r"\g<1>" + new_value + r"\g<3>", xml_content) else: version_pattern = r'(TcVersion="[^"]*")' - new_xml_content = re.sub(version_pattern, r'\g<1> TcVersionFixed="' + new_value + r'"', xml_content) + new_xml_content = re.sub( + version_pattern, r'\g<1> TcVersionFixed="' + new_value + r'"', xml_content + ) return new_xml_content @@ -40,43 +45,58 @@ def fix_pinned_version(xml_content: str, pin_version: bool) -> str: def main(args=None): if args is None: parser = argparse.ArgumentParser() - parser.add_argument('filenames', nargs='+', help='List of XML filenames to process') - parser.add_argument('--target-version', type=str, help='Target TcVersion to enforce') - parser.add_argument('--fix', action='store_true', help='Fix the versions if they do not match the target version') - parser.add_argument('--reason', type=str, help='Reason for pinning the version') + parser.add_argument( + "filenames", nargs="+", help="List of XML filenames to process" + ) + parser.add_argument( + "--target-version", type=str, help="Target TcVersion to enforce" + ) + parser.add_argument( + "--fix", + action="store_true", + help="Fix the versions if they do not match the target version", + ) + parser.add_argument("--reason", type=str, help="Reason for pinning the version") args = parser.parse_args() try: versions = {} for filename in args.filenames: - with open(filename, 'r') as file: + with open(filename, "r") as file: xml_content = file.read() version = get_tc_version(xml_content) versions[filename] = version itemize = "\n -" if args.target_version: - mismatched_files = [fname for fname, ver in versions.items() if ver != args.target_version] + mismatched_files = [ + fname for fname, ver in versions.items() if ver != args.target_version + ] if mismatched_files: if args.fix: for filename in mismatched_files: - with open(filename, 'r') as file: + with open(filename, "r") as file: xml_content = file.read() fixed_content = fix_tc_version(xml_content, args.target_version) - with open(filename, 'w') as file: + with open(filename, "w") as file: file.write(fixed_content) - print(f"Fixed TwinCAT versions for:{itemize}{itemize.join(mismatched_files)}") + print( + f"Fixed TwinCAT versions for:{itemize}{itemize.join(mismatched_files)}" + ) else: reason_msg = f"\nReason: {args.reason}" if args.reason else "" raise PreCommitException( "The following files are not set to the targeted TwinCAT version " - f"{args.target_version}:{itemize}{itemize.join(mismatched_files)}{reason_msg}") + f"{args.target_version}:{itemize}{itemize.join(mismatched_files)}{reason_msg}" + ) else: unique_versions = set(versions.values()) if len(unique_versions) > 1: raise PreCommitException( "Not all files have the same TwinCAT version:" - f"{itemize}" + itemize.join(f"{fname}: {ver}" for fname, ver in versions.items())) + f"{itemize}" + + itemize.join(f"{fname}: {ver}" for fname, ver in versions.items()) + ) return 0 except Exception as exc: diff --git a/pre_commit_hooks/tests/test_check_twincat_versions.py b/pre_commit_hooks/tests/test_check_twincat_versions.py index 0246a3d..4652cff 100644 --- a/pre_commit_hooks/tests/test_check_twincat_versions.py +++ b/pre_commit_hooks/tests/test_check_twincat_versions.py @@ -1,11 +1,14 @@ from importlib.resources import files -import pre_commit_hooks.tests.data as test_data import pytest -from pre_commit_hooks.check_twincat_versions import (fix_pinned_version, - fix_tc_version, - get_tc_version, - tc_version_pinned) + +import pre_commit_hooks.tests.data as test_data +from pre_commit_hooks.check_twincat_versions import ( + fix_pinned_version, + fix_tc_version, + get_tc_version, + tc_version_pinned, +) @pytest.fixture @@ -25,7 +28,10 @@ def not_pinned_4024_55(): @pytest.fixture def not_pinned_4024_44(): - return files(test_data).joinpath("not-pinned-version-3.1.4024.44.tsproj").read_text() + return ( + files(test_data).joinpath("not-pinned-version-3.1.4024.44.tsproj").read_text() + ) + def test_pinned_version(pinned_4024_44, not_pinned_4024_44): assert tc_version_pinned(pinned_4024_44)