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