Source code for mimesis.decorators

# -*- coding: utf-8 -*-

"""Decorators for the public API and for internal purpose."""

import functools
from string import ascii_letters, digits, punctuation
from typing import Any, Callable, TypeVar, cast

from mimesis import data
from mimesis.exceptions import LocaleError

__all__ = ["romanize"]

F = TypeVar("F", bound=Callable[..., Any])

[docs]def romanize(locale: str = "") -> Callable[[F], F]: """Romanize the cyrillic text. Transliterate the cyrillic script into the latin alphabet. .. note:: At this moment it works only for `ru`, `uk`, `kk`. :param locale: Locale code. :return: Romanized text. """ def romanize_deco(func: F) -> F: @functools.wraps(func) def wrapper(*args: Any, **kwargs: Any) -> Any: try: # Cyrillic string can contain ascii # symbols, digits and punctuation. alphabet = {s: s for s in ascii_letters + digits + punctuation} alphabet.update( { **data.ROMANIZATION_DICT[locale], **data.COMMON_LETTERS, } ) except KeyError: raise LocaleError(locale) result = func(*args, **kwargs) txt = "".join([alphabet[i] for i in result if i in alphabet]) return txt return cast(F, wrapper) return romanize_deco
# For backward compatibility romanized = romanize