From a68bdd1419150a98b4255ca6f7db6889e73b7aa0 Mon Sep 17 00:00:00 2001 From: Wolfgang Müller Date: Thu, 14 Nov 2024 20:12:19 +0100 Subject: backend/scraper: Add parser methods for Language We can expect a number of scraper sources to either give languages as ISO 639-3 or as their English name, so it makes sense to implement a simple parser method on our side. --- src/hircine/scraper/types.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'src') diff --git a/src/hircine/scraper/types.py b/src/hircine/scraper/types.py index 534792b..23cb634 100644 --- a/src/hircine/scraper/types.py +++ b/src/hircine/scraper/types.py @@ -137,6 +137,38 @@ class Language: def __bool__(self): return self.value is not None + @classmethod + def from_iso_639_3(cls, string): + """ + Returns a new instance of this class given a case-insensitive ISO 639-3 + language code. + + :param str string: The ISO 639-3 language code. + :raise: :exc:`~hircine.scraper.ScrapeWarning` if the language code could + not be parsed. + """ + try: + return Language(value=hircine.enums.Language[string.upper()]) + except KeyError as e: + raise ScrapeWarning( + f"Could not parse language code: '{string}' as ISO 639-3" + ) from e + + @classmethod + def from_name(cls, string): + """ + Returns a new instance of this class given a case-insensitive language name. + Permissible language names are defined in :class:`hircine.enums.Language`. + + :param str string: The language name. + :raise: :exc:`~hircine.scraper.ScrapeWarning` if the language name could + not be parsed. + """ + try: + return Language(value=hircine.enums.Language(string.capitalize())) + except ValueError as e: + raise ScrapeWarning(f"Could not parse language name: '{string}'") from e + @dataclass(frozen=True) class Direction: -- cgit v1.2.3-2-gb3c3