Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def mimesis_locale():
"""Specifies which locale to use."""
return DEFAULT_LOCALE
def test_locale(mimesis_locale, mimesis):
assert mimesis_locale == DEFAULT_LOCALE
assert mimesis.locale == DEFAULT_LOCALE
def _override_locale(self, locale: str = locales.DEFAULT_LOCALE) -> None:
"""Overrides current locale with passed and pull data for new locale.
:param locale: Locale
:return: Nothing.
"""
self.locale = locale
self.pull.cache_clear()
self.pull()
def __init__(self, locale: str = locales.DEFAULT_LOCALE,
seed: Seed = None) -> None:
"""Initialize attributes for data providers.
:param locale: Current locale.
:param seed: Seed to all the random functions.
"""
super().__init__(seed=seed)
self._data: Dict[str, Any] = {}
self._datafile = ''
self._setup_locale(locale)
self._data_dir = Path(__file__).parent.parent.joinpath('data')
from mimesis.schema import Field, Generic
_CacheKey = Tuple[str, Any]
_Providers = Iterable[Type[BaseProvider]]
class MimesisField(declarations.BaseDeclaration):
"""
Mimesis integration with FactoryBoy starts here.
This class provides common interface for FactoryBoy,
but inside it has Mimesis generators.
"""
_cached_instances: ClassVar[Dict[_CacheKey, Field]] = {}
_default_locale: ClassVar[str] = locales.DEFAULT_LOCALE
def __init__(
self, field: str, locale: Optional[str] = None, **kwargs,
) -> None:
"""
Creates a field instance.
The created field is lazy. It also receives build time parameters.
These parameters are not applied yet.
Args:
field: name to be passed to ``Field`` from ``Mimesis``.
locale: locale to use. This parameter has the highest priority.
kwargs: optional parameters that would be passed to ``Field``.
"""
def _setup_locale(self, locale: str = locales.DEFAULT_LOCALE) -> None:
"""Set up locale after pre-check.
:param str locale: Locale
:raises UnsupportedLocale: When locale is not supported.
:return: Nothing.
"""
if not locale:
locale = locales.DEFAULT_LOCALE
locale = locale.lower()
if locale not in locales.SUPPORTED_LOCALES:
raise UnsupportedLocale(locale)
self.locale = locale
def _setup_locale(self, locale: str = locales.DEFAULT_LOCALE) -> None:
"""Set up locale after pre-check.
:param str locale: Locale
:raises UnsupportedLocale: When locale is not supported.
:return: Nothing.
"""
if not locale:
locale = locales.DEFAULT_LOCALE
locale = locale.lower()
if locale not in locales.SUPPORTED_LOCALES:
raise UnsupportedLocale(locale)
self.locale = locale