4 _moduleLogger = logging.getLogger("merge_backend")
7 class MergedAddressBook(object):
9 Merger of all addressbooks
12 def __init__(self, addressbookFactories, sorter = None):
13 self.__addressbookFactories = addressbookFactories
14 self.__addressbooks = None
15 self.__sort_contacts = sorter if sorter is not None else self.null_sorter
17 def clear_caches(self):
18 self.__addressbooks = None
19 for factory in self.__addressbookFactories:
20 factory.clear_caches()
22 def get_addressbooks(self):
24 @returns Iterable of (Address Book Factory, Book Id, Book Name)
28 def open_addressbook(self, bookId):
31 def contact_source_short_name(self, contactId):
32 if self.__addressbooks is None:
34 bookIndex, originalId = contactId.split("-", 1)
35 return self.__addressbooks[int(bookIndex)].contact_source_short_name(originalId)
41 def get_contacts(self):
43 @returns Iterable of (contact id, contact name)
45 if self.__addressbooks is None:
46 self.__addressbooks = list(
47 factory.open_addressbook(id)
48 for factory in self.__addressbookFactories
49 for (f, id, name) in factory.get_addressbooks()
52 ("-".join([str(bookIndex), contactId]), contactName)
53 for (bookIndex, addressbook) in enumerate(self.__addressbooks)
54 for (contactId, contactName) in addressbook.get_contacts()
56 sortedContacts = self.__sort_contacts(contacts)
59 def get_contact_details(self, contactId):
61 @returns Iterable of (Phone Type, Phone Number)
63 if self.__addressbooks is None:
65 bookIndex, originalId = contactId.split("-", 1)
66 return self.__addressbooks[int(bookIndex)].get_contact_details(originalId)
69 def null_sorter(contacts):
71 Good for speed/low memory
76 def basic_firtname_sorter(contacts):
78 Expects names in "First Last" format
81 (contactName.rsplit(" ", 1)[0], (contactId, contactName))
82 for (contactId, contactName) in contacts
84 contactsWithKey.sort()
85 return (contactData for (lastName, contactData) in contactsWithKey)
88 def basic_lastname_sorter(contacts):
90 Expects names in "First Last" format
93 (contactName.rsplit(" ", 1)[-1], (contactId, contactName))
94 for (contactId, contactName) in contacts
96 contactsWithKey.sort()
97 return (contactData for (lastName, contactData) in contactsWithKey)
100 def reversed_firtname_sorter(contacts):
102 Expects names in "Last, First" format
105 (contactName.split(", ", 1)[-1], (contactId, contactName))
106 for (contactId, contactName) in contacts
108 contactsWithKey.sort()
109 return (contactData for (lastName, contactData) in contactsWithKey)
112 def reversed_lastname_sorter(contacts):
114 Expects names in "Last, First" format
117 (contactName.split(", ", 1)[0], (contactId, contactName))
118 for (contactId, contactName) in contacts
120 contactsWithKey.sort()
121 return (contactData for (lastName, contactData) in contactsWithKey)
124 def guess_firstname(name):
126 return name.split(", ", 1)[-1]
128 return name.rsplit(" ", 1)[0]
131 def guess_lastname(name):
133 return name.split(", ", 1)[0]
135 return name.rsplit(" ", 1)[-1]
138 def advanced_firstname_sorter(cls, contacts):
140 (cls.guess_firstname(contactName), (contactId, contactName))
141 for (contactId, contactName) in contacts
143 contactsWithKey.sort()
144 return (contactData for (lastName, contactData) in contactsWithKey)
147 def advanced_lastname_sorter(cls, contacts):
149 (cls.guess_lastname(contactName), (contactId, contactName))
150 for (contactId, contactName) in contacts
152 contactsWithKey.sort()
153 return (contactData for (lastName, contactData) in contactsWithKey)