test bkp
This commit is contained in:
1 parent
6ccab0347b
commit
954a2fd6d6
18684 files changed
+601012
-190
No files matched your search
@@ -0,0 +1,29 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from string import ascii_letters
|
||||
from typing import NamedTuple
|
||||
|
||||
|
||||
class UnknownLocaleError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class Locale(NamedTuple):
|
||||
language: str
|
||||
territory: str = ''
|
||||
|
||||
def __str__(self):
|
||||
if self.territory:
|
||||
return f'{self.language}_{self.territory}'
|
||||
return self.language
|
||||
|
||||
@classmethod
|
||||
def parse(cls, identifier, sep):
|
||||
if not isinstance(identifier, str):
|
||||
raise TypeError(f"Unexpected value for identifier: '{identifier}'")
|
||||
locale = cls(*identifier.split(sep, 1))
|
||||
if not all(x in ascii_letters for x in locale.language):
|
||||
raise ValueError(f"expected only letters, got '{locale.language}'")
|
||||
if len(locale.language) != 2:
|
||||
raise UnknownLocaleError(f"unknown locale '{locale.language}'")
|
||||
return locale
|
||||
Reference in new issue
Block a user