-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Alex Richey
authored and
Alex Richey
committed
Nov 22, 2024
1 parent
95805f5
commit b859aca
Showing
8 changed files
with
389 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
from pydantic import BaseModel | ||
from typing import Any | ||
|
||
from dcpy.models.product.dataset.metadata_v2 import Dataset | ||
from dcpy.models.product.data_dictionary import DataDictionary, FieldSet | ||
from dcpy.utils.logging import logger | ||
|
||
|
||
class Font(BaseModel): | ||
size: float | None = None | ||
rgb: str | None = None | ||
italic: bool = False | ||
bold: bool = False | ||
|
||
|
||
class CellStyle(BaseModel): | ||
font: Font = Font() | ||
borders: list[str] | None = None | ||
text_alignment_vertical: str | None = None | ||
|
||
|
||
class Cell(BaseModel): | ||
value: Any | list["Cell"] # can be a value or inline cells | ||
style: CellStyle = CellStyle() | ||
|
||
|
||
# might not be needed? Probably just use inline cells | ||
class ValueWithDescription(Cell): | ||
"""e.g. | ||
<b>Dataset Tags</b> | ||
<i>my long-winded explanation of dataset tags here...</i> | ||
""" | ||
|
||
value: str | ||
description: str | ||
|
||
|
||
class Row(BaseModel): | ||
cells: list[Cell] | ||
merge_cells: bool = False | ||
height: float | None = None | ||
|
||
|
||
class Table(BaseModel): | ||
title: str | ||
subtitle: str | None | ||
description: str | ||
rows: list[Row] | ||
cell_widths: list[float | None] = [] | ||
|
||
def total_cols(self): | ||
return max(len(r.cells) for r in self.rows) | ||
|
||
|
||
BLUE = "FF009DDC" | ||
TITLE_FONT_SIZE = 18.0 | ||
|
||
|
||
def _make_title_subtitle_cell(title: str, subtitle: str): | ||
return Cell( | ||
style=CellStyle(text_alignment_vertical="bottom"), | ||
value=[ | ||
Cell( | ||
value=title + " - ", | ||
style=CellStyle( | ||
font=Font(bold=True, size=TITLE_FONT_SIZE), | ||
), | ||
), | ||
Cell( | ||
value=subtitle, | ||
style=CellStyle( | ||
font=Font(bold=True, rgb=BLUE, size=TITLE_FONT_SIZE), | ||
), | ||
), | ||
], | ||
) | ||
|
||
|
||
def make_table( | ||
*, | ||
title: str, | ||
subtitle: str, | ||
description: str, | ||
fields: list[str], | ||
dataset: Dataset, | ||
data_dict: DataDictionary, | ||
) -> Table: | ||
rows = [] | ||
attributes = dataset.attributes.model_dump() | ||
|
||
for f in fields: | ||
dd_field = data_dict.dataset.fields[f] | ||
value = attributes.get(f) | ||
if value is None: | ||
logger.warning(f"Metadata field is empty for {f}") | ||
if type(value) is list: | ||
value = ", ".join(value) | ||
|
||
description_paragraphs = [ | ||
dd_field.extra_description, | ||
dd_field.custom.get("oti_extra_notes"), | ||
] | ||
|
||
rows.append( | ||
Row( | ||
cells=[ | ||
Cell( # <b>field title \n summary<b> for each field | ||
value=[ | ||
Cell( | ||
value="\n" + dd_field.summary + "\n", | ||
style=CellStyle( | ||
font=Font(bold=True, size=11), | ||
), | ||
), | ||
Cell( | ||
value=( | ||
"".join( | ||
[ | ||
p + "\n" | ||
for p in description_paragraphs | ||
if p is not None | ||
] | ||
) | ||
), | ||
style=CellStyle( | ||
font=Font(size=9, italic=True), | ||
), | ||
), | ||
], | ||
), | ||
Cell( # Value | ||
value=value, | ||
style=CellStyle(font=Font(italic=True)), | ||
), | ||
] | ||
) | ||
) | ||
|
||
return Table( | ||
title=title, | ||
subtitle=subtitle, | ||
cell_widths=[50, 80], | ||
description=description, | ||
rows=[ | ||
Row( | ||
merge_cells=True, | ||
height=30, | ||
cells=[_make_title_subtitle_cell(title, subtitle)], | ||
), | ||
Row( | ||
merge_cells=True, | ||
height=50, | ||
cells=[ | ||
Cell( | ||
value=description, | ||
style=CellStyle( | ||
text_alignment_vertical="center", | ||
font=Font(italic=True, rgb=BLUE), | ||
), | ||
), | ||
], | ||
), | ||
*rows, | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from .dataset.metadata_v2 import CustomizableBase | ||
from dcpy.models.base import TemplatedYamlReader | ||
|
||
|
||
class FieldDefinition(CustomizableBase): | ||
summary: str | ||
extra_description: str | ||
|
||
|
||
class FieldSet(CustomizableBase): | ||
fields: dict[str, FieldDefinition] = {} | ||
|
||
|
||
class DataDictionary(CustomizableBase, TemplatedYamlReader): | ||
org: FieldSet = FieldSet() | ||
product: FieldSet = FieldSet() | ||
dataset: FieldSet = FieldSet() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.