File: babel_stub.py

package info (click to toggle)
python-mkdocs 1.6.1%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,812 kB
  • sloc: python: 14,346; javascript: 10,535; perl: 143; sh: 57; makefile: 30; xml: 11
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