| 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 collections import OrderedDict |
| 18 |
|
| 19 |
from polib import pofile |
| 20 |
|
| 21 |
from ...message import ( |
| 22 |
Expression, |
| 23 |
FunctionAnnotation, |
| 24 |
Message, |
| 25 |
PatternMessage, |
| 26 |
SelectMessage, |
| 27 |
VariableRef, |
| 28 |
Variants, |
| 29 |
) |
| 30 |
from ..data import Entry, Metadata, Resource, Section |
| 31 |
from ..format import Format |
| 32 |
|
| 33 |
|
| 34 |
def po_parse(source: str | bytes) -> Resource[Message, str]: |
| 35 |
""" |
| 36 |
Parse a .po or .pot file into a message resource |
| 37 |
|
| 38 |
Messages may include the following metadata: |
| 39 |
- `translator-comments` |
| 40 |
- `extracted-comments` |
| 41 |
- `reference`: `f"{file}:{line}"`, separately for each reference |
| 42 |
- `obsolete`: `""` |
| 43 |
- `flag`: separately for each flag |
| 44 |
- `plural` |
| 45 |
""" |
| 46 |
pf = pofile(source if isinstance(source, str) else source.decode()) |
| 47 |
res_comment = pf.header.lstrip("\n").rstrip() |
| 48 |
res_meta: list[Metadata[str]] = [ |
| 49 |
Metadata(key, value.strip()) for key, value in pf.metadata.items() |
| 50 |
] |
| 51 |
sections: dict[str | None, Section[Message, str]] = OrderedDict() |
| 52 |
for pe in pf: |
| 53 |
meta: list[Metadata[str]] = [] |
| 54 |
if pe.tcomment: |
| 55 |
meta.append(Metadata("translator-comments", pe.tcomment)) |
| 56 |
if pe.comment: |
| 57 |
meta.append(Metadata("extracted-comments", pe.comment)) |
| 58 |
for file, line in pe.occurrences: |
| 59 |
meta.append(Metadata("reference", f"{file}:{line}")) |
| 60 |
if pe.obsolete: |
| 61 |
meta.append(Metadata("obsolete", "true")) |
| 62 |
for flag in pe.flags: |
| 63 |
meta.append(Metadata("flag", flag)) |
| 64 |
if pe.msgid_plural: |
| 65 |
meta.append(Metadata("plural", pe.msgid_plural)) |
| 66 |
if pe.msgstr_plural: |
| 67 |
keys = list(pe.msgstr_plural) |
| 68 |
keys.sort() |
| 69 |
sel = Expression(VariableRef("n"), FunctionAnnotation("number")) |
| 70 |
variants: Variants = { |
| 71 |
(str(idx),): ( |
| 72 |
[pe.msgstr_plural[idx]] if idx in pe.msgstr_plural else [] |
| 73 |
) |
| 74 |
for idx in range(keys[-1] + 1) |
| 75 |
} |
| 76 |
value: Message = SelectMessage([sel], variants) |
| 77 |
else: |
| 78 |
value = PatternMessage([pe.msgstr]) |
| 79 |
entry = Entry((pe.msgid,), value, meta=meta) |
| 80 |
if pe.msgctxt in sections: |
| 81 |
sections[pe.msgctxt].entries.append(entry) |
| 82 |
else: |
| 83 |
sections[pe.msgctxt] = Section((pe.msgctxt,) if pe.msgctxt else (), [entry]) |
| 84 |
return Resource(Format.po, list(sections.values()), res_comment, res_meta) |