#!/usr/bin/python import cgi, os, os.path, time, re lockFileName = "files/todolock" fileName = "files/todos.xml" while(os.path.exists(lockFileName)): time.sleep(.25) lockFileHandle = open(lockFileName, "w") lockFileHandle.write("locked") lockFileHandle.close() form = cgi.FieldStorage() catToRemove = form.getfirst("category") oldFile = open(fileName, 'r') oldFileLines = oldFile.readlines() oldFile.close() catStartRe = re.compile(r'^' % catToRemove) catEndRe = re.compile(r'^') lineIndex = 0 inCategory = False haveFoundCategory = False for line in oldFileLines: if (inCategory): if (catEndRe.match(line)): inCategory = False haveFoundCategory = True catEndLine = lineIndex elif (catStartRe.match(line)): inCategory = True catStartLine = lineIndex lineIndex = lineIndex + 1 if (haveFoundCategory): # To be safe, only delete empty categories. if (catEndLine == catStartLine + 1): del oldFileLines[catStartLine:catEndLine+1] newFile = open(fileName, 'w') for line in oldFileLines: newFile.write(line) newFile.close() os.remove(lockFileName) print "Content-type: text/plain\n\n" print "done."