From c56dadd27b0672e8929b39d9a7f5de7f9f76c40d Mon Sep 17 00:00:00 2001 From: Ed Page Date: Tue, 29 Sep 2009 21:57:39 -0500 Subject: [PATCH] Adding a contacts cache manager with callbacks --- src/gvoice/addressbook.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/gvoice/addressbook.py diff --git a/src/gvoice/addressbook.py b/src/gvoice/addressbook.py new file mode 100644 index 0000000..e796583 --- /dev/null +++ b/src/gvoice/addressbook.py @@ -0,0 +1,38 @@ +#!/usr/bin/python + + +import logging + + +_moduleLogger = logging.getLogger("gvoice.addressbook") + + +class Addressbook(object): + + def __init__(self, backend): + self._backend = backend + self._contacts = {} + + def clear_cache(self): + self._contacts.clear() + + def get_contacts(self): + self._populate_contacts() + return self._contacts.iterkeys() + + def get_contact_details(self, contactId): + self._populate_contacts() + self._populate_contact_details(contactId) + return self._contacts[contactId] + + def _populate_contacts(self): + if self._contacts: + return + contacts = self._backend.get_contacts() + for contactId, contactName in contacts: + self._contacts[contactId] = None + + def _populate_contact_details(self, contactId): + if self._contacts[contactId] is not None: + return + self._contacts[contactId] = self._backend.get_contact_details(contactId) -- 1.7.9.5