Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def blend(self, **values):
""" Generate object.
:param **values: Predefined fields
:return value: a generated value
"""
defaults = deepcopy(self.__fields)
# Prepare relations
for key, params in values.items():
if '__' in key:
name, value = key.split('__', 1)
if name not in defaults:
defaults[name] = t.Field(None, name)
defaults[name].params.update({value: params})
continue
defaults[key] = params
values = dict(
value.gen_value(self, name, value)
if isinstance(value, t.ServiceValue)
else self.get_value(name, value)
for name, value in defaults.items()
)
# Parse MIX and SKIP values
candidates = list(
(name, value & values if isinstance(value, t.Mix) else value)
for name, value in values.items()
if value is not SKIP_VALUE
generators = {
bool: faker.pybool,
float: faker.pyfloat,
int: faker.random_int,
str: faker.pystr,
bytes: faker.pybytes,
list: faker.pylist,
set: faker.pyset,
tuple: faker.pytuple,
dict: faker.pydict,
datetime.date: faker.date,
datetime.datetime: faker.date_time,
datetime.time: faker.time,
decimal.Decimal: faker.pydecimal,
t.BigInteger: faker.big_integer,
t.EmailString: faker.email,
t.HostnameString: faker.domain_name,
t.IP4String: faker.ipv4,
t.IP6String: faker.ipv6,
t.IPString: faker.ip_generic,
t.NullOrBoolean: faker.null_boolean,
t.PositiveDecimal: faker.positive_decimal,
t.PositiveInteger: faker.positive_integer,
t.PositiveSmallInteger: faker.small_positive_integer,
t.SmallInteger: faker.small_integer,
t.Text: faker.text,
t.URL: faker.url,
t.UUID: faker.uuid,
type(None): '',
}
def postprocess(self, target, postprocess_values):
""" Fill postprocess values. """
mixed = []
for name, deffered in postprocess_values:
value = deffered.value
if isinstance(value, GeneratorType):
value = next(value)
if isinstance(value, t.Mix):
mixed.append((name, value))
continue
if isinstance(getattr(target, name), InstrumentedList) and not isinstance(value, list):
value = [value]
setattr(target, name, value)
for name, mix in mixed:
setattr(target, name, mix & target)
if self.__mixer:
target = self.__mixer.postprocess(target)
return target
def __load_fields(self):
for field in self.__scheme._meta.sorted_fields:
yield field.name, t.Field(field, field.name)
def get_value(self, name, value):
""" Set value to generated instance.
:return : None or (name, value) for later use
"""
field = self.__fields.get(name)
if field:
if (field.scheme in self.__scheme._meta.local_many_to_many or
isinstance(field.scheme, GenericForeignKey)):
return name, t._Deffered(value, field.scheme)
return self._get_value(name, value, field)
return super(TypeMixer, self).get_value(name, value)
set: faker.pyset,
tuple: faker.pytuple,
dict: faker.pydict,
datetime.date: faker.date,
datetime.datetime: faker.date_time,
datetime.time: faker.time,
decimal.Decimal: faker.pydecimal,
t.BigInteger: faker.big_integer,
t.EmailString: faker.email,
t.HostnameString: faker.domain_name,
t.IP4String: faker.ipv4,
t.IP6String: faker.ipv6,
t.IPString: faker.ip_generic,
t.NullOrBoolean: faker.null_boolean,
t.PositiveDecimal: faker.positive_decimal,
t.PositiveInteger: faker.positive_integer,
t.PositiveSmallInteger: faker.small_positive_integer,
t.SmallInteger: faker.small_integer,
t.Text: faker.text,
t.URL: faker.url,
t.UUID: faker.uuid,
type(None): '',
}
fakers = {
('address', str): faker.street_address,
('body', str): faker.text,
('category', str): faker.genre,
('city', str): faker.city,
('company', str): faker.company,
('content', str): faker.text,
('country', str): faker.country,
FAKE = property(lambda cls: t.Fake())
MIX = property(lambda cls: t.Mix())
])
return TypeMixer(scheme, mixer=_typemixer._TypeMixer__mixer,
factory=_typemixer._TypeMixer__factory,
fake=_typemixer._TypeMixer__fake).blend(**params)
class GenFactory(BaseFactory):
""" Map a mongoengine classes to simple types. """
types = {
BooleanField: bool,
DateTimeField: datetime.datetime,
DecimalField: decimal.Decimal,
EmailField: t.EmailString,
FloatField: float,
IntField: int,
StringField: str,
URLField: t.URL,
UUIDField: t.UUID,
}
generators = {
GenericReferenceField: get_generic_reference,
GeoPointField: faker.coordinates,
LineStringField: get_linestring,
ObjectIdField: get_objectid,
PointField: get_pointfield,
PolygonField: get_polygon,
}