blob: c0e1bb3551d0ac43c3b78cccf21e886f9a716990 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
# Copyright(c) 2006, James Le Cuirot <chewi@aura-online.co.uk>
# Copyright(c) 2004, Karl Trygve Kalleberg <karltk@gentoo.org>
# Copyright(c) 2004, Gentoo Foundation
#
# Licensed under the GNU General Public License, v2
def expand(root, expr, realroot = None):
"""Evaluates a path expression on a given tree.
@param root - the root of the tree
@param expr - the expression to resolve
@return the expanded string
"""
if realroot == None:
realroot = root
expanded = ""
in_varref = False
varname = ""
for i in range(len(expr)):
x = expr[i]
if in_varref:
if x == "}":
in_varref = False
expanded += expand(root, realroot.find_node(varname).value, realroot)
varname = ""
elif x != "{":
varname += expr[i]
elif x == "$" and i < len(expr) and expr[i + 1] == "{":
in_varref = True
else:
expanded += x
return expanded
def strip_varmarker(s):
"""Strips away ${ and } in a variable expression. Idempotent if marker not found.
Example: "${foo}" -> "foo"
Example: "foo" -> "foo"
"""
if s.startswith("${") and s.endswith("}"):
return s[2:-1]
return s
# vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap:
|