Package Peach :: Package Publishers :: Module com
[hide private]

Source Code for Module Peach.Publishers.com

  1   
  2  ''' 
  3  Windows COM/DCOM/COM+ publishers. 
  4   
  5  @author: Michael Eddington 
  6  @version: $Id: Peach.Publishers.com-pysrc.html 1138 2008-08-16 19:39:03Z meddingt $ 
  7  ''' 
  8   
  9  # 
 10  # Copyright (c) 2005-2008 Michael Eddington 
 11  # Copyright (c) 2004-2005 IOActive Inc. 
 12  # 
 13  # Permission is hereby granted, free of charge, to any person obtaining a copy  
 14  # of this software and associated documentation files (the "Software"), to deal 
 15  # in the Software without restriction, including without limitation the rights  
 16  # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell  
 17  # copies of the Software, and to permit persons to whom the Software is  
 18  # furnished to do so, subject to the following conditions: 
 19  # 
 20  # The above copyright notice and this permission notice shall be included in     
 21  # all copies or substantial portions of the Software. 
 22  # 
 23  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  
 24  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  
 25  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE  
 26  # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER  
 27  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 28  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
 29  # SOFTWARE. 
 30  # 
 31   
 32  # Authors: 
 33  #   Michael Eddington (mike@phed.org) 
 34   
 35  # $Id: Peach.Publishers.com-pysrc.html 1138 2008-08-16 19:39:03Z meddingt $ 
 36   
 37  try: 
 38          import time, sys, pywintypes, signal, os 
 39          import win32com.client 
 40          import win32com.client.gencache 
 41  except: 
 42          pass 
 43  from Peach.publisher import Publisher 
 44   
45 -class Com(Publisher):
46 """ 47 Very simple Com publisher that allows for a single method call. The 48 method call is fed in as a string which is evaled. This allows for 49 calling any method with any number of parameters. 50 """ 51 52 _clsid = None 53 _methodFormat = None 54 _lastReturn = None 55 _object = None 56
57 - def __init__(self, clsid):
58 """ 59 Create Com Object. clsid = '{...}' 60 61 @type clsid: string 62 @param clsid: CLSID of COM object in {...} format 63 """ 64 65 self._clsid = clsid
66
67 - def start(self):
68 try: 69 self._object = None 70 self._object = win32com.client.DispatchEx(self._clsid) 71 72 except pywintypes.com_error, e: 73 print "Caught pywintypes.com_error creating ActiveX control [%s]" % e 74 raise 75 76 except: 77 print "Caught unkown exception creating ActiveX control [%s]" % sys.exc_info()[0] 78 raise
79
80 - def stop(self):
81 self._object = None
82
83 - def call(self, method, args):
84 ''' 85 Call method on COM object. 86 87 @type method: string 88 @param method: Name of method to invoke 89 @type args: array of objects 90 @param args: Arguments to pass 91 ''' 92 93 self._lastReturn = None 94 95 try: 96 ret = None 97 callStr = "ret = self._object.%s(" % str(method) 98 99 if len(args) > 0: 100 for i in range(0, len(args)): 101 callStr += "args[%d]," % i 102 103 callStr = callStr[:len(callStr)-1] + ")" 104 105 else: 106 callStr += ")" 107 108 exec callStr 109 return ret 110 111 except pywintypes.com_error, e: 112 print "Caught pywintypes.com_error on call [%s]" % e 113 raise 114 115 except NameError, e: 116 print "Caught NameError on call [%s]" % e 117 raise 118 119 except: 120 print "Com::send(): Caught unknown exception" 121 raise 122 123 return None
124
125 - def property(self, property, value = None):
126 ''' 127 Get or set property 128 129 @type property: string 130 @param property: Name of method to invoke 131 @type value: object 132 @param value: Value to set. If None, return property instead 133 ''' 134 135 try: 136 if value == None: 137 ret = None 138 callStr = "ret = self._object.%s" % str(property) 139 140 #print "Call string:", callStr 141 exec callStr 142 return ret 143 144 ret = None 145 callStr = "self._object.%s = value" % str(property) 146 147 #print "Call string:", callStr 148 exec callStr 149 return None 150 151 except pywintypes.com_error, e: 152 print "Caught pywintypes.com_error on property [%s]" % e 153 #raise 154 155 except NameError, e: 156 print "Caught NameError on property [%s]" % e 157 #raise 158 159 except: 160 print "Com::property(): Caught unknown exception" 161 raise 162 163 return None
164 165 # end 166