From 63f83aeb7399e344af124c52b3c210ecee94437a Mon Sep 17 00:00:00 2001 From: Brian Egge Date: Fri, 29 Nov 2024 12:28:50 +0000 Subject: [PATCH 1/2] Add default manufacturer file if missing --- .../scripts/profile_library/update-library-json.py | 13 +++++++++++-- .gitignore | 1 + 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/.github/scripts/profile_library/update-library-json.py b/.github/scripts/profile_library/update-library-json.py index 453a25e53..2b850d7bf 100644 --- a/.github/scripts/profile_library/update-library-json.py +++ b/.github/scripts/profile_library/update-library-json.py @@ -74,8 +74,17 @@ def generate_library_json(model_listing: list[dict]) -> None: def get_manufacturer_json(manufacturer: str) -> dict: - with open(os.path.join(DATA_DIR, manufacturer, "manufacturer.json")) as json_file: - return json.load(json_file) + json_path = os.path.join(DATA_DIR, manufacturer, "manufacturer.json") + try: + with open(json_path) as json_file: + return json.load(json_file) + except FileNotFoundError as e: + default_json = { "name": manufacturer, "aliases":[] } + with open(json_path, 'w', encoding='utf-8') as json_file: + json.dump(default_json, json_file, ensure_ascii=False, indent=4) + git.Repo(PROJECT_ROOT).git.add(json_path) + print(f"Added {json_path}") + return default_json def get_model_list() -> list[dict]: diff --git a/.gitignore b/.gitignore index 67f774445..6d40001ec 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ .idea .vscode __pycache__ +venv/ .coverage cov.xml From d8b4dec9061721ead0afaff9c6251fa3ec21abb0 Mon Sep 17 00:00:00 2001 From: Brian Egge Date: Fri, 29 Nov 2024 12:49:03 +0000 Subject: [PATCH 2/2] chore(lint): reformat .github/scripts --- .../scripts/profile_library/update-authors.py | 20 ++++++++----------- .../profile_library/update-library-json.py | 17 ++++++++-------- .github/scripts/update_hacs_manifest.py | 1 + 3 files changed, 17 insertions(+), 21 deletions(-) diff --git a/.github/scripts/profile_library/update-authors.py b/.github/scripts/profile_library/update-authors.py index 037442f60..79560ca4f 100644 --- a/.github/scripts/profile_library/update-authors.py +++ b/.github/scripts/profile_library/update-authors.py @@ -6,28 +6,28 @@ def run_git_command(command): - """ Run a git command and return the output. """ + """Run a git command and return the output.""" result = subprocess.run(command, shell=True, capture_output=True, text=True) result.check_returncode() # Raise an error if the command fails return result.stdout.strip() def get_commits_affected_directory(directory: str) -> list: - """ Get a list of commits that affected the given directory, including renames. """ + """Get a list of commits that affected the given directory, including renames.""" command = f"git log --follow --format='%H' -- '{directory}'" commits = run_git_command(command) return commits.splitlines() def get_commit_author(commit_hash: str) -> str: - """ Get the author of a given commit. """ + """Get the author of a given commit.""" command = f"git show -s --format='%an <%ae>' {commit_hash}" author = run_git_command(command) return author def find_first_commit_author(file: str, check_paths: bool = True) -> str | None: - """ Find the first commit that affected the directory and return the author's name. """ + """Find the first commit that affected the directory and return the author's name.""" commits = get_commits_affected_directory(file) for commit in reversed(commits): # Process commits from the oldest to newest command = f"git diff-tree --no-commit-id --name-only -r {commit}" @@ -35,11 +35,7 @@ def find_first_commit_author(file: str, check_paths: bool = True) -> str | None: return get_commit_author(commit) affected_files = run_git_command(command) - paths = [ - file.replace("profile_library", "custom_components/powercalc/data"), - file.replace("profile_library", "data"), - file - ] + paths = [file.replace("profile_library", "custom_components/powercalc/data"), file.replace("profile_library", "data"), file] if any(path in affected_files.splitlines() for path in paths): author = get_commit_author(commit) return author @@ -48,7 +44,7 @@ def find_first_commit_author(file: str, check_paths: bool = True) -> str | None: def process_model_json_files(root_dir): # Find all model.json files in the directory tree - model_json_files = glob.glob(os.path.join(root_dir, '**', 'model.json'), recursive=True) + model_json_files = glob.glob(os.path.join(root_dir, "**", "model.json"), recursive=True) for model_json_file in model_json_files: # Skip sub profiles @@ -71,7 +67,7 @@ def process_model_json_files(root_dir): def read_author_from_file(file_path: str) -> str | None: """Read the author from the model.json file.""" - with open(file_path, "r") as file: + with open(file_path) as file: json_data = json.load(file) return json_data.get("author") @@ -80,7 +76,7 @@ def read_author_from_file(file_path: str) -> str | None: def write_author_to_file(file_path: str, author: str) -> None: """Write the author to the model.json file.""" # Read the existing content - with open(file_path, "r") as file: + with open(file_path) as file: json_data = json.load(file) json_data["author"] = author diff --git a/.github/scripts/profile_library/update-library-json.py b/.github/scripts/profile_library/update-library-json.py index 2b850d7bf..588331322 100644 --- a/.github/scripts/profile_library/update-library-json.py +++ b/.github/scripts/profile_library/update-library-json.py @@ -3,12 +3,11 @@ import glob import json import os -import git import sys from datetime import datetime from pathlib import Path -from pytablewriter import MarkdownTableWriter +import git sys.path.insert( 1, @@ -39,7 +38,8 @@ def generate_library_json(model_listing: list[dict]) -> None: if not manufacturer: manufacturer = { **get_manufacturer_json(manufacturer_name), - **{"models": [], "device_types": []}, + "models": [], + "device_types": [], } manufacturers[manufacturer_name] = manufacturer @@ -53,7 +53,7 @@ def generate_library_json(model_listing: list[dict]) -> None: "device_type": "device_type", "aliases": "aliases", "updated_at": "updated_at", - "color_modes": "color_modes" + "color_modes": "color_modes", } # Create a new dictionary with updated keys @@ -78,9 +78,9 @@ def get_manufacturer_json(manufacturer: str) -> dict: try: with open(json_path) as json_file: return json.load(json_file) - except FileNotFoundError as e: - default_json = { "name": manufacturer, "aliases":[] } - with open(json_path, 'w', encoding='utf-8') as json_file: + except FileNotFoundError: + default_json = {"name": manufacturer, "aliases": []} + with open(json_path, "w", encoding="utf-8") as json_file: json.dump(default_json, json_file, ensure_ascii=False, indent=4) git.Repo(PROJECT_ROOT).git.add(json_path) print(f"Added {json_path}") @@ -138,8 +138,7 @@ def get_last_commit_time(directory: str) -> datetime: if commits: last_commit = commits[0] return last_commit.committed_datetime - else: - return datetime.fromtimestamp(0) + return datetime.fromtimestamp(0) model_list = get_model_list() diff --git a/.github/scripts/update_hacs_manifest.py b/.github/scripts/update_hacs_manifest.py index cbbe7b00a..c78c3b497 100644 --- a/.github/scripts/update_hacs_manifest.py +++ b/.github/scripts/update_hacs_manifest.py @@ -1,4 +1,5 @@ """Update the manifest file.""" + import json import os import sys