Reading addressbook
Ian Bicking
ianb at colorstudy.com
Wed Feb 5 15:22:38 EST 2003
More information about the Python-list mailing list
Wed Feb 5 15:22:38 EST 2003
- Previous message (by thread): Reading addressbook
- Next message (by thread): wxPython and Video/Multimedia
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On Mon, 2003-02-03 at 23:47, Wouter van Marle wrote: > Hi! > > Any tips/documents/etc on how to read: > - Gnome addressbook (GnomeCard) > - Evolution addressbook. > > Reading is enough... can't find documentation on that. I really hope there > are some python libs available! It's just a quick hack, but I wanted command-line access to the Evolution address book. Attached is a script that does that. You'd want some sort of vCard parser to do this properly. -- Ian Bicking ianb at colorstudy.com http://colorstudy.com 4869 N. Talman Ave., Chicago, IL 60625 / 773-275-7241 "There is no flag large enough to cover the shame of killing innocent people" -- Howard Zinn -------------- next part -------------- #!/usr/bin/python import anydbm, os, re addressBookLocation = os.path.join(os.environ['HOME'], 'evolution/local/Contacts/') class AddressBook: def __init__(self, book=None): location = addressBookLocation for name in book or []: location = os.path.join(location, 'subfolders', name) location = os.path.join(location, 'addressbook.db') self.contacts = [] file = anydbm.open(location) for key in file.keys(): data = file[key] if not data.startswith('BEGIN:VCARD'): continue self.contacts.append(Contact(data)) def find(self, text): results = [] for contact in self.contacts: if contact.find(text): results.append(contact) return results class Contact: _splitRE = re.compile(r'\r?\n') _ignoreFields = ['BEGIN', 'VERSION', 'END', 'UID', 'X-'] def __init__(self, data): lines = self._splitRE.split(data) self.data = {} for line in lines: if not line.strip() or line == '\x00': continue if startswithany(line, self._ignoreFields): continue if line.find(':') == -1: continue label, value = line.split(':', 1) labels = tuple(label.split(';')) self.data[labels] = value def __str__(self): s = [] for labels, value in self.data.items(): s.append('%s: %s' % ('/'.join(labels), value)) return '\n'.join(s) def find(self, text): for value in self.data.values(): if value.lower().find(text.lower()) != -1: return True return False def startswithany(s, patList): for pat in patList: if s.startswith(pat): return True return False if __name__ == '__main__': import sys for c in AddressBook().find(sys.argv[1]): print c
- Previous message (by thread): Reading addressbook
- Next message (by thread): wxPython and Video/Multimedia
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list