Module comm_notify
[hide private]
[frames] | no frames]

Source Code for Module comm_notify

  1  ### BITPIM 
  2  ### 
  3  ### Copyright (C) 2006 Joe Pham <djpham@bitpim.org> 
  4  ### 
  5  ### This program is free software; you can redistribute it and/or modify 
  6  ### it under the terms of the BitPim license as detailed in the LICENSE file. 
  7  ### 
  8  ### $Id: comm_notify.py 4219 2007-05-09 02:15:50Z djpham $ 
  9   
 10  """Handle notification of comm ports availability using DNOTIFY""" 
 11   
 12  import fcntl 
 13  import os 
 14  import signal 
 15  import sys 
 16   
 17  import wx 
 18   
 19  bpCOMM_NOTIFICATION_EVENT = wx.NewEventType() 
 20  COMM_NOTIFICATION_EVENT = wx.PyEventBinder(bpCOMM_NOTIFICATION_EVENT, 0) 
 21  default_port=5002 
 22   
23 -class CommNotificationEvent(wx.PyEvent):
24 add=0 25 remove=1
26 - def __init__(self):
27 super(CommNotificationEvent, self).__init__() 28 self.SetEventType=bpCOMM_NOTIFICATION_EVENT 29 self.type=None 30 self.comm=None
31
32 -class CommNotification(object):
33 - def __init__(self, mainwindow):
34 self.mw=mainwindow 35 self.evt=CommNotificationEvent()
36
37 - def add(self, comm):
38 # a new comm has just been added 39 self.evt.type=CommNotificationEvent.add 40 if comm.startswith('/proc/bus/usb/') or \ 41 comm.startswith('/dev/bus/usb/'): 42 # this is a Linux hotplug USB port, which may have many interfaces. 43 # can't figure which one so scan for all ports. 44 self.evt.comm=None 45 else: 46 self.evt.comm=comm 47 self.mw.OnCommNotification(self.evt) 48 return True
49
50 - def remove(self, comm):
51 # a new comm has just been deleted 52 if comm.startswith('/proc/bus/usb/') or \ 53 comm.startswith('/dev/bus/usb/'): 54 # This is a Linux hotplug USB port, just ignore it 55 return False 56 self.evt.type=CommNotificationEvent.remove 57 self.evt.comm=comm 58 self.mw.OnCommNotification(self.evt) 59 return True
60 61 NotificationPath='/var/bitpim' 62 NotificationFile='/var/bitpim/dnotify.log' 63
64 -def _process_notification(commobj):
65 # read the log file & process its content 66 # expecting a line: add|del <port> 67 global NotificationFile 68 try: 69 _s=file(NotificationFile, 'rt').read() 70 _tkns=_s.split(' ') 71 if len(_tkns)==2: 72 if _tkns[0]=='add': 73 commobj.add(_tkns[1]) 74 else: 75 commobj.remove(_tkns[1]) 76 except: 77 if __debug__: 78 raise
79 80 _global_fd=None 81 _global_obj=None 82
83 -def _sigio_handler(*args, **kwargs):
84 global _global_obj 85 if _global_obj: 86 wx.CallAfter(_process_notification, _global_obj)
87
88 -def run_server(mainwindow):
89 global NotificationPath, _global_fd, _global_obj 90 _global_obj=CommNotification(mainwindow) 91 try: 92 _global_fd=os.open(NotificationPath, os.O_RDONLY) 93 except OSError: 94 # Something's wrong with the dir, bail 95 mainwindow.log('Failed to open dir '+NotificationPath) 96 return False 97 except: 98 if __debug__: 99 raise 100 return False 101 fcntl.fcntl(_global_fd, fcntl.F_NOTIFY, 102 fcntl.DN_MODIFY|fcntl.DN_CREATE|fcntl.DN_MULTISHOT) 103 mainwindow.log('USB Comm Watch started') 104 return True
105
106 -def start_server(mainwindow):
107 signal.signal(signal.SIGIO, _sigio_handler) 108 return run_server(mainwindow)
109