whitespace2tab
Verfasst: Dienstag 5. September 2006, 18:30
Ein Script um fuer die indention verwendete whitespaces in tabs umzuwandeln
Was sagt ihr dazu?
Verbesserungsvorschlaege? Kritik?
Geht aber nur bei "class" "if" "else" "while" oder "def"s im code, sollten aber in jedem sein
Code: Alles auswählen
#! /usr/bin/env python
# Licensed under the CreativeCommons Attribution-NonCommercial-ShareAlike (http://creativecommons.org/licenses/by-nc-sa/2.5/) by Florian Mayer(aka name)
# THIS COMES WITH ABSOLUTELY NO WARRANTY
import sys
def Replace(file,tabsize):
source = open ( file )
source = source.readlines()
i = 0
while(i < len(source)):
tabs = getamount(source[i]) / tabsize
whitespaces = getamount(source[i])
if(source[i][0:tabs*tabsize] == " "*tabs*tabsize and not tabs == 0 and not whitespaces ==0 ):
source[i] = "\t"*tabs + source[i][tabs*tabsize:]
i+=1
towrite = "".join(source)
sourcefile = open ( file, 'w' )
sourcefile.write(towrite)
sourcefile.close()
return True
def Parsefile(file):
source = open ( file )
source = source.readlines()
i = 0
while(i < len(source)):
if("if" in source[i] or "while" in source[i] or "else" in source[i] or "class" in source[i] or "def" in source[i]):
tabsize=getamount(source[i+1])-getamount(source[i])
return tabsize
i+=1
def getamount(string):
i = 0
count = 0
while(i < len(string)):
if(string[i] == " "):
count+=1
else:
return count
i+=1
Replace(sys.argv[1],Parsefile(sys.argv[1]))
Verbesserungsvorschlaege? Kritik?
Geht aber nur bei "class" "if" "else" "while" oder "def"s im code, sollten aber in jedem sein
