Пример #1
0
def parse_recipe(recipe, loader,
                 cereals_loader=None,
                 hops_loader=None,
                 yeast_loader=None,
                 cereals_dir_suffix='cereals/',
                 hops_dir_suffix='hops/',
                 yeast_dir_suffix='yeast/'):
    """
    Parse a recipe from a python Dict

    :param dict recipe: A representation of a recipe
    :param DataLoader loader: A class to load additional information
    :param DataLoader cereal_loader: A class to load additional information specific to cereals
    :param DataLoader hops_loader: A class to load additional information specific to hops
    :param DataLoader yeast_loader: A class to load additional information specific to yeast

    A recipe must have the following top level attributes:

    * name         (str)
    * start_volume (float)
    * final_volume (float)
    * grains       (list(dict))
    * hops         (list(dict))
    * yeast        (dict)

    Additionally the recipe may contain override data in the 'data'
    attribute with the following keys:

    * percent_brew_house_yield (float)
    * units                    (str)

    All other fields will be ignored and may be used for other metadata.

    The dict objects in the grains, hops, and yeast values are required to have
    the key 'name' and the remaining attributes will be looked up in the data
    directory if they are not provided.
    """  # noqa
    if cereals_loader is None:
        cereals_loader = loader
    if hops_loader is None:
        hops_loader = loader
    if yeast_loader is None:
        yeast_loader = loader

    Recipe.validate(recipe)

    grain_additions = []
    for grain in recipe[u'grains']:
        grain_additions.append(parse_cereals(grain, cereals_loader,
                                             dir_suffix=cereals_dir_suffix))

    hop_additions = []
    for hop in recipe[u'hops']:
        hop_additions.append(parse_hops(hop, hops_loader,
                                        dir_suffix=hops_dir_suffix))

    yeast = parse_yeast(recipe[u'yeast'], yeast_loader,
                        dir_suffix=yeast_dir_suffix)

    recipe_kwargs = {
        u'grain_additions': grain_additions,
        u'hop_additions': hop_additions,
        u'yeast': yeast,
        u'start_volume': recipe[u'start_volume'],
        u'final_volume': recipe[u'final_volume'],
    }
    if u'data' in recipe:
        if u'percent_brew_house_yield' in recipe[u'data']:
            recipe_kwargs[u'percent_brew_house_yield'] = \
                recipe[u'data'][u'percent_brew_house_yield']
        if u'units' in recipe[u'data']:
            recipe_kwargs[u'units'] = recipe[u'data'][u'units']

    beer = Recipe(recipe[u'name'],
                  **recipe_kwargs)
    return beer
Пример #2
0
 def test_validate(self):
     data = self.recipe.to_dict()
     Recipe.validate(data)
Пример #3
0
 def test_validate(self):
     data = self.recipe.to_dict()
     Recipe.validate(data)