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
7f589fdd
Commit
7f589fdd
authored
Jul 11, 2000
by
Peter Schneider-Kamp
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add expandtabs command (-e)
change eliminate to delete (-d)
parent
acab3d61
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
64 additions
and
37 deletions
+64
-37
Tools/scripts/pindent.py
Tools/scripts/pindent.py
+64
-37
No files found.
Tools/scripts/pindent.py
View file @
7f589fdd
...
...
@@ -4,7 +4,7 @@
# related (though complimentary) formatting operations on Python
# programs. When called as "pindent -c", it takes a valid Python
# program as input and outputs a version augmented with block-closing
# comments. When called as "pindent -
e
", it assumes its input is a
# comments. When called as "pindent -
d
", it assumes its input is a
# Python program with block-closing comments and outputs a commentless
# version. When called as "pindent -r" it assumes its input is a
# Python program with block-closing comments but with its indentation
...
...
@@ -46,6 +46,7 @@
# Other options:
# -s stepsize: set the indentation step size (default 8)
# -t tabsize : set the number of spaces a tab character is worth (default 8)
# -e : expand TABs into spaces
# file ... : input file(s) (default standard input)
# The results always go to standard output
...
...
@@ -78,6 +79,7 @@
# Defaults
STEPSIZE
=
8
TABSIZE
=
8
EXPANDTABS
=
0
import
os
import
re
...
...
@@ -96,13 +98,14 @@ start = 'if', 'while', 'for', 'try', 'def', 'class'
class
PythonIndenter
:
def
__init__
(
self
,
fpi
=
sys
.
stdin
,
fpo
=
sys
.
stdout
,
indentsize
=
STEPSIZE
,
tabsize
=
TABSIZE
):
indentsize
=
STEPSIZE
,
tabsize
=
TABSIZE
,
expandtabs
=
EXPANDTABS
):
self
.
fpi
=
fpi
self
.
fpo
=
fpo
self
.
indentsize
=
indentsize
self
.
tabsize
=
tabsize
self
.
lineno
=
0
self
.
write
=
fpo
.
write
self
.
expandtabs
=
expandtabs
self
.
_write
=
fpo
.
write
self
.
kwprog
=
re
.
compile
(
r'^\
s*(?P<kw>[
a-z]+)'
r'(\
s+(?P<id>[
a-zA-Z_]\
w*))?
'
...
...
@@ -114,6 +117,14 @@ class PythonIndenter:
self.wsprog = re.compile(r'
^
[
\
t
]
*
')
# end def __init__
def write(self, line):
if self.expandtabs:
self._write(string.expandtabs(line, self.tabsize))
else:
self._write(line)
# end if
# end def write
def readline(self):
line = self.fpi.readline()
if line: self.lineno = self.lineno + 1
...
...
@@ -196,7 +207,7 @@ class PythonIndenter:
# end if
# end def reformat
def
elimina
te
(
self
):
def
dele
te
(
self
):
begin_counter
=
0
end_counter
=
0
while
1
:
...
...
@@ -222,7 +233,7 @@ class PythonIndenter:
elif
begin_counter
-
end_counter
>
0
:
sys
.
stderr
.
write
(
'Warning: input contained less end tags than expected
\
n
'
)
# end if
# end def
elimina
te
# end def
dele
te
def
complete
(
self
):
self
.
indentsize
=
1
...
...
@@ -325,20 +336,20 @@ class PythonIndenter:
# - xxx_file(filename): process file in place, return true iff changed
def
complete_filter
(
input
=
sys
.
stdin
,
output
=
sys
.
stdout
,
stepsize
=
STEPSIZE
,
tabsize
=
TABSIZE
):
pi
=
PythonIndenter
(
input
,
output
,
stepsize
,
tabsize
)
stepsize
=
STEPSIZE
,
tabsize
=
TABSIZE
,
expandtabs
=
EXPANDTABS
):
pi
=
PythonIndenter
(
input
,
output
,
stepsize
,
tabsize
,
expandtabs
)
pi
.
complete
()
# end def complete_filter
def
elimina
te_filter
(
input
=
sys
.
stdin
,
output
=
sys
.
stdout
,
stepsize
=
STEPSIZE
,
tabsize
=
TABSIZE
):
pi
=
PythonIndenter
(
input
,
output
,
stepsize
,
tabsize
)
pi
.
elimina
te
()
# end def
elimina
te_filter
def
dele
te_filter
(
input
=
sys
.
stdin
,
output
=
sys
.
stdout
,
stepsize
=
STEPSIZE
,
tabsize
=
TABSIZE
,
expandtabs
=
EXPANDTABS
):
pi
=
PythonIndenter
(
input
,
output
,
stepsize
,
tabsize
,
expandtabs
)
pi
.
dele
te
()
# end def
dele
te_filter
def
reformat_filter
(
input
=
sys
.
stdin
,
output
=
sys
.
stdout
,
stepsize
=
STEPSIZE
,
tabsize
=
TABSIZE
):
pi
=
PythonIndenter
(
input
,
output
,
stepsize
,
tabsize
)
stepsize
=
STEPSIZE
,
tabsize
=
TABSIZE
,
expandtabs
=
EXPANDTABS
):
pi
=
PythonIndenter
(
input
,
output
,
stepsize
,
tabsize
,
expandtabs
)
pi
.
reformat
()
# end def reformat_filter
...
...
@@ -386,33 +397,33 @@ class StringWriter:
# end def getvalue
# end class StringWriter
def
complete_string
(
source
,
stepsize
=
STEPSIZE
,
tabsize
=
TABSIZE
):
def
complete_string
(
source
,
stepsize
=
STEPSIZE
,
tabsize
=
TABSIZE
,
expandtabs
=
EXPANDTABS
):
input
=
StringReader
(
source
)
output
=
StringWriter
()
pi
=
PythonIndenter
(
input
,
output
,
stepsize
,
tabsize
)
pi
=
PythonIndenter
(
input
,
output
,
stepsize
,
tabsize
,
expandtabs
)
pi
.
complete
()
return
output
.
getvalue
()
# end def complete_string
def
eliminate_string
(
source
,
stepsize
=
STEPSIZE
,
tabsize
=
TABSIZE
):
def
delete_string
(
source
,
stepsize
=
STEPSIZE
,
tabsize
=
TABSIZE
,
expandtabs
=
EXPANDTABS
):
input
=
StringReader
(
source
)
output
=
StringWriter
()
pi
=
PythonIndenter
(
input
,
output
,
stepsize
,
tabsize
)
pi
.
elimina
te
()
pi
=
PythonIndenter
(
input
,
output
,
stepsize
,
tabsize
,
expandtabs
)
pi
.
dele
te
()
return
output
.
getvalue
()
# end def
elimina
te_string
# end def
dele
te_string
def
reformat_string
(
source
,
stepsize
=
STEPSIZE
,
tabsize
=
TABSIZE
):
def
reformat_string
(
source
,
stepsize
=
STEPSIZE
,
tabsize
=
TABSIZE
,
expandtabs
=
EXPANDTABS
):
input
=
StringReader
(
source
)
output
=
StringWriter
()
pi
=
PythonIndenter
(
input
,
output
,
stepsize
,
tabsize
)
pi
=
PythonIndenter
(
input
,
output
,
stepsize
,
tabsize
,
expandtabs
)
pi
.
reformat
()
return
output
.
getvalue
()
# end def reformat_string
def
complete_file
(
filename
,
stepsize
=
STEPSIZE
,
tabsize
=
TABSIZE
):
def
complete_file
(
filename
,
stepsize
=
STEPSIZE
,
tabsize
=
TABSIZE
,
expandtabs
=
EXPANDTABS
):
source
=
open
(
filename
,
'r'
).
read
()
result
=
complete_string
(
source
,
stepsize
,
tabsize
)
result
=
complete_string
(
source
,
stepsize
,
tabsize
,
expandtabs
)
if
source
==
result
:
return
0
# end if
import
os
...
...
@@ -425,9 +436,9 @@ def complete_file(filename, stepsize = STEPSIZE, tabsize = TABSIZE):
return
1
# end def complete_file
def
eliminate_file
(
filename
,
stepsize
=
STEPSIZE
,
tabsize
=
TABSIZE
):
def
delete_file
(
filename
,
stepsize
=
STEPSIZE
,
tabsize
=
TABSIZE
,
expandtabs
=
EXPANDTABS
):
source
=
open
(
filename
,
'r'
).
read
()
result
=
eliminate_string
(
source
,
stepsize
,
tabsize
)
result
=
delete_string
(
source
,
stepsize
,
tabsize
,
expandtabs
)
if
source
==
result
:
return
0
# end if
import
os
...
...
@@ -438,11 +449,11 @@ def eliminate_file(filename, stepsize = STEPSIZE, tabsize = TABSIZE):
f
.
write
(
result
)
f
.
close
()
return
1
# end def
elimina
te_file
# end def
dele
te_file
def
reformat_file
(
filename
,
stepsize
=
STEPSIZE
,
tabsize
=
TABSIZE
):
def
reformat_file
(
filename
,
stepsize
=
STEPSIZE
,
tabsize
=
TABSIZE
,
expandtabs
=
EXPANDTABS
):
source
=
open
(
filename
,
'r'
).
read
()
result
=
reformat_string
(
source
,
stepsize
,
tabsize
)
result
=
reformat_string
(
source
,
stepsize
,
tabsize
,
expandtabs
)
if
source
==
result
:
return
0
# end if
import
os
...
...
@@ -458,21 +469,28 @@ def reformat_file(filename, stepsize = STEPSIZE, tabsize = TABSIZE):
# Test program when called as a script
usage
=
"""
usage: pindent (-c|-
e|-r) [-s stepsize] [-t tabsiz
e] [file] ...
usage: pindent (-c|-
d|-r) [-s stepsize] [-t tabsize] [-
e] [file] ...
-c : complete a correctly indented program (add #end directives)
-
e : elimina
te #end directives
-
d : dele
te #end directives
-r : reformat a completed program (use #end directives)
-s stepsize: indentation step (default %(STEPSIZE)d)
-t tabsize : the worth in spaces of a tab (default %(TABSIZE)d)
-e : expand TABs into spaces (defailt OFF)
[file] ... : files are changed in place, with backups in file~
If no files are specified or a single - is given,
the program acts as a filter (reads stdin, writes stdout).
"""
%
vars
()
def
error_both
(
op1
,
op2
):
sys
.
stderr
.
write
(
'Error: You can not specify both '
+
op1
+
' and -'
+
op2
[
0
]
+
' at the same time
\
n
'
)
sys
.
stderr
.
write
(
usage
)
sys
.
exit
(
2
)
# end def error_both
def
test
():
import
getopt
try
:
opts
,
args
=
getopt
.
getopt
(
sys
.
argv
[
1
:],
'c
ers:t:
'
)
opts
,
args
=
getopt
.
getopt
(
sys
.
argv
[
1
:],
'c
drs:t:e
'
)
except
getopt
.
error
,
msg
:
sys
.
stderr
.
write
(
'Error: %s
\
n
'
%
msg
)
sys
.
stderr
.
write
(
usage
)
...
...
@@ -481,32 +499,41 @@ def test():
action
=
None
stepsize
=
STEPSIZE
tabsize
=
TABSIZE
expandtabs
=
EXPANDTABS
for
o
,
a
in
opts
:
if
o
==
'-c'
:
if
action
:
error_both
(
o
,
action
)
# end if
action
=
'complete'
elif
o
==
'-e'
:
action
=
'eliminate'
elif
o
==
'-d'
:
if
action
:
error_both
(
o
,
action
)
# end if
action
=
'delete'
elif
o
==
'-r'
:
if
action
:
error_both
(
o
,
action
)
# end if
action
=
'reformat'
elif
o
==
'-s'
:
stepsize
=
string
.
atoi
(
a
)
elif
o
==
'-t'
:
tabsize
=
string
.
atoi
(
a
)
elif
o
==
'-e'
:
expandtabs
=
1
# end if
# end for
if
not
action
:
sys
.
stderr
.
write
(
'You must specify -c(omplete), -
e(elimina
te) or -r(eformat)
\
n
'
)
'You must specify -c(omplete), -
d(ele
te) or -r(eformat)
\
n
'
)
sys
.
stderr
.
write
(
usage
)
sys
.
exit
(
2
)
# end if
if
not
args
or
args
==
[
'-'
]:
action
=
eval
(
action
+
'_filter'
)
action
(
sys
.
stdin
,
sys
.
stdout
,
stepsize
,
tabsize
)
action
(
sys
.
stdin
,
sys
.
stdout
,
stepsize
,
tabsize
,
expandtabs
)
else
:
action
=
eval
(
action
+
'_file'
)
for
file
in
args
:
action
(
file
,
stepsize
,
tabsize
)
action
(
file
,
stepsize
,
tabsize
,
expandtabs
)
# end for
# end if
# end def test
...
...
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