def recreate(cls, usage_id): # if usage_id in cls._usage_index: # raise KeyError('Usage %s is already in the index' % usage_id) self = cls.__new__(cls) self.id = usage_id self.parent = None self.block_name = BASE_KVS.get('USAGE_DATA')[self.id]['class_id'] self.def_id = BASE_KVS.get('USAGE_DATA')[self.id]['def_id'] children_ids = BASE_KVS.get('USAGE_DATA')[self.id]['children'] self.children = [Usage.recreate(x) for x in children_ids] self._usage_index[self.id] = self #TODO store_initial_state or similar might be necessary for child in self.children: child.parent = self return self
def __init__(self, class_id, children=None, initial_state=None, def_id=None): self.id = "usage_%d" % BASE_KVS.incr('ids',0) self.def_id = def_id or ("def_%d" % BASE_KVS.incr('ids', 0)) self.children = children or [] usage_data = BASE_KVS.get('USAGE_DATA', {}) usage_data[self.id] = { 'class_id': class_id, 'def_id': self.def_id, 'children': [x.id for x in self.children] } BASE_KVS.set('USAGE_DATA', usage_data) self.parent = None self.block_name = class_id self.initial_state = initial_state or {} # Update our global index of all usages. self._usage_index[self.id] = self # Create the parent references as we construct children. for child in self.children: child.parent = self