def parse_cereals(cereal, loader, dir_suffix='cereals/'): """ Parse grains data from a recipe :param dict cereal: A representation of a cereal :param DataLoader loader: A class to load additional information Grain must have the following top level attributes: * name (str) * weight (float) * data (dict) (optional) Additionally grains may contain override data in the 'data' attribute with the following keys: * color (float) * ppg (int) """ GrainAddition.validate(cereal) cereal_data = loader.get_item(dir_suffix, cereal[u'name']) name = cereal_data.get(u'name', cereal[u'name']) color = None ppg = None if u'data' in cereal: color = cereal[u'data'].get(u'color', None) ppg = cereal[u'data'].get(u'ppg', None) if not color: color = cereal_data.get(u'color', None) if not ppg: ppg = cereal_data.get(u'ppg', None) grain_obj = Grain(name, color=color, ppg=ppg) grain_add_kwargs = { u'weight': float(cereal[u'weight']), } if u'grain_type' in cereal: grain_add_kwargs[u'grain_type'] = cereal[u'grain_type'] if u'units' in cereal: grain_add_kwargs[u'units'] = cereal[u'units'] return GrainAddition(grain_obj, **grain_add_kwargs)
def test_validate(self): data = self.grain_add.to_dict() GrainAddition.validate(data)