From 5db8c275e1eb026ae464168d12febdb5b7673a66 Mon Sep 17 00:00:00 2001 From: bullmoose20 <12549033+bullmoose20@users.noreply.github.com.> Date: Fri, 14 Feb 2025 20:15:11 -0500 Subject: [PATCH 1/3] cleanup json-schema folder --- json-schema/README.md | 38 -------------------------------------- 1 file changed, 38 deletions(-) delete mode 100644 json-schema/README.md diff --git a/json-schema/README.md b/json-schema/README.md deleted file mode 100644 index d43cc26..0000000 --- a/json-schema/README.md +++ /dev/null @@ -1,38 +0,0 @@ -JSON schemas for Kometa YAML files - -How to: - -Add this as the first line in your `config.yml`: -``` -# yaml-language-server: $schema=https://raw.githubusercontent.com/Kometa-Team/Kometa/nightly/json-schema/config-schema.json -``` -[change `nightly` to `develop`, or `master` if you wish] - -Then open your config file in an editor that supports the use of JSON schema. - -For example, VS Code with the Red Hat YAML extension. - -This will give you context-sensitive hints and auto-complete for much of the Kometa `config.yml` - - - - - -limitations: - -- template variables not cased for specific default file -- template variables with keys are wildcarded -- "position" attribute has no validation -- "streaming" default has no validation -- search has no validation; just accepts string -- schedule has no validation; just accepts string - -TODO: -"list of coordinates" - -- schema for collection yaml -- schema for metadata yaml -- schema for overlay yaml -- schema for template yaml - -Notes: From 8170f0e92a48149d032ea9df3762c8db1979d792 Mon Sep 17 00:00:00 2001 From: bullmoose20 <12549033+bullmoose20@users.noreply.github.com.> Date: Fri, 14 Feb 2025 20:18:11 -0500 Subject: [PATCH 2/3] update to ignore more directories --- .gitignore | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 664f934..31742dd 100644 --- a/.gitignore +++ b/.gitignore @@ -39,7 +39,8 @@ logs/ # Ignore macOS system files .DS_Store -# Ignore uploads +# Ignore these direct uploads/ - .vscode/ +config/ +json-schema/ From 049204fd6d2b84964d6e841533360688a43f901c Mon Sep 17 00:00:00 2001 From: bullmoose20 <12549033+bullmoose20@users.noreply.github.com.> Date: Fri, 14 Feb 2025 21:27:44 -0500 Subject: [PATCH 3/3] feature for dynamic branch detection --- modules/helpers.py | 92 +++- modules/output.py | 13 +- modules/persistence.py | 11 +- quickstart.py | 10 +- templates/001-navigation.html | 6 +- templates/025-libraries.html | 552 +++++++++++------------ templates/modals/001-start.html | 6 +- templates/modals/010-plex.html | 2 +- templates/modals/020-tmdb.html | 2 +- templates/modals/025-libraries.html | 4 +- templates/modals/027-playlist_files.html | 2 +- templates/modals/030-tautulli.html | 2 +- templates/modals/040-github.html | 2 +- templates/modals/050-omdb.html | 2 +- templates/modals/060-mdblist.html | 2 +- templates/modals/070-notifiarr.html | 2 +- templates/modals/080-gotify.html | 2 +- templates/modals/085-ntfy.html | 2 +- templates/modals/090-webhooks.html | 2 +- templates/modals/100-anidb.html | 2 +- templates/modals/110-radarr.html | 2 +- templates/modals/120-sonarr.html | 2 +- templates/modals/130-trakt.html | 2 +- templates/modals/140-mal.html | 2 +- templates/modals/150-settings.html | 2 +- templates/modals/900-final.html | 2 +- 26 files changed, 421 insertions(+), 309 deletions(-) diff --git a/modules/helpers.py b/modules/helpers.py index 7c7b5c9..f3ecdb3 100644 --- a/modules/helpers.py +++ b/modules/helpers.py @@ -1,3 +1,4 @@ +import hashlib import os import re import requests @@ -15,6 +16,91 @@ } +JSON_SCHEMA_DIR = "json-schema" +FILES_TO_DOWNLOAD = ["prototype_config.yml", "config-schema.json"] +GITHUB_BASE_URL = "https://raw.githubusercontent.com/Kometa-Team/Kometa" + +HASH_FILE = os.path.join( + JSON_SCHEMA_DIR, "file_hashes.txt" +) # Stores previous file hashes + + +def get_kometa_branch(): + """Fetch the correct branch (master or nightly).""" + from .helpers import check_for_update # Prevent circular import + + version_info = check_for_update() + return version_info.get("kometa_branch", "nightly") # Default to nightly + + +def calculate_hash(content): + """Compute the SHA256 hash of the given content.""" + return hashlib.sha256(content.encode("utf-8")).hexdigest() + + +def load_previous_hashes(): + """Load the last known hashes of schema files.""" + if not os.path.exists(HASH_FILE): + return {} + + hashes = {} + with open(HASH_FILE, "r", encoding="utf-8") as f: + for line in f: + filename, file_hash = line.strip().split(":", 1) + hashes[filename] = file_hash + return hashes + + +def save_hashes(hashes): + """Save updated hashes to the hash file.""" + with open(HASH_FILE, "w", encoding="utf-8") as f: + for filename, file_hash in hashes.items(): + f.write(f"{filename}:{file_hash}\n") + + +def ensure_json_schema(): + """Ensure json-schema files exist and are up-to-date based on hash checks.""" + branch = get_kometa_branch() + + if not os.path.exists(JSON_SCHEMA_DIR): + os.makedirs(JSON_SCHEMA_DIR) + + previous_hashes = load_previous_hashes() + new_hashes = {} + + for filename in FILES_TO_DOWNLOAD: + file_path = os.path.join(JSON_SCHEMA_DIR, filename) + url = f"{GITHUB_BASE_URL}/{branch}/json-schema/{filename}" + + # print(f"[INFO] Checking for updates: {filename}...") + + try: + response = requests.get(url, timeout=10) + response.raise_for_status() + new_content = response.text + new_hash = calculate_hash(new_content) + + # Compare hash with previous version + if filename in previous_hashes and previous_hashes[filename] == new_hash: + # print(f"[INFO] No changes detected for {filename}. Skipping download.") + new_hashes[filename] = new_hash # Keep existing hash + continue + + # Save the new file if hash has changed + # print(f"[INFO] Changes detected in {filename}. Downloading new version...") + with open(file_path, "w", encoding="utf-8") as f: + f.write(new_content) + + new_hashes[filename] = new_hash + + except requests.RequestException as e: + print(f"[ERROR] Failed to download {filename}: {e}") + continue # Skip to the next file + + # Save updated hashes + save_hashes(new_hashes) + + def get_local_version(): """Read the local VERSION file and determine the branch.""" version_file = "VERSION" @@ -43,16 +129,20 @@ def get_remote_version(branch): def check_for_update(): - """Compare the local version with the remote version.""" + """Compare the local version with the remote version and determine Kometa branch.""" local_version, branch = get_local_version() remote_version = get_remote_version(branch) update_available = remote_version and remote_version != local_version + # Determine Kometa branch + kometa_branch = "master" if branch == "master" else "nightly" + return { "local_version": local_version, "remote_version": remote_version, "branch": branch, + "kometa_branch": kometa_branch, "update_available": update_available, } diff --git a/modules/output.py b/modules/output.py index 2910122..22f2aa9 100644 --- a/modules/output.py +++ b/modules/output.py @@ -16,7 +16,9 @@ build_config_dict, get_template_list, get_bits, + check_for_update, enforce_string_fields, + ensure_json_schema, STRING_FIELDS, ) @@ -406,12 +408,19 @@ def build_config(header_style="ascii"): yaml.default_flow_style = False yaml.sort_keys = False + ensure_json_schema() + with open("json-schema/config-schema.json", "r") as file: schema = yaml.load(file) - # Prepare the final YAML content + # Fetch kometa_branch dynamically + version_info = check_for_update() + kometa_branch = version_info.get( + "kometa_branch", "nightly" + ) # Default to nightly if not found + yaml_content = ( - "# yaml-language-server: $schema=https://raw.githubusercontent.com/Kometa-Team/Kometa/nightly/json-schema/config-schema.json\n\n" + f"# yaml-language-server: $schema=https://raw.githubusercontent.com/Kometa-Team/Kometa/{kometa_branch}/json-schema/config-schema.json\n\n" f"{section_heading('KOMETA') if header_style == 'ascii' else ('#==================== KOMETA ====================#' if header_style == 'divider' else '')}\n\n" f"{header_comment}\n\n" ) diff --git a/modules/persistence.py b/modules/persistence.py index 8594e36..4723c8f 100644 --- a/modules/persistence.py +++ b/modules/persistence.py @@ -4,7 +4,13 @@ from ruamel.yaml import YAML from flask import current_app as app -from .helpers import build_config_dict, get_template_list, get_bits, booler +from .helpers import ( + build_config_dict, + get_template_list, + get_bits, + booler, + ensure_json_schema, +) from .iso_639_1 import iso_639_1_languages # Importing the languages list from .iso_639_2 import iso_639_2_languages # Importing the languages list from .iso_3166_1 import iso_3166_1_regions # Importing the regions list @@ -202,6 +208,9 @@ def retrieve_status(target): def get_dummy_data(target): yaml = YAML(typ="safe", pure=True) + + ensure_json_schema() + with open("json-schema/prototype_config.yml", "r") as file: base_config = yaml.load(file) diff --git a/quickstart.py b/quickstart.py index a9fbc6f..0e74c73 100644 --- a/quickstart.py +++ b/quickstart.py @@ -43,6 +43,9 @@ redact_sensitive_data, check_for_update, update_checker_loop, + booler, + is_default_image, + ensure_json_schema, ) from modules.persistence import ( save_settings, @@ -51,9 +54,7 @@ flush_session_storage, notification_systems_available, ) -from modules.database import reset_data -from modules.database import get_unique_config_names -from modules.helpers import booler, is_default_image +from modules.database import reset_data, get_unique_config_names from PIL import Image, ImageDraw @@ -111,6 +112,9 @@ def inject_version_info(): server_session = Session(app) +# Ensure json-schema files are up to date at startup +ensure_json_schema() + @app.route("/check_base_images", methods=["GET"]) def check_base_images(): diff --git a/templates/001-navigation.html b/templates/001-navigation.html index 8a02309..720719f 100644 --- a/templates/001-navigation.html +++ b/templates/001-navigation.html @@ -12,10 +12,10 @@