Split recipe_from_toml into recipe_from_dict helper.

This will be useful for creating Recipe objects from json created dicts.
This commit is contained in:
Brian C. Lane 2017-11-10 13:58:36 -08:00
parent 3c75711b30
commit 7b5115d19c
2 changed files with 22 additions and 14 deletions

View File

@ -35,7 +35,7 @@ class CommitTimeValError(Exception):
class RecipeFileError(Exception):
pass
class RecipeTOMLError(Exception):
class RecipeError(Exception):
pass
@ -124,24 +124,34 @@ def recipe_from_toml(recipe_str):
:rtype: Recipe
:raises: TomlError
"""
recipe_toml = toml.loads(recipe_str)
recipe_dict = toml.loads(recipe_str)
return recipe_from_dict(recipe_dict)
def recipe_from_dict(recipe_dict):
"""Create a Recipe object from a plain dict.
:param recipe_dict: A plain dict of the recipe
:type recipe_dict: dict
:returns: A Recipe object
:rtype: Recipe
:raises: RecipeError
"""
# Make RecipeModule objects from the toml
# The TOML may not have modules or packages in it. Set them to None in this case
try:
if recipe_toml.get("modules"):
modules = [RecipeModule(m.get("name"), m.get("version")) for m in recipe_toml["modules"]]
if recipe_dict.get("modules"):
modules = [RecipeModule(m.get("name"), m.get("version")) for m in recipe_dict["modules"]]
else:
modules = None
if recipe_toml.get("packages"):
packages = [RecipePackage(p.get("name"), p.get("version")) for p in recipe_toml["packages"]]
if recipe_dict.get("packages"):
packages = [RecipePackage(p.get("name"), p.get("version")) for p in recipe_dict["packages"]]
else:
packages = None
name = recipe_toml["name"]
description = recipe_toml["description"]
version = recipe_toml.get("version", None)
except KeyError:
raise RecipeTOMLError
name = recipe_dict["name"]
description = recipe_dict["description"]
version = recipe_dict.get("version", None)
except KeyError as e:
raise RecipeError("There was a problem parsing the recipe: %s" % str(e))
return Recipe(name, description, version, modules, packages)
@ -659,5 +669,3 @@ def is_parent_diff(repo, filename, tree, parent):
diff_opts.set_pathspec([filename])
diff = Git.Diff.new_tree_to_tree(repo, parent.get_tree(), tree, diff_opts)
return diff.get_num_deltas() > 0

View File

@ -54,7 +54,7 @@ class BasicRecipeTest(unittest.TestCase):
with self.assertRaises(TomlError):
recipes.recipe_from_toml("This is not a TOML string\n")
with self.assertRaises(recipes.RecipeTOMLError):
with self.assertRaises(recipes.RecipeError):
recipes.recipe_from_toml('name = "a failed toml string"\n')
def recipe_to_toml_test(self):