Created
April 25, 2010 01:32
-
-
Save kergoth/378086 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pyparsing import * | |
from string import printable | |
data = "${foo} ${${bar}} whee ${ bar ${baz ${meh} moo}}" | |
datastore = { | |
"foo": "fooval", | |
"bar": "barval", | |
"barval": "barvalval", | |
"meh": "MEH", | |
"baz MEH moo": "heh.", | |
" bar heh.": "dance!", | |
} | |
expanded_data = "fooval barvalval whee dance!" | |
def getVar(tokens): | |
var = tokens[0] | |
val = "".join(var.asList()) | |
if val in datastore: | |
tokens[0] = datastore[val] | |
content = Combine(OneOrMore(~Literal("${") + ~Literal("}") + | |
Word(printable, exact=1))) | |
content.leaveWhitespace() | |
var = nestedExpr("${", "}", ignoreExpr=None, content=content) | |
var.setParseAction(getVar) | |
newdata = var.transformString(data) | |
assert(newdata == expanded_data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment