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_account(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 nameColumns, nameFallbacks, 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 nameParts = (row[i].strip() for i in nameColumns)
90 nameParts = (part for part in nameParts if part)
91 fullName = " ".join(nameParts).strip()
93 for fallbackColumn in nameFallbacks:
94 if row[fallbackColumn].strip():
95 fullName = row[fallbackColumn].strip()
99 yield str(yieldCount), {
100 "contactId": "%s-%d" % (self._name, yieldCount),
102 "numbers": contactDetails,
107 def _guess_columns(cls, row):
108 firstMiddleLast = [-1, -1, -1]
112 for i, item in enumerate(row):
113 lowerItem = item.lower()
114 if 0 <= lowerItem.find("name"):
115 names.append((item, i))
117 if 0 <= lowerItem.find("couple"):
118 names.insert(0, (item, i))
120 if 0 <= lowerItem.find("first") or 0 <= lowerItem.find("given"):
121 firstMiddleLast[0] = i
122 elif 0 <= lowerItem.find("middle"):
123 firstMiddleLast[1] = i
124 elif 0 <= lowerItem.find("last") or 0 <= lowerItem.find("family"):
125 firstMiddleLast[2] = i
126 elif 0 <= lowerItem.find("phone"):
127 phones.append((item, i))
128 elif 0 <= lowerItem.find("mobile"):
129 phones.append((item, i))
130 elif 0 <= lowerItem.find("email") or 0 <= lowerItem.find("e-mail"):
131 nameFallbacks.append(i)
133 names.append(("Name", 0))
135 phones.append(("Phone", 1))
137 nameColumns = [i for i in firstMiddleLast if 0 <= i]
138 if len(nameColumns) < 2:
140 nameColumns.append(names[0][1])
142 return nameColumns, nameFallbacks, phones
145 class FilesystemAddressBookFactory(object):
148 "csv": CsvAddressBook,
151 def __init__(self, path):
154 def get_addressbooks(self):
155 for root, dirs, filenames in os.walk(self._path):
156 for filename in filenames:
158 name, ext = filename.rsplit(".", 1)
163 cls = self.FILETYPE_SUPPORT[ext]
166 yield cls(name, os.path.join(root, filename))