From ccf68d0b9982a2d321a15578ad6f5893aa979ba0 Mon Sep 17 00:00:00 2001 From: cicdguy <26552821+cicdguy@users.noreply.github.com> Date: Mon, 4 Nov 2024 09:27:40 -0600 Subject: [PATCH] fix: This is pure XML replacements --- core.py | 466 ++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 298 insertions(+), 168 deletions(-) diff --git a/core.py b/core.py index 16e0744..b2b9c49 100644 --- a/core.py +++ b/core.py @@ -1,3 +1,7 @@ +""" +Multi-version dropdown updater. +""" + import argparse import os import re @@ -7,246 +11,375 @@ from packaging.version import InvalidVersion, Version -def generate_dropdown_list(directory, pattern, refs_order, base_url): +def compile_pattern(pattern): """ - Generates version drop-down list to be inserted based - on matching directories in the given directory and refs_order. + Compile the given regular expression pattern. - :param directory: The root directory to search for matching directories. :param pattern: A regular expression pattern to match directory names. - :param refs_order: List determining the order of items to appear at the beginning. - :param base_url: The base URL to be used in the hrefs. - :return: str, Generated HTML markup. + :return: Compiled regex pattern. """ + return re.compile(pattern) - # Compile the pattern - regex = re.compile(pattern) - # Find all matching directories - matching_dirs = [ +def find_matching_directories(directory, regex): + """ + Find all matching directories in the given directory based on the regex pattern. + + :param directory: The root directory to search for matching directories. + :param regex: Compiled regular expression pattern to match directory names. + :return: List of matching directories. + """ + return [ d for d in os.listdir(directory) if os.path.isdir(os.path.join(directory, d)) and regex.match(d) ] - # Separate items in refs_order and other items for semantic versioning sorting + +def separate_refs(matching_dirs, refs_order): + """ + Separate items in refs_order and other items for semantic versioning sorting. + + :param matching_dirs: List of matching directories. + :param refs_order: List determining the order of items to appear at the beginning. + :return: Tuple of ordered_refs and remaining_refs. + """ ordered_refs = [d for d in refs_order if d in matching_dirs] remaining_refs = [d for d in matching_dirs if d not in refs_order] + return ordered_refs, remaining_refs + + +def sorting_key(ref): + """ + Define a custom sorting key function. + + :param ref: Reference to be sorted. + :return: Tuple for sorting. + """ + try: + return (0, Version(ref)) + except InvalidVersion: + return (1, ref) - # Define a custom sorting key function - def sorting_key(ref): - try: - return (0, Version(ref)) - except InvalidVersion: - return (1, ref) - # Sort the remaining items using the custom sorting key (semantic versioning first, then alphabetically) +def sort_remaining_refs(remaining_refs): + """ + Sort the remaining items using the custom sorting key + (semantic versioning first, then alphabetically). + + :param remaining_refs: List of remaining references. + :return: Sorted list of remaining references. + """ remaining_refs.sort(key=sorting_key, reverse=True) + return remaining_refs - # Combine the ordered and remaining items - ordered_refs.extend(remaining_refs) - # Generate the full URLs for the directories - refs_dict = {ref: f"{base_url}{ref}" for ref in ordered_refs} +def generate_refs_dict(ordered_refs, base_url): + """ + Generate the full URLs for the directories. + + :param ordered_refs: List of ordered references. + :param base_url: The base URL to be used in the hrefs. + :return: Dictionary of references and their URLs. + """ + return {ref: f"{base_url}{ref}" for ref in ordered_refs} - # Generate the markup + +def generate_markup(ordered_refs, refs_dict): + """ + Generate the HTML markup for the drop-down list. + + :param ordered_refs: List of ordered references. + :param refs_dict: Dictionary of references and their URLs. + :return: str, Generated HTML markup. + """ nav_item = """ " - return nav_item -def insert_html_after_last_li(tree, dropdown_list): +def generate_dropdown_list(directory, pattern, refs_order, base_url): + """ + Generates version drop-down list to be inserted based + on matching directories in the given directory and refs_order. + + :param directory: The root directory to search for matching directories. + :param pattern: A regular expression pattern to match directory names. + :param refs_order: List determining the order of items to appear at the beginning. + :param base_url: The base URL to be used in the hrefs. + :return: str, Generated HTML markup. + """ + regex = compile_pattern(pattern) + matching_dirs = find_matching_directories(directory, regex) + ordered_refs, remaining_refs = separate_refs(matching_dirs, refs_order) + remaining_refs = sort_remaining_refs(remaining_refs) + ordered_refs.extend(remaining_refs) + refs_dict = generate_refs_dict(ordered_refs, base_url) + return generate_markup(ordered_refs, refs_dict) + + +def find_navbar(tree): + """ + Find the first