| 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 typing import Callable |
| 18 |
|
| 19 |
from ..message import Message |
| 20 |
from .data import Resource |
| 21 |
from .dtd.parse import dtd_parse |
| 22 |
from .fluent.parse import fluent_parse |
| 23 |
from .format import Format, detect_format |
| 24 |
from .inc.parse import inc_parse |
| 25 |
from .ini.parse import ini_parse |
| 26 |
from .plain_json.parse import plain_json_parse |
| 27 |
from .po.parse import po_parse |
| 28 |
from .properties.parse import properties_parse |
| 29 |
from .webext.parse import webext_parse |
| 30 |
|
| 31 |
android_parse: Callable[[str | bytes], Resource[Message, str]] | None |
| 32 |
xliff_parse: Callable[[str | bytes], Resource[Message, str]] | None |
| 33 |
try: |
| 34 |
from .android.parse import android_parse |
| 35 |
from .xliff.parse import xliff_parse |
| 36 |
except ImportError: |
| 37 |
android_parse = None |
| 38 |
xliff_parse = None |
| 39 |
|
| 40 |
|
| 41 |
class UnsupportedResource(Exception): |
| 42 |
pass |
| 43 |
|
| 44 |
|
| 45 |
def parse_resource( |
| 46 |
input: Format | str | None, source: str | bytes | None = None |
| 47 |
) -> Resource[Message, str]: |
| 48 |
""" |
| 49 |
Parse a Resource from its string representation. |
| 50 |
|
| 51 |
The first argument may be an explicit Format, |
| 52 |
the file path as a string, or None. |
| 53 |
For the latter two types, |
| 54 |
an attempt is made to detect the appropriate format. |
| 55 |
|
| 56 |
If the first argument is a string path, |
| 57 |
the `source` argument is optional, |
| 58 |
as the file will be opened and read. |
| 59 |
""" |
| 60 |
if source is None: |
| 61 |
if not isinstance(input, str): |
| 62 |
raise TypeError("Source is required if type is not a string path") |
| 63 |
with open(input, mode="rb") as file: |
| 64 |
source = file.read() |
| 65 |
# TODO post-py38: should be a match |
| 66 |
format = input if isinstance(input, Format) else detect_format(input, source) |
| 67 |
if format == Format.dtd: |
| 68 |
return dtd_parse(source) |
| 69 |
elif format == Format.fluent: |
| 70 |
return fluent_parse(source) |
| 71 |
elif format == Format.inc: |
| 72 |
return inc_parse(source) |
| 73 |
elif format == Format.ini: |
| 74 |
return ini_parse(source) |
| 75 |
elif format == Format.plain_json: |
| 76 |
return plain_json_parse(source) |
| 77 |
elif format == Format.po: |
| 78 |
return po_parse(source) |
| 79 |
elif format == Format.properties: |
| 80 |
return properties_parse(source) |
| 81 |
elif format == Format.webext: |
| 82 |
return webext_parse(source) |
| 83 |
elif format == Format.android and android_parse is not None: |
| 84 |
return android_parse(source) |
| 85 |
elif format == Format.xliff and xliff_parse is not None: |
| 86 |
return xliff_parse(source) |
| 87 |
else: |
| 88 |
raise UnsupportedResource(f"Unsupported resource format: {input}") |