mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-27 07:01:22 -07:00
Add some comments to templating system
This commit is contained in:
parent
8f9bef9d51
commit
24b536d7ed
1 changed files with 32 additions and 3 deletions
|
@ -34,14 +34,22 @@ _LISTRE = re.compile(r"(\w+)\[(\d+)\]")
|
||||||
|
|
||||||
|
|
||||||
def _varFindLimitSpace(vars, space, part, depth):
|
def _varFindLimitSpace(vars, space, part, depth):
|
||||||
|
''' limits the search space of space to part
|
||||||
|
|
||||||
# TODO: comments
|
basically does space.get(part, None), but with
|
||||||
|
templating for part and a few more things
|
||||||
|
'''
|
||||||
|
|
||||||
|
# Previous part couldn't be found, nothing to limit to
|
||||||
if space is None:
|
if space is None:
|
||||||
return space
|
return space
|
||||||
|
# A part with escaped .s in it is compounded by { and }, remove them
|
||||||
if part[0] == '{' and part[-1] == '}':
|
if part[0] == '{' and part[-1] == '}':
|
||||||
part = part[1:-1]
|
part = part[1:-1]
|
||||||
|
# Template part to resolve variables within (${var$var2})
|
||||||
part = varReplace(part, vars, depth=depth + 1)
|
part = varReplace(part, vars, depth=depth + 1)
|
||||||
|
|
||||||
|
# Now find it
|
||||||
if part in space:
|
if part in space:
|
||||||
space = space[part]
|
space = space[part]
|
||||||
elif "[" in part:
|
elif "[" in part:
|
||||||
|
@ -55,11 +63,30 @@ def _varFindLimitSpace(vars, space, part, depth):
|
||||||
return None
|
return None
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
return space
|
return space
|
||||||
|
|
||||||
def _varFind(text, vars, depth=0):
|
def _varFind(text, vars, depth=0):
|
||||||
|
''' Searches for a variable in text and finds its replacement in vars
|
||||||
|
|
||||||
# TODO: comments
|
The variables can have two formats;
|
||||||
|
- simple, $ followed by alphanumerics and/or underscores
|
||||||
|
- complex, ${ followed by alphanumerics, underscores, periods, braces and brackets, ended by a }
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
- $variable: simple variable that will have vars['variable'] as its replacement
|
||||||
|
- ${variable.complex}: complex variable that will have vars['variable']['complex'] as its replacement
|
||||||
|
- $variable.complex: simple variable, identical to the first, .complex ignored
|
||||||
|
|
||||||
|
Complex variables are broken into parts by separating on periods, except if enclosed in {}.
|
||||||
|
${variable.{fully.qualified.domain}} would be parsed as two parts, variable and fully.qualified.domain,
|
||||||
|
whereas ${variable.fully.qualified.domain} would be parsed as four parts.
|
||||||
|
|
||||||
|
Returns a dict(replacement=<value in vars>, start=<index into text where the variable stated>,
|
||||||
|
end=<index into text where the variable ends>)
|
||||||
|
or None if no variable could be found in text. If replacement is None, it should be replaced with the
|
||||||
|
original data in the caller.
|
||||||
|
'''
|
||||||
|
|
||||||
start = text.find("$")
|
start = text.find("$")
|
||||||
if start == -1:
|
if start == -1:
|
||||||
|
@ -80,6 +107,8 @@ def _varFind(text, vars, depth=0):
|
||||||
is_complex = False
|
is_complex = False
|
||||||
brace_level = 0
|
brace_level = 0
|
||||||
end = var_start
|
end = var_start
|
||||||
|
# part_start is a tuple of where the current part started and its current brace_level
|
||||||
|
# brace_level is used to implement .-escaping
|
||||||
part_start = (var_start, brace_level)
|
part_start = (var_start, brace_level)
|
||||||
space = vars
|
space = vars
|
||||||
while end < len(text) and ((is_complex and brace_level > 0) or not is_complex):
|
while end < len(text) and ((is_complex and brace_level > 0) or not is_complex):
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue