Skip to content
This repository has been archived by the owner on Feb 15, 2024. It is now read-only.

Commit

Permalink
Standardize work with related data
Browse files Browse the repository at this point in the history
sources and upstreams are transformed to raw JSON now so we avoid
problems when mixing middleware CLI data and component registry data
(eg. Component registry bindings models vs JSON)
  • Loading branch information
JakubFrejlach committed Dec 8, 2023
1 parent fa14c84 commit 79758e9
Showing 1 changed file with 36 additions and 18 deletions.
54 changes: 36 additions & 18 deletions griffon/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import re

import click
from component_registry_bindings.bindings.python_client.types import (
ComponentRegistryModel,
)
from packageurl import PackageURL
from rich.console import Console
from rich.text import Text
Expand All @@ -29,25 +32,38 @@ class DEST(enum.Enum):
FILE = "file"


def raw_json_transform_related(data: dict, name):
"""normalise ralated data which were piggy-backed to data"""
transformed = []
related_data = data.get(name)
if related_data is not None:
if isinstance(related_data, list):
transformed = [
item.to_dict() if isinstance(item, ComponentRegistryModel) else item
for item in related_data
]

return transformed


def raw_json_transform(data, show_count: bool) -> dict:
"""normalise all data to dict"""
if type(data) is list:
results = []
for d in data:
if type(d) is dict:
results.append(d)
else:
results.append(d.to_dict())
for item in data:
transformed = item if type(item) is dict else item.to_dict()
for related_data in ("upstreams", "sources"):
transformed[related_data] = raw_json_transform_related(transformed, related_data)
results.append(transformed)
output = {
"results": results,
}
if show_count:
output["count"] = len(results) # type: ignore
else:
if type(data) is dict:
output = data
else:
output = data.to_dict()
transformed = data if type(data) is dict else data.to_dict()
for related_data in ("upstreams", "sources"):
transformed[related_data] = raw_json_transform_related(transformed, related_data)
return output


Expand Down Expand Up @@ -274,7 +290,7 @@ def generate_affects(
for nvr in result_tree[pv][ps][component_name].keys():
if result_tree[pv][ps][component_name][nvr]["sources"]:
source_names = [
source.name
source["name"]
for source in result_tree[pv][ps][component_name][nvr]["sources"]
if source.namespace == "REDHAT"
]
Expand Down Expand Up @@ -389,6 +405,7 @@ def text_output_products_contain_component(
nvr = list(result_tree[pv][ps][cn].keys())[-1]
# highlight search term
dep_name = nvr
print(dep_name)
try:
dep_name = re.sub(
re.escape(search_component_name),
Expand All @@ -398,6 +415,7 @@ def text_output_products_contain_component(
except re.error:
pass
dep = f"[grey93]{dep_name} ({result_tree[pv][ps][cn][nvr]['type']})[/grey93]" # noqa
print(dep)
related_url = result_tree[pv][ps][cn][nvr].get("related_url")
try:
if result_tree[pv][ps][cn][nvr]["related_url"]:
Expand All @@ -413,7 +431,7 @@ def text_output_products_contain_component(
list(
set(
[
source.name
source["name"]
for source in result_tree[pv][ps][cn][nvr][
"upstreams"
]
Expand All @@ -439,7 +457,7 @@ def text_output_products_contain_component(
list(
set(
[
source.name
source["name"]
for source in result_tree[pv][ps][cn][nvr][
"sources"
]
Expand Down Expand Up @@ -503,7 +521,7 @@ def text_output_products_contain_component(
list(
set(
[
source.name
source["name"]
for source in result_tree[pv][ps][cn][nvr][
"upstreams"
]
Expand All @@ -530,7 +548,7 @@ def text_output_products_contain_component(
list(
set(
[
source.name
source["name"]
for source in result_tree[pv][ps][cn][nvr][
"sources"
]
Expand Down Expand Up @@ -598,7 +616,7 @@ def text_output_products_contain_component(
list(
set(
[
source.name
source["name"]
for source in result_tree[pv][ps][cn][nvr][
"upstreams"
]
Expand All @@ -620,7 +638,7 @@ def text_output_products_contain_component(
list(
set(
[
source.name
source["name"]
for source in result_tree[pv][ps][cn][nvr][
"sources"
]
Expand Down Expand Up @@ -684,7 +702,7 @@ def text_output_products_contain_component(
list(
set(
[
source.nvr
source["nvr"]
for source in result_tree[pv][ps][cn][nvr][
"upstreams"
]
Expand All @@ -706,7 +724,7 @@ def text_output_products_contain_component(
list(
set(
[
source.nvr
source["nvr"]
for source in result_tree[pv][ps][cn][nvr][
"sources"
]
Expand Down

0 comments on commit 79758e9

Please sign in to comment.