#!/usr/bin/env python import win32api import win32con import time import os import sys import syncer import samsung_phone.calendar def sync(): cellphone_calendar_connection_1 = samsung_phone.calendar.Connection() try: return syncer.sync_items(cellphone_calendar_connection_1) finally: cellphone_calendar_connection_1.close() def GUI_sync(): import gui return gui.with_status_GUI(sync, lambda result : "sent %d appointments to the cell phone and received %d new appointments from it" % (len(result[0]), len(result[1]))) my_directory = os.path.dirname(sys.argv[0]) REG_NOTIFY_CHANGE_NAME = 1 REG_NOTIFY_CHANGE_ATTRIBUTES = 2 # last is "async?" usb_key = win32api.RegOpenKey(win32con.HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Enum\\USB\\Vid_04e8&Pid_663f&Mi_03\\00000000_0_03\\Device Parameters") port_value, port_type = win32api.RegQueryValueEx(usb_key, "PortName") def registry_items(key): value_index = 0 try: while True: entry_name, entry_value, entry_type = win32api.RegEnumValue(key, value_index) yield entry_name, entry_value, entry_type value_index = value_index + 1 except: # pywintypes.error: 259 PyRegEnumValue "No more data is available" pass if __name__ == "__main__": #port_value, port_type = win32api.RegQueryValueEx(comm_port_key, "PortName") #key = win32api.RegOpenKeyEx(win32con.HKEY_LOCAL_MACHINE, r"SYSTEM\\CurrentControlSet\\Enum\\USB") # REG_NOTIFY_CHANGE_LAST_SET timestamp comm_port_key = win32api.RegOpenKeyEx(win32con.HKEY_LOCAL_MACHINE, r"HARDWARE\DEVICEMAP\SERIALCOMM") previous_port_exists_p = False while True: win32api.RegNotifyChangeKeyValue(comm_port_key, False, 4, None, False) # blocks. port_exists_p = len([entry_name for entry_name, entry_value, entry_type in registry_items(comm_port_key) if entry_value == port_value]) > 0 if port_exists_p and not previous_port_exists_p: # new device found. #print "found cell." # wait for time to settle. time.sleep(1) #child_image_name = os.path.join(my_directory, "push.pyw") #os.spawnv(os.P_WAIT, "python", ["python", child_image_name]) GUI_sync() previous_port_exists_p = port_exists_p # HKEY_LOCAL_MACHINE\SYSTEM\ControlSet003\Enum\USB\Vid_04e8&Pid_663f&Mi_03\00000000_0_03\Device Parameters # HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Ports