def __new__(cls, name, bases, attrs): super_new = super(BlockMeta, cls).__new__ module = attrs.pop('__module__') new_class = super_new(cls, name, bases, {'__module__': module}) if hasattr(bases[0], "_block_serializer"): setattr(new_class, "_block_serializer", BlockSerializer.clone(bases[0]._block_serializer)) setattr(new_class, "_trans", TransSystem.clone(bases[0]._trans)) _attrs = {} for base in bases: for attr_name, attr_val in getattr(base, "_attrs", {}).iteritems(): if attr_name not in taboo_attrs: _attrs[attr_name] = attr_val _attrs.update(attrs) setattr(new_class, "_attrs", _attrs) for attr_name, attr_val in itertools.chain(attrs.iteritems(), _attrs.iteritems()): new_class.add_to_class(attr_name, attr_val) if not attrs.get("is_abstract", False): register_block(name, _attrs.get("name"), _attrs.get("block_group"), new_class) return new_class
class BaseBlock(object): _block_serializer = BlockSerializer() _trans = TransSystem() __attrs_collect = dict() is_abstract = True """ WARNING: For blocks always inherit other *Block class at first position """ __metaclass__ = BlockMeta
def __init__(self, exp_id=None, scope_name=None): """ Building block for workflow """ # TODO: due to dynamic inputs, find better solution self._block_serializer = BlockSerializer.clone( self.__class__._block_serializer) self.state = "created" self.uuid = "B" + uuid1().hex[:8] self.exp_id = exp_id exp = None if exp_id: exp = Experiment.get_exp_by_id(exp_id) self.scope_name = scope_name self.base_name = "" # Used only be meta-blocks self.children_blocks = [] # End self._out_data = dict() self.out_manager = OutManager() self.input_manager = InputManager() # Automatic execution status map self.auto_exec_status_ready = set(["ready"]) self.auto_exec_status_done = set(["done"]) self.auto_exec_status_working = set(["working"]) self.auto_exec_status_error = set(["execution_error"]) # Init block fields for f_name, f in itertools.chain( self._block_serializer.fields.iteritems(), self._block_serializer.params.iteritems()): #if f_name not in self.__dict__ and not f.is_a_property: if not f.is_a_property and not hasattr(self, f_name): try: setattr(self, f_name, f.init_val) except: import ipdb ipdb.set_trace() for f_name, f in self._block_serializer.inputs.iteritems(): if f.multiply_extensible: setattr(self, f_name, []) # Names of dynamically added ports # TODO: Hmm maybe more metaclass magic can be applied here scope = self.get_scope() scope.load() for f_name, f in self._block_serializer.outputs.iteritems(): if exp: exp.log(self.uuid, "Registering normal outputs: %s" % f_name) log.debug("Registering normal outputs: %s", f_name) self.register_provided_objects( scope, ScopeVar(self.uuid, f_name, f.provided_data_type)) # TODO: Use factories for init values #if f.init_val is not None: # setattr(self, f.name, f.init_val) scope.store() for f_name, f in self._block_serializer.fields.items(): if f.init_val is not None: #setattr(self, f.name, f.init_val) pass for f_name, f in self._block_serializer.inputs.iteritems(): self.input_manager.register(f)
def __init__(self, exp_id=None, scope_name=None): """ Building block for workflow """ # TODO: due to dynamic inputs, find better solution self._block_serializer = BlockSerializer.clone(self.__class__._block_serializer) self.state = "created" self.uuid = "B" + uuid1().hex[:8] self.exp_id = exp_id exp = None if exp_id: exp = Experiment.get_exp_by_id(exp_id) self.scope_name = scope_name self.base_name = "" # Used only be meta-blocks self.children_blocks = [] # End self._out_data = dict() self.out_manager = OutManager() self.input_manager = InputManager() # Automatic execution status map self.auto_exec_status_ready = set(["ready"]) self.auto_exec_status_done = set(["done"]) self.auto_exec_status_working = set(["working"]) self.auto_exec_status_error = set(["execution_error"]) # Init block fields for f_name, f in itertools.chain( self._block_serializer.fields.iteritems(), self._block_serializer.params.iteritems()): #if f_name not in self.__dict__ and not f.is_a_property: if not f.is_a_property and not hasattr(self, f_name): try: setattr(self, f_name, f.init_val) except: import ipdb; ipdb.set_trace() for f_name, f in self._block_serializer.inputs.iteritems(): if f.multiply_extensible: setattr(self, f_name, []) # Names of dynamically added ports # TODO: Hmm maybe more metaclass magic can be applied here scope = self.get_scope() scope.load() for f_name, f in self._block_serializer.outputs.iteritems(): if exp: exp.log(self.uuid, "Registering normal outputs: %s" % f_name) log.debug("Registering normal outputs: %s", f_name) self.register_provided_objects(scope, ScopeVar(self.uuid, f_name, f.provided_data_type)) # TODO: Use factories for init values #if f.init_val is not None: # setattr(self, f.name, f.init_val) scope.store() for f_name, f in self._block_serializer.fields.items(): if f.init_val is not None: #setattr(self, f.name, f.init_val) pass for f_name, f in self._block_serializer.inputs.iteritems(): self.input_manager.register(f)