From 888f319b60c418eb038a523647c5e9b750a2098d Mon Sep 17 00:00:00 2001 From: "Brian C. Lane" Date: Thu, 1 Feb 2018 10:46:31 -0800 Subject: [PATCH] Add reading a recipe directly from a file Adds helper function recipe_from_file to recipes.py --- src/pylorax/api/recipes.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/pylorax/api/recipes.py b/src/pylorax/api/recipes.py index 4f05c758..5bceefb2 100644 --- a/src/pylorax/api/recipes.py +++ b/src/pylorax/api/recipes.py @@ -148,6 +148,17 @@ class RecipeModule(dict): class RecipePackage(RecipeModule): pass +def recipe_from_file(recipe_path): + """Return a recipe file as a Recipe object + + :param recipe_path: Path to the recipe fila + :type recipe_path: str + :returns: A Recipe object + :rtype: Recipe + """ + with open(recipe_path, 'rb') as f: + return recipe_from_toml(f.read()) + def recipe_from_toml(recipe_str): """Create a Recipe object from a toml string. @@ -506,8 +517,7 @@ def commit_recipe_file(repo, branch, filename): :raises: Can raise errors from Ggit or RecipeFileError """ try: - f = open(filename, 'rb') - recipe = recipe_from_toml(f.read()) + recipe = recipe_from_file(filename) except IOError: raise RecipeFileError