14 substitution_str =
r'(^[^\n\S]*)?\$([^\d\W]\w*|\{,?[^\d\W]\w*\,?})' 20 substitution_str = substitution_str.replace(
r'\w',
r'[a-zA-Z0-9_]')
22 subtitution = re.compile(substitution_str, re.MULTILINE)
25 def from_file(filename):
26 with open(filename,
'r') as f: 29 def __init__(self, pattern, filename=""):
33 def substitute(self, env={}, **kwargs):
35 return kwargs[v]
if v
in kwargs
else env[v]
37 def indent_lines(indent, v):
38 return "".join([indent + l +
"\n" for e
in v
for l
in str(e).splitlines()]).rstrip()
41 indent = match.group(1)
54 if indent
is not None:
55 if not isinstance(v, list):
57 return indent_lines(indent, v)
58 elif isinstance(v, list):
59 middle =
', '.join([str(x)
for x
in v])
62 return comma_before + middle + comma_after
65 return self.subtitution.sub(replace, self.
pattern)
68 if __name__ ==
"__main__":
76 int commatest(int a${,stuff}) 77 int notest(int a${,empty,}) 79 print(c.substitute(args=[
"hi", 8], bar=[
"what", 7],
80 a=3, b=4, stuff=[
"things...",
"others"], empty=[]))