Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
ansible
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
OpenEdx
ansible
Commits
aee940aa
Commit
aee940aa
authored
Jul 28, 2014
by
James Cammarata
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Reworking _clean_data() to be smarter about replaces
Fixes #8228
parent
6e814566
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
94 additions
and
10 deletions
+94
-10
lib/ansible/utils/__init__.py
+63
-10
test/units/TestUtils.py
+31
-0
No files found.
lib/ansible/utils/__init__.py
View file @
aee940aa
...
...
@@ -350,16 +350,69 @@ def json_loads(data):
return
json
.
loads
(
data
)
def
_clean_data
(
orig_data
,
from_remote
=
False
,
from_inventory
=
False
):
''' remove template tags from a string '''
data
=
orig_data
if
isinstance
(
orig_data
,
basestring
):
sub_list
=
[(
'{
%
'
,
'{#'
),
(
'
%
}'
,
'#}'
)]
if
from_remote
or
(
from_inventory
and
'{{'
in
data
and
LOOKUP_REGEX
.
search
(
data
)):
# if from a remote, we completely disable any jinja2 blocks
sub_list
.
extend
([(
'{{'
,
'{#'
),
(
'}}'
,
'#}'
)])
for
pattern
,
replacement
in
sub_list
:
data
=
data
.
replace
(
pattern
,
replacement
)
return
data
''' remove jinja2 template tags from a string '''
if
not
isinstance
(
orig_data
,
basestring
):
return
orig_data
data
=
StringIO
.
StringIO
(
""
)
# when the data is marked as having come from a remote, we always
# replace any print blocks (ie. {{var}}), however when marked as coming
# from inventory we only replace print blocks that contain a call to
# a lookup plugin (ie. {{lookup('foo','bar'))}})
replace_prints
=
from_remote
or
(
from_inventory
and
'{{'
in
orig_data
and
LOOKUP_REGEX
.
search
(
orig_data
)
is
not
None
)
# these variables keep track of opening block locations, as we only
# want to replace matched pairs of print/block tags
print_openings
=
[]
block_openings
=
[]
for
idx
,
c
in
enumerate
(
orig_data
):
# if the current character is an opening brace, check to
# see if this is a jinja2 token. Otherwise, if the current
# character is a closing brace, we backup one character to
# see if we have a closing.
if
c
==
'{'
and
idx
<
len
(
orig_data
)
-
1
:
token
=
orig_data
[
idx
:
idx
+
2
]
# if so, and we want to replace this block, push
# this token's location onto the appropriate array
if
token
==
'{{'
and
replace_prints
:
print_openings
.
append
(
idx
)
elif
token
==
'{
%
'
:
block_openings
.
append
(
idx
)
# finally we write the data to the buffer and write
data
.
seek
(
0
,
os
.
SEEK_END
)
data
.
write
(
c
)
elif
c
==
'}'
and
idx
>
0
:
token
=
orig_data
[
idx
-
1
:
idx
+
1
]
prev_idx
=
-
1
if
token
==
'
%
}'
and
len
(
block_openings
)
>
0
:
prev_idx
=
block_openings
.
pop
()
elif
token
==
'}}'
and
len
(
print_openings
)
>
0
:
prev_idx
=
print_openings
.
pop
()
# if we have a closing token, and we have previously found
# the opening to the same kind of block represented by this
# token, replace both occurrences, otherwise we just write
# the current character to the buffer
if
prev_idx
!=
-
1
:
# replace the opening
data
.
seek
(
prev_idx
,
os
.
SEEK_SET
)
data
.
write
(
'{#'
)
# replace the closing
data
.
seek
(
-
1
,
os
.
SEEK_END
)
data
.
write
(
'#}'
)
else
:
data
.
seek
(
0
,
os
.
SEEK_END
)
data
.
write
(
c
)
else
:
# not a jinja2 token, so we just write the current char
# to the output buffer
data
.
seek
(
0
,
os
.
SEEK_END
)
data
.
write
(
c
)
return_data
=
data
.
getvalue
()
data
.
close
()
return
return_data
def
_clean_data_struct
(
orig_data
,
from_remote
=
False
,
from_inventory
=
False
):
'''
...
...
test/units/TestUtils.py
View file @
aee940aa
...
...
@@ -716,4 +716,35 @@ class TestUtils(unittest.TestCase):
# invalid jinja2 nesting detection
# invalid quote nesting detection
def
test_clean_data
(
self
):
# clean data removes jinja2 tags from data
self
.
assertEqual
(
ansible
.
utils
.
_clean_data
(
'this is a normal string'
,
from_remote
=
True
),
'this is a normal string'
)
self
.
assertEqual
(
ansible
.
utils
.
_clean_data
(
'this string has a {{variable}}'
,
from_remote
=
True
),
'this string has a {#variable#}'
)
self
.
assertEqual
(
ansible
.
utils
.
_clean_data
(
'this string has a {{variable with a
\n
newline}}'
,
from_remote
=
True
),
'this string has a {#variable with a
\n
newline#}'
)
self
.
assertEqual
(
ansible
.
utils
.
_clean_data
(
'this string is from inventory {{variable}}'
,
from_inventory
=
True
),
'this string is from inventory {{variable}}'
)
self
.
assertEqual
(
ansible
.
utils
.
_clean_data
(
'this string is from inventory too but uses lookup {{lookup("foo","bar")}}'
,
from_inventory
=
True
),
'this string is from inventory too but uses lookup {#lookup("foo","bar")#}'
)
self
.
assertEqual
(
ansible
.
utils
.
_clean_data
(
'this string has JSON in it: {"foo":{"bar":{"baz":"oops"}}}'
,
from_remote
=
True
),
'this string has JSON in it: {"foo":{"bar":{"baz":"oops"}}}'
)
self
.
assertEqual
(
ansible
.
utils
.
_clean_data
(
'this string contains unicode: ¢ £ ¤ ¥'
,
from_remote
=
True
),
'this string contains unicode: ¢ £ ¤ ¥'
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment