Source code for factory.faker

# -*- coding: utf-8 -*-
# Copyright (c) 2010 Mark Sandstrom
# Copyright (c) 2011-2015 Raphaël Barrois
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.


"""Additional declarations for "faker" attributes.

Usage:

    class MyFactory(factory.Factory):
        class Meta:
            model = MyProfile

        first_name = factory.Faker('name')
"""


from __future__ import absolute_import
from __future__ import unicode_literals

import contextlib

import faker
import faker.config

from . import declarations

[docs]class Faker(declarations.OrderedDeclaration): """Wrapper for 'faker' values. Args: provider (str): the name of the Faker field locale (str): the locale to use for the faker All other kwargs will be passed to the underlying provider (e.g ``factory.Faker('ean', length=10)`` calls ``faker.Faker.ean(length=10)``) Usage: >>> foo = factory.Faker('name') """ def __init__(self, provider, locale=None, **kwargs): self.provider = provider self.provider_kwargs = kwargs self.locale = locale def generate(self, extra_kwargs): kwargs = {} kwargs.update(self.provider_kwargs) kwargs.update(extra_kwargs) faker = self._get_faker(self.locale) return faker.format(self.provider, **kwargs) def evaluate(self, sequence, obj, create, extra=None, containers=()): return self.generate(extra or {}) _FAKER_REGISTRY = {} _DEFAULT_LOCALE = faker.config.DEFAULT_LOCALE @classmethod @contextlib.contextmanager
[docs] def override_default_locale(cls, locale): old_locale = cls._DEFAULT_LOCALE cls._DEFAULT_LOCALE = locale try: yield finally: cls._DEFAULT_LOCALE = old_locale
@classmethod def _get_faker(cls, locale=None): if locale is None: locale = cls._DEFAULT_LOCALE if locale not in cls._FAKER_REGISTRY: cls._FAKER_REGISTRY[locale] = faker.Faker(locale=locale) return cls._FAKER_REGISTRY[locale] @classmethod
[docs] def add_provider(cls, provider, locale=None): """Add a new Faker provider for the specified locale""" cls._get_faker(locale).add_provider(provider)