in python/moz/l10n/formats/ini/parse.py [0:0]
def ini_parse(source: TextIO | str | bytes) -> Resource[Message]:
"""
Parse an .ini file into a message resource.
The parsed resource will not include any metadata.
"""
if isinstance(source, str):
file: TextIO = StringIO(source)
elif isinstance(source, bytes):
file = StringIO(source.decode())
else:
file = source
cfg = ini.INIConfig(file, optionxformvalue=None)
resource = Resource[Message](Format.ini, [])
section: Section[Message] | None = None
pattern: list[str] | None = None
comment = ""
def add_comment(cl: str | None) -> None:
nonlocal comment
if cl is not None:
cv = cl[1:] if cl and cl.startswith(" ") else cl
comment = f"{comment}\n{cv}" if comment else cv
for line in ini_lines(cfg._data):
if pattern:
if isinstance(line, ini.ContinuationLine):
pattern[0] += "\n" + line.value
continue
elif isinstance(line, ini.EmptyLine):
pattern[0] += "\n"
else:
pattern[0] = pattern[0].rstrip("\n")
pattern = None
if isinstance(line, ini.SectionLine):
add_comment(line.comment)
section = Section((line.name,), [], comment)
comment = ""
resource.sections.append(section)
elif isinstance(line, ini.OptionLine):
if not section:
raise ValueError(f"Unexpected value {line.name} before section header")
add_comment(line.comment)
pattern = [line.value]
msg = PatternMessage(pattern) # type: ignore[arg-type]
section.entries.append(Entry((line.name,), msg, comment))
comment = ""
elif isinstance(line, ini.CommentLine):
add_comment(line.comment)
elif isinstance(line, ini.EmptyLine):
if comment:
if section:
section.entries.append(Comment(comment))
else:
resource.comment = (
f"{resource.comment}\n\n{comment}"
if resource.comment
else comment
)
comment = ""
else:
raise ValueError(f"Unexpected {line.__class__.__name__}: {line.__dict__}")
if pattern:
pattern[0] = pattern[0].rstrip("\n")
if comment:
if section:
section.entries.append(Comment(comment))
else:
resource.comment = (
f"{resource.comment}\n\n{comment}" if resource.comment else comment
)
return resource