def onBuild(self, *args, **kwargs): """ Called when this object is built. Notifies ``self.destination`` that it now has a value. """ if self.destination and kwargs.get('instance'): logger.debug('Setting value %s for %s' % (kwargs['instance'], self.destination)) if not self.value: self.destination.value = kwargs['instance']
def build(self, *args, **kwargs): self.onBuild(**kwargs) if self.value: logger.debug('%s has a set value %s' % (self, self.value)) # self.add(self.value) self.value = None result = self.doBuild(*args, **kwargs) return result
def __init__(self, clazzToBuild): ''' :arg clazzToBuild: a model class with :py:class:`builders:construct:Construct` fields to populate Creates new :py:class:`builders.builder.Builder` instance. Instance contains it's own set of modifiers (see :py:method:`withA`) and produces one result tree at a time. ''' self.clazz = clazzToBuild self.modifiers = [] logger.debug("New builder for clazz <%s>" % self.clazz)
def build(self, *args, **kwargs): """ Called by :py:class:`builders.builder.Builder` on the model construction. Returns actual pre-set value (via :py:class:`Link` mechanism) or a newly built one. """ self.onBuild(**kwargs) if self.value: logger.debug('%s returning pre-built value %s' % (self, self.value)) result = self.value self.value = None else: result = self.doBuild(*args, **kwargs) return result
def doBuild(self, modifiers, instance=None, **kwargs): if not self.destination: raise ValueError('Link %s has no attachment' % self) logger.debug('Up-linking instance for %s with Given %s value of %s' % (self.clazz, self.destination, instance)) mods = [modifiers] if isinstance(self.destination, Collection): mods.append(modifiers_package.HavingIn(self.destination, instance)) else: mods.append(modifiers_package.Given(self.destination, instance)) if self.reuser: return self.reuser.doBuild(modifiers, **dict(instance=instance, **kwargs)) else: return builder.Builder(self.clazz).withA(*mods).build()
def _buildclazz(self, clazzToBuild): [ m.apply(clazz=self.clazz) for m in self.modifiers if m.shouldRun(clazz=self.clazz) ] instance = self.clazz() logger.debug('Created new instance %s of clazz %s' % (instance, self.clazz)) def make_construct(name, construct): logger.debug('Constructing <%s.%s>' % (self.clazz, name)) setattr(instance, name, construct.build(self.modifiers, instance=instance)) logger.debug('Creating nested constructst of %s' % instance) for name, value in getmembers( instance, lambda x: isinstance(x, construct.Construct) and not isinstance(x, construct.Uplink)): make_construct(name, value) logger.debug('Creating uplinks of %s' % instance) for name, value in getmembers( instance, lambda x: isinstance(x, construct.Uplink)): make_construct(name, value) # instance level modifier application for modifier in self.modifiers: if modifier.shouldRun(instance=instance): modifier.apply(instance=instance) return instance
def doBuild(self, modifiers, **kwargs): total_amount = self.number for o in self.overrides: total_amount = o(total_amount) self.overrides = [] result = list(self.items) self.items = [] if self.destination: logger.debug("Collection %s found destinations %s" % (self, self.destination)) saved_value = self.destination[0].value else: saved_value = None while len(result) < total_amount: logger.debug("Collection %s building item of type %s" % (self, self.type)) logger.debug("Collection destination is %s, %s" % (self.destination, modifiers_package.classvars(self.destination))) if saved_value: self.destination[0].value = saved_value if not saved_value and self.destination: self.destination[0].value = kwargs['instance'] extra_modifiers = self.modifiers and self.modifiers.pop() item = Unique.doBuild(self, modifiers + extra_modifiers) result.append(item) return result
def do(self, clazz): logger.debug('%s applied to %s' % (self, clazz)) for name, value in classvars(clazz).items(): logger.debug('Checking %s attr <%s> of value %s' % (clazz, name, value)) if value == self.construct: logger.debug('Applying %s to the clazz %s attr <%s> with value %s' % (self, clazz, name, self.value)) self.doApply(value)
def do(self, clazz): logger.debug('%s applied to %s' % (self, clazz)) for name, value in classvars(clazz).items(): logger.debug('Checking %s attr <%s> of value %s' % (clazz, name, value)) if value == self.construct: logger.debug( 'Applying %s to the clazz %s attr <%s> with value %s' % (self, clazz, name, self.value)) self.doApply(value)
def _buildclazz(self, clazzToBuild): [m.apply(clazz=self.clazz) for m in self.modifiers if m.shouldRun(clazz=self.clazz)] instance = self.clazz() logger.debug('Created new instance %s of clazz %s' % (instance, self.clazz)) for a in dir(instance): attr = getattr(instance, a) if isinstance(attr, construct.Construct): logger.debug('Constructing <%s.%s>' % (self.clazz, a)) setattr(instance, a, attr.build(self.modifiers, instance=instance)) elif type(attr) == type and not a.startswith('__'): setattr(instance, a, attr()) else: logger.debug('<%s.%s> is not a <%s> but a <%s>' % (self.clazz, a, construct.Construct, attr)) # instance level modifier application for modifier in self.modifiers: if modifier.shouldRun(instance=instance): modifier.apply(instance=instance) return instance
def _buildclazz(self, clazzToBuild): [m.apply(clazz=self.clazz) for m in self.modifiers if m.shouldRun(clazz=self.clazz)] instance = self.clazz() logger.debug('Created new instance %s of clazz %s' % (instance, self.clazz)) def make_construct(name, construct): logger.debug('Constructing <%s.%s>' % (self.clazz, name)) setattr(instance, name, construct.build(self.modifiers, instance=instance)) logger.debug('Creating nested constructst of %s' % instance) for name, value in getmembers(instance, lambda x: isinstance(x, construct.Construct) and not isinstance(x, construct.Uplink)): make_construct(name, value) logger.debug('Creating uplinks of %s' % instance) for name, value in getmembers(instance, lambda x: isinstance(x, construct.Uplink)): make_construct(name, value) # instance level modifier application for modifier in self.modifiers: if modifier.shouldRun(instance=instance): modifier.apply(instance=instance) return instance
def make_construct(name, construct): logger.debug('Constructing <%s.%s>' % (self.clazz, name)) setattr(instance, name, construct.build(self.modifiers, instance=instance))
def onBuild(self, *args, **kwargs): if self.destination and kwargs.get('instance'): logger.debug('Receiving value %s for %s' % (kwargs['instance'], self.destination)) if not self.value: for destination in self.destination: destination.value = kwargs['instance']