[Pythonmac-SIG] Please critique my first appscript - a fix for Palm sync of iCal Tasks (for kGTD)

Marcin Komorowski marcink at ieee.org
Tue Aug 29 06:49:44 CEST 2006


I am in the process of learning Python and appscript, and I wrote my  
first script using these two technologies.

Please kindly critique my code - what could I have done better?  What  
would have made this code more Pythonic?  Any suggestions and  
comments are greatly appreciated.

Thank You in advance,
Marcin
---------------------------------------

#!/usr/bin/env /usr/bin/pythonw

from appscript import *
import sys
import cPickle
import pprint
import datetime


def myDebug( txt ):
     if txt == None:
         sys.stderr.flush()
     else:
         sys.stderr.write( txt )

def getTaskList():
     appCal = app( 'iCal' )
     cal_todos = appCal.calendars.filter(its.name !=  
'').todos.properties()
     tasks = [ todo  for cals in cal_todos  for todo in cals]

     # Iterate flattened list, builing a local dictionary of  
dictionaries, by uid
     tasks_data = {}
     for task in tasks:
         if task[ k.class__ ] == k.todo:
             uid = task[ k.uid ]
             tasks_data[uid] = task;
             myDebug('.')
             myDebug(None)

     myDebug('\n')

     return tasks_data

def exportTaskListToFile( t_data, filename ):
     cPickle.dump( t_data, open( filename, 'w' ) )

def importTaskListFromFile( filename ):
     return cPickle.load( open( filename, 'r' ) )

k_decode = {
         k.priority      : "k.priority",
         k.due_date      : "k.due_date",
         k.completion_date : "k.completion_date",
         k.description   : "k.description",
         k.url           : "k.url",
         k.uid           : "k.uid",
         k.summary       : "k.summary",
         k.class__       : "k.class__"
     }

def diffTaskLists( task_list1, task_list2 ):
     diff_list = []
     for uid in [ id  for id in task_list1.keys()  if  
task_list2.has_key( id ) ]:
         t1 = task_list1[uid]
         t2 = task_list2[uid]
         found_difference = False
         for field in [ k.priority, k.due_date, k.completion_date,  
k.description, k.url, k.summary ]:
             if t1[field] != t2[field]:
                 if found_difference == False:
                     myDebug( "  task ID %s:\n" % str( uid ) )
                 myDebug( "    field %s: " % k_decode[field] );
                 try:
                     myDebug( "t1 = '%s' t2 = '%s'" % ( t1[field], t2 
[field] ) )
                 except:
                     pass
                 myDebug( "\n" )
                 found_difference = True
                 break

         if found_difference and t1[k.stamp_date] == t2[k.stamp_date]:
             diff_list.append( uid )

     return diff_list

def updateTimeStamp( task_uid_list, timestamp ):
     myDebug( "Setting new timestamp for task UIDs %s\n" % repr 
( task_uid_list ) )

     for task_uid in task_uid_list:
         task_list = [ t for t in app('iCal').calendars.todos.filter 
( its.uid == task_uid ).get() if t != k.MissingValue ]
         if len( task_list ) > 1:
             sys.stderr.write( "ERROR: found two tasks with UID %s,  
updating timestamp for both" % task_uid )

         for t in task_list:
             t.stamp_date.set( timestamp )

def usage():
     print "USAGE: " + sys.argv[0] + " save|check"

def main(mode):
     if mode != 'save' and mode != 'check':
         usage()
         return

     myDebug( "Getting list of tasks from iCal...\n" )
     tasks = getTaskList()

     pp = pprint.PrettyPrinter(indent=4)
     myDebug( "%d tasks\n" % len( tasks ) )

     if mode == 'save':
         myDebug( "Saving current iCal tasks...\n" )
         exportTaskListToFile( tasks, 'presync_task_save.tmp' )
         sys.stdout.write( "%d tasks saved.\n" % len( tasks ) )

     else:
         myDebug( "Getting saved iCal tasks...\n" )
         ref_tasks = importTaskListFromFile( 'presync_task_save.tmp' )
         myDebug( "Comparing iCal tasks...\n" );
         diff_list = diffTaskLists( ref_tasks, tasks )
         pp.pprint( diff_list )

         myDebug( "Updating timestamps...\n" );
         new_timestamp = datetime.datetime.today()
         updateTimeStamp( diff_list, new_timestamp )

         myDebug( "Done.\n" )
         sys.stdout.write( "%d timestamps updated.\n" % len 
( diff_list ) )

if __name__ == '__main__':
     if len( sys.argv ) < 2:
         usage()
     else:
         main( sys.argv[1] )

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/pythonmac-sig/attachments/20060829/66968ec5/attachment.htm 


More information about the Pythonmac-SIG mailing list