Coverage for rendercv/data/models/locale.py: 100%
27 statements
« prev ^ index » next coverage.py v7.6.10, created at 2025-01-26 00:25 +0000
« prev ^ index » next coverage.py v7.6.10, created at 2025-01-26 00:25 +0000
1"""
2The `rendercv.models.locale` module contains the data model of the
3`locale` field of the input file.
4"""
6from typing import Annotated, Literal, Optional
8import annotated_types as at
9import pydantic
10import pydantic_extra_types.language_code
12from .base import RenderCVBaseModelWithoutExtraKeys
15class Locale(RenderCVBaseModelWithoutExtraKeys):
16 """This class is the data model of the locale catalog. The values of each field
17 updates the `locale` dictionary.
18 """
20 language: pydantic_extra_types.language_code.LanguageAlpha2 = pydantic.Field(
21 default="en", # type: ignore
22 title="Language",
23 description=(
24 "The language as an ISO 639 alpha-2 code. It is used for hyphenation"
25 " patterns."
26 ),
27 )
28 phone_number_format: Optional[Literal["national", "international", "E164"]] = (
29 pydantic.Field(
30 default="national",
31 title="Phone Number Format",
32 description=(
33 "If 'national', phone numbers are formatted without the country code."
34 " If 'international', phone numbers are formatted with the country"
35 ' code. The default value is "national"'
36 ),
37 )
38 )
39 page_numbering_template: str = pydantic.Field(
40 default="NAME - Page PAGE_NUMBER of TOTAL_PAGES",
41 title="Page Numbering Template",
42 description=(
43 "The template of the page numbering. The following placeholders can be"
44 " used:\n- NAME: The name of the person\n- PAGE_NUMBER: The current page"
45 " number\n- TOTAL_PAGES: The total number of pages\n- TODAY: Today's date"
46 ' with `locale.date_template`\nThe default value is "NAME -'
47 ' Page PAGE_NUMBER of TOTAL_PAGES".'
48 ),
49 )
50 last_updated_date_template: str = pydantic.Field(
51 default="Last updated in TODAY",
52 title="Last Updated Date Template",
53 description=(
54 "The template of the last updated date. The following placeholders can be"
55 " used:\n- TODAY: Today's date with `locale.date_template`\nThe"
56 ' default value is "Last updated in TODAY".'
57 ),
58 )
59 date_template: Optional[str] = pydantic.Field(
60 default="MONTH_ABBREVIATION YEAR",
61 title="Date Template",
62 description=(
63 "The template of the date. The following placeholders can be"
64 " used:\n-FULL_MONTH_NAME: Full name of the month\n- MONTH_ABBREVIATION:"
65 " Abbreviation of the month\n- MONTH: Month as a number\n-"
66 " MONTH_IN_TWO_DIGITS: Month as a number in two digits\n- YEAR: Year as a"
67 " number\n- YEAR_IN_TWO_DIGITS: Year as a number in two digits\nThe"
68 ' default value is "MONTH_ABBREVIATION YEAR".'
69 ),
70 )
71 month: Optional[str] = pydantic.Field(
72 default="month",
73 title='Translation of "Month"',
74 description='Translation of the word "month" in the locale.',
75 )
76 months: Optional[str] = pydantic.Field(
77 default="months",
78 title='Translation of "Months"',
79 description='Translation of the word "months" in the locale.',
80 )
81 year: Optional[str] = pydantic.Field(
82 default="year",
83 title='Translation of "Year"',
84 description='Translation of the word "year" in the locale.',
85 )
86 years: Optional[str] = pydantic.Field(
87 default="years",
88 title='Translation of "Years"',
89 description='Translation of the word "years" in the locale.',
90 )
91 present: Optional[str] = pydantic.Field(
92 default="present",
93 title='Translation of "Present"',
94 description='Translation of the word "present" in the locale.',
95 )
96 to: Optional[str] = pydantic.Field(
97 default="–", # NOQA: RUF001
98 title='Translation of "To"',
99 description=(
100 "The word or character used to indicate a range in the locale (e.g.,"
101 ' "2020 - 2021").'
102 ),
103 )
104 abbreviations_for_months: Optional[
105 Annotated[list[str], at.Len(min_length=12, max_length=12)]
106 ] = pydantic.Field(
107 # Month abbreviations are taken from
108 # https://web.library.yale.edu/cataloging/months:
109 default=[
110 "Jan",
111 "Feb",
112 "Mar",
113 "Apr",
114 "May",
115 "June",
116 "July",
117 "Aug",
118 "Sept",
119 "Oct",
120 "Nov",
121 "Dec",
122 ],
123 title="Abbreviations of Months",
124 description="Abbreviations of the months in the locale.",
125 )
126 full_names_of_months: Optional[
127 Annotated[list[str], at.Len(min_length=12, max_length=12)]
128 ] = pydantic.Field(
129 default=[
130 "January",
131 "February",
132 "March",
133 "April",
134 "May",
135 "June",
136 "July",
137 "August",
138 "September",
139 "October",
140 "November",
141 "December",
142 ],
143 title="Full Names of Months",
144 description="Full names of the months in the locale.",
145 )
147 @pydantic.field_validator(
148 "month",
149 "months",
150 "year",
151 "years",
152 "present",
153 "abbreviations_for_months",
154 "to",
155 "full_names_of_months",
156 "phone_number_format",
157 "date_template",
158 )
159 @classmethod
160 def update_locale(cls, value: str, info: pydantic.ValidationInfo) -> str:
161 """Update the `locale` dictionary."""
162 if value:
163 locale[info.field_name] = value # type: ignore
165 return value
168# The dictionary below will be overwritten by Locale class, which will contain
169# month names, month abbreviations, and other locale-specific strings.
170locale: dict[str, str | list[str]] = {}
172# Initialize even if the RenderCVDataModel is not called (to make `format_date` function
173# work on its own):
174Locale()