Dependency inject service ID, so that it can be stamped on friends and
[hermes] / package / src / org / maemo / hermes / engine / hermes.py
index 732b04d..0d9034b 100644 (file)
@@ -66,28 +66,51 @@ class Hermes:
     # -----------------------------------------------------------------------
     def run_alt(self, overwrite_existing_fields=False):
         contacts = []
-        book = evolution.ebook.open_addressbook('default')
-        for contact in book.get_all_contacts():
-            contacts.append(Contact(book, contact))
+        self.addresses = evolution.ebook.open_addressbook('default')
+        for contact in self.addresses.get_all_contacts():
+            contacts.append(Contact(self.addresses, contact))
 
         # warm up
         for service in self._services:
+            print "pre-process:", service.get_id()
             for contact in contacts:
                 service.pre_process_contact(contact)
                 
         # fetch data
         for service in self._services:
+            print "process_friends:", service.get_id()
             service.process_friends()
         
         # combine results into one friend
         for contact in contacts:
             result = Friend()
             for service in self._services:
+                print "process_contact:", service.get_id()
                 friend = service.process_contact(contact)
-                if (friend):
+                if friend:
+                    contact.add_mapping(service.get_id())
                     friend.decorate(result)
             
-            self.update_contact(friend, overwrite_existing_fields)
+            if result.get_name() is not None:
+                self.update_contact(result, overwrite_existing_fields)
+            else:
+                self.unmatched.add(contact)
+            
+        # give services a chance to create new contacts
+        for service in self._services:
+            print "create_contacts:", service.get_id()
+            for friend in service.create_contacts():
+                self.create_contact_from_friend(friend)
+                
+        # finalisation
+        for service in self._services:
+            print "finalize:", service.get_id()
+            service.finalise(self.updated, overwrite_existing_fields)
+            
+        # commit changes
+        for contact in self.updated:
+            print "committing changes to:", contact.get_name(), contact
+            self.addresses.commit_contact(contact.get_econtact())
         
 
     # -----------------------------------------------------------------------
@@ -95,22 +118,21 @@ class Hermes:
         """Update the given contact with information from the given friend."""
         
         print "updating contact ", contact, " with friend ", friend
+        self.updated.append(contact)
+        self.matched.append(contact)
+        if friend.get_source() is not None:
+            contact.add_mapping(friend.get_source())
 
+
+    # -----------------------------------------------------------------------
     def create_contact_from_friend(self, friend):
-        contact = evolution.ebook.EContact()
-        contact.props.full_name = friend['name']
-        contact.props.given_name = friend['first_name']
-        contact.props.family_name = friend['last_name']
-        
-        self.update_contact(contact, friend)
-        
+        econtact = evolution.ebook.EContact()
+        econtact.props.full_name = friend['name']
+        econtact.props.given_name = friend['first_name']
+        econtact.props.family_name = friend['last_name']
+        contact = Contact(self.addresses, econtact)
+                
         self.addresses.add_contact(contact)
-        self.updated.append(contact)
-        self.addresses.commit_contact(contact)
+        self.update_contact(contact, friend)
         
         print "Created [%s]" % (contact.get_name())
-        self.matched.append(contact)
-
-
-    def commit(self):
-        self.store.close()