summaryrefslogtreecommitdiffstats
path: root/Demo/scripts/pp.py
blob: c3be09164b34350817065fc04ee438750741f90a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#! /usr/local/python

# Wrapper around Python to emulate the Perl -ae options:
# (1) first argument is a Python command
# (2) rest of arguments are input to the command in an implied loop
# (3) each line is put into the string L with trailing '\n' stripped
# (4) the fields of the line are put in the list F
# (5) also: FILE: full filename; LINE: full line; FP: open file object
# The command line option "-f FS" sets the field separator;
# this is available to the program as FS.

import sys
import string
import getopt

FS = ''

optlist, args = getopt.getopt(sys.argv[1:], 'f:')
for option, optarg in optlist:
	if option == '-f': FS = optarg

command = args[0]

if not args[1:]: args.append('-')

prologue = [ \
	'for FILE in args[1:]:', \
	'\tif FILE == \'-\':', \
	'\t\tFP = sys.stdin', \
	'\telse:', \
	'\t\tFP = open(FILE, \'r\')', \
	'\twhile 1:', \
	'\t\tLINE = FP.readline()', \
	'\t\tif not LINE: break', \
	'\t\tL = LINE[:-1]', \
	'\t\tif FS: F = string.splitfields(L, FS)', \
	'\t\telse: F = string.split(L)' \
	]

# Note that we indent using tabs only, so that any indentation style
# used in 'command' will come out right after re-indentation.

program = string.joinfields(prologue, '\n')
for line in string.splitfields(command, '\n'):
	program = program + ('\n\t\t' + line)
program = program + '\n'

exec(program)