-
Notifications
You must be signed in to change notification settings - Fork 1
/
display_base.py
107 lines (76 loc) · 2.97 KB
/
display_base.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
"""Functionality common to displaying dataset and variables metadata."""
from __future__ import annotations
import logging
import typing as t
from dataclasses import dataclass, field
from typing import TYPE_CHECKING, Any
from dash import dcc
from datadoc import state
if TYPE_CHECKING:
from collections.abc import Callable
from dash.development.base_component import Component
from datadoc_model.LanguageStrings import LanguageStrings
from pydantic import BaseModel
from datadoc.frontend.callbacks.utils import MetadataInputTypes
logger = logging.getLogger(__name__)
INPUT_KWARGS = {
"debounce": True,
"style": {"width": "100%"},
"className": "ssb-input",
}
NUMBER_KWARGS = dict(type="number", **INPUT_KWARGS)
DROPDOWN_KWARGS = {
"style": {"width": "100%"},
"className": "ssb-dropdown",
}
def kwargs_factory() -> dict[str, t.Any]:
"""Initialize the field extra_kwargs.
We aren't allowed to directly assign a mutable type like a dict to
a dataclass field.
"""
return INPUT_KWARGS
def get_standard_metadata(metadata: BaseModel, identifier: str) -> MetadataInputTypes:
"""Get a metadata value from the model."""
value = metadata.model_dump()[identifier]
if value is None:
return None
return str(value)
def get_metadata_and_stringify(metadata: BaseModel, identifier: str) -> str:
"""Get a metadata value from the model and cast to string."""
return str(get_standard_metadata(metadata, identifier))
def get_multi_language_metadata(metadata: BaseModel, identifier: str) -> str | None:
"""Get a metadata value supportng multiple languages from the model."""
value: LanguageStrings = getattr(metadata, identifier)
if value is None:
return value
return getattr(value, state.current_metadata_language)
def get_comma_separated_string(metadata: BaseModel, identifier: str) -> str:
"""Get a metadata value which is a list of strings from the model and convert it to a comma separated string."""
value: list[str] = getattr(metadata, identifier)
if value is None:
return ""
return ", ".join(value)
@dataclass
class DisplayMetadata:
"""Controls for how a given metadata field should be displayed."""
identifier: str
display_name: str
description: str
obligatory: bool = False
editable: bool = True
multiple_language_support: bool = False
@dataclass
class DisplayVariablesMetadata(DisplayMetadata):
"""Controls for how a given metadata field should be displayed.
Specific to variable fields.
"""
options: dict[str, list[dict[str, str]]] | None = None
presentation: str | None = "input"
@dataclass
class DisplayDatasetMetadata(DisplayMetadata):
"""Controls for how a given metadata field should be displayed.
Specific to dataset fields.
"""
extra_kwargs: dict[str, Any] = field(default_factory=kwargs_factory)
component: type[Component] = dcc.Input
value_getter: Callable[[BaseModel, str], Any] = get_standard_metadata