#!/usr/bin/python import cgi, os, os.path, time, re, xml.sax.saxutils 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() #name = form['title'].value #nextAction = form['nextAction'].value idToChange = int(form.getfirst("todoID")) category = form.getfirst("category") oldFile = open(fileName, 'r') oldFileLines = oldFile.readlines() oldFile.close() usedIDs = [] todoIDre = re.compile(r'^' % category) categoryEndre = re.compile(r'^') lineToMove = None lineIndex = 0 for line in oldFileLines: todoIDmatch = todoIDre.match(line) if (todoIDmatch): lineToMove = oldFileLines[lineIndex] del oldFileLines[lineIndex] break lineIndex = lineIndex + 1 if (lineToMove): # Find the category, if it exists. lineNumber = 0 foundCategory = False for line in oldFileLines: if (foundCategory and categoryEndre.match(line)): oldFileLines.insert(lineNumber, lineToMove) break if (categoryStartre.match(line)): foundCategory = True lineNumber = lineNumber + 1 if (not foundCategory): # Need to add the new category. oldFileLines.insert(len(oldFileLines)-1, '\n' % category) oldFileLines.insert(len(oldFileLines)-1, lineToMove) oldFileLines.insert(len(oldFileLines)-1, '\n') 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."