forked from PyQwt/PyQwt5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
untabify.py
executable file
·70 lines (55 loc) · 1.52 KB
/
untabify.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
#!/usr/bin/env python
import optparse
import os
import sys
def parse_invokation():
"""Return the parsed options and arguments from the command line
"""
usage = (
'%prog [-h] [-t N] directory (one or more file name extensions)'
)
parser = optparse.OptionParser(usage=usage)
parser.add_option(
'-t', '--tabs', default=8, action='store',
type='int', metavar='N',
help='Have tabs N characters apart [default 8]'
)
options, arguments = parser.parse_args()
if len(arguments) < 2:
parser.print_help()
sys.exit(1)
return options, arguments
# parse_invokation()
def process(filename, tabsize):
"""Stolen from Python-X.Y.Z/Tools/scripts/untabify
"""
try:
f = open(filename)
text = f.read()
f.close()
except IOError, msg:
print "%r: I/O error: %s" % (filename, msg)
return
newtext = text.expandtabs(tabsize)
if newtext == text:
return
f = open(filename, "w")
f.write(newtext)
f.close()
print filename
# process()
def main():
options, arguments = parse_invokation()
top = os.path.abspath(arguments[0])
extensions = arguments[1:]
for root, directories, files in os.walk(top):
for file in files:
_, extension = os.path.splitext(file)
if extension in extensions:
process(os.path.join(root, file), options.tabs)
# main
if __name__ == "__main__":
main()
# Local Variables: ***
# mode: python ***
# End: ***