Coverage for kwai/core/domain/value_objects/text.py: 89%
35 statements
« prev ^ index » next coverage.py v7.3.0, created at 2023-09-05 17:55 +0000
« prev ^ index » next coverage.py v7.3.0, created at 2023-09-05 17:55 +0000
1"""Module that defines a value object for text content."""
2from dataclasses import dataclass, field
4from kwai.core.domain.value_objects.owner import Owner
5from kwai.core.domain.value_objects.traceable_time import TraceableTime
8@dataclass(frozen=True, kw_only=True, slots=True)
9class LocaleText:
10 """Value object for the content of a news story.
12 Attributes:
13 locale: The locale of the content.
14 format: The format of the content.
15 title: The title of the content.
16 content: The long text of the content.
17 summary: The summary of the content.
18 author: The author of the content.
19 traceable_time: The creation and modification timestamp of the content.
20 """
22 locale: str
23 format: str
24 title: str
25 content: str
26 summary: str
27 author: Owner
28 traceable_time: TraceableTime = field(default_factory=TraceableTime)
31class Text:
32 """A value object containing content in several locales.
34 This class is immutable. A new Text instance will be created and returned when
35 content is added, changed or removed.
36 """
38 def __init__(self, content: dict[str, LocaleText] = None):
39 """Initialize the text value object with content.
41 Args:
42 content: A dictionary with content and locale as key.
43 """
44 self._content = {} if content is None else content.copy()
46 def contains_translation(self, locale: str) -> bool:
47 """Check if the given locale is available as translation.
49 Returns:
50 True when the translation is available.
51 """
52 return locale in self._content
54 def get_translation(self, locale: str) -> LocaleText:
55 """Get a translation.
57 Args:
58 locale: The locale of the content
60 Returns:
61 The content, when available.
63 Raises:
64 KeyError when the locale is not available.
65 """
66 if locale in self._content:
67 return self._content[locale]
68 raise KeyError(f"{locale} is not found.")
70 def add_translation(self, content: LocaleText) -> "Text":
71 """Add a new translation.
73 Args:
74 content: The translated content.
76 Returns:
77 A new Text value object that also contains the translated content.
79 Raises:
80 KeyError: when a translation already existed.
81 """
82 if content.locale not in self._content:
83 return Text(self._content | {content.locale: content})
85 raise KeyError(f"{content.locale} was already used.")
87 def remove_translation(self, content: LocaleText) -> "Text":
88 """Remove a translation.
90 Args:
91 content: The content to remove.
93 Returns:
94 A new Text value object that also contains the rest of the translated
95 content.
97 Raises:
98 KeyError: when the translation does not exist.
99 """
100 if content.locale not in self._content:
101 raise KeyError(f"{content.locale} is not found.")
102 new_dict = self._content.copy()
103 new_dict.pop(content.locale)
104 return Text(new_dict)
106 def replace_translation(self, content: LocaleText):
107 """Replace a translation.
109 Args:
110 content: The content to remove.
112 Returns:
113 A new Text value object that also contains the rest of the translated
114 content.
116 Raises:
117 KeyError: when the translation does not exist.
119 """
120 if content.locale not in self._content:
121 raise KeyError(f"{content.locale} is not found.")
122 return Text(self._content | {content.locale: content})