- Notifications
You must be signed in to change notification settings - Fork 31.7k
/
Copy pathuntabify.py
executable file
·55 lines (47 loc) · 1.3 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
#! /usr/bin/env python3
"Replace tabs with spaces in argument files. Print names of changed files."
importos
importsys
importgetopt
importtokenize
defmain():
tabsize=8
try:
opts, args=getopt.getopt(sys.argv[1:], "t:")
ifnotargs:
raisegetopt.error("At least one file argument required")
exceptgetopt.errorasmsg:
print(msg)
print("usage:", sys.argv[0], "[-t tabwidth] file ...")
return
foroptname, optvalueinopts:
ifoptname=='-t':
tabsize=int(optvalue)
returnmax(process(filename, tabsize) forfilenameinargs)
defprocess(filename, tabsize, verbose=True):
try:
withtokenize.open(filename) asf:
text=f.read()
encoding=f.encoding
exceptIOErrorasmsg:
print("%r: I/O error: %s"% (filename, msg))
return2
newtext=text.expandtabs(tabsize)
ifnewtext==text:
return0
backup=filename+"~"
try:
os.unlink(backup)
exceptOSError:
pass
try:
os.rename(filename, backup)
exceptOSError:
pass
withopen(filename, "w", encoding=encoding) asf:
f.write(newtext)
ifverbose:
print(filename)
return1
if__name__=='__main__':
raiseSystemExit(main())