3 # DialCentral - Front end for Google's Grand Central service.
4 # Copyright (C) 2008 Eric Warnke ericew AT gmail DOT com
6 # This library is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU Lesser General Public
8 # License as published by the Free Software Foundation; either
9 # version 2.1 of the License, or (at your option) any later version.
11 # This library is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # Lesser General Public License for more details.
16 # You should have received a copy of the GNU Lesser General Public
17 # License along with this library; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 Filesystem backend for contact support
30 class CsvAddressBook(object):
32 Currently supported file format
33 @li Has the first line as a header
34 @li Escapes with quotes
35 @li Comma as delimiter
36 @li Column 0 is name, column 1 is number
39 def __init__(self, csvPath):
40 self.__csvPath = csvPath
41 self.__contacts = list(
42 self.read_csv(csvPath)
46 def read_csv(csvPath):
47 csvReader = iter(csv.reader(open(csvPath, "rU")))
49 for i, row in enumerate(csvReader):
50 yield str(i), row[0], row[1]
52 def clear_caches(self):
60 def contact_source_short_name(contactId):
63 def get_contacts(self):
65 @returns Iterable of (contact id, contact name)
67 for contact in self.__contacts:
70 def get_contact_details(self, contactId):
72 @returns Iterable of (Phone Type, Phone Number)
74 contactId = int(contactId)
75 yield "", self.__contacts[contactId][2]
78 class FilesystemAddressBookFactory(object):
81 "csv": CsvAddressBook,
84 def __init__(self, path):
87 def clear_caches(self):
90 def get_addressbooks(self):
92 @returns Iterable of (Address Book Factory, Book Id, Book Name)
94 for root, dirs, files in os.walk(self.__path):
96 name, ext = file.rsplit(".", 1)
97 if ext in self.FILETYPE_SUPPORT:
98 yield self, os.path.join(root, file), name
100 def open_addressbook(self, bookId):
101 name, ext = bookId.rsplit(".", 1)
102 assert ext in self.FILETYPE_SUPPORT
103 return self.FILETYPE_SUPPORT[ext](bookId)
112 Included here for debugging.
114 Either insert it into the code or launch python with the "-i" flag
116 eab = FilesystemAddressBookFactory(os.path.expanduser("~/Desktop"))
117 for book in eab.get_addressbooks():
118 eab = eab.open_addressbook(book[1])
120 for contact in eab.get_contacts():
122 for details in eab.get_contact_details(contact[0]):
123 print "\t\t", details