Debian Codegraph @

third_party/python/moz.l10n/moz/l10n/resource/data.py

raw
1 # Copyright Mozilla Foundation
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 from __future__ import annotations
16
17 from dataclasses import dataclass, field
18 from typing import Generic, TypeVar
19
20 from .format import Format
21
22 __all__ = [
23 "Comment",
24 "Entry",
25 "Metadata",
26 "Resource",
27 "Section",
28 ]
29
30 M = TypeVar("M")
31 """
32 The metadata value type.
33 """
34
35 V = TypeVar("V")
36 """
37 The Message value type.
38 """
39
40
41 @dataclass
42 class LinePos:
43 """
44 The source line position of an entry or section header.
45 """
46
47 start: int
48 """
49 The starting line of the entry or section.
50 May be less than `value` if preceded by a comment.
51 """
52
53 key: int
54 """
55 The start line of the entry or section header key or name.
56 """
57
58 value: int
59 """
60 The start line of the entry pattern or section header.
61 """
62
63 end: int
64 """
65 The line one past the end of the entry or section header.
66 """
67
68
69 @dataclass
70 class Metadata(Generic[M]):
71 """
72 Metadata is attached to a resource, section, or a single entry.
73
74 The type parameter defines the metadata value type.
75 """
76
77 key: str
78 """
79 A non-empty string keyword.
80
81 Most likely a sequence of `a-z` characters,
82 but may technically contain any characters
83 which might require escaping in the syntax.
84 """
85
86 value: M
87 """
88 The metadata contents.
89
90 String values have all their character \\escapes processed.
91 """
92
93
94 @dataclass
95 class Comment:
96 comment: str
97 """
98 A standalone comment.
99
100 May contain multiple lines separated by newline characters.
101 Lines should have any comment-start sigil and up to one space trimmed from the start,
102 along with any trailing whitespace.
103
104 An empty or whitespace-only comment will be represented by an empty string.
105 """
106
107
108 @dataclass
109 class Entry(Generic[V, M]):
110 """
111 A message entry.
112
113 The first type parameter defines the Message value type,
114 and the second one defines the metadata value type.
115 """
116
117 id: tuple[str, ...]
118 """
119 The entry identifier.
120
121 This MUST be a non-empty array of non-empty `string` values.
122
123 The entry identifiers are not normalized,
124 i.e. they do not include this identifier.
125
126 In a valid resource, each entry has a distinct normalized identifier,
127 i.e. the concatenation of its section header identifier (if any) and its own.
128 """
129
130 value: V
131 """
132 The value of an entry, i.e. the message.
133
134 String values have all their character \\escapes processed.
135 """
136
137 comment: str = ""
138 """
139 A comment on this entry.
140
141 May contain multiple lines separated by newline characters.
142 Lines should have any comment-start sigil and up to one space trimmed from the start,
143 along with any trailing whitespace.
144
145 An empty or whitespace-only comment will be represented by an empty string.
146 """
147
148 meta: list[Metadata[M]] = field(default_factory=list)
149 """
150 Metadata attached to this entry.
151 """
152
153 linepos: LinePos | None = None
154 """
155 The parsed position of the entry,
156 available for some formats.
157 """
158
159
160 @dataclass
161 class Section(Generic[V, M]):
162 """
163 A section of a resource.
164
165 The first type parameter defines the Message value type,
166 and the second one defines the metadata value type.
167 """
168
169 id: tuple[str, ...]
170 """
171 The section identifier.
172
173 Each `string` part of the identifier MUST be a non-empty string.
174
175 The top-level or anonymous section has an empty `id` array.
176 The resource syntax requires this array to be non-empty
177 for all sections after the first one,
178 but empty identifier arrays MAY be used
179 when this data model is used to represent other message resource formats,
180 such as Fluent FTL files.
181
182 The entry identifiers are not normalized,
183 i.e. they do not include this identifier.
184 """
185
186 entries: list[Entry[V, M] | Comment]
187 """
188 Section entries consist of message entries and comments.
189
190 Empty lines are not included in the data model.
191 """
192
193 comment: str = ""
194 """
195 A comment on the whole section, which applies to all of its entries.
196
197 May contain multiple lines separated by newline characters.
198 Lines should have any comment-start sigil and up to one space trimmed from the start,
199 along with any trailing whitespace.
200
201 An empty or whitespace-only comment will be represented by an empty string.
202 """
203
204 meta: list[Metadata[M]] = field(default_factory=list)
205 """
206 Metadata attached to this section.
207 """
208
209 linepos: LinePos | None = None
210 """
211 The parsed position of the section,
212 available for some formats.
213 """
214
215
216 @dataclass
217 class Resource(Generic[V, M]):
218 """
219 A message resource.
220
221 The first type parameter defines the Message value type,
222 and the second one defines the metadata value type.
223 """
224
225 format: Format | None
226 """
227 The serialization format for the resource, if any.
228 """
229
230 sections: list[Section[V, M]]
231 """
232 The body of a resource, consisting of an array of sections.
233
234 A valid resource may have an empty sections array.
235 """
236
237 comment: str = ""
238 """
239 A comment on the whole resource, which applies to all of its sections and entries.
240
241 May contain multiple lines separated by newline characters.
242 Lines should have any comment-start sigil and up to one space trimmed from the start,
243 along with any trailing whitespace.
244
245 An empty or whitespace-only comment will be represented by an empty string.
246 """
247
248 meta: list[Metadata[M]] = field(default_factory=list)
249 """
250 Metadata attached to the whole resource.
251 """