Skip to content

Commit

Permalink
DOP-4915: add display_name property to facets (#621)
Browse files Browse the repository at this point in the history
* add display_name property to facets

* format test

* add return statement

* remove unused param

* update regex
  • Loading branch information
seungpark authored Sep 27, 2024
1 parent 86e5f9f commit 23ebf39
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 17 deletions.
15 changes: 14 additions & 1 deletion snooty/postprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -1800,11 +1800,24 @@ def get_children_total(

facet_values = node.options["values"]
parent = None
parent_facets = []
if self.parent_stack:
parent = self.parent_stack[-1][0]
parent_facets = list(map(lambda tuple: tuple[0], self.parent_stack))

for facet_value in facet_values.split(","):
facet_node = Facet(category=node.options["name"], value=facet_value.strip())
unparsed_facet = {
"value": facet_value.strip(),
"category": node.options["name"],
}
facet_display_name = ProjectConfig.get_facet_display_name(
parent_facets, unparsed_facet
)
facet_node = Facet(
category=unparsed_facet["category"],
value=unparsed_facet["value"],
display_name=facet_display_name,
)

if not parent:
self.facets.append(facet_node)
Expand Down
4 changes: 1 addition & 3 deletions snooty/taxonomy.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ class FacetDefinition:

@checked
@dataclass
class TargetProductDefinition:
name: str
display_name: Optional[str]
class TargetProductDefinition(FacetDefinition):
sub_product: Optional[List[FacetDefinition]]
version: Optional[List[FacetDefinition]]

Expand Down
39 changes: 31 additions & 8 deletions snooty/test_postprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -3104,7 +3104,7 @@ def test_facets() -> None:
.. facet::
:name: sub_product
:values: charts
:values: charts, data-federation
===========================
Expand All @@ -3126,7 +3126,16 @@ def test_facets() -> None:
value="atlas",
sub_facets=[
Facet(category="version", value="v1.2"),
Facet(category="sub_product", value="charts"),
Facet(
category="sub_product",
value="charts",
display_name="Charts",
),
Facet(
category="sub_product",
value="data-federation",
display_name="Data Federation",
),
],
),
]
Expand All @@ -3153,16 +3162,19 @@ def test_toml_facets() -> None:
.. facet::
:name: genre
:values: reference
.. facet::
:name: target_product
:values: atlas
.. facet::
:name: version
:values: v1.2
.. facet::
:name: sub_product
:values: charts,atlas-cli
.. facet::
:name: genre
:values: tutorial
Expand Down Expand Up @@ -3202,18 +3214,29 @@ def test_toml_facets() -> None:
assert facets is not None
assert sorted(facets) == sorted(
[
Facet(category="genre", value="reference"),
Facet(category="genre", value="tutorial"),
Facet(category="genre", value="reference", display_name="Reference"),
Facet(category="genre", value="tutorial", display_name="Tutorial"),
Facet(
category="target_product",
value="atlas",
sub_facets=[
Facet(category="version", value="v1.2"),
Facet(category="sub_product", value="charts"),
Facet(category="sub_product", value="atlas-cli"),
Facet(category="version", value="v1.2", display_name="v1.2"),
Facet(
category="sub_product",
value="charts",
display_name="Charts",
),
Facet(
category="sub_product",
value="atlas-cli",
display_name="Atlas CLI",
),
],
display_name="Atlas",
),
Facet(
category="programming_language", value="shell", display_name="Shell"
),
Facet(category="programming_language", value="shell"),
]
)

Expand Down
51 changes: 46 additions & 5 deletions snooty/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import logging
import os.path
import re
from dataclasses import dataclass, field
from dataclasses import asdict, dataclass, field
from pathlib import Path, PurePath
from typing import (
Any,
Expand Down Expand Up @@ -132,6 +132,7 @@ class ParsedBannerConfig:
class Facet:
category: str
value: str
display_name: Optional[str] = ""
sub_facets: Optional[List["Facet"]] = None

def __lt__(self, other: "Facet") -> bool:
Expand All @@ -151,6 +152,7 @@ def serialize(self) -> n.SerializedNode:
"category": self.category,
"value": self.value,
"sub_facets": None,
"display_name": self.display_name,
}
if self.sub_facets:
result["sub_facets"] = [
Expand Down Expand Up @@ -346,6 +348,7 @@ def validate_facets(
category=facet.category,
value=facet.value,
sub_facets=validated_sub_facets,
display_name=facet.display_name,
)

validated_facets.append(validated_facet)
Expand All @@ -362,12 +365,22 @@ def validate_facets(
return validated_facets, diagnostics

@staticmethod
def parse_facet(unparsed_facet: Dict[str, Any]) -> Facet:
def parse_facet(
unparsed_facet: Dict[str, Any], parent_facets: List[Facet]
) -> Facet:
try:
facet = Facet(**unparsed_facet)
display_name = ProjectConfig.get_facet_display_name(
parent_facets, unparsed_facet
)
facet = Facet(
**unparsed_facet,
display_name=display_name,
)
if facet.sub_facets:
new_parent_facets = parent_facets + [facet]
facet.sub_facets = [
ProjectConfig.parse_facet(f) for f in unparsed_facet["sub_facets"]
ProjectConfig.parse_facet(f, new_parent_facets)
for f in unparsed_facet["sub_facets"]
]
except Exception as e:
logger.error(e)
Expand All @@ -384,7 +397,7 @@ def load_facets_from_file(
try:
with path.open("rb") as f:
data = tomli.load(f)["facets"]
facets = [ProjectConfig.parse_facet(facet) for facet in data]
facets = [ProjectConfig.parse_facet(facet, []) for facet in data]
(
validated_facets_result,
validation_diagnostics,
Expand Down Expand Up @@ -427,6 +440,34 @@ def merge_facets(

return merged_facets

@staticmethod
def get_facet_display_name(
parent_facets: List[Facet], unparsed_facet: Dict[str, Any]
) -> str:
try:
# for version facets, return the version name as is
if unparsed_facet["category"] == "version":
return str(unparsed_facet["value"])

# start with base taxonomy and iterate through parent facets list
taxonomy_ref = asdict(taxonomy.TaxonomySpec.get_taxonomy())
for parent in parent_facets:
options_list = taxonomy_ref[parent.category] or []
taxonomy_ref = [x for x in options_list if x["name"] == parent.value][0]

# find target unparsed_facet within taxonomy facet
options_list = taxonomy_ref[unparsed_facet["category"]]
taxonomy_ref = [
x for x in options_list if x["name"] == str(unparsed_facet["value"])
][0]
return str(
taxonomy_ref["display_name"]
or re.sub("_|-", " ", str(taxonomy_ref["name"])).title()
)
except Exception:
logger.warning(f"Display name not found for invalid facet {unparsed_facet}")
return ""

@staticmethod
def _substitute(
source: str, constants: Dict[str, Union[str, int, float]]
Expand Down

0 comments on commit 23ebf39

Please sign in to comment.