def cast(self, data, kind): ''' Transform the given data to the given kind. :param data: the data to be transform :param str kind: the kind of data of the transformed data :return: the data of the given kind ''' actual_data = data.data() if isinstance(data, Kotoba) else data if kind == 'entity': actual_data = self.__locator.get(actual_data) elif kind == 'class': actual_data = Loader(actual_data).package elif kind == 'int': actual_data = int(actual_data) elif kind == 'float': actual_data = float(actual_data) elif kind == 'bool': actual_data = actual_data.capitalize() assert actual_data == 'True' or actual_data == 'False' actual_data = actual_data == 'True' elif kind in ['list', 'tuple', 'set']: assert isinstance(data, Kotoba), 'Asking for a Kotoba object, getting an instance of type {}.'.format(type(data).__name__) actual_data = [] for item in data.children(): item_type = item.attribute('type') actual_data.append(self.cast(item, item_type)) if kind != 'list': actual_data = eval(kind)(actual_data) elif kind == 'dict': assert isinstance(data, Kotoba), 'Asking for a Kotoba object, getting an instance of type {}.'.format(type(data).__name__) actual_data = {} for item in data.children(): item_name = item.attribute('name') item_type = item.attribute('type') actual_data[item_name] = self.cast(item, item_type) elif kind == 'str': actual_data = str(actual_data) elif kind not in ['str', 'unicode']: raise ValueError('Unknown type: {} (Given data: {})'.format(kind, data)) return actual_data
def load_fixtures(self, kind): if kind in self.__loaded_kinds: return loader = Loader(kind) fixtures = self.__fixture_map[kind] self.__loaded_kinds[kind] = loader self.__graphs[kind] = [] for id, fixture in fixtures.iteritems(): fixture = self._prepare_fixture(fixture) entity = loader.package(**fixture) self.db.post(entity) self.db.session.refresh(entity) fixtures[id] = entity self.__graphs[kind].append(entity)