Skip to content

Instantly share code, notes, and snippets.

@Andersama
Last active September 17, 2018 12:35
Show Gist options
  • Select an option

  • Save Andersama/2f1f813d83a02f1c4a18430fd6064827 to your computer and use it in GitHub Desktop.

Select an option

Save Andersama/2f1f813d83a02f1c4a18430fd6064827 to your computer and use it in GitHub Desktop.
#!/user/bin/python
import re, sys, getopt, os
def main(argv):
inputfile = ''
outputfile= ''
try:
opts, args = getopt.getopt(argv, "hi:o",["ifile=","ofile="])
except getopt.GetoptError:
print("style.py -i <inputfile> -o <outputfile>")
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print("style.py -i <inputfile> -o <outputfile>'")
sys.exit()
elif opt in ("-i", "--ifile"):
inputfile = arg
elif opt in ("-o", "--ofile"):
outputfile = arg
print 'Input File: ', inputfile
print 'Output File: ', outputfile
if(inputfile == ""):
print("style.py -i <inputfile> -o <outputfile>'")
sys.exit()
ifile = open(inputfile, 'r')
tablength = 8
columnlimit = 120
text = ifile.read()
ifile.close()
#Gets bin packed lines that have all their parameters pushed to a second line
regex = re.compile("^(\t*)(.*\()(?:\n)(\t*)(.*(?:[,][ ])*)([)];)$", re.MULTILINE)
matches = regex.finditer(text)
for match in matches:
print '%02d-%02d:\n%s' % (match.start(), match.end(), match.group(0))
spacetaken = (match.group(1).count('\t') * tablength) + match.group(2).__len__()
spaceremaining = columnlimit - spacetaken
#Creates a regex that'll find the right , to split on
replaceregex = '(.{0,%d}[,])[ ](.*)' % (spaceremaining-1)
match2 = re.match(replaceregex, match.group(4), re.MULTILINE)
outstr = '%s%s%s\n%s%s%s' % (match.group(1), match.group(2), match2.group(1), match.group(3), match2.group(2), match.group(5))
print '->:\n%s' % (outstr)
text = text.replace(match.group(0), outstr)
#Gets maligned *'s
text = re.sub(r'([^\/])(\s*)(\*)(\s{2,})', '\\1\\2\\4\\3', text)
print text
if(outputfile == ""):
outputfile = inputfile
ofile = open(outputfile, 'w')
ofile.write(text)
ofile.close()
if __name__ == "__main__":
main(sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment