| 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 enum import Enum |
| 18 |
from io import BytesIO |
| 19 |
from json import JSONDecodeError, loads |
| 20 |
from os.path import splitext |
| 21 |
from typing import Any |
| 22 |
|
| 23 |
# from moz.l10n.resource.xliff.common import xliff_ns |
| 24 |
xliff_ns = { |
| 25 |
"urn:oasis:names:tc:xliff:document:1.0", |
| 26 |
"urn:oasis:names:tc:xliff:document:1.1", |
| 27 |
"urn:oasis:names:tc:xliff:document:1.2", |
| 28 |
} |
| 29 |
|
| 30 |
Format = Enum( |
| 31 |
"Format", |
| 32 |
( |
| 33 |
"android", |
| 34 |
"dtd", |
| 35 |
"fluent", |
| 36 |
"inc", |
| 37 |
"ini", |
| 38 |
"plain_json", |
| 39 |
"po", |
| 40 |
"properties", |
| 41 |
"webext", |
| 42 |
"xliff", |
| 43 |
), |
| 44 |
) |
| 45 |
""" |
| 46 |
Identifier for a supported resource format. |
| 47 |
""" |
| 48 |
|
| 49 |
l10n_extensions = { |
| 50 |
".dtd", |
| 51 |
".ftl", |
| 52 |
".inc", |
| 53 |
".ini", |
| 54 |
".json", |
| 55 |
".properties", |
| 56 |
".po", |
| 57 |
".pot", |
| 58 |
".xlf", |
| 59 |
".xliff", |
| 60 |
".xml", |
| 61 |
} |
| 62 |
"""Extensions used by localization file formats.""" |
| 63 |
|
| 64 |
bilingual_extensions = { |
| 65 |
".po", |
| 66 |
".pot", |
| 67 |
".xlf", |
| 68 |
".xliff", |
| 69 |
} |
| 70 |
""" |
| 71 |
Extensions used by file formats (XLIFF & gettext) that contain |
| 72 |
both the reference and target languages in the same file. |
| 73 |
""" |
| 74 |
|
| 75 |
|
| 76 |
def detect_format(name: str | None, source: bytes | str) -> Format | None: |
| 77 |
""" |
| 78 |
Detect the format of the input based on its file extension |
| 79 |
and/or contents. |
| 80 |
|
| 81 |
Returns a `Format` enum value, or `None` if the input is not recognized. |
| 82 |
""" |
| 83 |
if not name: |
| 84 |
ext = None |
| 85 |
else: |
| 86 |
_, ext = splitext(name) |
| 87 |
if ext == ".dtd": |
| 88 |
return Format.dtd |
| 89 |
elif ext == ".ftl": |
| 90 |
return Format.fluent |
| 91 |
elif ext == ".inc": |
| 92 |
return Format.inc |
| 93 |
elif ext == ".ini": |
| 94 |
return Format.ini |
| 95 |
elif ext == ".properties": |
| 96 |
return Format.properties |
| 97 |
elif ext in {".po", ".pot"}: |
| 98 |
return Format.po |
| 99 |
elif ext in {".xlf", ".xliff"}: |
| 100 |
return Format.xliff |
| 101 |
|
| 102 |
# Try parsing as JSON first, unless we're pretty sure it's XML |
| 103 |
if ext != ".xml": |
| 104 |
try: |
| 105 |
json: dict[str, Any] = loads(source) |
| 106 |
if not is_object_of_strings(json): |
| 107 |
return None |
| 108 |
if all(is_webext_message(m) for m in json.values()): |
| 109 |
return Format.webext |
| 110 |
return Format.plain_json |
| 111 |
except JSONDecodeError: |
| 112 |
pass |
| 113 |
|
| 114 |
# Let's presume the input is XML and look at its root node. |
| 115 |
try: |
| 116 |
from lxml.etree import LxmlError, iterparse |
| 117 |
|
| 118 |
bs = source.encode() if isinstance(source, str) else source |
| 119 |
_, xml_root = next(iterparse(BytesIO(bs), events=("start",))) |
| 120 |
ns = xml_root.nsmap.get(None, None) |
| 121 |
if ns: |
| 122 |
return Format.xliff if ns in xliff_ns else None |
| 123 |
return Format.android if xml_root.tag == "resources" else None |
| 124 |
except ImportError: |
| 125 |
pass |
| 126 |
except LxmlError: |
| 127 |
# Must be separate and after ImportError |
| 128 |
pass |
| 129 |
|
| 130 |
return None |
| 131 |
|
| 132 |
|
| 133 |
def is_object_of_strings(obj: Any) -> bool: |
| 134 |
if not isinstance(obj, dict): |
| 135 |
return False |
| 136 |
for value in obj.values(): |
| 137 |
if isinstance(value, dict): |
| 138 |
if not is_object_of_strings(value): |
| 139 |
return False |
| 140 |
elif not isinstance(value, str): |
| 141 |
return False |
| 142 |
return True |
| 143 |
|
| 144 |
|
| 145 |
def is_webext_message(obj: Any) -> bool: |
| 146 |
if not isinstance(obj, dict): |
| 147 |
return False |
| 148 |
has_message = False |
| 149 |
for key, value in obj.items(): |
| 150 |
if key == "message": |
| 151 |
has_message = True |
| 152 |
elif key == "description": |
| 153 |
pass |
| 154 |
elif key == "placeholders": |
| 155 |
if not isinstance(value, dict): |
| 156 |
return False |
| 157 |
else: |
| 158 |
return False |
| 159 |
return has_message |