From 733906f01bed0b2c33529a46677c2a9aa7e2c229 Mon Sep 17 00:00:00 2001 From: Darragh Bailey Date: Tue, 30 Jan 2018 12:47:33 +0000 Subject: [PATCH] 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 --- jenkins_jobs/formatter.py | 11 +---------- jenkins_jobs/parser.py | 5 +++-- 2 files changed, 4 insertions(+), 12 deletions(-) diff --git a/jenkins_jobs/formatter.py b/jenkins_jobs/formatter.py index 7eaab5980..008d66e74 100644 --- a/jenkins_jobs/formatter.py +++ b/jenkins_jobs/formatter.py @@ -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 diff --git a/jenkins_jobs/parser.py b/jenkins_jobs/parser.py index b3b9bcffc..656af17b9 100644 --- a/jenkins_jobs/parser.py +++ b/jenkins_jobs/parser.py @@ -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 = {}