| 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 io import StringIO |
| 18 |
from typing import Any, Generator, TextIO |
| 19 |
|
| 20 |
from iniparse import ini # type: ignore[import-untyped] |
| 21 |
|
| 22 |
from moz.l10n.message import Message, PatternMessage |
| 23 |
|
| 24 |
from ..data import Comment, Entry, Resource, Section |
| 25 |
from ..format import Format |
| 26 |
|
| 27 |
|
| 28 |
def ini_parse(source: TextIO | str | bytes) -> Resource[Message, Any]: |
| 29 |
""" |
| 30 |
Parse an .ini file into a message resource. |
| 31 |
|
| 32 |
The parsed resource will not include any metadata. |
| 33 |
""" |
| 34 |
if isinstance(source, str): |
| 35 |
file: TextIO = StringIO(source) |
| 36 |
elif isinstance(source, bytes): |
| 37 |
file = StringIO(source.decode()) |
| 38 |
else: |
| 39 |
file = source |
| 40 |
cfg = ini.INIConfig(file, optionxformvalue=None) |
| 41 |
|
| 42 |
resource = Resource[Message, str](Format.ini, []) |
| 43 |
section: Section[Message, str] | None = None |
| 44 |
pattern: list[str] | None = None |
| 45 |
comment = "" |
| 46 |
|
| 47 |
def add_comment(cl: str | None) -> None: |
| 48 |
nonlocal comment |
| 49 |
if cl is not None: |
| 50 |
cv = cl[1:] if cl and cl.startswith(" ") else cl |
| 51 |
comment = f"{comment}\n{cv}" if comment else cv |
| 52 |
|
| 53 |
for line in ini_lines(cfg._data): |
| 54 |
if pattern: |
| 55 |
if isinstance(line, ini.ContinuationLine): |
| 56 |
pattern[0] += "\n" + line.value |
| 57 |
continue |
| 58 |
elif isinstance(line, ini.EmptyLine): |
| 59 |
pattern[0] += "\n" |
| 60 |
else: |
| 61 |
pattern[0] = pattern[0].rstrip("\n") |
| 62 |
pattern = None |
| 63 |
if isinstance(line, ini.SectionLine): |
| 64 |
add_comment(line.comment) |
| 65 |
section = Section((line.name,), [], comment) |
| 66 |
comment = "" |
| 67 |
resource.sections.append(section) |
| 68 |
elif isinstance(line, ini.OptionLine): |
| 69 |
if not section: |
| 70 |
raise ValueError(f"Unexpected value {line.name} before section header") |
| 71 |
add_comment(line.comment) |
| 72 |
pattern = [line.value] |
| 73 |
msg = PatternMessage(pattern) # type: ignore[arg-type] |
| 74 |
section.entries.append(Entry((line.name,), msg, comment)) |
| 75 |
comment = "" |
| 76 |
elif isinstance(line, ini.CommentLine): |
| 77 |
add_comment(line.comment) |
| 78 |
elif isinstance(line, ini.EmptyLine): |
| 79 |
if comment: |
| 80 |
if section: |
| 81 |
section.entries.append(Comment(comment)) |
| 82 |
else: |
| 83 |
resource.comment = ( |
| 84 |
f"{resource.comment}\n\n{comment}" |
| 85 |
if resource.comment |
| 86 |
else comment |
| 87 |
) |
| 88 |
comment = "" |
| 89 |
else: |
| 90 |
raise ValueError(f"Unexpected {line.__class__.__name__}: {line.__dict__}") |
| 91 |
if pattern: |
| 92 |
pattern[0] = pattern[0].rstrip("\n") |
| 93 |
if comment: |
| 94 |
if section: |
| 95 |
section.entries.append(Comment(comment)) |
| 96 |
else: |
| 97 |
resource.comment = ( |
| 98 |
f"{resource.comment}\n\n{comment}" if resource.comment else comment |
| 99 |
) |
| 100 |
return resource |
| 101 |
|
| 102 |
|
| 103 |
def ini_lines(data: ini.LineContainer) -> Generator[ini.LineType, None, None]: |
| 104 |
for line in data.contents: |
| 105 |
if isinstance(line, ini.LineContainer): |
| 106 |
yield from ini_lines(line) |
| 107 |
else: |
| 108 |
yield line |