Purge Completed Todo Utility
In Simple XML Processing With elementtree, Uche Ogbuji claimed that “elementtree is fast, pythonic and very simple to use”. I decided to put it to the test by writing something I’ve been meaning to do for a while, writing a script to purge completed entries from the Zaurus ToDo application. I wasn’t let down.
Maybe I’m blind but I just couldn’t find a way of bulk removing entries using the limited GUI interface of the application, and I was building up hundreds of completed entries which must slow down processing over time. Luckily help was at hand because Sharp, in their wisdom, decided to store its data in XML documents.
Here is an example of todolist.xml
<!DOCTYPE Tasks> <Tasks> <RIDMax> 223 </RIDMax> <Task Completed="1" HasDate="0" Priority="3" Categories="-1042017918" Description="make a cup of tea" Uid="-1045778896" rid="221" rinfo="0"/> <Task Completed="0" HasDate="0" Priority="2" Categories="-1044048088" Description="write photolog plugin" Uid="-1045038462" rid="153" rinfo="0"/> </Tasks>
The Task element has the attibute of Completed; 1 for completed, 0 for uncompleted. It should be a simple task to remove all the completed Tasks.
import sys
from elementtree.ElementTree import ElementTree, Element
tasks = ElementTree(file="/home/root/Applications/todolist/todolist.xml")
tree = tasks.getroot()
iter = tasks.getiterator()
for task in iter:
if task.get("Completed") == "1":
tree.remove(task)
f = open("/home/root/Applications/todolist/todolist.xml",'w')
tasks.write(f)
f.close()
There was just a couple of things to work out. One, you couldn’t remove from the iterator, so you have to use the getroot() function as well as the getiterator() function. Also, the examples looked like you could do
tasks.write("/home/root/Applications/todolist/todolist.xml")
but I couldn’t get this to work.
Note: you have to make sure that the todo applications isn’t “fastloaded” on the system, otherwise it will write over your changes with its in-memory copy.
Finally, for this to work on the Zaurus, you’ll need more than the standard Python IPK. You need python-xml, python-compress and python-codecs (all available from Riverbank). (You might also want python-devel to install modules but this isn’t completely necessary)
Recent Comments