| 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 |
from re import search |
| 19 |
from typing import Any |
| 20 |
|
| 21 |
from moz.l10n.message import Message, PatternMessage |
| 22 |
|
| 23 |
from ..data import Entry, Resource |
| 24 |
|
| 25 |
|
| 26 |
def ini_serialize( |
| 27 |
resource: Resource[str, Any] | Resource[Message, Any], |
| 28 |
trim_comments: bool = False, |
| 29 |
) -> Iterator[str]: |
| 30 |
""" |
| 31 |
Serialize a resource as the contents of an .ini file. |
| 32 |
|
| 33 |
Anonymous sections are not supported. |
| 34 |
Multi-part section and message identifiers will be joined with `.` between each part. |
| 35 |
|
| 36 |
Metadata is not supported. |
| 37 |
|
| 38 |
Comment lines not starting with `#` will be separated from their `#` prefix with a space. |
| 39 |
|
| 40 |
Yields each entry, continuation line, comment, and empty line separately. |
| 41 |
Re-parsing a serialized .ini file is not guaranteed to result in the same Resource, |
| 42 |
as the serialization may lose information about metadata. |
| 43 |
""" |
| 44 |
|
| 45 |
at_empty_line = True |
| 46 |
|
| 47 |
def comment(comment: str, meta: Any, standalone: bool) -> Iterator[str]: |
| 48 |
nonlocal at_empty_line |
| 49 |
if trim_comments: |
| 50 |
return |
| 51 |
if meta: |
| 52 |
raise ValueError("Metadata is not supported") |
| 53 |
if comment: |
| 54 |
if standalone and not at_empty_line: |
| 55 |
yield "\n" |
| 56 |
for line in comment.strip("\n").split("\n"): |
| 57 |
if not line or line.isspace(): |
| 58 |
yield "#\n" |
| 59 |
else: |
| 60 |
line = line.rstrip() + "\n" |
| 61 |
yield f"#{line}" if line.startswith("#") else f"# {line}" |
| 62 |
if standalone: |
| 63 |
yield "\n" |
| 64 |
at_empty_line = True |
| 65 |
|
| 66 |
yield from comment(resource.comment, resource.meta, True) |
| 67 |
for section in resource.sections: |
| 68 |
if not section.id: |
| 69 |
raise ValueError("Anonymous sections are not supported") |
| 70 |
yield from comment(section.comment, section.meta, False) |
| 71 |
yield f"[{id_str(section.id)}]\n" |
| 72 |
at_empty_line = False |
| 73 |
for entry in section.entries: |
| 74 |
if isinstance(entry, Entry): |
| 75 |
yield from comment(entry.comment, entry.meta, False) |
| 76 |
msg = entry.value |
| 77 |
if isinstance(msg, str): |
| 78 |
value = msg |
| 79 |
elif isinstance(msg, PatternMessage) and all( |
| 80 |
isinstance(p, str) for p in msg.pattern |
| 81 |
): |
| 82 |
value = "".join(msg.pattern) # type: ignore[arg-type] |
| 83 |
else: |
| 84 |
raise ValueError(f"Unsupported message for {entry.id}: {msg}") |
| 85 |
lines = value.rstrip().splitlines() |
| 86 |
yield ( |
| 87 |
f"{id_str(entry.id)}={lines.pop(0) if lines else ''}".rstrip() |
| 88 |
+ "\n" |
| 89 |
) |
| 90 |
for line in lines: |
| 91 |
ls = line.rstrip() |
| 92 |
yield f" {ls}\n" if ls else "\n" |
| 93 |
at_empty_line = False |
| 94 |
else: |
| 95 |
yield from comment(entry.comment, None, True) |
| 96 |
|
| 97 |
|
| 98 |
def id_str(id: tuple[str, ...]) -> str: |
| 99 |
name = ".".join(id) |
| 100 |
if search(r"^\s|[\n:=[\]]|\s$", name): |
| 101 |
raise ValueError(f"Unsupported character in identifier: {id}") |
| 102 |
return name |