| 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 typing import Callable |
| 19 |
|
| 20 |
from moz.l10n.message import Message |
| 21 |
|
| 22 |
from .data import Resource |
| 23 |
from .dtd.serialize import dtd_serialize |
| 24 |
from .fluent.serialize import fluent_serialize |
| 25 |
from .format import Format |
| 26 |
from .inc.serialize import inc_serialize |
| 27 |
from .ini.serialize import ini_serialize |
| 28 |
from .plain_json.serialize import plain_json_serialize |
| 29 |
from .po.serialize import po_serialize |
| 30 |
from .properties.serialize import properties_serialize |
| 31 |
from .webext.serialize import webext_serialize |
| 32 |
|
| 33 |
android_serialize: ( |
| 34 |
Callable[[Resource[str, str] | Resource[Message, str], bool], Iterator[str]] | None |
| 35 |
) |
| 36 |
xliff_serialize: ( |
| 37 |
Callable[[Resource[str, str] | Resource[Message, str], bool], Iterator[str]] | None |
| 38 |
) |
| 39 |
try: |
| 40 |
from .android.serialize import android_serialize |
| 41 |
from .xliff.serialize import xliff_serialize |
| 42 |
except ImportError: |
| 43 |
android_serialize = None |
| 44 |
xliff_serialize = None |
| 45 |
|
| 46 |
|
| 47 |
def serialize_resource( |
| 48 |
resource: Resource[str, str] | Resource[Message, str], |
| 49 |
format: Format | None = None, |
| 50 |
trim_comments: bool = False, |
| 51 |
) -> Iterator[str]: |
| 52 |
""" |
| 53 |
Serialize a Resource as its string representation. |
| 54 |
|
| 55 |
If `format` is set, it overrides the `resource.format` value. |
| 56 |
|
| 57 |
With `trim_comments`, |
| 58 |
all standalone and attached comments are left out of the serialization. |
| 59 |
""" |
| 60 |
# TODO post-py38: should be a match |
| 61 |
if not format: |
| 62 |
format = resource.format |
| 63 |
if format == Format.dtd: |
| 64 |
return dtd_serialize(resource, trim_comments=trim_comments) |
| 65 |
elif format == Format.fluent: |
| 66 |
return fluent_serialize(resource, trim_comments=trim_comments) |
| 67 |
elif format == Format.inc: |
| 68 |
return inc_serialize(resource, trim_comments=trim_comments) |
| 69 |
elif format == Format.ini: |
| 70 |
return ini_serialize(resource, trim_comments=trim_comments) |
| 71 |
elif format == Format.plain_json: |
| 72 |
return plain_json_serialize(resource, trim_comments=trim_comments) |
| 73 |
elif format == Format.po: |
| 74 |
return po_serialize(resource, trim_comments=trim_comments) |
| 75 |
elif format == Format.properties: |
| 76 |
return properties_serialize(resource, trim_comments=trim_comments) |
| 77 |
elif format == Format.webext: |
| 78 |
return webext_serialize(resource, trim_comments=trim_comments) |
| 79 |
elif format == Format.android and android_serialize is not None: |
| 80 |
return android_serialize(resource, trim_comments) |
| 81 |
elif format == Format.xliff and xliff_serialize is not None: |
| 82 |
return xliff_serialize(resource, trim_comments) |
| 83 |
else: |
| 84 |
raise ValueError(f"Unsupported resource format: {format or resource.format}") |