-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpycleaner.py
executable file
·90 lines (67 loc) · 2.2 KB
/
pycleaner.py
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env python -B
# vi: set syntax=python ts=4 sw=4 sts=4 et ff=unix ai si :
#
# (c) Steven Scholnick <[email protected]>
# The pycleaner source code is published under a MIT license.
"""
pycleaner: clean python source files
Usage:
pycleaner [--verbose] <files> ...
Files can be a directory. If it is a directory, the directory and all of its sub-directories are search for .py files.
The following cleaning is performed:
* Ending whitespace is removed
* Tabs are converted to 4 spaces
* Spaces before a colon are removed
* Trailing blank lines are removed
Options:
-h, --help Show this help screen
-v, --verbose Verbose Mode
--version Prints the version
"""
import os, sys, re
def main(files):
"""main method"""
for path in files:
path = os.path.abspath(path)
if os.path.isdir(path):
processDirectory(path)
else:
processFile(path)
sys.exit(0)
def processDirectory(startingDirectory):
"""Cleans all .py files in the startingDirectory and any .py files in any child directories"""
for root, dirs, files in os.walk(startingDirectory):
for f in files:
if f.endswith(".py"):
processFile(os.path.join(root,f))
def processFile(filePath):
"""cleans a single .py file"""
if arguments['--verbose']:
print("Cleaning {0}".format(filePath))
lines = []
with open(filePath,'r') as inStream:
for line in inStream:
line = line.rstrip()
line = line.expandtabs(4)
line = re.sub(r'\s+:',':',line)
lines.append(line)
count = countTrailingBlankLines(lines)
if count > 0:
lines = lines[:len(lines) - count]
with open(filePath,'w') as outStream:
for line in lines:
print(line,file=outStream)
def countTrailingBlankLines(lines):
"""Returns the number of trailing blank lines"""
count = 0
for line in reversed(lines):
line = line.rstrip()
if line:
break
else:
count = count + 1
return count
if __name__ == '__main__':
from docopt import docopt
arguments = docopt(__doc__, version='1.0.1')
main(arguments['<files>'])