Merge "Fix the formatter regex"
This commit is contained in:
commit
a601f54f2f
@ -83,19 +83,25 @@ class CustomFormatter(Formatter):
|
|||||||
Custom formatter to allow non-existing key references when formatting a
|
Custom formatter to allow non-existing key references when formatting a
|
||||||
string
|
string
|
||||||
"""
|
"""
|
||||||
_expr = '{({{)*(?:obj:)?(?P<key>\w+)(?:\|(?P<default>[\w\s]*))?}(}})*'
|
_expr = """
|
||||||
|
(?<!{){({{)* # non-pair opening {
|
||||||
|
(?:obj:)? # obj:
|
||||||
|
(?P<key>\w+) # key
|
||||||
|
(?:\|(?P<default>[\w\s]*))? # default fallback
|
||||||
|
}(}})*(?!}) # non-pair closing }
|
||||||
|
"""
|
||||||
|
|
||||||
def __init__(self, allow_empty=False):
|
def __init__(self, allow_empty=False):
|
||||||
super(CustomFormatter, self).__init__()
|
super(CustomFormatter, self).__init__()
|
||||||
self.allow_empty = allow_empty
|
self.allow_empty = allow_empty
|
||||||
|
|
||||||
def vformat(self, format_string, args, kwargs):
|
def vformat(self, format_string, args, kwargs):
|
||||||
matcher = re.compile(self._expr)
|
matcher = re.compile(self._expr, re.VERBOSE)
|
||||||
|
|
||||||
# special case of returning the object if the entire string
|
# special case of returning the object if the entire string
|
||||||
# matches a single parameter
|
# matches a single parameter
|
||||||
try:
|
try:
|
||||||
result = re.match('^%s$' % self._expr, format_string)
|
result = re.match('^%s$' % self._expr, format_string, re.VERBOSE)
|
||||||
except TypeError:
|
except TypeError:
|
||||||
return format_string.format(**kwargs)
|
return format_string.format(**kwargs)
|
||||||
if result is not None:
|
if result is not None:
|
||||||
|
23
tests/yamlparser/fixtures/variable_escaping.xml
Normal file
23
tests/yamlparser/fixtures/variable_escaping.xml
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<project>
|
||||||
|
<actions/>
|
||||||
|
<description><!-- Managed by Jenkins Job Builder --></description>
|
||||||
|
<keepDependencies>false</keepDependencies>
|
||||||
|
<blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
|
||||||
|
<blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
|
||||||
|
<concurrentBuild>false</concurrentBuild>
|
||||||
|
<canRoam>true</canRoam>
|
||||||
|
<properties/>
|
||||||
|
<scm class="hudson.scm.NullSCM"/>
|
||||||
|
<builders>
|
||||||
|
<hudson.tasks.Shell>
|
||||||
|
<command>echo "{var}"
|
||||||
|
echo "{defined_var|def}"
|
||||||
|
echo "{undefined_var|def}"
|
||||||
|
echo "{undefined_var|defined_var}"
|
||||||
|
</command>
|
||||||
|
</hudson.tasks.Shell>
|
||||||
|
</builders>
|
||||||
|
<publishers/>
|
||||||
|
<buildWrappers/>
|
||||||
|
</project>
|
17
tests/yamlparser/fixtures/variable_escaping.yaml
Normal file
17
tests/yamlparser/fixtures/variable_escaping.yaml
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
- project:
|
||||||
|
name: test_template_variable_escaping
|
||||||
|
jobs:
|
||||||
|
- 'template_variable_escaping':
|
||||||
|
defined_var: 'Hello'
|
||||||
|
|
||||||
|
- job-template:
|
||||||
|
name: 'template_variable_escaping'
|
||||||
|
builders:
|
||||||
|
- shell: |
|
||||||
|
echo "{{var}}"
|
||||||
|
echo "{{defined_var|def}}"
|
||||||
|
echo "{{undefined_var|def}}"
|
||||||
|
echo "{{undefined_var|defined_var}}"
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user