| 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.abc import Iterator |
| 18 |
|
| 19 |
from polib import POEntry, POFile |
| 20 |
|
| 21 |
from ...message import FunctionAnnotation, Message, PatternMessage, SelectMessage |
| 22 |
from ..data import Entry, Resource |
| 23 |
|
| 24 |
|
| 25 |
def po_serialize( |
| 26 |
resource: Resource[str, str] | Resource[Message, str], |
| 27 |
trim_comments: bool = False, |
| 28 |
wrapwidth: int = 78, |
| 29 |
) -> Iterator[str]: |
| 30 |
""" |
| 31 |
Serialize a resource as the contents of a .po file. |
| 32 |
|
| 33 |
Multi-part identifiers will be joined with `.` between each part. |
| 34 |
Section identifiers are serialized as message contexts. |
| 35 |
Comments and metadata on sections is not supported. |
| 36 |
|
| 37 |
Yields each entry and empty line separately. |
| 38 |
Re-parsing a serialized .properties file is not guaranteed to result in the same Resource, |
| 39 |
as the serialization may lose information about message identifiers. |
| 40 |
""" |
| 41 |
|
| 42 |
pf = POFile(wrapwidth=wrapwidth) |
| 43 |
if not trim_comments: |
| 44 |
pf.header = resource.comment.rstrip() + "\n" |
| 45 |
pf.metadata = {m.key: str(m.value) for m in resource.meta} |
| 46 |
yield str(pf) |
| 47 |
|
| 48 |
for section in resource.sections: |
| 49 |
if section.comment: |
| 50 |
raise ValueError(f"Section comments are not supported: {section.id}") |
| 51 |
if section.meta: |
| 52 |
raise ValueError(f"Section metadata is not supported: {section.id}") |
| 53 |
context = ".".join(section.id) if section.id else None |
| 54 |
for entry in section.entries: |
| 55 |
if isinstance(entry, Entry): |
| 56 |
pe = POEntry(msgctxt=context, msgid=".".join(entry.id)) |
| 57 |
msg = entry.value |
| 58 |
if isinstance(msg, str): |
| 59 |
pe.msgstr = msg |
| 60 |
elif isinstance(msg, PatternMessage) and all( |
| 61 |
isinstance(p, str) for p in msg.pattern |
| 62 |
): |
| 63 |
pe.msgstr = "".join(msg.pattern) # type: ignore[arg-type] |
| 64 |
elif ( |
| 65 |
isinstance(msg, SelectMessage) |
| 66 |
and not msg.declarations |
| 67 |
and len(msg.selectors) == 1 |
| 68 |
and isinstance(msg.selectors[0].annotation, FunctionAnnotation) |
| 69 |
and msg.selectors[0].annotation.name == "number" |
| 70 |
and not msg.selectors[0].annotation.options |
| 71 |
and all( |
| 72 |
len(keys) == 1 and all(isinstance(p, str) for p in pattern) |
| 73 |
for keys, pattern in msg.variants.items() |
| 74 |
) |
| 75 |
): |
| 76 |
values = [ |
| 77 |
"".join(pattern) # type: ignore[arg-type] |
| 78 |
for pattern in msg.variants.values() |
| 79 |
] |
| 80 |
if len(values) == 1: |
| 81 |
pe.msgstr = values[0] |
| 82 |
else: |
| 83 |
pe.msgstr_plural = {idx: str for idx, str in enumerate(values)} |
| 84 |
else: |
| 85 |
raise ValueError( |
| 86 |
f"Value for {entry.id} is not supported: {entry.value}" |
| 87 |
) |
| 88 |
if not trim_comments: |
| 89 |
pe.tcomment = entry.comment.rstrip() |
| 90 |
for m in entry.meta: |
| 91 |
if m.key == "obsolete": |
| 92 |
pe.obsolete = m.value != "false" |
| 93 |
elif m.key == "plural": |
| 94 |
pe.msgid_plural = m.value |
| 95 |
elif not trim_comments: |
| 96 |
if m.key == "translator-comments": |
| 97 |
cs = (m.value).lstrip("\n").rstrip() |
| 98 |
pe.tcomment = f"{pe.tcomment}\n{cs}" if pe.tcomment else cs |
| 99 |
elif m.key == "extracted-comments": |
| 100 |
pe.comment = (m.value).lstrip("\n").rstrip() |
| 101 |
elif m.key == "reference": |
| 102 |
pos = m.value.split(":", 1) |
| 103 |
pe.occurrences.append( |
| 104 |
(pos[0], pos[1]) if len(pos) == 2 else (m.value, "") |
| 105 |
) |
| 106 |
elif m.key == "flag": |
| 107 |
pe.flags.append(m.value) |
| 108 |
else: |
| 109 |
raise ValueError( |
| 110 |
f'Unsupported meta entry "{m.key}" for {entry.id}: {m.value}' |
| 111 |
) |
| 112 |
if not pe.obsolete or not trim_comments: |
| 113 |
yield "\n" |
| 114 |
yield pe.__unicode__(wrapwidth=wrapwidth) |
| 115 |
else: |
| 116 |
raise ValueError( |
| 117 |
f"Standalone comments are not supported: {entry.comment}" |
| 118 |
) |