Coverage for rendercv/data/models/locale_catalog.py: 100%

23 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-10-07 17:51 +0000

1""" 

2The `rendercv.models.locale_catalog` module contains the data model of the 

3`locale_catalog` field of the input file. 

4""" 

5 

6from typing import Annotated, Literal, Optional 

7 

8import annotated_types as at 

9import pydantic 

10 

11from .base import RenderCVBaseModelWithoutExtraKeys 

12 

13 

14class LocaleCatalog(RenderCVBaseModelWithoutExtraKeys): 

15 """This class is the data model of the locale catalog. The values of each field 

16 updates the `locale_catalog` dictionary. 

17 """ 

18 

19 phone_number_format: Optional[Literal["national", "international", "E164"]] = ( 

20 pydantic.Field( 

21 default="national", 

22 title="Phone Number Format", 

23 description=( 

24 "If 'national', phone numbers are formatted without the country code." 

25 " If 'international', phone numbers are formatted with the country" 

26 " code. The default value is 'national'." 

27 ), 

28 ) 

29 ) 

30 date_style: Optional[str] = pydantic.Field( 

31 default="MONTH_ABBREVIATION YEAR", 

32 title="Date Style", 

33 description=( 

34 "The style of the date. The following placeholders can be used:\n-" 

35 " FULL_MONTH_NAME: Full name of the month\n- MONTH_ABBREVIATION:" 

36 " Abbreviation of the month\n- MONTH: Month as a number\n-" 

37 " MONTH_IN_TWO_DIGITS: Month as a number in two digits\n- YEAR: Year as a" 

38 " number\n- YEAR_IN_TWO_DIGITS: Year as a number in two digits\nThe default" 

39 ' value is "MONTH_ABBREVIATION YEAR".' 

40 ), 

41 ) 

42 month: Optional[str] = pydantic.Field( 

43 default="month", 

44 title='Translation of "Month"', 

45 description='Translation of the word "month" in the locale.', 

46 ) 

47 months: Optional[str] = pydantic.Field( 

48 default="months", 

49 title='Translation of "Months"', 

50 description='Translation of the word "months" in the locale.', 

51 ) 

52 year: Optional[str] = pydantic.Field( 

53 default="year", 

54 title='Translation of "Year"', 

55 description='Translation of the word "year" in the locale.', 

56 ) 

57 years: Optional[str] = pydantic.Field( 

58 default="years", 

59 title='Translation of "Years"', 

60 description='Translation of the word "years" in the locale.', 

61 ) 

62 present: Optional[str] = pydantic.Field( 

63 default="present", 

64 title='Translation of "Present"', 

65 description='Translation of the word "present" in the locale.', 

66 ) 

67 to: Optional[str] = pydantic.Field( 

68 default="–", # en dash 

69 title='Translation of "To"', 

70 description=( 

71 "The word or character used to indicate a range in the locale (e.g.," 

72 ' "2020 - 2021").' 

73 ), 

74 ) 

75 abbreviations_for_months: Optional[ 

76 Annotated[list[str], at.Len(min_length=12, max_length=12)] 

77 ] = pydantic.Field( 

78 # Month abbreviations are taken from 

79 # https://web.library.yale.edu/cataloging/months: 

80 default=[ 

81 "Jan", 

82 "Feb", 

83 "Mar", 

84 "Apr", 

85 "May", 

86 "June", 

87 "July", 

88 "Aug", 

89 "Sept", 

90 "Oct", 

91 "Nov", 

92 "Dec", 

93 ], 

94 title="Abbreviations of Months", 

95 description="Abbreviations of the months in the locale.", 

96 ) 

97 full_names_of_months: Optional[ 

98 Annotated[list[str], at.Len(min_length=12, max_length=12)] 

99 ] = pydantic.Field( 

100 default=[ 

101 "January", 

102 "February", 

103 "March", 

104 "April", 

105 "May", 

106 "June", 

107 "July", 

108 "August", 

109 "September", 

110 "October", 

111 "November", 

112 "December", 

113 ], 

114 title="Full Names of Months", 

115 description="Full names of the months in the locale.", 

116 ) 

117 

118 @pydantic.field_validator( 

119 "month", 

120 "months", 

121 "year", 

122 "years", 

123 "present", 

124 "abbreviations_for_months", 

125 "to", 

126 "full_names_of_months", 

127 "phone_number_format", 

128 "date_style", 

129 ) 

130 @classmethod 

131 def update_locale_catalog(cls, value: str, info: pydantic.ValidationInfo) -> str: 

132 """Update the `locale_catalog` dictionary.""" 

133 if value: 

134 LOCALE_CATALOG[info.field_name] = value # type: ignore 

135 

136 return value 

137 

138 

139# The dictionary below will be overwritten by LocaleCatalog class, which will contain 

140# month names, month abbreviations, and other locale-specific strings. 

141LOCALE_CATALOG: dict[str, str | list[str]] = dict() 

142 

143# Initialize even if the RenderCVDataModel is not called (to make `format_date` function 

144# work on its own): 

145LocaleCatalog()