conduit r1273 - in trunk: . conduit/datatypes conduit/modules/GmailModule test/python-tests



Author: jstowers
Date: Mon Feb  4 20:37:49 2008
New Revision: 1273
URL: http://svn.gnome.org/viewvc/conduit?rev=1273&view=rev

Log:
2008-02-05  John Stowers  <john stowers gmail com>

	* conduit/datatypes/Contact.py:
	* conduit/modules/GmailModule/GmailModule.py:
	* test/python-tests/TestCoreContact.py: Add a function to parse a vcf file
	into multiple Contact objects and use this to get the Gmail contacts 
	datasource to work and not be useless.



Added:
   trunk/test/python-tests/TestCoreContact.py
Modified:
   trunk/ChangeLog
   trunk/conduit/datatypes/Contact.py
   trunk/conduit/modules/GmailModule/GmailModule.py

Modified: trunk/conduit/datatypes/Contact.py
==============================================================================
--- trunk/conduit/datatypes/Contact.py	(original)
+++ trunk/conduit/datatypes/Contact.py	Mon Feb  4 20:37:49 2008
@@ -1,20 +1,48 @@
 import vobject 
 import conduit.datatypes.DataType as DataType
 
+def parse_vcf(string):
+    """
+    Parses a vcf string, potentially containing many vcards
+    @returns: A list of Contacts
+    """
+    contacts = []
+    for vobj in vobject.readComponents(string):
+        if vobj.behavior == vobject.vcard.VCard3_0:
+            contacts.append(Contact(vcard=vobj))
+    return contacts
+    
 class Contact(DataType.DataType):
     """
     Very basic contact representation
+    @keyword vcard: A vobject.vcard.VCard3_0 instance
     """
     _name_ = "contact"
     def __init__(self, **kwargs):
         DataType.DataType.__init__(self)
-        self.vCard = vobject.vCard()
+        self.vcard = kwargs.get('vcard',vobject.vCard())
 
     def set_from_vcard_string(self, string):
-        self.vCard = vobject.readOne(string)
+        self.vcard = vobject.readOne(string)
 
     def get_vcard_string(self, version=2.1):
-        return self.vCard.serialize()
+        return self.vcard.serialize()
+        
+    def get_emails(self):
+        emails = []
+        for email in self.vcard.contents['email']:
+            emails.append(email.value)
+        return emails
+        
+    def get_name(self):
+        #In order of preference, 1)formatted name, 2)name, 3)""
+        #FIXME:Think about this more
+        for attr in [self.vcard.fn, self.vcard.n]:
+            #because str() on a vobject.vcard.Name pads with whitespace
+            name = str(attr.value).strip()
+            if len(name) > 0:
+                return name
+        return None
 
     def __getstate__(self):
         data = DataType.DataType.__getstate__(self)
@@ -26,7 +54,7 @@
         DataType.DataType.__setstate__(self, data)
 
     def __str__(self):
-        return self.get_vcard_string()
+        return "Name: %s" % self.get_name()
         
     def get_hash(self):
         return hash(self.get_vcard_string())

Modified: trunk/conduit/modules/GmailModule/GmailModule.py
==============================================================================
--- trunk/conduit/modules/GmailModule/GmailModule.py	(original)
+++ trunk/conduit/modules/GmailModule/GmailModule.py	Mon Feb  4 20:37:49 2008
@@ -1,6 +1,5 @@
 from gettext import gettext as _
 import traceback
-import vobject
 import logging
 log = logging.getLogger("modules.Gmail")
 
@@ -326,21 +325,14 @@
         if self.loggedIn:
             log.debug("Getting all contacts as vcards")
             pageData = self.ga._retrievePage(GmailContactSource.VCARD_EXPORT_URI)
-            print pageData
-            #for txt in vobject.readComponents(pageData):
-            #    contact = Contact.Contact()
-            #    contact.set_from_vcard_string(txt)
-            #    #FIXME: Get the email
-            #    self.contacts[i] = contact
-            
+            for c in Contact.parse_vcf(pageData):
+                self.contacts[c.get_emails()[0]] = c
             #FIXME: Libgmail is not really reliable....
             #result = self.ga.getContacts().getAllContacts()
             #for c in result:
             #   contact = Contact.Contact()
             #   contact.set_from_vcard_string(c.getVCard())
-            #FIXME: Get the email
-            #   self.contacts[c.email] = contact
-            
+            #   self.contacts[c.get_emails()[0]] = contact
         else:
             raise Exceptions.SyncronizeFatalError
 
@@ -351,8 +343,8 @@
     def get(self, LUID):
         DataProvider.DataSource.get(self, LUID)
         c = self.contacts[LUID]
-        c.set_UID(c)
-        return self.contacts[LUID]
+        c.set_UID(LUID)
+        return c
 
     def finish(self, aborted, error, conflict):
         DataProvider.DataSource.finish(self)

Added: trunk/test/python-tests/TestCoreContact.py
==============================================================================
--- (empty file)
+++ trunk/test/python-tests/TestCoreContact.py	Mon Feb  4 20:37:49 2008
@@ -0,0 +1,33 @@
+#common sets up the conduit environment
+from common import *
+import conduit.datatypes.Contact as Contact
+
+vcfData="""
+BEGIN:VCARD
+VERSION:3.0
+FN:
+N:;;;;
+EMAIL;TYPE=INTERNET:yws-flickr-unsubscribe yahoogroups com
+END:VCARD
+BEGIN:VCARD
+VERSION:3.0
+FN:
+N:;;;;
+EMAIL;TYPE=INTERNET:yws-flickr yahoogroups com
+END:VCARD
+BEGIN:VCARD
+VERSION:3.0
+FN:STA Travel Canterbury Uni
+N:Uni;STA;Travel Canterbury;;
+EMAIL;TYPE=INTERNET:cantiuni branch statravel co nz
+END:VCARD"""
+
+contacts = Contact.parse_vcf(vcfData)
+ok("Parsed vcf file (got %s vcards)" % len(contacts), len(contacts) == vcfData.count("BEGIN:VCARD"))
+
+c = contacts[-1]
+ok("Got vcard data", len(c.get_vcard_string()) > 0)
+ok("Got email addresses", len(c.get_emails()) > 0)
+ok("Got name", c.get_name() != None)
+
+finished()



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]