Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
70c76399
Commit
70c76399
authored
Aug 10, 1992
by
Guido van Rossum
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Almost totally rewritten.
parent
8b5863fb
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
123 additions
and
33 deletions
+123
-33
Demo/scripts/pp.py
Demo/scripts/pp.py
+123
-33
No files found.
Demo/scripts/pp.py
View file @
70c76399
#! /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.
# Emulate some Perl command line options.
# Usage: pp [-a] [-c] [-d] [-e scriptline] [-F fieldsep] [-n] [-p] [file] ...
# Where the options mean the following:
# -a : together with -n or -p, splits each line into list F
# -c : check syntax only, do not execute any code
# -d : run the script under the debugger, pdb
# -e scriptline : gives one line of the Python script; may be repeated
# -F fieldsep : sets the field separator for the -a option [not in Perl]
# -n : runs the script for each line of input
# -p : prints the line after the script has run
# When no script lines have been passed, the first file argument
# contains the script. With -n or -p, the remaining arguments are
# read as input to the script, line by line. If a file is '-'
# or missing, standard input is read.
# XXX To do:
# - add -i extension option (change files in place)
# - make a single loop over the files and lines (changes effect of 'break')?
# - add an option to specify the record separator
# - except for -n/-p, run directly from the file if at all possible
import
sys
import
string
import
getopt
FS
=
''
SCRIPT
=
[]
AFLAG
=
0
CFLAG
=
0
DFLAG
=
0
NFLAG
=
0
PFLAG
=
0
try
:
optlist
,
ARGS
=
getopt
.
getopt
(
sys
.
argv
[
1
:],
'acde:F:np'
)
except
getopt
.
error
,
msg
:
sys
.
stderr
.
write
(
sys
.
argv
[
0
]
+
': '
+
msg
+
'
\
n
'
)
sys
.
exit
(
2
)
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:]:'
,
\
'
\
t
if FILE ==
\
'
-
\
'
:'
,
\
'
\
t
\
t
FP = sys.stdin'
,
\
'
\
t
else:'
,
\
'
\
t
\
t
FP = open(FILE,
\
'
r
\
'
)'
,
\
'
\
t
while 1:'
,
\
'
\
t
\
t
LINE = FP.readline()'
,
\
'
\
t
\
t
if not LINE: break'
,
\
'
\
t
\
t
L = LINE[:-1]'
,
\
'
\
t
\
t
if FS: F = string.splitfields(L, FS)'
,
\
'
\
t
\
t
else: F = string.split(L)'
\
if
option
==
'-a'
:
AFLAG
=
1
elif
option
==
'-c'
:
CFLAG
=
1
elif
option
==
'-d'
:
DFLAG
=
1
elif
option
==
'-e'
:
for
line
in
string
.
splitfields
(
optarg
,
'
\
n
'
):
SCRIPT
.
append
(
line
)
elif
option
==
'-F'
:
FS
=
optarg
elif
option
==
'-n'
:
NFLAG
=
1
PFLAG
=
0
elif
option
==
'-p'
:
NFLAG
=
1
PFLAG
=
1
else
:
print
option
,
'not recognized???'
if
not
ARGS
:
ARGS
.
append
(
'-'
)
if
not
SCRIPT
:
if
ARGS
[
0
]
==
'-'
:
fp
=
sys
.
stdin
else
:
fp
=
open
(
ARGS
[
0
],
'r'
)
while
1
:
line
=
fp
.
readline
()
if
not
line
:
break
SCRIPT
.
append
(
line
[:
-
1
])
del
fp
del
ARGS
[
0
]
if
not
ARGS
:
ARGS
.
append
(
'-'
)
if
CFLAG
:
prologue
=
[
'if 0:'
]
epilogue
=
[]
elif
NFLAG
:
# Note that it is on purpose that AFLAG and PFLAG are
# tested dynamically each time through the loop
prologue
=
[
\
'LINECOUNT = 0'
,
\
'for FILE in ARGS:'
,
\
'
\
t
if FILE ==
\
'
-
\
'
:'
,
\
'
\
t
\
t
FP = sys.stdin'
,
\
'
\
t
else:'
,
\
'
\
t
\
t
FP = open(FILE,
\
'
r
\
'
)'
,
\
'
\
t
LINENO = 0'
,
\
'
\
t
while 1:'
,
\
'
\
t
\
t
LINE = FP.readline()'
,
\
'
\
t
\
t
if not LINE: break'
,
\
'
\
t
\
t
LINENO = LINENO + 1'
,
\
'
\
t
\
t
LINECOUNT = LINECOUNT + 1'
,
\
'
\
t
\
t
L = LINE[:-1]'
,
\
'
\
t
\
t
aflag = AFLAG'
,
\
'
\
t
\
t
if aflag:'
,
\
'
\
t
\
t
\
t
if FS: F = string.splitfields(L, FS)'
,
\
'
\
t
\
t
\
t
else: F = string.split(L)'
\
]
epilogue
=
[
\
'
\
t
\
t
if not PFLAG: continue'
,
\
'
\
t
\
t
if aflag:'
,
\
'
\
t
\
t
\
t
if FS: print string.joinfields(F, FS)'
,
\
'
\
t
\
t
\
t
else: print string.join(F)'
,
\
'
\
t
\
t
else: print L'
,
\
]
else
:
prologue
=
[
'if 1:'
]
epilogue
=
[]
# 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
'
program
=
string
.
joinfields
(
prologue
,
'
\
n
'
)
+
'
\
n
'
for
line
in
SCRIPT
:
program
=
program
+
(
'
\
t
\
t
'
+
line
+
'
\
n
'
)
program
=
program
+
(
string
.
joinfields
(
epilogue
,
'
\
n
'
)
+
'
\
n
'
)
exec
(
program
)
import
tempfile
tfn
=
tempfile
.
mktemp
()
try
:
fp
=
open
(
tfn
,
'w'
)
fp
.
write
(
program
)
fp
.
close
()
if
DFLAG
:
import
pdb
pdb
.
run
(
'execfile('
+
`tfn`
+
')'
)
else
:
execfile
(
tfn
)
finally
:
import
os
try
:
os
.
unlink
(
tfn
)
except
:
pass
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment