home *** CD-ROM | disk | FTP | other *** search
/ PC World 2002 May / PCWorld_2002-05_cd.bin / Software / TemaCD / activepython / ActivePython-2.1.1.msi / Python21_win32com_client_connect.py < prev    next >
Encoding:
Python Source  |  2001-07-26  |  1.1 KB  |  39 lines

  1. """Utilities for working with Connections"""
  2. import win32com.server.util, pythoncom
  3.  
  4. class SimpleConnection:
  5.     "A simple, single connection object"
  6.     def __init__(self, coInstance = None, eventInstance = None, eventCLSID = None):
  7.         self.cp = None
  8.         self.cookie = None
  9.         if not coInstance is None:
  10.             self.Connect(coInstance , eventInstance, eventCLSID)
  11.  
  12.     def __del__(self):
  13.         try:
  14.             self.Disconnect()
  15.         except pythoncom.error:
  16.             # Ignore disconnection as we are torn down.
  17.             pass
  18.  
  19.     def _wrap(self, obj):
  20.         return win32com.server.util.wrap(obj)
  21.  
  22.     def Connect(self, coInstance, eventInstance, eventCLSID = None, dispatcher = None):
  23.         try:
  24.             oleobj = coInstance._oleobj_
  25.         except AttributeError:
  26.             oleobj = coInstance
  27.         cpc=oleobj.QueryInterface(pythoncom.IID_IConnectionPointContainer)
  28.         if eventCLSID is None: eventCLSID = eventInstance.CLSID
  29.         comEventInstance = self._wrap(eventInstance)
  30.         self.cp=cpc.FindConnectionPoint(eventCLSID)
  31.         self.cookie = self.cp.Advise(comEventInstance)
  32.  
  33.     def Disconnect(self):
  34.         if not self.cp is None:
  35.             if self.cookie:
  36.                 self.cp.Unadvise(self.cookie)
  37.                 self.cookie = None
  38.             self.cp = None        
  39.