0001 ### BITPIM 0002 ### 0003 ### Copyright (C) 2004 Roger Binns <rogerb@rogerbinns.com> 0004 ### 0005 ### This program is free software; you can redistribute it and/or modify 0006 ### it under the terms of the BitPim license as detailed in the LICENSE file. 0007 ### 0008 ### $Id: qtopiadesktop.py 1559 2004-09-04 07:05:08Z rogerb $ 0009 0010 """Be at one with Qtopia desktop (eg as used by the Zaurus)""" 0011 0012 # See recipe 12.6 in the Python Cookbook for the XML parsing bit 0013 0014 import xml.parsers.expat 0015 import os 0016 0017 import encodings.ascii 0018 0019 class XMLParser: 0020 0021 def __init__(self, filename): 0022 parser=xml.parsers.expat.ParserCreate() 0023 parser.CharacterDataHandler=self._handleCharData 0024 parser.StartElementHandler=self._handleStartElement 0025 parser.EndElementHandler=self._handleEndElement 0026 0027 parser.ParseFile(open(filename, "rt")) 0028 0029 def _handleCharData(self, data): pass 0030 def _handleEndElement(self, name): pass 0031 0032 def _handleStartElement(self, name, attrs): 0033 raise NotImplementedException() 0034 0035 class CategoriesParser(XMLParser): 0036 0037 def __init__(self, filename="Categories.xml"): 0038 filename=getqdpath(filename) 0039 self._data={} 0040 XMLParser.__init__(self, filename) 0041 0042 def _handleStartElement(self, name, attrs): 0043 if name!="Category": 0044 return 0045 self._data[int(ununicodify(attrs["id"]))]=ununicodify(attrs["name"]) 0046 0047 def categories(self): 0048 return self._data 0049 0050 0051 class ABParser(XMLParser): 0052 def __init__(self, filename="addressbook/addressbook.xml", categories={}): 0053 filename=getqdpath(filename) 0054 self.categories=categories 0055 self._data=[] 0056 XMLParser.__init__(self, filename) 0057 0058 def _handleStartElement(self, name, attrs): 0059 if name!="Contact": 0060 return 0061 d=cleandict(attrs) 0062 # fixup fields 0063 # d["Uid"]=int(d["Uid"]) - leaving as string for moment since semantics of value are not clear (eg does two's complement of number mean the same thing?) 0064 # categories 0065 c=d.get("Categories", "") 0066 cats=[] 0067 if len(c): 0068 c=[int(x) for x in c.split(";")] 0069 for cat in c: 0070 if self.categories.has_key(cat): 0071 cats.append(self.categories[cat]) 0072 else: 0073 if __debug__: 0074 print "couldn't find category",cat,"in",`self.categories` 0075 if len(cats): 0076 d["Categories"]=cats 0077 else: 0078 if d.has_key("Categories"): 0079 del d["Categories"] 0080 # emails 0081 if d.has_key("Emails"): 0082 d["Emails"]=d["Emails"].split() 0083 # ::TODO:: gender also needs to be munged 0084 self._data.append(d) 0085 0086 def contacts(self): 0087 return self._data 0088 0089 def getqdpath(filename): 0090 filename=os.path.expanduser(os.path.join("~/.palmtopcenter", filename)) 0091 if filename.startswith("~"): 0092 # windows 98, no home directory present 0093 filename="c:\\"+filename[1:] 0094 return os.path.abspath(filename) 0095 0096 0097 def getfilename(): 0098 "Returns the filename we need" 0099 return getqdpath("addressbook/addressbook.xml") 0100 0101 # XML returns all the values as strings, so these two routines 0102 # help strip out the unicode bit 0103 def cleandict(d): 0104 # remove all unicode from the dict 0105 newd={} 0106 for k,v in d.items(): 0107 newd[ununicodify(k)]=ununicodify(v) 0108 return newd 0109 0110 def ununicodify(value): 0111 try: 0112 return str(value) 0113 except UnicodeEncodeError: 0114 return value.encode("ascii", 'xmlcharrefreplace') 0115 0116 def getcontacts(): 0117 cats=CategoriesParser() 0118 ab=ABParser(categories=cats.categories()) 0119 return ab.contacts() 0120 0121 if __name__=="__main__": 0122 0123 print getcontacts() 0124
Generated by PyXR 0.9.4