Checking GMail contacts for iPhone sync issues

Google sync allows you to push sync your Google contacts to your iPhone. There are some quite arbitrary restrictions on the type and quantity of phone numbers that can be synced, as described here. Running the following piece of python next to an exported google.csv will check for entries that will loose phone numbers: ``

#Check a google.csv contact dump for iphone sync issues.
import csv
import collections 

#This is the problem: iPhone (when synced via exchange?) only allows
#a limited number of phone numbers -- which must be of specific types.
# See www.google.com/support/mobile/bin/answer.py?answer=139635&topic=14252
PhoneLimits={
 'Home':2,
 'Home Fax':1, 
 "Mobile":1, 
 "Pager":1, 
 "Work":3, 
 "Work Fax":1
 }

def limitViolations(limits,types):
 for (type,num) in types.iteritems():
  if num>limits.get(type,0):
   return '%d > %d entries of type %s'%(num,limits.get(type,0),type)
 return ''

reader = csv.DictReader(open('google.csv','rb'),quoting=csv.QUOTE_MINIMAL)
nPhone=-1
for row in reader:
 # First pass only: see how many phone columns are active
 if nPhone<0:
  nPhone=1
  while row.has_key('Phone %d - Type'%nPhone):
   nPhone+=1
  nPhone-=1
 # Use phonetypes to tally the types for the active row
 phonetypes=collections.defaultdict(int)
 for kPhone in range(1,nPhone):
  #if a phone value is given for this index, tally the type
  if len(row['Phone %d - Value'%kPhone])>0:
   phonetypes[row['Phone %d - Type'%kPhone]]+=1
 v=limitViolations(PhoneLimits,dict(phonetypes))
 if v:
  print('%25s: %s'%(row['Name'],v))