Guido van Rossum | d211220 | 1995-03-02 14:05:29 +0000 | [diff] [blame] | 1 | # |
| 2 | # Variable substitution. Variables are $delimited$ |
| 3 | # |
| 4 | import string |
Jack Jansen | 9aaee93 | 2001-12-27 23:35:43 +0000 | [diff] [blame] | 5 | import re |
Guido van Rossum | d211220 | 1995-03-02 14:05:29 +0000 | [diff] [blame] | 6 | |
| 7 | error = 'varsubst.error' |
| 8 | |
| 9 | class Varsubst: |
| 10 | def __init__(self, dict): |
Guido van Rossum | a16a509 | 1998-04-10 19:15:27 +0000 | [diff] [blame] | 11 | self.dict = dict |
Jack Jansen | 9aaee93 | 2001-12-27 23:35:43 +0000 | [diff] [blame] | 12 | self.prog = re.compile('\$([a-zA-Z0-9_]*)\$') |
Guido van Rossum | a16a509 | 1998-04-10 19:15:27 +0000 | [diff] [blame] | 13 | self.do_useindent = 0 |
Guido van Rossum | d211220 | 1995-03-02 14:05:29 +0000 | [diff] [blame] | 14 | |
| 15 | def useindent(self, onoff): |
Guido van Rossum | a16a509 | 1998-04-10 19:15:27 +0000 | [diff] [blame] | 16 | self.do_useindent = onoff |
| 17 | |
Jack Jansen | 9aaee93 | 2001-12-27 23:35:43 +0000 | [diff] [blame] | 18 | def subst(self, s): |
Guido van Rossum | a16a509 | 1998-04-10 19:15:27 +0000 | [diff] [blame] | 19 | rv = '' |
| 20 | while 1: |
Jack Jansen | 9aaee93 | 2001-12-27 23:35:43 +0000 | [diff] [blame] | 21 | m = self.prog.search(s) |
| 22 | if not m: |
| 23 | return rv + s |
| 24 | rv = rv + s[:m.start()] |
| 25 | s = s[m.end():] |
| 26 | if m.end() - m.start() == 2: |
Guido van Rossum | a16a509 | 1998-04-10 19:15:27 +0000 | [diff] [blame] | 27 | # Escaped dollar |
| 28 | rv = rv + '$' |
Jack Jansen | 9aaee93 | 2001-12-27 23:35:43 +0000 | [diff] [blame] | 29 | s = s[2:] |
Guido van Rossum | a16a509 | 1998-04-10 19:15:27 +0000 | [diff] [blame] | 30 | continue |
Jack Jansen | 9aaee93 | 2001-12-27 23:35:43 +0000 | [diff] [blame] | 31 | name = m.group(1) |
Guido van Rossum | a16a509 | 1998-04-10 19:15:27 +0000 | [diff] [blame] | 32 | if not self.dict.has_key(name): |
| 33 | raise error, 'No such variable: '+name |
| 34 | value = self.dict[name] |
| 35 | if self.do_useindent and '\n' in value: |
| 36 | value = self._modindent(value, rv) |
| 37 | rv = rv + value |
Guido van Rossum | d211220 | 1995-03-02 14:05:29 +0000 | [diff] [blame] | 38 | |
| 39 | def _modindent(self, value, old): |
Guido van Rossum | a16a509 | 1998-04-10 19:15:27 +0000 | [diff] [blame] | 40 | lastnl = string.rfind(old, '\n', 0) + 1 |
| 41 | lastnl = len(old) - lastnl |
| 42 | sub = '\n' + (' '*lastnl) |
Jack Jansen | 9aaee93 | 2001-12-27 23:35:43 +0000 | [diff] [blame] | 43 | return re.sub('\n', sub, value) |
Guido van Rossum | d211220 | 1995-03-02 14:05:29 +0000 | [diff] [blame] | 44 | |
| 45 | def _test(): |
| 46 | import sys |
| 47 | import os |
| 48 | |
| 49 | sys.stderr.write('-- Copying stdin to stdout with environment map --\n') |
| 50 | c = Varsubst(os.environ) |
| 51 | c.useindent(1) |
| 52 | d = sys.stdin.read() |
| 53 | sys.stdout.write(c.subst(d)) |
| 54 | sys.exit(1) |
| 55 | |
| 56 | if __name__ == '__main__': |
| 57 | _test() |