1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
|
#---------------------------------------------------------------------------
# Some helper and utility functions for ActiveX
t4 = " " * 4
t8 = " " * 8
def GetAXInfo(ax):
"""
Returns a printable summary of the TypeInfo from the ActiveX instance
passed in.
"""
def ProcessFuncX(f, out, name):
out.append(name)
out.append(t4 + "retType: %s" % f.retType.vt_type)
if f.params:
out.append(t4 + "params:")
for p in f.params:
out.append(t8 + p.name)
out.append(t8+t4+ "in:%s out:%s optional:%s type:%s" % (p.isIn, p.isOut, p.isOptional, p.vt_type))
out.append('')
def ProcessPropX(p, out):
out.append(GernerateAXModule.trimPropName(p.name))
out.append(t4+ "type:%s arg:%s canGet:%s canSet:%s" % (p.type.vt_type, p.arg.vt_type, p.canGet, p.canSet))
out.append('')
out = []
out.append("PROPERTIES")
out.append("-"*20)
for p in ax.GetAXProperties():
ProcessPropX(p, out)
out.append('\n\n')
out.append("METHODS")
out.append("-"*20)
for m in ax.GetAXMethods():
ProcessFuncX(m, out, GernerateAXModule.trimMethodName(m.name))
out.append('\n\n')
out.append("EVENTS")
out.append("-"*20)
for e in ax.GetAXEvents():
ProcessFuncX(e, out, GernerateAXModule.trimEventName(e.name))
out.append('\n\n')
return "\n".join(out)
class GernerateAXModule:
def __init__(self, ax, className, modulePath, moduleName=None, verbose=False):
"""
Make a Python module file with a class that has been specialized
for the AcitveX object.
ax An instance of the ActiveXWindow class
className The name to use for the new class
modulePath The path where the new module should be written to
moduleName The name of the .py file to create. If not given
then the className will be used.
"""
import os
if moduleName is None:
moduleName = className + '.py'
filename = os.path.join(modulePath, moduleName)
if verbose:
print "Creating module in:", filename
print " ProgID: ", ax.GetCLSID().GetProgIDString()
print " CLSID: ", ax.GetCLSID().GetCLSIDString()
print
self.mf = file(filename, "w")
self.WriteFileHeader(ax)
self.WriteEvents(ax)
self.WriteClassHeader(ax, className)
self.WriteMethods(ax)
self.WriteProperties(ax)
self.WriteDocs(ax)
self.mf.close()
del self.mf
def WriteFileHeader(self, ax):
self.write("# This module was generated by the wx.activex.GernerateAXModule class\n"
"# (See also the genaxmodule script.)\n")
self.write("import wx")
self.write("import wx.activex\n")
self.write("clsID = '%s'\nprogID = '%s'\n"
% (ax.GetCLSID().GetCLSIDString(), ax.GetCLSID().GetProgIDString()))
self.write("\n")
def WriteEvents(self, ax):
events = ax.GetAXEvents()
if events:
self.write("# Create eventTypes and event binders")
for e in events:
self.write("wxEVT_%s = wx.activex.RegisterActiveXEvent('%s')"
% (self.trimEventName(e.name), e.name))
self.write()
for e in events:
n = self.trimEventName(e.name)
self.write("EVT_%s = wx.PyEventBinder(wxEVT_%s, 1)" % (n,n))
self.write("\n")
def WriteClassHeader(self, ax, className):
self.write("# Derive a new class from ActiveXWindow")
self.write("""\
class %s(wx.activex.ActiveXWindow):
def __init__(self, parent, ID=-1, pos=wx.DefaultPosition,
size=wx.DefaultSize, style=0, name='%s'):
wx.activex.ActiveXWindow.__init__(self, parent,
wx.activex.CLSID('%s'),
ID, pos, size, style, name)
""" % (className, className, ax.GetCLSID().GetCLSIDString()) )
def WriteMethods(self, ax):
methods = ax.GetAXMethods()
if methods:
self.write(t4, "# Methods exported by the ActiveX object")
for m in methods:
name = self.trimMethodName(m.name)
self.write(t4, "def %s(self%s):" % (name, self.getParameters(m, True)))
self.write(t8, "return self.CallAXMethod('%s'%s)" % (m.name, self.getParameters(m, False)))
self.write()
def WriteProperties(self, ax):
props = ax.GetAXProperties()
if props:
self.write(t4, "# Getters, Setters and properties")
for p in props:
getterName = setterName = "None"
if p.canGet:
getterName = "_get_" + p.name
self.write(t4, "def %s(self):" % getterName)
self.write(t8, "return self.GetAXProp('%s')" % p.name)
if p.canSet:
setterName = "_set_" + p.name
self.write(t4, "def %s(self, %s):" % (setterName, p.arg.name))
self.write(t8, "self.SetAXProp('%s', %s)" % (p.name, p.arg.name))
self.write(t4, "%s = property(%s, %s)" %
(self.trimPropName(p.name), getterName, setterName))
self.write()
def WriteDocs(self, ax):
self.write()
doc = GetAXInfo(ax)
for line in doc.split('\n'):
self.write("# ", line)
def write(self, *args):
for a in args:
self.mf.write(a)
self.mf.write("\n")
def trimEventName(name):
if name.startswith("On"):
name = name[2:]
return name
trimEventName = staticmethod(trimEventName)
def trimPropName(name):
#name = name[0].lower() + name[1:]
name = name.lower()
import keyword
if name in keyword.kwlist: name += '_'
return name
trimPropName = staticmethod(trimPropName)
def trimMethodName(name):
import keyword
if name in keyword.kwlist: name += '_'
return name
trimMethodName = staticmethod(trimMethodName)
def getParameters(self, m, withDefaults):
import keyword
st = ""
# collect the input parameters, if both isIn and isOut are
# False then assume it is an input paramater
params = []
for p in m.params:
if p.isIn or (not p.isIn and not p.isOut):
params.append(p)
# did we get any?
for p in params:
name = p.name
if name in keyword.kwlist: name += '_'
st += ", "
st += name
if withDefaults and p.isOptional:
st += '=None'
return st
#---------------------------------------------------------------------------
|