Trees | Indices | Help |
|
---|
|
1 ### BITPIM 2 ### 3 ### Copyright (C) 2004 Roger Binns <rogerb@rogerbinns.com> 4 ### 5 ### This program is free software; you can redistribute it and/or modify 6 ### it under the terms of the BitPim license as detailed in the LICENSE file. 7 ### 8 ### $Id: qtopiadesktop.py 1559 2004-09-04 07:05:08Z rogerb $ 9 10 """Be at one with Qtopia desktop (eg as used by the Zaurus)""" 11 12 # See recipe 12.6 in the Python Cookbook for the XML parsing bit 13 14 import xml.parsers.expat 15 import os 16 17 import encodings.ascii 18203422 parser=xml.parsers.expat.ParserCreate() 23 parser.CharacterDataHandler=self._handleCharData 24 parser.StartElementHandler=self._handleStartElement 25 parser.EndElementHandler=self._handleEndElement 26 27 parser.ParseFile(open(filename, "rt"))28 3136 4149 5043 if name!="Category": 44 return 45 self._data[int(ununicodify(attrs["id"]))]=ununicodify(attrs["name"])468853 filename=getqdpath(filename) 54 self.categories=categories 55 self._data=[] 56 XMLParser.__init__(self, filename)5759 if name!="Contact": 60 return 61 d=cleandict(attrs) 62 # fixup fields 63 # 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?) 64 # categories 65 c=d.get("Categories", "") 66 cats=[] 67 if len(c): 68 c=[int(x) for x in c.split(";")] 69 for cat in c: 70 if self.categories.has_key(cat): 71 cats.append(self.categories[cat]) 72 else: 73 if __debug__: 74 print "couldn't find category",cat,"in",`self.categories` 75 if len(cats): 76 d["Categories"]=cats 77 else: 78 if d.has_key("Categories"): 79 del d["Categories"] 80 # emails 81 if d.has_key("Emails"): 82 d["Emails"]=d["Emails"].split() 83 # ::TODO:: gender also needs to be munged 84 self._data.append(d)8590 filename=os.path.expanduser(os.path.join("~/.palmtopcenter", filename)) 91 if filename.startswith("~"): 92 # windows 98, no home directory present 93 filename="c:\\"+filename[1:] 94 return os.path.abspath(filename)95 96 100 101 # XML returns all the values as strings, so these two routines 102 # help strip out the unicode bit104 # remove all unicode from the dict 105 newd={} 106 for k,v in d.items(): 107 newd[ununicodify(k)]=ununicodify(v) 108 return newd109111 try: 112 return str(value) 113 except UnicodeEncodeError: 114 return value.encode("ascii", 'xmlcharrefreplace')115 120 121 if __name__=="__main__": 122 123 print getcontacts() 124
Trees | Indices | Help |
|
---|
Generated by Epydoc 3.0.1 on Sun Jan 24 16:21:00 2010 | http://epydoc.sourceforge.net |