Fixing the phone type selector
[gc-dialer] / src / file_backend.py
index 2aa77a2..ddd89b3 100644 (file)
@@ -1,29 +1,29 @@
 #!/usr/bin/python
 
-# DialCentral - Front end for Google's Grand Central service.
-# Copyright (C) 2008  Eric Warnke ericew AT gmail DOT com
-# 
-# This library is free software; you can redistribute it and/or
-# modify it under the terms of the GNU Lesser General Public
-# License as published by the Free Software Foundation; either
-# version 2.1 of the License, or (at your option) any later version.
-# 
-# This library is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-# Lesser General Public License for more details.
-# 
-# You should have received a copy of the GNU Lesser General Public
-# License along with this library; if not, write to the Free Software
-# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+"""
+DialCentral - Front end for Google's Grand Central service.
+Copyright (C) 2008  Eric Warnke ericew AT gmail DOT com
 
+This library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Lesser General Public
+License as published by the Free Software Foundation; either
+version 2.1 of the License, or (at your option) any later version.
+
+This library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public
+License along with this library; if not, write to the Free Software
+Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
-"""
 Filesystem backend for contact support
 """
 
 
 import os
+import re
 import csv
 
 
@@ -36,18 +36,54 @@ class CsvAddressBook(object):
        @li Column 0 is name, column 1 is number
        """
 
+       _nameRe = re.compile("name", re.IGNORECASE)
+       _phoneRe = re.compile("phone", re.IGNORECASE)
+       _mobileRe = re.compile("mobile", re.IGNORECASE)
+
        def __init__(self, csvPath):
                self.__csvPath = csvPath
                self.__contacts = list(
                        self.read_csv(csvPath)
                )
 
-       @staticmethod
-       def read_csv(csvPath):
+       @classmethod
+       def read_csv(cls, csvPath):
                csvReader = iter(csv.reader(open(csvPath, "rU")))
-               csvReader.next()
-               for i, row in enumerate(csvReader):
-                       yield str(i), row[0], row[1]
+
+               header = csvReader.next()
+               nameColumn, phoneColumns = cls._guess_columns(header)
+
+               yieldCount = 0
+               for row in csvReader:
+                       contactDetails = []
+                       for (phoneType, phoneColumn) in phoneColumns:
+                               try:
+                                       if len(row[phoneColumn]) == 0:
+                                               continue
+                                       contactDetails.append((phoneType, row[phoneColumn]))
+                               except IndexError:
+                                       pass
+                       if len(contactDetails) != 0:
+                               yield str(yieldCount), row[nameColumn], contactDetails
+                               yieldCount += 1
+
+       @classmethod
+       def _guess_columns(cls, row):
+               names = []
+               phones = []
+               for i, item in enumerate(row):
+                       if cls._nameRe.search(item) is not None:
+                               names.append((item, i))
+                       elif cls._phoneRe.search(item) is not None:
+                               phones.append((item, i))
+                       elif cls._mobileRe.search(item) is not None:
+                               phones.append((item, i))
+               if len(names) == 0:
+                       names.append(("Name", 0))
+               if len(phones) == 0:
+                       phones.append(("Phone", 1))
+
+               return names[0][1], phones
 
        def clear_caches(self):
                pass
@@ -72,7 +108,7 @@ class CsvAddressBook(object):
                @returns Iterable of (Phone Type, Phone Number)
                """
                contactId = int(contactId)
-               yield "", self.__contacts[contactId][2]
+               return iter(self.__contacts[contactId][2])
 
 
 class FilesystemAddressBookFactory(object):
@@ -91,11 +127,11 @@ class FilesystemAddressBookFactory(object):
                """
                @returns Iterable of (Address Book Factory, Book Id, Book Name)
                """
-               for root, dirs, files in os.walk(self.__path):
-                       for file in files:
-                               name, ext = file.rsplit(".", 1)
+               for root, dirs, filenames in os.walk(self.__path):
+                       for filename in filenames:
+                               name, ext = filename.rsplit(".", 1)
                                if ext in self.FILETYPE_SUPPORT:
-                                       yield self, os.path.join(root, file), name
+                                       yield self, os.path.join(root, filename), name
 
        def open_addressbook(self, bookId):
                name, ext = bookId.rsplit(".", 1)
@@ -107,17 +143,20 @@ class FilesystemAddressBookFactory(object):
                return "File"
 
 
-def print_books():
+def print_filebooks(contactPath = None):
        """
        Included here for debugging.
 
        Either insert it into the code or launch python with the "-i" flag
        """
-       eab = FilesystemAddressBookFactory(os.path.expanduser("~/Desktop"))
-       for book in eab.get_addressbooks():
-               eab = eab.open_addressbook(book[1])
+       if contactPath is None:
+               contactPath = os.path.join(os.path.expanduser("~"), ".dialcentral", "contacts")
+
+       abf = FilesystemAddressBookFactory(contactPath)
+       for book in abf.get_addressbooks():
+               ab = abf.open_addressbook(book[1])
                print book
-               for contact in eab.get_contacts():
+               for contact in ab.get_contacts():
                        print "\t", contact
-                       for details in eab.get_contact_details(contact[0]):
+                       for details in ab.get_contact_details(contact[0]):
                                print "\t\t", details