From de7ba70af481cf79e9bec70bcb500e8da972df9f Mon Sep 17 00:00:00 2001 From: mdeweerd Date: Thu, 15 Aug 2024 11:17:45 +0200 Subject: [PATCH 1/3] Add method to update regular files --- .github/scripts/update_hacs_manifest.py | 74 +++++++++++++++++++++---- 1 file changed, 63 insertions(+), 11 deletions(-) diff --git a/.github/scripts/update_hacs_manifest.py b/.github/scripts/update_hacs_manifest.py index 47e3a2a..88d92bc 100755 --- a/.github/scripts/update_hacs_manifest.py +++ b/.github/scripts/update_hacs_manifest.py @@ -7,8 +7,8 @@ # # MIT License # -# Copyright (c) 2021 Bram Gerritsen -# Copyright (c) 2022 Mario DE WEERD +# Copyright (c) 2021 Bram Gerritsen +# Copyright (c) 2022-2024 Mario DE WEERD # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal @@ -28,21 +28,20 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. -"""Update the manifest file.""" +"""Update the files with new version.""" import json import os +import re import sys -def update_manifest(): +def update_manifest(path=None, version=None): """Update the manifest file.""" - version = "0.0.0" - for index, value in enumerate(sys.argv): - if value in ["--version", "-V"]: - version = sys.argv[index + 1] + if path is None: + return with open( - f"{os.getcwd()}/custom_components/zha_toolkit/manifest.json", + path, encoding="utf_8", ) as manifestfile: manifest = json.load(manifestfile) @@ -50,11 +49,64 @@ def update_manifest(): manifest["version"] = version with open( - f"{os.getcwd()}/custom_components/zha_toolkit/manifest.json", + path, "w", encoding="utf_8", ) as manifestfile: manifestfile.write(json.dumps(manifest, indent=4, sort_keys=True)) -update_manifest() +def replace_version_in_file(path: str, regex: str, version: str): + # Remove any leading 'v' from the provided version + new_version = version.lstrip("v") + + # Compile the regex pattern + pattern = re.compile(regex) + + # Function to replace the version part in the match + def version_replacer(match): + print("YAS") + # Extract the original version from the match + original_version = match.group("version") + + # Determine if the original version started with 'v' + if original_version.startswith("v"): + replacement_version = f"v{new_version}" + else: + replacement_version = new_version + + # Replace the version in the matched string + replacement_match = match.group(0).replace( + original_version, replacement_version + ) + + return replacement_match + + # Read the file content + with open(path, encoding="utf_8") as file: + content = file.read() + + # Replace the versions in the content + new_content = pattern.sub(version_replacer, content) + + # Write the modified content back to the file + with open(path, "w", encoding="utf_8") as file: + file.write(new_content) + + +newVersion = "0.0.0" +for index, value in enumerate(sys.argv): + if value in ["--version", "-V", "-v"]: + newVersion = sys.argv[index + 1] + + +filepath = f"{os.getcwd()}/custom_components/zha_toolkit/manifest.json" +update_manifest(filepath, newVersion) + +# Example: +# replace_version_in_file('file.txt', r'(?Pv?\d+\.\d+\.\d+)', '2.3.4') + +# filepath = f"{os.getcwd()}/README" +# Example1: regex = r'toolkit version \[(?P.*?)]' +# Example2: regex= r'(?Pv?\d+\.\d+\.\d+)' +# replace_version_in_file(filepath, regex, newVersion) From 596b49fe2e851023431f8a4ea22a3bd5fc0f3d80 Mon Sep 17 00:00:00 2001 From: mdeweerd Date: Thu, 15 Aug 2024 11:21:05 +0200 Subject: [PATCH 2/3] Add debug information when hass could not be fetched from gateway --- custom_components/zha_toolkit/utils.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/custom_components/zha_toolkit/utils.py b/custom_components/zha_toolkit/utils.py index 5df20ea..f062b88 100644 --- a/custom_components/zha_toolkit/utils.py +++ b/custom_components/zha_toolkit/utils.py @@ -1183,4 +1183,9 @@ def get_hass(gateway: ZHAGateway): hass = getattr(gateway, "_hass", None) if hass is None: hass = getattr(gateway, "hass", None) + if hass is None: + msg = f"Could not get hass from {gateway!r}." + if gateway is not None: + msg += "Attributes available {dir(gateway)}." + raise ValueError(msg) return hass From b9bb11aade8913b381b5d235de4fd3b22cbe9896 Mon Sep 17 00:00:00 2001 From: mdeweerd Date: Thu, 15 Aug 2024 11:22:16 +0200 Subject: [PATCH 3/3] Update stats file --- STATS.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/STATS.md b/STATS.md index af8a80c..ec96986 100644 --- a/STATS.md +++ b/STATS.md @@ -1,6 +1,11 @@ # Badges showing number of downloads per version - ![badge latest](https://img.shields.io/github/downloads/mdeweerd/zha-toolkit/latest/total.svg) +- ![badge v1.1.18](https://img.shields.io/github/downloads/mdeweerd/zha-toolkit/v1.1.18/total.svg) +- ![badge v1.1.17](https://img.shields.io/github/downloads/mdeweerd/zha-toolkit/v1.1.17/total.svg) +- ![badge v1.1.16](https://img.shields.io/github/downloads/mdeweerd/zha-toolkit/v1.1.16/total.svg) +- ![badge v1.1.15](https://img.shields.io/github/downloads/mdeweerd/zha-toolkit/v1.1.15/total.svg) +- ![badge v1.1.14](https://img.shields.io/github/downloads/mdeweerd/zha-toolkit/v1.1.14/total.svg) - ![badge v1.1.13](https://img.shields.io/github/downloads/mdeweerd/zha-toolkit/v1.1.13/total.svg) - ![badge v1.1.12](https://img.shields.io/github/downloads/mdeweerd/zha-toolkit/v1.1.12/total.svg) - ![badge v1.1.11](https://img.shields.io/github/downloads/mdeweerd/zha-toolkit/v1.1.11/total.svg)