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

1"""Module that defines a value object for text content.""" 

2from dataclasses import dataclass, field 

3 

4from kwai.core.domain.value_objects.owner import Owner 

5from kwai.core.domain.value_objects.traceable_time import TraceableTime 

6 

7 

8@dataclass(frozen=True, kw_only=True, slots=True) 

9class LocaleText: 

10 """Value object for the content of a news story. 

11 

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 """ 

21 

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) 

29 

30 

31class Text: 

32 """A value object containing content in several locales. 

33 

34 This class is immutable. A new Text instance will be created and returned when 

35 content is added, changed or removed. 

36 """ 

37 

38 def __init__(self, content: dict[str, LocaleText] = None): 

39 """Initialize the text value object with content. 

40 

41 Args: 

42 content: A dictionary with content and locale as key. 

43 """ 

44 self._content = {} if content is None else content.copy() 

45 

46 def contains_translation(self, locale: str) -> bool: 

47 """Check if the given locale is available as translation. 

48 

49 Returns: 

50 True when the translation is available. 

51 """ 

52 return locale in self._content 

53 

54 def get_translation(self, locale: str) -> LocaleText: 

55 """Get a translation. 

56 

57 Args: 

58 locale: The locale of the content 

59 

60 Returns: 

61 The content, when available. 

62 

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.") 

69 

70 def add_translation(self, content: LocaleText) -> "Text": 

71 """Add a new translation. 

72 

73 Args: 

74 content: The translated content. 

75 

76 Returns: 

77 A new Text value object that also contains the translated content. 

78 

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}) 

84 

85 raise KeyError(f"{content.locale} was already used.") 

86 

87 def remove_translation(self, content: LocaleText) -> "Text": 

88 """Remove a translation. 

89 

90 Args: 

91 content: The content to remove. 

92 

93 Returns: 

94 A new Text value object that also contains the rest of the translated 

95 content. 

96 

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) 

105 

106 def replace_translation(self, content: LocaleText): 

107 """Replace a translation. 

108 

109 Args: 

110 content: The content to remove. 

111 

112 Returns: 

113 A new Text value object that also contains the rest of the translated 

114 content. 

115 

116 Raises: 

117 KeyError: when the translation does not exist. 

118 

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})