From 944713e39167d7ebe05687c97ad7e03359399912 Mon Sep 17 00:00:00 2001 From: jfuller Date: Thu, 18 Jan 2024 11:56:23 +0100 Subject: [PATCH] -r search should highlight matched term --- CHANGELOG.md | 1 + griffon/commands/queries.py | 1 + griffon/output.py | 80 ++++++++++++++++++------------------- scripts/smoke-tests.sh | 1 + 4 files changed, 41 insertions(+), 42 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3bef181..e2c06bd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed * refactor output format of service products-contain-component (GRIF-209) * container roots are excluded by default (need to use --include-container-roots) +* when using -r highlight matched term ### Added * --include-container_roots in service products-contain-component diff --git a/griffon/commands/queries.py b/griffon/commands/queries.py index 9bddf77..b9decf8 100644 --- a/griffon/commands/queries.py +++ b/griffon/commands/queries.py @@ -417,6 +417,7 @@ def get_product_contain_component( if verbose: ctx.obj["VERBOSE"] = verbose ctx.obj["INCLUDE_CONTAINER_ROOTS"] = include_container_roots + ctx.obj["REGEX_NAME_SEARCH"] = regex_name_search if ( not search_latest and not search_all diff --git a/griffon/output.py b/griffon/output.py index 26fca8b..bbc631b 100644 --- a/griffon/output.py +++ b/griffon/output.py @@ -403,6 +403,10 @@ def process_product_color(build_type: str) -> str: return "magenta" +def highlight_search_term(search_pattern, text_value): + return re.sub(search_pattern, "[b]\\g<0>[/b]", text_value) + + def text_output_products_contain_component( ctx, output, @@ -410,7 +414,13 @@ def text_output_products_contain_component( exclude_components, no_wrap=False, ): - search_component_name = ctx.params["component_name"] + logger.info(ctx.obj["REGEX_NAME_SEARCH"]) + # if -r option used we need to escape it + search_component_name = ( + re.escape(ctx.params["component_name"]) + if not ctx.obj["REGEX_NAME_SEARCH"] + else ctx.params["component_name"] + ) # handle single component if ctx.params["purl"]: @@ -470,10 +480,8 @@ def text_output_products_contain_component( dep_name = nvr # highlight search term try: - dep_name = re.sub( - re.escape(search_component_name), - f"[b]{search_component_name}[/b]", - dep_name, + dep_name = highlight_search_term( + search_component_name, dep_name ) except re.error: pass @@ -541,10 +549,8 @@ def text_output_products_contain_component( # dep_name = nvr dep_name = nvr try: - dep_name = re.sub( - re.escape(search_component_name), - f"[b]{search_component_name}[/b]", - dep_name, + dep_name = highlight_search_term( + search_component_name, dep_name ) except re.error: pass @@ -552,9 +558,8 @@ def text_output_products_contain_component( related_url = result_tree[pv][ps][cn][nvr].get("related_url") try: if result_tree[pv][ps][cn][nvr]["related_url"]: - related_url = re.sub( - re.escape(search_component_name), - f"[b]{search_component_name}[/b]", + related_url = highlight_search_term( + search_component_name, result_tree[pv][ps][cn][nvr]["related_url"], ) except re.error: @@ -645,9 +650,8 @@ def text_output_products_contain_component( child_dep_names = "" if provides_components: try: - child_dep_names = re.sub( - re.escape(search_component_name), - f"[b]{search_component_name}[/b]", + child_dep_names = highlight_search_term( + search_component_name, ", ".join(provides_components), ) child_dep_names = f"[{child_dep_names}]" @@ -677,9 +681,8 @@ def text_output_products_contain_component( # highlight search term dep_name = nvr try: - dep_name = re.sub( - re.escape(search_component_name), - f"[b]{search_component_name}[/b]", + dep_name = highlight_search_term( + search_component_name, dep_name, ) except re.error: @@ -688,11 +691,11 @@ def text_output_products_contain_component( related_url = result_tree[pv][ps][cn][nvr].get("related_url") try: if result_tree[pv][ps][cn][nvr]["related_url"]: - related_url = re.sub( - re.escape(search_component_name), - f"[b]{search_component_name}[/b]", + related_url = highlight_search_term( + search_component_name, result_tree[pv][ps][cn][nvr]["related_url"], ) + except re.error: pass build_source_url = "" @@ -797,9 +800,8 @@ def text_output_products_contain_component( child_dep_names = "" if provides_components: try: - child_dep_names = re.sub( - re.escape(search_component_name), - f"[b]{search_component_name}[/b]", + child_dep_names = highlight_search_term( + search_component_name, ", ".join(provides_components), ) child_dep_names = f"[{child_dep_names}]" @@ -833,9 +835,8 @@ def text_output_products_contain_component( # highlight search term dep_name = nvr try: - dep_name = re.sub( - re.escape(search_component_name), - f"[b]{search_component_name}[/b]", + dep_name = highlight_search_term( + search_component_name, dep_name, ) except re.error: @@ -844,9 +845,8 @@ def text_output_products_contain_component( related_url = result_tree[pv][ps][cn][nvr].get("related_url") try: if result_tree[pv][ps][cn][nvr]["related_url"]: - related_url = re.sub( - re.escape(search_component_name), - f"[b]{search_component_name}[/b]", + related_url = highlight_search_term( + search_component_name, result_tree[pv][ps][cn][nvr]["related_url"], ) except re.error: @@ -920,9 +920,8 @@ def text_output_products_contain_component( child_dep_names = "" if provides_components: try: - child_dep_names = re.sub( - re.escape(search_component_name), - f"[b]{search_component_name}[/b]", + child_dep_names = highlight_search_term( + search_component_name, ", ".join(provides_components), ) child_dep_names = f"[{child_dep_names}]" @@ -956,9 +955,8 @@ def text_output_products_contain_component( # highlight search term dep_name = result_tree[pv][ps][cn][nvr]["purl"] try: - dep_name = re.sub( - re.escape(search_component_name), - f"[b]{search_component_name}[/b]", + dep_name = highlight_search_term( + search_component_name, dep_name, ) except re.error: @@ -967,9 +965,8 @@ def text_output_products_contain_component( related_url = result_tree[pv][ps][cn][nvr].get("related_url") try: if result_tree[pv][ps][cn][nvr]["related_url"]: - related_url = re.sub( - re.escape(search_component_name), - f"[b]{search_component_name}[/b]", + related_url = highlight_search_term( + search_component_name, result_tree[pv][ps][cn][nvr]["related_url"], ) except re.error: @@ -1043,9 +1040,8 @@ def text_output_products_contain_component( child_dep_names = "" if provides_components: try: - child_dep_names = re.sub( - re.escape(search_component_name), - f"[b]{search_component_name}[/b]", + child_dep_names = highlight_search_term( + search_component_name, ", ".join(provides_components), ) child_dep_names = f"[{child_dep_names}]" diff --git a/scripts/smoke-tests.sh b/scripts/smoke-tests.sh index 2351f53..63b10d1 100755 --- a/scripts/smoke-tests.sh +++ b/scripts/smoke-tests.sh @@ -80,3 +80,4 @@ griffon service component-flaws npm griffon service component-flaws python-marshmallow --affectedness AFFECTED griffon service products-contain-component --search-all --search-upstreams -s libxml2 -a griffon service products-contain-component -s grep -v --search-all +griffon service products-contain-component -r 'webkit.tk' -vv