4 DialCentral - Front end for Google's Grand Central service.
5 Copyright (C) 2008 Eric Warnke ericew AT gmail DOT com
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 Filesystem backend for contact support
29 class CsvAddressBook(object):
31 Currently supported file format
32 @li Has the first line as a header
33 @li Escapes with quotes
34 @li Comma as delimiter
35 @li Column 0 is name, column 1 is number
38 def __init__(self, name, csvPath):
40 self._csvPath = csvPath
47 def update_contacts(self, force = True):
48 if not force or not self._contacts:
50 self._contacts = dict(
51 self._read_csv(self._csvPath)
54 def get_contacts(self):
56 @returns Iterable of (contact id, contact name)
58 if not self._contacts:
59 self._contacts = dict(
60 self._read_csv(self._csvPath)
64 def _read_csv(self, csvPath):
66 csvReader = iter(csv.reader(open(csvPath, "rU")))
72 header = csvReader.next()
73 nameColumn, phoneColumns = self._guess_columns(header)
78 for (phoneType, phoneColumn) in phoneColumns:
80 if len(row[phoneColumn]) == 0:
82 contactDetails.append({
83 "phoneType": phoneType,
84 "phoneNumber": row[phoneColumn],
88 if 0 < len(contactDetails):
89 yield str(yieldCount), {
90 "contactId": "%s-%d" % (self._name, yieldCount),
91 "name": row[nameColumn],
92 "numbers": contactDetails,
97 def _guess_columns(cls, row):
100 for i, item in enumerate(row):
101 if 0 <= item.lower().find("name"):
102 names.append((item, i))
103 elif 0 <= item.lower().find("phone"):
104 phones.append((item, i))
105 elif 0 <= item.lower().find("mobile"):
106 phones.append((item, i))
108 names.append(("Name", 0))
110 phones.append(("Phone", 1))
112 return names[0][1], phones
115 class FilesystemAddressBookFactory(object):
118 "csv": CsvAddressBook,
121 def __init__(self, path):
124 def get_addressbooks(self):
125 for root, dirs, filenames in os.walk(self._path):
126 for filename in filenames:
128 name, ext = filename.rsplit(".", 1)
133 cls = self.FILETYPE_SUPPORT[ext]
136 yield cls(name, os.path.join(root, filename))