Package native :: Package qtopiadesktop :: Module qtopiadesktop
[hide private]
[frames] | no frames]

Source Code for Module native.qtopiadesktop.qtopiadesktop

  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 
 18   
19 -class XMLParser:
20
21 - def __init__(self, filename):
22 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
29 - def _handleCharData(self, data): pass
30 - def _handleEndElement(self, name): pass
31
32 - def _handleStartElement(self, name, attrs):
33 raise NotImplementedException()
34
35 -class CategoriesParser(XMLParser):
36
37 - def __init__(self, filename="Categories.xml"):
38 filename=getqdpath(filename) 39 self._data={} 40 XMLParser.__init__(self, filename)
41
42 - def _handleStartElement(self, name, attrs):
43 if name!="Category": 44 return 45 self._data[int(ununicodify(attrs["id"]))]=ununicodify(attrs["name"])
46
47 - def categories(self):
48 return self._data
49 50
51 -class ABParser(XMLParser):
52 - def __init__(self, filename="addressbook/addressbook.xml", categories={}):
53 filename=getqdpath(filename) 54 self.categories=categories 55 self._data=[] 56 XMLParser.__init__(self, filename)
57
58 - def _handleStartElement(self, name, attrs):
59 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)
85
86 - def contacts(self):
87 return self._data
88
89 -def getqdpath(filename):
90 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
97 -def getfilename():
98 "Returns the filename we need" 99 return getqdpath("addressbook/addressbook.xml")
100 101 # XML returns all the values as strings, so these two routines 102 # help strip out the unicode bit
103 -def cleandict(d):
104 # remove all unicode from the dict 105 newd={} 106 for k,v in d.items(): 107 newd[ununicodify(k)]=ununicodify(v) 108 return newd
109
110 -def ununicodify(value):
111 try: 112 return str(value) 113 except UnicodeEncodeError: 114 return value.encode("ascii", 'xmlcharrefreplace')
115
116 -def getcontacts():
117 cats=CategoriesParser() 118 ab=ABParser(categories=cats.categories()) 119 return ab.contacts()
120 121 if __name__=="__main__": 122 123 print getcontacts() 124