示例#1
0
 def __init__(self, sconn=None):
     self.sconn = sconn
     self.cmanager = ContactsManager()
示例#2
0
 def __init__(self, sconn=None):
     self.sconn = sconn
     self.cmanager = ContactsManager()
     self.evlcmanager = EVContactsManager()
     self.kdecmanager = KDEContactsManager()
示例#3
0
class PhoneBook(object):
    """
    I manage all your contacts
    
    PhoneBook presents a single interface to access contacts from
    both the SIM and DB
    """
    
    def __init__(self, sconn=None):
        self.sconn = sconn
        self.cmanager = ContactsManager()
    
    def close(self):
        self.cmanager.close()
        self.cmanager = None
        self.sconn = None

    def add_contact(self, contact, sim=False):
        def add_sim_contact_cb(index):
            contact.index = int(index)
            return contact
        
        def invalid_chars_eb(failure):
            failure.trap(ex.CMEErrorInvalidCharactersInDialString,
                         ex.CMEErrorStringTooLong)
            log.err(failure)
        
        if sim:
            d = self.sconn.add_contact(contact)
            d.addCallback(add_sim_contact_cb)
            d.addErrback(invalid_chars_eb)
        else:
            d = defer.maybeDeferred(self.cmanager.add_contact, contact)
        
        return d
    
    def add_contacts(self, contacts, sim=False):
        responses = [self.add_contact(contact, sim) for contact in contacts]
        return defer.gatherResults(responses)
    
    def _find_contact_in_sim(self, pattern):
        return self.sconn.find_contacts(pattern)
    
    def _find_contact_in_db(self, pattern):
        return list(self.cmanager.find_contacts(pattern))
    
    def find_contact(self, name=None, number=None):
        if (not name and not number) or (name and number):
            return defer.fail()
        
        if name:
            d = self._find_contact_in_sim(name)
            def find_contacts(contacts):
                return self._find_contact_in_db(name) + contacts
            def find_contacts_eb(failure):
                failure.trap(ex.ATError, ex.CMEErrorNotFound)
                return self._find_contact_in_db(name)
            
            d.addCallback(find_contacts)
            d.addErrback(find_contacts_eb)
            return d
        
        elif number:
            # searching by name is pretty easy as the SIM allows to lookup
            # contacts by name. However searching by number is more difficult
            # as the SIM doesn't provides any facility for it. Thus we need
            # to get *all* contacts and iterate through them looking for
            # a number that matches the pattern
            d = self.get_contacts()
            d.addCallback(lambda contacts: [c for c in contacts
                                                if c.get_number() == number])
            return d
    
    def get_contacts(self):
        d = self.sconn.get_contacts()
        d.addCallback(lambda simc: list(self.cmanager.get_contacts()) + simc)
        d.addErrback(log.err)
        return d
    
    def sync(self, SIM_PC=False, PC_SIM=False):
        DBContacts = {}
        for c in list(self.cmanager.get_contacts()):
            DBContacts[c.get_number()] = (c.get_name(), c.get_group())
        SIMContacts = {}
        d = self.sconn.get_contacts()

        def syncContacts(simc):
            for c in simc:
                SIMContacts[c.get_number()] = (c.get_name(), c.get_group())
            changes =[]

            if SIM_PC:
                for number, (name, group) in SIMContacts.iteritems():
                    contact = Contact (name, number, group=group)
                    if number not in DBContacts:
                        changes.append(self.cmanager.add_contact(contact))
                    else:
                        if name != DBContacts[number][0]:
                            ### TODO
                            # contact.name = name
                            # Any way to refresh info to the treeview
                            log.msg('SIM Conflict with Number %s' % number)

            if PC_SIM:
                for number, (name, group) in DBContacts.iteritems():
                    contact = Contact (name, number, group=group)
                    if number not in SIMContacts:
                        self.add_contact(contact, True)
                        contact.group = 'SIM'
                        changes.append(contact)
                    else:
                        if name != SIMContacts[number][0]: # Modify!
                            self.edit_contact(contact)
                            log.msg('DB Conflict with Number %s' % number)

            return changes
        
        d.addCallback(syncContacts)
        d.addErrback(log.err)

        return d
    
    def delete_contacts(self, clist):
        deflist = [self.delete_contact(contact) for contact in clist]
        return defer.gatherResults(deflist)
    
    def delete_objs(self, objs):
        return self.delete_contacts([o for o in objs if o])
    
    def delete_contact(self, contact):
        if is_sim_contact(contact):
            return self.sconn.delete_contact(contact.get_index())
        else:
            return defer.maybeDeferred(self.cmanager.delete_contact, contact)
    
    def edit_contact(self, contact):
        if is_sim_contact(contact):
            def add_contact_cb(index):
                contact.index = index
                return contact
            
            d = self.sconn.add_contact(contact)
            d.addCallback(add_contact_cb)
            return d
        else:
            raise NotImplementedError()
 def setUpClass(self):
     self.mana = ContactsManager(TMPFILE)
示例#5
0
class PhoneBook(object):
    """
    I manage all your contacts

    PhoneBook presents a single interface to access contacts from
    both the SIM and DB
    """
    def __init__(self, sconn=None):
        self.sconn = sconn
        self.cmanager = ContactsManager()
        self.evlcmanager = EVContactsManager()
        self.kdecmanager = KDEContactsManager()

    def close(self):
        self.cmanager.close()
        self.cmanager = None
        self.sconn = None

    def add_contact(self, contact, sim=False):
        def add_sim_contact_cb(index):
            contact.index = int(index)
            return contact

        def invalid_chars_eb(failure):
            failure.trap(ex.CMEErrorInvalidCharactersInDialString,
                         ex.CMEErrorStringTooLong)
            log.err(failure)

        if sim:
            d = self.sconn.add_contact(contact)
            d.addCallback(add_sim_contact_cb)
            d.addErrback(invalid_chars_eb)
        else:
            d = defer.maybeDeferred(self.cmanager.add_contact, contact)

        return d

    def add_contacts(self, contacts, sim=False):
        responses = [self.add_contact(contact, sim) for contact in contacts]
        return defer.gatherResults(responses)

    def _find_contact_in_sim(self, pattern):
        return self.sconn.find_contacts(pattern)

    def _find_contact_in_db(self, pattern):
        return list(self.cmanager.find_contacts(pattern))

    def _find_contact_in_ev(self, pattern):
        return list(self.evlcmanager.find_contacts(pattern))

    def _find_contact_in_kde(self, pattern):
        return list(self.kdecmanager.find_contacts(pattern))

    def find_contact(self, name=None, number=None):
        if (not name and not number) or (name and number):
            return defer.fail()

        if name:
            d = self._find_contact_in_sim(name)

            def find_contacts_db(contacts):
                return self._find_contact_in_db(name) + contacts

            def find_contacts_ev(contacts):
                return self._find_contact_in_ev(name) + contacts

            def find_contacts_kde(contacts):
                return self._find_contact_in_kde(name) + contacts

            def find_contacts_eb(failure):
                failure.trap(ex.ATError, ex.CMEErrorNotFound)
                return (self._find_contact_in_db(name) +
                        self._find_contact_in_ev(name) +
                        self._find_contact_in_kde(name))

            d.addCallback(find_contacts_db)
            d.addCallback(find_contacts_ev)
            d.addCallback(find_contacts_kde)
            d.addErrback(find_contacts_eb)
            return d

        elif number:
            # searching by name is pretty easy as the SIM allows to lookup
            # contacts by name. However searching by number is more difficult
            # as the SIM doesn't provides any facility for it. Thus we need
            # to get *all* contacts and iterate through them looking for
            # a number that matches the pattern
            d = self.get_contacts()
            d.addCallback(lambda contacts:
                          [c for c in contacts if c.get_number() == number])
            return d

    def get_contacts(self):
        d = self.sconn.get_contacts()
        d.addCallback(lambda simc: list(self.cmanager.get_contacts()) + simc)
        d.addCallback(
            lambda prec: list(self.evlcmanager.get_contacts()) + prec)
        d.addCallback(
            lambda prec: list(self.kdecmanager.get_contacts()) + prec)
        d.addErrback(log.err)
        return d

    def delete_contacts(self, clist):
        deflist = [self.delete_contact(contact) for contact in clist]
        return defer.gatherResults(deflist)

    def delete_objs(self, objs):
        return self.delete_contacts(objs)

    def delete_contact(self, contact):
        if is_sim_contact(contact):
            return self.sconn.delete_contact(contact.get_index())
        else:
            return defer.maybeDeferred(self.cmanager.delete_contact, contact)

    def edit_contact(self, contact):
        if is_sim_contact(contact):

            def add_contact_cb(index):
                contact.index = index
                return contact

            d = self.sconn.add_contact(contact)
            d.addCallback(add_contact_cb)
            return d
        else:
            raise NotImplementedError()
class TestContactsManager(unittest.TestCase):
    """Test for L{vmc.common.persistent.ContactsManager} functionality"""

    def setUpClass(self):
        self.mana = ContactsManager(TMPFILE)
    
    def tearDownClass(self):
        self.mana.close()
        shutil.rmtree(TMPFILE)
    
    def test_add_contact(self):
        contact = Contact('Peter', '+3453453452')
        c1 = self.mana.add_contact(contact)
        self.assertEqual(contact, self.mana.get_contact_by_id(c1.get_index()))
        contact2 = Contact('John', '+45364563345')
        c2  = self.mana.add_contact(contact2)
        self.assertEqual(contact2,
                         self.mana.get_contact_by_id(c2.get_index()))
    
    def test_find_contact(self):
        contact = Contact('Pepito', '+3453423423423')
        c1 = self.mana.add_contact(contact)
        resp = list(self.mana.find_contacts('Pe'))
        self.failUnlessIn(c1, resp)
    
    def test_delete_contact(self):
        contact = Contact(pack('Paul'), '+45364563345')
        c1 = self.mana.add_contact(contact)
        self.mana.delete_contact_by_id(c1.get_index())
        self.assertRaises(KeyError, self.mana.get_contact_by_id,
                                    c1.get_index())
 def __init__(self, sconn=None):
     self.sconn = sconn
     self.cmanager = ContactsManager()
     self.evlcmanager = EVContactsManager()
     self.kdecmanager = KDEContactsManager()
class PhoneBook(object):
    """
    I manage all your contacts

    PhoneBook presents a single interface to access contacts from
    both the SIM and DB
    """

    def __init__(self, sconn=None):
        self.sconn = sconn
        self.cmanager = ContactsManager()
        self.evlcmanager = EVContactsManager()
        self.kdecmanager = KDEContactsManager()

    def close(self):
        self.cmanager.close()
        self.cmanager = None
        self.sconn = None
    
    def add_contact(self, contact, sim=False):
        def add_sim_contact_cb(index):
            contact.index = int(index)
            return contact
        
        def invalid_chars_eb(failure):
            failure.trap(ex.CMEErrorInvalidCharactersInDialString,
                         ex.CMEErrorStringTooLong)
            log.err(failure)
        
        if sim:
            d = self.sconn.add_contact(contact)
            d.addCallback(add_sim_contact_cb)
            d.addErrback(invalid_chars_eb)
        else:
            d = defer.maybeDeferred(self.cmanager.add_contact, contact)
        
        return d
    
    def add_contacts(self, contacts, sim=False):
        responses = [self.add_contact(contact, sim) for contact in contacts]
        return defer.gatherResults(responses)
    
    def _find_contact_in_sim(self, pattern):
        return self.sconn.find_contacts(pattern)

    def _find_contact_in_db(self, pattern):
        return list(self.cmanager.find_contacts(pattern))

    def _find_contact_in_ev(self, pattern):
        return list(self.evlcmanager.find_contacts(pattern))

    def _find_contact_in_kde(self, pattern):
        return list(self.kdecmanager.find_contacts(pattern))

    def find_contact(self, name=None, number=None):
        if (not name and not number) or (name and number):
            return defer.fail()

        if name:
            d = self._find_contact_in_sim(name)
            def find_contacts_db(contacts):
                return self._find_contact_in_db(name) + contacts
            def find_contacts_ev(contacts):
                return self._find_contact_in_ev(name) + contacts
            def find_contacts_kde(contacts):
                return self._find_contact_in_kde(name) + contacts

            def find_contacts_eb(failure):
                failure.trap(ex.ATError, ex.CMEErrorNotFound)
                return ( self._find_contact_in_db(name) +
                         self._find_contact_in_ev(name) +
                         self._find_contact_in_kde(name) )

            d.addCallback(find_contacts_db)
            d.addCallback(find_contacts_ev)
            d.addCallback(find_contacts_kde)
            d.addErrback(find_contacts_eb)
            return d

        elif number:
            # searching by name is pretty easy as the SIM allows to lookup
            # contacts by name. However searching by number is more difficult
            # as the SIM doesn't provides any facility for it. Thus we need
            # to get *all* contacts and iterate through them looking for
            # a number that matches the pattern
            d = self.get_contacts()
            d.addCallback(lambda contacts: [c for c in contacts
                                                if c.get_number() == number])
            return d

    def get_contacts(self):
        d = self.sconn.get_contacts()
        d.addCallback(lambda simc: list(self.cmanager.get_contacts()) + simc)
        d.addCallback(lambda prec: list(self.evlcmanager.get_contacts()) + prec)
        d.addCallback(lambda prec: list(self.kdecmanager.get_contacts()) + prec)
        d.addErrback(log.err)
        return d

    def delete_contacts(self, clist):
        deflist = [self.delete_contact(contact) for contact in clist]
        return defer.gatherResults(deflist)
    
    def delete_objs(self, objs):
        return self.delete_contacts(objs)
    
    def delete_contact(self, contact):
        if is_sim_contact(contact):
            return self.sconn.delete_contact(contact.get_index())
        else:
            return defer.maybeDeferred(self.cmanager.delete_contact, contact)
    
    def edit_contact(self, contact):
        if is_sim_contact(contact):
            def add_contact_cb(index):
                contact.index = index
                return contact
            
            d = self.sconn.add_contact(contact)
            d.addCallback(add_contact_cb)
            return d
        else:
            raise NotImplementedError()
 def setUpClass(self):
     self.mana = ContactsManager(TMPFILE)
class TestContactsManager(unittest.TestCase):
    """Test for L{vmc.common.persistent.ContactsManager} functionality"""
    def setUpClass(self):
        self.mana = ContactsManager(TMPFILE)

    def tearDownClass(self):
        self.mana.close()
        shutil.rmtree(TMPFILE)

    def test_add_contact(self):
        contact = Contact('Peter', '+3453453452')
        c1 = self.mana.add_contact(contact)
        self.assertEqual(contact, self.mana.get_contact_by_id(c1.get_index()))
        contact2 = Contact('John', '+45364563345')
        c2 = self.mana.add_contact(contact2)
        self.assertEqual(contact2, self.mana.get_contact_by_id(c2.get_index()))

    def test_find_contact(self):
        contact = Contact('Pepito', '+3453423423423')
        c1 = self.mana.add_contact(contact)
        resp = list(self.mana.find_contacts('Pe'))
        self.failUnlessIn(c1, resp)

    def test_delete_contact(self):
        contact = Contact(pack('Paul'), '+45364563345')
        c1 = self.mana.add_contact(contact)
        self.mana.delete_contact_by_id(c1.get_index())
        self.assertRaises(KeyError, self.mana.get_contact_by_id,
                          c1.get_index())