2 # Variable substitution. Variables are $delimited$
8 error
= 'varsubst.error'
11 def __init__(self
, dict):
13 self
.prog
= regex
.compile('\$[a-zA-Z0-9_]*\$')
16 def useindent(self
, onoff
):
17 self
.do_useindent
= onoff
22 pos
= self
.prog
.search(str)
28 len = self
.prog
.match(str)
36 if not self
.dict.has_key(name
):
37 raise error
, 'No such variable: '+name
38 value
= self
.dict[name
]
39 if self
.do_useindent
and '\n' in value
:
40 value
= self
._modindent
(value
, rv
)
43 def _modindent(self
, value
, old
):
44 lastnl
= string
.rfind(old
, '\n', 0) + 1
45 lastnl
= len(old
) - lastnl
46 sub
= '\n' + (' '*lastnl
)
47 return regsub
.gsub('\n', sub
, value
)
53 sys
.stderr
.write('-- Copying stdin to stdout with environment map --\n')
54 c
= Varsubst(os
.environ
)
57 sys
.stdout
.write(c
.subst(d
))
60 if __name__
== '__main__':