There are many templating systems out there. Pick the one that suits you
best. A very basic one is str.format():
>>> "{foo} {bar}".format(foo=1, bar=2)
'1 2'
Here's what your script might become if you choose that one:
$ cat interactive_template.txt
$include "_dispatcher_publish_filters.any"
/1000 {{ /type "allow" /glob "* /{CONTENT_PATH}/*.html*" }}
/1001 {{ /type "allow" /glob "POST /{DAMPATH}/www/*.html *" }}
$ cat interactive_template.py
try:
format_map = str.format_map
except AttributeError: # python 2 compatibility
import string
def format_map(text, lookup):
return string.Formatter().vformat(text, (), lookup)
input = raw_input
class Lookup(dict):
def __missing__(self, key):
self[key] = value = input("{}: ".format(key))
return value
def main():
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("template_file")
parser.add_argument("generated_file", nargs="?")
args = parser.parse_args()
with open(args.template_file) as instream:
template_text = instream.read()
lookup = Lookup()
generated_text = format_map(template_text, lookup)
generated_file = args.generated_file
if generated_file is None:
generated_file = format_map("{nnn}{brand}_farm.any", lookup)
print("Writing {}".format(generated_file))
with open(generated_file, "w") as outstream:
outstream.write(generated_text)
if __name__ == "__main__":
main()
$ python interactive_template.py interactive_template.txt
CONTENT_PATH: foo
DAMPATH: bar
nnn: baz
brand: ham
Writing bazham_farm.any
$ cat bazham_farm.any
$include "_dispatcher_publish_filters.any"
/1000 { /type "allow" /glob "* /foo/*.html*" }
/1001 { /type "allow" /glob "POST /bar/www/*.html *" }