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