#!/usr/bin/env python # new import win32api import win32con import time import os import sys import syncer import samsung_phone.calendar import win32.event import win32.mainloop 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_B_port_exists = False event_1 = win32.event.Event() win32api.RegNotifyChangeKeyValue(comm_port_key, False, 4, event_1.handle, True) while True: #print "selecting..." ready_item = win32.mainloop.select([event_1]) # TODO timeout? (FIXME: Windows Messages will not always be handled in time) if ready_item == win32.mainloop.windows_message_loop: #print "windows message received, pumping messages..." ready_item.pump_waiting_messages() # ??? #print "done." continue # here, the registry changed. #print "registry key changed" B_port_exists = len([entry_name for entry_name, entry_value, entry_type in registry_items(comm_port_key) if entry_value == port_value]) > 0 if B_port_exists and not previous_B_port_exists: # new device found. #print "found cell." # wait for cell connection to settle. time.sleep(1) #child_out, child_in = os.popen4("FIXME", "t") #child_out.close() # FIXME: child_in.read on demand() #child_image_name = os.path.join(my_directory, "push.pyw") #os.spawnv(os.P_WAIT, "python", ["python", child_image_name]) GUI_sync() previous_B_port_exists = B_port_exists #print "reregistering" win32api.RegNotifyChangeKeyValue(comm_port_key, False, 4, event_1.handle, True) #win32.mainloop.windows_message_loop.pump_waiting_messages() # doesn't help. # 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