Adding a contacts cache manager with callbacks
[theonering] / src / gvoice / addressbook.py
1 #!/usr/bin/python
2
3
4 import logging
5
6
7 _moduleLogger = logging.getLogger("gvoice.addressbook")
8
9
10 class Addressbook(object):
11
12         def __init__(self, backend):
13                 self._backend = backend
14                 self._contacts = {}
15
16         def clear_cache(self):
17                 self._contacts.clear()
18
19         def get_contacts(self):
20                 self._populate_contacts()
21                 return self._contacts.iterkeys()
22
23         def get_contact_details(self, contactId):
24                 self._populate_contacts()
25                 self._populate_contact_details(contactId)
26                 return self._contacts[contactId]
27
28         def _populate_contacts(self):
29                 if self._contacts:
30                         return
31                 contacts = self._backend.get_contacts()
32                 for contactId, contactName in contacts:
33                         self._contacts[contactId] = None
34
35         def _populate_contact_details(self, contactId):
36                 if self._contacts[contactId] is not None:
37                         return
38                 self._contacts[contactId] = self._backend.get_contact_details(contactId)