Print context when key fails to be formatted

Print the parameter dict when a template fails to format a dictionary
key.  This change also pretty prints the parameter dict for convenience.

Change-Id: Ib0918272126b5c04f713b2e9cd391aa2a36615c7
This commit is contained in:
Russell Sim 2014-10-20 09:18:52 +11:00
parent 1db5ad59c1
commit 79fd17f481

@ -28,6 +28,7 @@ from xml.dom import minidom
import jenkins
import re
import pkg_resources
from pprint import pformat
import logging
import copy
import itertools
@ -93,8 +94,8 @@ def deep_format(obj, paramdict):
ret = obj.format(**paramdict)
except KeyError as exc:
missing_key = exc.message
desc = "%s parameter missing to format %s\nGiven: %s" % (
missing_key, obj, paramdict)
desc = "%s parameter missing to format %s\nGiven:\n%s" % (
missing_key, obj, pformat(paramdict))
raise JenkinsJobsException(desc)
elif isinstance(obj, list):
ret = []
@ -103,7 +104,14 @@ def deep_format(obj, paramdict):
elif isinstance(obj, dict):
ret = {}
for item in obj:
ret[item.format(**paramdict)] = deep_format(obj[item], paramdict)
try:
ret[item.format(**paramdict)] = \
deep_format(obj[item], paramdict)
except KeyError as exc:
missing_key = exc.message
desc = "%s parameter missing to format %s\nGiven:\n%s" % (
missing_key, obj, pformat(paramdict))
raise JenkinsJobsException(desc)
else:
ret = obj
return ret