2aa77a2b8eae93b01d775d406859eafb176d86a6
[gc-dialer] / src / file_backend.py
1 #!/usr/bin/python
2
3 # DialCentral - Front end for Google's Grand Central service.
4 # Copyright (C) 2008  Eric Warnke ericew AT gmail DOT com
5
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.
10
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.
15
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
19
20
21 """
22 Filesystem backend for contact support
23 """
24
25
26 import os
27 import csv
28
29
30 class CsvAddressBook(object):
31         """
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
37         """
38
39         def __init__(self, csvPath):
40                 self.__csvPath = csvPath
41                 self.__contacts = list(
42                         self.read_csv(csvPath)
43                 )
44
45         @staticmethod
46         def read_csv(csvPath):
47                 csvReader = iter(csv.reader(open(csvPath, "rU")))
48                 csvReader.next()
49                 for i, row in enumerate(csvReader):
50                         yield str(i), row[0], row[1]
51
52         def clear_caches(self):
53                 pass
54
55         @staticmethod
56         def factory_name():
57                 return "csv"
58
59         @staticmethod
60         def contact_source_short_name(contactId):
61                 return "csv"
62
63         def get_contacts(self):
64                 """
65                 @returns Iterable of (contact id, contact name)
66                 """
67                 for contact in self.__contacts:
68                         yield contact[0:2]
69
70         def get_contact_details(self, contactId):
71                 """
72                 @returns Iterable of (Phone Type, Phone Number)
73                 """
74                 contactId = int(contactId)
75                 yield "", self.__contacts[contactId][2]
76
77
78 class FilesystemAddressBookFactory(object):
79
80         FILETYPE_SUPPORT = {
81                 "csv": CsvAddressBook,
82         }
83
84         def __init__(self, path):
85                 self.__path = path
86
87         def clear_caches(self):
88                 pass
89
90         def get_addressbooks(self):
91                 """
92                 @returns Iterable of (Address Book Factory, Book Id, Book Name)
93                 """
94                 for root, dirs, files in os.walk(self.__path):
95                         for file in files:
96                                 name, ext = file.rsplit(".", 1)
97                                 if ext in self.FILETYPE_SUPPORT:
98                                         yield self, os.path.join(root, file), name
99
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)
104
105         @staticmethod
106         def factory_name():
107                 return "File"
108
109
110 def print_books():
111         """
112         Included here for debugging.
113
114         Either insert it into the code or launch python with the "-i" flag
115         """
116         eab = FilesystemAddressBookFactory(os.path.expanduser("~/Desktop"))
117         for book in eab.get_addressbooks():
118                 eab = eab.open_addressbook(book[1])
119                 print book
120                 for contact in eab.get_contacts():
121                         print "\t", contact
122                         for details in eab.get_contact_details(contact[0]):
123                                 print "\t\t", details