Changeset 1785
- Timestamp:
- 02/21/07 01:06:19 (22 months ago)
- Files:
-
- 1 modified
-
plugins/moto-sync/motosync.py (modified) (17 diffs)
Legend:
- Unmodified
- Added
- Removed
-
plugins/moto-sync/motosync.py
r1777 r1785 12 12 __revision__ = "$Id$" 13 13 14 import sys, os, types, traceback,md5, time, calendar, re14 import sys, os, types, md5, time, calendar, re 15 15 import xml.dom.minidom 16 16 from datetime import date, datetime, timedelta, time as datetime_time … … 396 396 397 397 def connect(self): 398 """Connect to the phone and initiate communication.""" 398 399 if BT_MAC_RE.match(self.devstr): 399 400 assert(USE_BLUETOOTH_MODULE, 400 401 "MAC address specified, but pybluez module is not available") 401 402 402 403 # search for the port to use on the device 403 404 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) 405 407 if found: 406 408 assert(found[0]['protocol'] == 'RFCOMM') … … 437 439 438 440 def disconnect(self): 441 """Disconnect from the phone.""" 439 442 self.close_calendar() 440 443 if self.__fd: … … 503 506 for (expos, exnum, extype) in self.__parse_results('MDBRE', data): 504 507 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) 506 509 if not exceptions.has_key(expos): 507 510 exceptions[expos] = [] … … 1483 1486 Interfaces between PhoneComms/PhoneEntry classes and SyncClass below. 1484 1487 """ 1485 def __init__(self, comms ):1488 def __init__(self, comms, info): 1486 1489 self.comms = comms 1487 1490 self.serial = None … … 1490 1493 self.revcategories = {} 1491 1494 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 1492 1501 def connect(self): 1502 """Connect to the phone and setup our data structures.""" 1493 1503 self.comms.connect() 1494 self.serial = comms.read_serial()1504 self.serial = self.comms.read_serial() 1495 1505 1496 1506 # check that the phone supports the features we need 1497 features = comms.read_features()1507 features = self.comms.read_features() 1498 1508 for (bit, desc) in REQUIRED_FEATURES: 1499 1509 if not features[bit]: … … 1503 1513 # read current time on the phone, check if it matches our local time 1504 1514 # 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] 1506 1516 phone_now = time.strptime(timestr, '%y/%m/%d,%H:%M:%S') 1507 1517 local_now = time.localtime() … … 1522 1532 1523 1533 def disconnect(self): 1534 """Disconnect from the phone.""" 1524 1535 self.comms.disconnect() 1525 1536 1526 def list_changes(self, objtype , info):1537 def list_changes(self, objtype): 1527 1538 """Return a list of change objects for all entries of the given type.""" 1528 1539 … … 1544 1555 ret = [] 1545 1556 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 1546 1563 change = opensync.Change() 1547 change.member = member 1548 change.objtype = objtype 1564 change.data = data 1549 1565 change.uid = self.__generate_uid(entry) 1550 change.format = "xml-%s-doc" % objtype1551 if objtype == 'contact':1552 change.data = entry.to_xml(self.categories)1553 else:1554 change.data = entry.to_xml()1555 1566 change.hash = self.__gen_hash(entry) 1556 1567 ret.append(change) … … 1578 1589 """ 1579 1590 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, 1582 1593 opensync.ERROR_NOT_SUPPORTED) 1583 1594 try: 1584 1595 if objtype == 'event': 1585 entry = PhoneEventXML(change.data )1596 entry = PhoneEventXML(change.data.data) 1586 1597 elif objtype == 'contact': 1587 entry = PhoneContactXML(change.data , self.revcategories)1598 entry = PhoneContactXML(change.data.data, self.revcategories) 1588 1599 except UnsupportedDataError, e: 1589 1600 warning("%s is unsupported (%s), ignored" % (change.uid, str(e))) … … 1591 1602 # if its modified and we've seen it before, delete it 1592 1603 # otherwise just ignore it 1593 if (change.changetype == opensync.CHANGE_ MODIFIED1604 if (change.changetype == opensync.CHANGE_TYPE_MODIFIED 1594 1605 and self.uid_seen(change.uid)): 1595 change.changetype = opensync.CHANGE_ DELETED1606 change.changetype = opensync.CHANGE_TYPE_DELETED 1596 1607 change.data = None 1597 1608 return self.delete_entry(change.uid) … … 1599 1610 return False 1600 1611 1601 if change.changetype == opensync.CHANGE_ ADDED:1612 if change.changetype == opensync.CHANGE_TYPE_ADDED: 1602 1613 # allocate positions for the new entry 1603 1614 positions = self.positions[objtype].alloc(entry.num_pos()) … … 1677 1688 1678 1689 1679 class MotoSink(opensync.ObjTypeSink ):1690 class MotoSink(opensync.ObjTypeSinkCallbacks): 1680 1691 """Event synchronisation class used by OpenSync.""" 1681 1692 1682 1693 def __init__(self, objtype, info, access): 1683 opensync.ObjTypeSink .__init__(self, objtype, self)1694 opensync.ObjTypeSinkCallbacks.__init__(self, objtype) 1684 1695 self.objtype = objtype 1685 self. add_objformat("xml-%s-doc" % objtype)1696 self.sink.add_objformat("xml-%s-doc" % objtype) 1686 1697 self.access = access 1687 1698 hashpath = os.path.join(info.configdir, "%s-hash.db" % objtype) … … 1694 1705 def get_changes(self, info, ctx): 1695 1706 """Report all OSyncChange objects for entries on the phone.""" 1696 if self.s lowsink:1707 if self.sink.slowsync: 1697 1708 self.hashtable.reset() 1698 for change in self.access.list_changes(self.objtype , info):1709 for change in self.access.list_changes(self.objtype): 1699 1710 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) 1703 1716 ctx.report_change(change) 1704 1717 for uid in self.hashtable.get_deleted(): 1705 1718 change = opensync.Change() 1706 1719 change.uid = uid 1707 change.changetype = opensync.CHANGE_ DELETED1720 change.changetype = opensync.CHANGE_TYPE_DELETED 1708 1721 ctx.report_change(change) 1709 hashtable.update_hash(opensync.CHANGE_DELETED, uid, None)1722 self.hashtable.update_hash(opensync.CHANGE_TYPE_DELETED, uid, None) 1710 1723 1711 1724 def commit(self, info, ctx, change): … … 1714 1727 raise opensync.Error('unsupported objtype %s' % change.objtype, 1715 1728 opensync.ERROR_NOT_SUPPORTED) 1716 if change.changetype == opensync.CHANGE_ DELETED:1729 if change.changetype == opensync.CHANGE_TYPE_DELETED: 1717 1730 success = (self.access.uid_seen(change.uid) 1718 1731 and self.access.delete_entry(change.uid)) 1719 elif change.changetype == opensync.CHANGE_ MODIFIED:1732 elif change.changetype == opensync.CHANGE_TYPE_MODIFIED: 1720 1733 old_uid = change.uid 1721 1734 success = self.access.update_entry(change) … … 1723 1736 # the old one was deleted, to keep it consistent 1724 1737 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) 1726 1740 else: 1727 1741 success = self.access.update_entry(change) 1728 1742 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) 1735 1745 1736 1746 def disconnect(self, info, ctx): … … 1758 1768 access = PhoneAccess(comms) 1759 1769 for objtype in SUPPORTED_OBJTYPES: 1760 info.add_objtype(MotoSink(objtype, info, access) )1770 info.add_objtype(MotoSink(objtype, info, access).sink) 1761 1771 1762 1772 def discover(info): 1763 """Called by python-module pluginwrapper, discovers capabilities of device."""1773 """Called by python-module wrapper, discovers capabilities of device.""" 1764 1774 for sink in info.objtypes: 1765 1775 sink.available = True
