From 303ebe78e4e7f04c87dc65dfbc9c74d30d7df8b6 Mon Sep 17 00:00:00 2001 From: ddl-ebrown Date: Tue, 4 Mar 2025 08:22:27 -0800 Subject: [PATCH 1/2] Add new cucu tags command - Parses all of the available tags through the given features directory and emits them in table format --- src/cucu/cli/core.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/cucu/cli/core.py b/src/cucu/cli/core.py index 92d6e5b2..befef77a 100644 --- a/src/cucu/cli/core.py +++ b/src/cucu/cli/core.py @@ -2,6 +2,7 @@ import glob import json import os +import re import shutil import signal import sys @@ -765,6 +766,42 @@ def vars(filepath): print(tabulate(variables, tablefmt="fancy_grid")) +@main.command() +@click.argument("filepath", default="features") +def tags(filepath): + """ + print list of defined tags + """ + init_global_hook_variables() + + tags = _find_unique_tags(root_dir=filepath, pattern="*.feature", string_capture="^\(@[\w-]+\)") + + print(tabulate(tags, tablefmt="fancy_grid")) + +def _find_unique_tags(root_dir, pattern, string_capture): + """ + Recursively searches files matching a pattern, extracting unique pattern matches. + + Args: + root_dir: The root directory to start the search from. + pattern: The file pattern to match (e.g., "*.feature"). + string_capture: The pattern to find strings within the files (e.g., r'"(.*?)"'). + + Returns: + A set of unique regex captures found in the files. + """ + unique = set() + for filename in glob.iglob(os.path.join(root_dir, "**", pattern), recursive=True): + try: + with open(filename, "r") as f: + content = f.read() + matches = re.findall(string_capture, content) + unique.update(matches) + except Exception as e: + print(f"Error processing file {filename}: {e}") + return unique + + @main.command() @click.option( "-b", From e69ed648e97db6abc0206a090e87fde39351c2eb Mon Sep 17 00:00:00 2001 From: ddl-ebrown Date: Tue, 4 Mar 2025 08:40:53 -0800 Subject: [PATCH 2/2] Note about using behave library --- src/cucu/cli/core.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/cucu/cli/core.py b/src/cucu/cli/core.py index befef77a..8e198da5 100644 --- a/src/cucu/cli/core.py +++ b/src/cucu/cli/core.py @@ -774,6 +774,9 @@ def tags(filepath): """ init_global_hook_variables() + # TODO: this is waaaaay too naive and won't work + # instead need to use the behave parse_file helper (or find some other state that's loaded in behave) + # maybe call behave_init with each file and pop some state from the library? tags = _find_unique_tags(root_dir=filepath, pattern="*.feature", string_capture="^\(@[\w-]+\)") print(tabulate(tags, tablefmt="fancy_grid"))