File: babel_stub.py

package info (click to toggle)
python-mkdocs 1.4.2%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 5,640 kB
  • sloc: python: 12,310; javascript: 2,315; perl: 142; sh: 84; makefile: 24; xml: 12
file content (29 lines) | stat: -rw-r--r-- 860 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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