MCPcopy Create free account
hub / github.com/ActiveState/code / tabify

Function tabify

recipes/Python/496893_Tabify/recipe-496893.py:13–39  ·  view source on GitHub ↗
(filename)

Source from the content-addressed store, hash-verified

11__VERSION__ = "0.5.1"
12
13def tabify(filename):
14 mode = os.stat(filename)[ST_MODE]
15 os.rename(filename, filename+".bak")
16
17 infile = file(filename+".bak")
18 outfile = file(filename,"w")
19 tokens = tokenize.generate_tokens(infile.readline)
20
21 text = []
22 indent = 0
23 minlineno = 0
24 for (toktype, token, start, end, line) in tokens:
25 y, x = end
26
27 if toktype == tokenize.INDENT:
28 indent += 1
29 elif toktype == tokenize.DEDENT:
30 indent -= 1
31 elif y > minlineno:
32 minlineno = y
33 text += "%s%s\n" % ("\t"*indent,line.strip())
34
35 outfile.write("".join(text))
36
37 infile.close()
38 outfile.close()
39 os.chmod(filename, mode)
40
41def main():
42 if len(sys.argv) < 2:

Callers 1

mainFunction · 0.85

Calls 5

renameMethod · 0.45
stripMethod · 0.45
writeMethod · 0.45
joinMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected