From: Aarni Koskela Date: Tue, 26 Mar 2024 13:32:00 +0000 (+0200) Subject: read_po: note interface also supports iterable-of-strings, not a filelike X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e2880f583f44dc6ca896f0274f8b76a1911a2c82;p=thirdparty%2Fbabel.git read_po: note interface also supports iterable-of-strings, not a filelike --- diff --git a/babel/messages/pofile.py b/babel/messages/pofile.py index b64a5085..7c8518de 100644 --- a/babel/messages/pofile.py +++ b/babel/messages/pofile.py @@ -291,7 +291,7 @@ class PoFileParser: # These are called user comments self.user_comments.append(line[1:].strip()) - def parse(self, fileobj: IO[AnyStr]) -> None: + def parse(self, fileobj: IO[AnyStr] | Iterable[AnyStr]) -> None: """ Reads from the file-like object `fileobj` and adds any po file units found in it to the `Catalog` supplied to the constructor. @@ -329,7 +329,7 @@ class PoFileParser: def read_po( - fileobj: IO[AnyStr], + fileobj: IO[AnyStr] | Iterable[AnyStr], locale: str | Locale | None = None, domain: str | None = None, ignore_obsolete: bool = False, @@ -337,7 +337,7 @@ def read_po( abort_invalid: bool = False, ) -> Catalog: """Read messages from a ``gettext`` PO (portable object) file from the given - file-like object and return a `Catalog`. + file-like object (or an iterable of lines) and return a `Catalog`. >>> from datetime import datetime >>> from io import StringIO @@ -373,7 +373,7 @@ def read_po( .. versionadded:: 1.0 Added support for explicit charset argument. - :param fileobj: the file-like object to read the PO file from + :param fileobj: the file-like object (or iterable of lines) to read the PO file from :param locale: the locale identifier or `Locale` object, or `None` if the catalog is not bound to a locale (which basically means it's a template) diff --git a/tests/messages/test_pofile.py b/tests/messages/test_pofile.py index 043f9c8b..d322857d 100644 --- a/tests/messages/test_pofile.py +++ b/tests/messages/test_pofile.py @@ -884,3 +884,12 @@ def test_unknown_language_write(): buf = BytesIO() pofile.write_po(buf, catalog) assert 'sr_SP' in buf.getvalue().decode() + + +def test_iterable_of_strings(): + """ + Test we can parse from an iterable of strings. + """ + catalog = pofile.read_po(['msgid "foo"', b'msgstr "Voh"'], locale="en_US") + assert catalog.locale == Locale("en", "US") + assert catalog.get("foo").string == "Voh"