aboutsummaryrefslogtreecommitdiff
path: root/factory/faker.py
diff options
context:
space:
mode:
Diffstat (limited to 'factory/faker.py')
-rw-r--r--factory/faker.py96
1 files changed, 96 insertions, 0 deletions
diff --git a/factory/faker.py b/factory/faker.py
new file mode 100644
index 0000000..10a0cba
--- /dev/null
+++ b/factory/faker.py
@@ -0,0 +1,96 @@
+# -*- 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
+
+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
+ 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]