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
f8e946b7
Commit
f8e946b7
authored
Oct 25, 2012
by
Michael DeHaan
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'varfind-cleanups' of
git://github.com/dhozac/ansible
into devel
parents
ffca0283
c9c5fc14
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
47 additions
and
37 deletions
+47
-37
lib/ansible/utils.py
+39
-37
test/TestUtils.py
+8
-0
No files found.
lib/ansible/utils.py
View file @
f8e946b7
...
...
@@ -220,36 +220,39 @@ def parse_json(raw_data):
_LISTRE
=
re
.
compile
(
r"(\w+)\[(\d+)\]"
)
class
VarNotFoundException
(
Exception
):
pass
def
_varLookup
(
path
,
vars
,
depth
=
0
):
''' find the contents of a possibly complex variable in vars. '''
space
=
vars
for
part
in
path
:
part
=
varReplace
(
part
,
vars
,
depth
=
depth
+
1
)
if
part
in
space
:
space
=
space
[
part
]
elif
"["
in
part
:
m
=
_LISTRE
.
search
(
part
)
if
not
m
:
raise
VarNotFoundException
()
def
_varFindLimitSpace
(
space
,
part
,
depth
):
if
space
is
None
:
return
space
if
part
[
0
]
==
'{'
and
part
[
-
1
]
==
'}'
:
part
=
part
[
1
:
-
1
]
part
=
varReplace
(
part
,
vars
,
depth
=
depth
+
1
)
if
part
in
space
:
space
=
space
[
part
]
elif
"["
in
part
:
m
=
_LISTRE
.
search
(
part
)
if
not
m
:
return
None
else
:
try
:
space
=
space
[
m
.
group
(
1
)][
int
(
m
.
group
(
2
))]
except
(
KeyError
,
IndexError
):
r
aise
VarNotFoundException
()
else
:
raise
VarNotFoundException
()
r
eturn
None
else
:
return
None
return
space
def
_varFind
(
text
):
def
_varFind
(
text
,
vars
,
depth
=
0
):
start
=
text
.
find
(
"$"
)
if
start
==
-
1
:
return
None
var_start
=
start
+
1
if
var_start
>
=
len
(
text
):
# $ as last character
if
start
+
1
=
=
len
(
text
):
return
None
# Escaped var
if
start
>
0
and
text
[
start
-
1
]
==
'
\\
'
:
return
{
'replacement'
:
'$'
,
'start'
:
start
-
1
,
'end'
:
start
+
1
}
var_start
=
start
+
1
if
text
[
var_start
]
==
'{'
:
is_complex
=
True
brace_level
=
1
...
...
@@ -260,6 +263,7 @@ def _varFind(text):
end
=
var_start
path
=
[]
part_start
=
(
var_start
,
brace_level
)
space
=
vars
while
end
<
len
(
text
)
and
((
is_complex
and
brace_level
>
0
)
or
not
is_complex
):
if
text
[
end
]
.
isalnum
()
or
text
[
end
]
==
'_'
:
pass
...
...
@@ -271,10 +275,7 @@ def _varFind(text):
pass
elif
is_complex
and
text
[
end
]
==
'.'
:
if
brace_level
==
part_start
[
1
]:
if
text
[
part_start
[
0
]]
==
'{'
:
path
.
append
(
text
[
part_start
[
0
]
+
1
:
end
-
1
])
else
:
path
.
append
(
text
[
part_start
[
0
]:
end
])
space
=
_varFindLimitSpace
(
space
,
text
[
part_start
[
0
]:
end
],
depth
)
part_start
=
(
end
+
1
,
brace_level
)
else
:
break
...
...
@@ -284,8 +285,10 @@ def _varFind(text):
var_end
-=
1
if
text
[
var_end
]
!=
'}'
or
brace_level
!=
0
:
return
None
path
.
append
(
text
[
part_start
[
0
]:
var_end
])
return
{
'path'
:
path
,
'start'
:
start
,
'end'
:
end
}
if
var_end
==
part_start
[
0
]:
return
None
space
=
_varFindLimitSpace
(
space
,
text
[
part_start
[
0
]:
var_end
],
depth
)
return
{
'replacement'
:
space
,
'start'
:
start
,
'end'
:
end
}
def
varReplace
(
raw
,
vars
,
depth
=
0
,
expand_lists
=
False
):
''' Perform variable replacement of $variables in string raw using vars dictionary '''
...
...
@@ -297,7 +300,7 @@ def varReplace(raw, vars, depth=0, expand_lists=False):
done
=
[]
# Completed chunks to return
while
raw
:
m
=
_varFind
(
raw
)
m
=
_varFind
(
raw
,
vars
,
depth
)
if
not
m
:
done
.
append
(
raw
)
break
...
...
@@ -305,13 +308,12 @@ def varReplace(raw, vars, depth=0, expand_lists=False):
# Determine replacement value (if unknown variable then preserve
# original)
try
:
replacement
=
_varLookup
(
m
[
'path'
],
vars
,
depth
)
if
expand_lists
and
isinstance
(
replacement
,
(
list
,
tuple
)):
replacement
=
","
.
join
(
replacement
)
if
isinstance
(
replacement
,
(
str
,
unicode
)):
replacement
=
varReplace
(
replacement
,
vars
,
depth
=
depth
+
1
,
expand_lists
=
expand_lists
)
except
VarNotFoundException
:
replacement
=
m
[
'replacement'
]
if
expand_lists
and
isinstance
(
replacement
,
(
list
,
tuple
)):
replacement
=
","
.
join
(
replacement
)
if
isinstance
(
replacement
,
(
str
,
unicode
)):
replacement
=
varReplace
(
replacement
,
vars
,
depth
=
depth
+
1
,
expand_lists
=
expand_lists
)
if
replacement
is
None
:
replacement
=
raw
[
m
[
'start'
]:
m
[
'end'
]]
start
,
end
=
m
[
'start'
],
m
[
'end'
]
...
...
@@ -362,12 +364,12 @@ def varReplaceWithItems(basedir, varname, vars):
''' helper function used by with_items '''
if
isinstance
(
varname
,
basestring
):
m
=
_varFind
(
varname
)
m
=
_varFind
(
varname
,
vars
)
if
not
m
:
return
varname
if
m
[
'start'
]
==
0
and
m
[
'end'
]
==
len
(
varname
):
try
:
return
varReplaceWithItems
(
basedir
,
_varLookup
(
m
[
'path'
],
vars
)
,
vars
)
return
varReplaceWithItems
(
basedir
,
m
[
'replacement'
]
,
vars
)
except
VarNotFoundException
:
return
varname
else
:
...
...
test/TestUtils.py
View file @
f8e946b7
...
...
@@ -230,6 +230,14 @@ class TestUtils(unittest.TestCase):
res
=
ansible
.
utils
.
varReplace
(
template
,
vars
,
expand_lists
=
True
)
assert
res
==
'yum pkg=foo,bar,baz state=installed'
def
test_varReplace_escaped_var
(
self
):
vars
=
{
'foo'
:
'bar'
,
}
template
=
'action
\
$foo'
res
=
ansible
.
utils
.
varReplace
(
template
,
vars
)
assert
res
==
'action $foo'
def
test_template_varReplace_iterated
(
self
):
template
=
'hello $who'
vars
=
{
...
...
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