2 # Variable substitution. Variables are $delimited$
6 error
= 'varsubst.error'
9 def __init__(self
, dict):
11 self
.prog
= re
.compile('\$([a-zA-Z0-9_]*)\$')
14 def useindent(self
, onoff
):
15 self
.do_useindent
= onoff
20 m
= self
.prog
.search(s
)
23 rv
= rv
+ s
[:m
.start()]
25 if m
.end() - m
.start() == 2:
31 if not self
.dict.has_key(name
):
32 raise error
, 'No such variable: '+name
33 value
= self
.dict[name
]
34 if self
.do_useindent
and '\n' in value
:
35 value
= self
._modindent
(value
, rv
)
38 def _modindent(self
, value
, old
):
39 lastnl
= old
.rfind('\n', 0) + 1
40 lastnl
= len(old
) - lastnl
41 sub
= '\n' + (' '*lastnl
)
42 return re
.sub('\n', sub
, value
)
48 sys
.stderr
.write('-- Copying stdin to stdout with environment map --\n')
49 c
= Varsubst(os
.environ
)
52 sys
.stdout
.write(c
.subst(d
))
55 if __name__
== '__main__':