Debian Codegraph @

mkdocs/utils/babel_stub.py

raw
1 from __future__ import annotations
2
3 from string import ascii_letters
4 from typing import NamedTuple
5
6
7 class UnknownLocaleError(Exception):
8 pass
9
10
11 class Locale(NamedTuple):
12 language: str
13 territory: str = ''
14
15 def __str__(self):
16 if self.territory:
17 return f'{self.language}_{self.territory}'
18 return self.language
19
20 @classmethod
21 def parse(cls, identifier, sep):
22 if not isinstance(identifier, str):
23 raise TypeError(f"Unexpected value for identifier: '{identifier}'")
24 locale = cls(*identifier.split(sep, 1))
25 if not all(x in ascii_letters for x in locale.language):
26 raise ValueError(f"expected only letters, got '{locale.language}'")
27 if len(locale.language) != 2:
28 raise UnknownLocaleError(f"unknown locale '{locale.language}'")
29 return locale