7 SPACESONLY
= 'SPACESONLY'
11 def __init__(self
, fnm
, optdict
):
12 self
.optdict
= optdict
14 self
.txt
= open(self
.fnm
, 'r').readlines()
15 self
.indents
= [(0, 0, )]
19 if self
.lnndx
< len(self
.txt
):
20 txt
= self
.txt
[self
.lnndx
]
21 self
.lnndx
= self
.lnndx
+ 1
25 def tokeneater(self
, type, token
, start
, end
, line
):
26 if type == tokenize
.INDENT
:
27 (lvl
, s
) = self
.indents
[-1]
28 self
.indents
[-1] = (lvl
, s
, start
[0]-1)
29 self
.indents
.append((lvl
+1, start
[0]-1,))
30 elif type == tokenize
.DEDENT
:
31 (lvl
, s
) = self
.indents
[-1]
32 self
.indents
[-1] = (lvl
, s
, start
[0]-1)
33 self
.indents
.append((lvl
-1, start
[0]-1,))
34 elif type == tokenize
.ENDMARKER
:
35 (lvl
, s
) = self
.indents
[-1]
36 self
.indents
[-1] = (lvl
, s
, len(self
.txt
))
38 content
= string
.lstrip(ln
)
41 lead
= ln
[:len(ln
) - len(content
)]
42 lead
= string
.expandtabs(lead
)
43 return (lead
, content
)
46 style
= self
.optdict
.get('style', TABSONLY
)
47 indent
= string
.atoi(self
.optdict
.get('indent', '4'))
48 tabsz
= string
.atoi(self
.optdict
.get('tabs', '8'))
49 print 'file %s -> style %s, tabsize %d, indent %d' % (self
.fnm
, style
, tabsz
, indent
)
50 tokenize
.tokenize(self
.getline
, self
.tokeneater
)
52 #pprint.pprint(self.indents)
54 for (lvl
, s
, e
) in self
.indents
:
55 if s
>= len(self
.txt
):
59 oldlead
, content
= self
.split(self
.txt
[s
])
60 #print "oldlead", len(oldlead), `oldlead`
63 elif style
== SPACESONLY
:
64 newlead
= ' '*(indent
*lvl
)
67 t
,spcs
= divmod(sz
, tabsz
)
68 newlead
= '\t'*t
+ ' '*spcs
69 new
.append(newlead
+ content
)
70 for ln
in self
.txt
[s
+1:e
]:
71 lead
, content
= self
.split(ln
)
72 #print "lead:", len(lead)
73 new
.append(newlead
+ lead
[len(oldlead
):] + content
)
75 #print "---", self.fnm
81 bakname
= os
.path
.splitext(self
.fnm
)[0]+'.bak'
82 print "backing up", self
.fnm
, "to", bakname
85 os
.rename(self
.fnm
, bakname
)
88 os
.rename(self
.fnm
, bakname
)
89 open(self
.fnm
, 'w').writelines(txt
)
92 tc
= PyText('test1.py')
94 tc
= PyText('test1.py')
95 tc
.process(style
=TABSONLY
)
96 tc
= PyText('test1.py')
97 tc
.process(style
=MIXED
, indent
=4, tabs
=8)
98 tc
= PyText('test1.py')
99 tc
.process(style
=MIXED
, indent
=2, tabs
=8)
101 def cleanfile(fnm
, d
):
102 if os
.path
.isdir(fnm
) and not os
.path
.islink(fnm
):
103 names
= os
.listdir(fnm
)
105 fullnm
= os
.path
.join(fnm
, name
)
106 if (os
.path
.isdir(fullnm
) and not os
.path
.islink(fullnm
)) or \
107 os
.path
.normcase(fullnm
[-3:]) == ".py":
114 %s [options] [path...]
116 -T : reformat to TABS ONLY
117 -S : reformat to SPACES ONLY ( -i option is important)
118 -M : reformat to MIXED SPACES / TABS ( -t and -i options important)
119 -t<n> : tab is worth <n> characters
120 -i<n> : indents should be <n> characters
122 path is file or directory
124 if __name__
== '__main__':
125 import sys
, getopt
, os
126 opts
, args
= getopt
.getopt(sys
.argv
[1:], "TSMht:i:")
131 d
['style'] = TABSONLY
133 d
['style'] = SPACESONLY
141 print usage
% sys
.argv
[0]
144 print usage
% sys
.argv
[0]