Changeset 1785

Show
Ignore:
Timestamp:
02/21/07 01:06:19 (22 months ago)
Author:
abaumann
Message:

update motosync to new data/change API, use new ObjTypeSinkCallbacks? class, and clean up a bit
this is still untested, but has a slim chance of working if the OpenSync? XML format is unchanged

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • plugins/moto-sync/motosync.py

    r1777 r1785  
    1212__revision__ = "$Id$" 
    1313 
    14 import sys, os, types, traceback, md5, time, calendar, re 
     14import sys, os, types, md5, time, calendar, re 
    1515import xml.dom.minidom 
    1616from datetime import date, datetime, timedelta, time as datetime_time 
     
    396396 
    397397    def connect(self): 
     398        """Connect to the phone and initiate communication.""" 
    398399        if BT_MAC_RE.match(self.devstr): 
    399400            assert(USE_BLUETOOTH_MODULE, 
    400401                   "MAC address specified, but pybluez module is not available") 
    401              
     402 
    402403            # search for the port to use on the device 
    403404            port = BT_DEFAULT_CHANNEL 
    404             found = bluetooth.find_service(name=BT_SERVICE_NAME, address=self.devstr) 
     405            found = bluetooth.find_service(name=BT_SERVICE_NAME, 
     406                                           address=self.devstr) 
    405407            if found: 
    406408                assert(found[0]['protocol'] == 'RFCOMM') 
     
    437439 
    438440    def disconnect(self): 
     441        """Disconnect from the phone.""" 
    439442        self.close_calendar() 
    440443        if self.__fd: 
     
    503506            for (expos, exnum, extype) in self.__parse_results('MDBRE', data): 
    504507                if extype != 1: # haven't seen anything else 
    505                     raise opensync.Error('unexpected exception type %d' % extype) 
     508                    raise opensync.Error('unexpected exception type %d'%extype) 
    506509                if not exceptions.has_key(expos): 
    507510                    exceptions[expos] = [] 
     
    14831486     Interfaces between PhoneComms/PhoneEntry classes and SyncClass below. 
    14841487     """ 
    1485     def __init__(self, comms): 
     1488    def __init__(self, comms, info): 
    14861489        self.comms = comms 
    14871490        self.serial = None 
     
    14901493        self.revcategories = {} 
    14911494 
     1495        # find ObjFormat objects for our types 
     1496        self.objformats = {} 
     1497        for objtype in SUPPORTED_OBJTYPES: 
     1498            formatstr = "xml-%s-doc" % objtype 
     1499            self.objformats[objtype] = info.format_env.find_objformat(formatstr) 
     1500 
    14921501    def connect(self): 
     1502        """Connect to the phone and setup our data structures.""" 
    14931503        self.comms.connect() 
    1494         self.serial = comms.read_serial() 
     1504        self.serial = self.comms.read_serial() 
    14951505 
    14961506        # check that the phone supports the features we need 
    1497         features = comms.read_features() 
     1507        features = self.comms.read_features() 
    14981508        for (bit, desc) in REQUIRED_FEATURES: 
    14991509            if not features[bit]: 
     
    15031513        # read current time on the phone, check if it matches our local time 
    15041514        # FIXME: allow the user to configure a different timezone for the phone 
    1505         timestr = comms.read_time()[:-3] 
     1515        timestr = self.comms.read_time()[:-3] 
    15061516        phone_now = time.strptime(timestr, '%y/%m/%d,%H:%M:%S') 
    15071517        local_now = time.localtime() 
     
    15221532 
    15231533    def disconnect(self): 
     1534        """Disconnect from the phone.""" 
    15241535        self.comms.disconnect() 
    15251536 
    1526     def list_changes(self, objtype, info): 
     1537    def list_changes(self, objtype): 
    15271538        """Return a list of change objects for all entries of the given type.""" 
    15281539 
     
    15441555        ret = [] 
    15451556        for entry in entries: 
     1557            if objtype == 'contact': 
     1558                xmldata = entry.to_xml(self.categories) 
     1559            else: 
     1560                xmldata = entry.to_xml() 
     1561            data = opensync.Data(xmldata, self.objformats[objtype]) 
     1562            data.objtype = objtype 
    15461563            change = opensync.Change() 
    1547             change.member = member 
    1548             change.objtype = objtype 
     1564            change.data = data 
    15491565            change.uid = self.__generate_uid(entry) 
    1550             change.format = "xml-%s-doc" % objtype 
    1551             if objtype == 'contact': 
    1552                 change.data = entry.to_xml(self.categories) 
    1553             else: 
    1554                 change.data = entry.to_xml() 
    15551566            change.hash = self.__gen_hash(entry) 
    15561567            ret.append(change) 
     
    15781589        """ 
    15791590        objtype = change.objtype 
    1580         if change.format != 'xml-%s-doc' % objtype: 
    1581             raise opensync.Error("unhandled data format %s" % change.format, 
     1591        if change.objformat != self.objformats[objtype]: 
     1592            raise opensync.Error("unhandled data format "+change.objformat.name, 
    15821593                                 opensync.ERROR_NOT_SUPPORTED) 
    15831594        try: 
    15841595            if objtype == 'event': 
    1585                 entry = PhoneEventXML(change.data) 
     1596                entry = PhoneEventXML(change.data.data) 
    15861597            elif objtype == 'contact': 
    1587                 entry = PhoneContactXML(change.data, self.revcategories) 
     1598                entry = PhoneContactXML(change.data.data, self.revcategories) 
    15881599        except UnsupportedDataError, e: 
    15891600            warning("%s is unsupported (%s), ignored" % (change.uid, str(e))) 
     
    15911602            # if its modified and we've seen it before, delete it 
    15921603            # otherwise just ignore it 
    1593             if (change.changetype == opensync.CHANGE_MODIFIED 
     1604            if (change.changetype == opensync.CHANGE_TYPE_MODIFIED 
    15941605                and self.uid_seen(change.uid)): 
    1595                 change.changetype = opensync.CHANGE_DELETED 
     1606                change.changetype = opensync.CHANGE_TYPE_DELETED 
    15961607                change.data = None 
    15971608                return self.delete_entry(change.uid) 
     
    15991610                return False 
    16001611         
    1601         if change.changetype == opensync.CHANGE_ADDED: 
     1612        if change.changetype == opensync.CHANGE_TYPE_ADDED: 
    16021613            # allocate positions for the new entry 
    16031614            positions = self.positions[objtype].alloc(entry.num_pos()) 
     
    16771688 
    16781689 
    1679 class MotoSink(opensync.ObjTypeSink): 
     1690class MotoSink(opensync.ObjTypeSinkCallbacks): 
    16801691    """Event synchronisation class used by OpenSync.""" 
    16811692 
    16821693    def __init__(self, objtype, info, access): 
    1683         opensync.ObjTypeSink.__init__(self, objtype, self) 
     1694        opensync.ObjTypeSinkCallbacks.__init__(self, objtype) 
    16841695        self.objtype = objtype 
    1685         self.add_objformat("xml-%s-doc" % objtype) 
     1696        self.sink.add_objformat("xml-%s-doc" % objtype) 
    16861697        self.access = access 
    16871698        hashpath = os.path.join(info.configdir, "%s-hash.db" % objtype) 
     
    16941705    def get_changes(self, info, ctx): 
    16951706        """Report all OSyncChange objects for entries on the phone.""" 
    1696         if self.slowsink: 
     1707        if self.sink.slowsync: 
    16971708            self.hashtable.reset() 
    1698         for change in self.access.list_changes(self.objtype, info): 
     1709        for change in self.access.list_changes(self.objtype): 
    16991710            self.hashtable.report(change.uid) 
    1700             change.changetype = self.hashtable.get_changetype(change.uid, change.hash) 
    1701             if change.changetype != opensync.CHANGE_UNMODIFIED: 
    1702                 self.hashtable.update_hash(change.changetype, change.uid, change.hash) 
     1711            change.changetype = self.hashtable.get_changetype(change.uid, 
     1712                                                              change.hash) 
     1713            if change.changetype != opensync.CHANGE_TYPE_UNMODIFIED: 
     1714                self.hashtable.update_hash(change.changetype, change.uid, 
     1715                                           change.hash) 
    17031716                ctx.report_change(change) 
    17041717        for uid in self.hashtable.get_deleted(): 
    17051718            change = opensync.Change() 
    17061719            change.uid = uid 
    1707             change.changetype = opensync.CHANGE_DELETED 
     1720            change.changetype = opensync.CHANGE_TYPE_DELETED 
    17081721            ctx.report_change(change) 
    1709             hashtable.update_hash(opensync.CHANGE_DELETED, uid, None) 
     1722            self.hashtable.update_hash(opensync.CHANGE_TYPE_DELETED, uid, None) 
    17101723 
    17111724    def commit(self, info, ctx, change): 
     
    17141727            raise opensync.Error('unsupported objtype %s' % change.objtype, 
    17151728                                 opensync.ERROR_NOT_SUPPORTED) 
    1716         if change.changetype == opensync.CHANGE_DELETED: 
     1729        if change.changetype == opensync.CHANGE_TYPE_DELETED: 
    17171730            success = (self.access.uid_seen(change.uid) 
    17181731                       and self.access.delete_entry(change.uid)) 
    1719         elif change.changetype == opensync.CHANGE_MODIFIED: 
     1732        elif change.changetype == opensync.CHANGE_TYPE_MODIFIED: 
    17201733            old_uid = change.uid 
    17211734            success = self.access.update_entry(change) 
     
    17231736            # the old one was deleted, to keep it consistent 
    17241737            if (success and old_uid != change.uid): 
    1725                 self.hashtable.update_hash(opensync.CHANGE_DELETED, old_uid, None) 
     1738                self.hashtable.update_hash(opensync.CHANGE_TYPE_DELETED, 
     1739                                           old_uid, None) 
    17261740        else: 
    17271741            success = self.access.update_entry(change) 
    17281742        if success: 
    1729             self.hashtable.update_hash(change.changetype, change.uid, change.hash) 
    1730  
    1731     def sync_done(self, info, ctx): 
    1732         """Called when the sync is complete.""" 
    1733         self.hashtable.forget() 
    1734         self.hashtable.close() 
     1743            self.hashtable.update_hash(change.changetype, change.uid, 
     1744                                       change.hash) 
    17351745 
    17361746    def disconnect(self, info, ctx): 
     
    17581768    access = PhoneAccess(comms) 
    17591769    for objtype in SUPPORTED_OBJTYPES: 
    1760         info.add_objtype(MotoSink(objtype, info, access)) 
     1770        info.add_objtype(MotoSink(objtype, info, access).sink) 
    17611771 
    17621772def discover(info): 
    1763     """Called by python-module plugin wrapper, discovers capabilities of device.""" 
     1773    """Called by python-module wrapper, discovers capabilities of device.""" 
    17641774    for sink in info.objtypes: 
    17651775        sink.available = True