Preserve template-name via escaping

Make use of an improved regex pattern matching to simply escape the
template name when passing into the params to allow the name be
preserved and used as input to other params.

This avoids needing to perform an additional branch test against all
variables being formatted.

Change-Id: I9c0ac8996d520b9acff3d29039c5c3d76f56d899
Depends-On: I8a74f9b4236ca7bcc72dd207fca23c2bf6a7c801
This commit is contained in:
Darragh Bailey 2018-01-30 12:47:33 +00:00 committed by Thanh Ha
parent ed07d8fa94
commit 733906f01b
2 changed files with 4 additions and 12 deletions

View File

@ -33,9 +33,7 @@ def deep_format(obj, paramdict, allow_empty=False):
# limitations on the values in paramdict - the post-format result must
# still be valid YAML (so substituting-in a string containing quotes, for
# example, is problematic).
if getattr(obj, 'verbatim', False) is True:
ret = obj
elif hasattr(obj, 'format'):
if hasattr(obj, 'format'):
try:
ret = CustomFormatter(allow_empty).format(obj, **paramdict)
except KeyError as exc:
@ -131,10 +129,3 @@ class CustomFormatter(Formatter):
)
return ''
raise
class VerbatimString(str):
"""
String which is not expanded by `deep_format`.
"""
verbatim = True

View File

@ -20,11 +20,12 @@ import fnmatch
import io
import itertools
import logging
import re
import os
from jenkins_jobs.constants import MAGIC_MANAGE_STRING
from jenkins_jobs.errors import JenkinsJobsException
from jenkins_jobs.formatter import deep_format, VerbatimString
from jenkins_jobs.formatter import deep_format
import jenkins_jobs.local_yaml as local_yaml
from jenkins_jobs import utils
@ -346,8 +347,8 @@ class YamlParser(object):
for values in itertools.product(*dimensions):
params = copy.deepcopy(project)
params['template-name'] = VerbatimString(template_name)
params = self._applyDefaults(params, template)
params['template-name'] = re.sub(r'({|})', r'\1\1', template_name)
try:
expanded_values = {}