summaryrefslogtreecommitdiffstats
path: root/Demo/scripts/pp.py
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1992-07-07 09:11:53 (GMT)
committerGuido van Rossum <guido@python.org>1992-07-07 09:11:53 (GMT)
commitca7b213b469fe07f0fadb78901f1b25957bab4ef (patch)
tree79c1ff5630fcaa19beee431d6db11ee3454b39e6 /Demo/scripts/pp.py
parent0b927e2e3e17edcdf81947ebd674dc0828542c97 (diff)
downloadcpython-ca7b213b469fe07f0fadb78901f1b25957bab4ef.zip
cpython-ca7b213b469fe07f0fadb78901f1b25957bab4ef.tar.gz
cpython-ca7b213b469fe07f0fadb78901f1b25957bab4ef.tar.bz2
Initial revision
Diffstat (limited to 'Demo/scripts/pp.py')
-rwxr-xr-xDemo/scripts/pp.py48
1 files changed, 48 insertions, 0 deletions
diff --git a/Demo/scripts/pp.py b/Demo/scripts/pp.py
new file mode 100755
index 0000000..c3be091
--- /dev/null
+++ b/Demo/scripts/pp.py
@@ -0,0 +1,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)