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
24 from __future__ import with_statement
32 return s.decode("UTF-8")
33 except UnicodeDecodeError:
37 class CsvAddressBook(object):
39 Currently supported file format
40 @li Has the first line as a header
41 @li Escapes with quotes
42 @li Comma as delimiter
43 @li Column 0 is name, column 1 is number
46 def __init__(self, name, csvPath):
48 self._csvPath = csvPath
55 def update_account(self, force = True):
56 if not force or not self._contacts:
58 self._contacts = dict(
59 self._read_csv(self._csvPath)
62 def get_contacts(self):
64 @returns Iterable of (contact id, contact name)
66 if not self._contacts:
67 self._contacts = dict(
68 self._read_csv(self._csvPath)
72 def _read_csv(self, csvPath):
74 f = open(csvPath, "rU")
75 csvReader = iter(csv.reader(f))
81 header = csvReader.next()
82 nameColumns, nameFallbacks, phoneColumns = self._guess_columns(header)
87 for (phoneType, phoneColumn) in phoneColumns:
89 if len(row[phoneColumn]) == 0:
91 contactDetails.append({
92 "phoneType": try_unicode(phoneType),
93 "phoneNumber": row[phoneColumn],
97 if 0 < len(contactDetails):
98 nameParts = (row[i].strip() for i in nameColumns)
99 nameParts = (part for part in nameParts if part)
100 fullName = " ".join(nameParts).strip()
102 for fallbackColumn in nameFallbacks:
103 if row[fallbackColumn].strip():
104 fullName = row[fallbackColumn].strip()
108 fullName = try_unicode(fullName)
109 yield str(yieldCount), {
110 "contactId": "%s-%d" % (self._name, yieldCount),
112 "numbers": contactDetails,
117 def _guess_columns(cls, row):
118 firstMiddleLast = [-1, -1, -1]
122 for i, item in enumerate(row):
123 lowerItem = item.lower()
124 if 0 <= lowerItem.find("name"):
125 names.append((item, i))
127 if 0 <= lowerItem.find("couple"):
128 names.insert(0, (item, i))
130 if 0 <= lowerItem.find("first") or 0 <= lowerItem.find("given"):
131 firstMiddleLast[0] = i
132 elif 0 <= lowerItem.find("middle"):
133 firstMiddleLast[1] = i
134 elif 0 <= lowerItem.find("last") or 0 <= lowerItem.find("family"):
135 firstMiddleLast[2] = i
136 elif 0 <= lowerItem.find("phone"):
137 phones.append((item, i))
138 elif 0 <= lowerItem.find("mobile"):
139 phones.append((item, i))
140 elif 0 <= lowerItem.find("email") or 0 <= lowerItem.find("e-mail"):
141 nameFallbacks.append(i)
143 names.append(("Name", 0))
145 phones.append(("Phone", 1))
147 nameColumns = [i for i in firstMiddleLast if 0 <= i]
148 if len(nameColumns) < 2:
150 nameColumns.append(names[0][1])
152 return nameColumns, nameFallbacks, phones
155 class FilesystemAddressBookFactory(object):
158 "csv": CsvAddressBook,
161 def __init__(self, path):
164 def get_addressbooks(self):
165 for root, dirs, filenames in os.walk(self._path):
166 for filename in filenames:
168 name, ext = filename.rsplit(".", 1)
173 cls = self.FILETYPE_SUPPORT[ext]
176 yield cls(name, os.path.join(root, filename))