-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathstrip_python.py
More file actions
55 lines (46 loc) · 1.57 KB
/
Copy pathstrip_python.py
File metadata and controls
55 lines (46 loc) · 1.57 KB
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
49
50
51
52
53
54
55
#!/usr/bin/env python3
"""
Strip comments and docstrings from a file.
Copied from https://gist.github.com/BroHui/aca2b8e6e6bdf3cb4af4b246c9837fa3 with modifications.
"""
import sys, token, tokenize
def process_file(infile, outfile):
"""
Remove comments and docstrings from one file and write the result to another.
@param infile The path to the file to process
@param outfile The path to the file we're generating
"""
source = open(infile, "r")
mod = open(outfile, "w")
prev_toktype = token.INDENT
last_lineno = -1
last_col = 0
tokgen = tokenize.generate_tokens(source.readline)
for toktype, ttext, (slineno, scol), (elineno, ecol), ltext in tokgen:
if 0: # Change to if 1 to see the tokens fly by.
print(
"%10s %-14s %-20r %r"
% (
tokenize.tok_name.get(toktype, toktype),
"%d.%d-%d.%d" % (slineno, scol, elineno, ecol),
ttext,
ltext,
)
)
if slineno > last_lineno:
last_col = 0
if scol > last_col:
mod.write(" " * (scol - last_col))
if toktype == token.STRING and prev_toktype == token.INDENT:
# Docstring
mod.write("#--")
elif toktype == tokenize.COMMENT:
# Comment
mod.write("##")
else:
mod.write(ttext)
prev_toktype = toktype
last_col = ecol
last_lineno = elineno
if __name__ == "__main__":
process_file(sys.argv[1], sys.argv[2])