Wednesday, July 9, 2008

Small file to auto-create php comments spaceholder

I'm so lazy. Wrote a script to help me comment my commentless php code.


#!/usr/bin/env python

import sys
import re

def main():
try:
print "Trying to open %s" % (sys.argv[1])
fin=open(sys.argv[1],'r')
fout=open(sys.argv[1]+'.out','w')
except:
print 'Unable to open file'
quit()
varcond=re.compile('(\$(?!this)\w+)')
classvarcond=re.compile('var\s')
funccond=re.compile('function\s(\w+)')
for line in fin:
if classvarcond.search(line):
variables=varcond.findall(line)
outtext = "/**\n * Define %s\n *\n */\n" % (variables[0])
fout.write(outtext)
if funccond.search(line):
funcname=funccond.findall(line)
variables=varcond.findall(line)
print funcname," function is found"
outtext = "/**\n * Describe %s\n *\n" % (funcname[0])
for variable in variables:
outtext += " * @param %s\n" % (variable)
outtext += " * @return null\n */\n"

fout.write(outtext)
fout.write(line)

if __name__=="__main__":
if len(sys.argv)>1:
main()
else:
print 'Please provide filename'



Just put it here to share in case anyone find it useful. And also as a backup.. :P

0 comments: