build.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. """Contains knowledge to build a COM object definition.
  2. This module is used by both the @dynamic@ and @makepy@ modules to build
  3. all knowledge of a COM object.
  4. This module contains classes which contain the actual knowledge of the object.
  5. This include parameter and return type information, the COM dispid and CLSID, etc.
  6. Other modules may use this information to generate .py files, use the information
  7. dynamically, or possibly even generate .html documentation for objects.
  8. """
  9. #
  10. # NOTES: DispatchItem and MapEntry used by dynamic.py.
  11. # the rest is used by makepy.py
  12. #
  13. # OleItem, DispatchItem, MapEntry, BuildCallList() is used by makepy
  14. import sys
  15. import string
  16. from keyword import iskeyword
  17. import pythoncom
  18. from pywintypes import TimeType
  19. import winerror
  20. import datetime
  21. # It isn't really clear what the quoting rules are in a C/IDL string and
  22. # literals like a quote char and backslashes makes life a little painful to
  23. # always render the string perfectly - so just punt and fall-back to a repr()
  24. def _makeDocString(s):
  25. if sys.version_info < (3,):
  26. s = s.encode("mbcs")
  27. return repr(s)
  28. error = "PythonCOM.Client.Build error"
  29. class NotSupportedException(Exception): pass # Raised when we cant support a param type.
  30. DropIndirection="DropIndirection"
  31. NoTranslateTypes = [
  32. pythoncom.VT_BOOL, pythoncom.VT_CLSID, pythoncom.VT_CY,
  33. pythoncom.VT_DATE, pythoncom.VT_DECIMAL, pythoncom.VT_EMPTY,
  34. pythoncom.VT_ERROR, pythoncom.VT_FILETIME, pythoncom.VT_HRESULT,
  35. pythoncom.VT_I1, pythoncom.VT_I2, pythoncom.VT_I4,
  36. pythoncom.VT_I8, pythoncom.VT_INT, pythoncom.VT_NULL,
  37. pythoncom.VT_R4, pythoncom.VT_R8, pythoncom.VT_NULL,
  38. pythoncom.VT_STREAM,
  39. pythoncom.VT_UI1, pythoncom.VT_UI2, pythoncom.VT_UI4,
  40. pythoncom.VT_UI8, pythoncom.VT_UINT, pythoncom.VT_VOID,
  41. ]
  42. NoTranslateMap = {}
  43. for v in NoTranslateTypes:
  44. NoTranslateMap[v] = None
  45. class MapEntry:
  46. "Simple holder for named attibutes - items in a map."
  47. def __init__(self, desc_or_id, names=None, doc=None, resultCLSID=pythoncom.IID_NULL, resultDoc = None, hidden=0):
  48. if type(desc_or_id)==type(0):
  49. self.dispid = desc_or_id
  50. self.desc = None
  51. else:
  52. self.dispid = desc_or_id[0]
  53. self.desc = desc_or_id
  54. self.names = names
  55. self.doc = doc
  56. self.resultCLSID = resultCLSID
  57. self.resultDocumentation = resultDoc
  58. self.wasProperty = 0 # Have I been transformed into a function so I can pass args?
  59. self.hidden = hidden
  60. def GetResultCLSID(self):
  61. rc = self.resultCLSID
  62. if rc == pythoncom.IID_NULL: return None
  63. return rc
  64. # Return a string, suitable for output - either "'{...}'" or "None"
  65. def GetResultCLSIDStr(self):
  66. rc = self.GetResultCLSID()
  67. if rc is None: return "None"
  68. return repr(str(rc)) # Convert the IID object to a string, then to a string in a string.
  69. def GetResultName(self):
  70. if self.resultDocumentation is None:
  71. return None
  72. return self.resultDocumentation[0]
  73. class OleItem:
  74. typename = "OleItem"
  75. def __init__(self, doc=None):
  76. self.doc = doc
  77. if self.doc:
  78. self.python_name = MakePublicAttributeName(self.doc[0])
  79. else:
  80. self.python_name = None
  81. self.bWritten = 0
  82. self.bIsDispatch = 0
  83. self.bIsSink = 0
  84. self.clsid = None
  85. self.co_class = None
  86. class DispatchItem(OleItem):
  87. typename = "DispatchItem"
  88. def __init__(self, typeinfo=None, attr=None, doc=None, bForUser=1):
  89. OleItem.__init__(self,doc)
  90. self.propMap = {}
  91. self.propMapGet = {}
  92. self.propMapPut = {}
  93. self.mapFuncs = {}
  94. self.defaultDispatchName = None
  95. self.hidden = 0
  96. if typeinfo:
  97. self.Build(typeinfo, attr, bForUser)
  98. def _propMapPutCheck_(self,key,item):
  99. ins, outs, opts = self.CountInOutOptArgs(item.desc[2])
  100. if ins>1: # if a Put property takes more than 1 arg:
  101. if opts+1==ins or ins==item.desc[6]+1:
  102. newKey = "Set" + key
  103. deleteExisting = 0 # This one is still OK
  104. else:
  105. deleteExisting = 1 # No good to us
  106. if key in self.mapFuncs or key in self.propMapGet:
  107. newKey = "Set" + key
  108. else:
  109. newKey = key
  110. item.wasProperty = 1
  111. self.mapFuncs[newKey] = item
  112. if deleteExisting:
  113. del self.propMapPut[key]
  114. def _propMapGetCheck_(self,key,item):
  115. ins, outs, opts = self.CountInOutOptArgs(item.desc[2])
  116. if ins > 0: # if a Get property takes _any_ in args:
  117. if item.desc[6]==ins or ins==opts:
  118. newKey = "Get" + key
  119. deleteExisting = 0 # This one is still OK
  120. else:
  121. deleteExisting = 1 # No good to us
  122. if key in self.mapFuncs:
  123. newKey = "Get" + key
  124. else:
  125. newKey = key
  126. item.wasProperty = 1
  127. self.mapFuncs[newKey] = item
  128. if deleteExisting:
  129. del self.propMapGet[key]
  130. def _AddFunc_(self,typeinfo,fdesc,bForUser):
  131. id = fdesc.memid
  132. funcflags = fdesc.wFuncFlags
  133. try:
  134. names = typeinfo.GetNames(id)
  135. name=names[0]
  136. except pythoncom.ole_error:
  137. name = ""
  138. names = None
  139. doc = None
  140. try:
  141. if bForUser:
  142. doc = typeinfo.GetDocumentation(id)
  143. except pythoncom.ole_error:
  144. pass
  145. if id==0 and name:
  146. self.defaultDispatchName = name
  147. invkind = fdesc.invkind
  148. # We need to translate any Alias', Enums, structs etc in result and args
  149. typerepr, flag, defval = fdesc.rettype
  150. # sys.stderr.write("%s result - %s -> " % (name, typerepr))
  151. typerepr, resultCLSID, resultDoc = _ResolveType(typerepr, typeinfo)
  152. # sys.stderr.write("%s\n" % (typerepr,))
  153. fdesc.rettype = typerepr, flag, defval, resultCLSID
  154. # Translate any Alias or Enums in argument list.
  155. argList = []
  156. for argDesc in fdesc.args:
  157. typerepr, flag, defval = argDesc
  158. # sys.stderr.write("%s arg - %s -> " % (name, typerepr))
  159. arg_type, arg_clsid, arg_doc = _ResolveType(typerepr, typeinfo)
  160. argDesc = arg_type, flag, defval, arg_clsid
  161. # sys.stderr.write("%s\n" % (argDesc[0],))
  162. argList.append(argDesc)
  163. fdesc.args = tuple(argList)
  164. hidden = (funcflags & pythoncom.FUNCFLAG_FHIDDEN) != 0
  165. if invkind == pythoncom.INVOKE_PROPERTYGET:
  166. map = self.propMapGet
  167. # This is not the best solution, but I dont think there is
  168. # one without specific "set" syntax.
  169. # If there is a single PUT or PUTREF, it will function as a property.
  170. # If there are both, then the PUT remains a property, and the PUTREF
  171. # gets transformed into a function.
  172. # (in vb, PUT=="obj=other_obj", PUTREF="set obj=other_obj
  173. elif invkind in (pythoncom.INVOKE_PROPERTYPUT, pythoncom.INVOKE_PROPERTYPUTREF):
  174. # Special case
  175. existing = self.propMapPut.get(name, None)
  176. if existing is not None:
  177. if existing.desc[4]==pythoncom.INVOKE_PROPERTYPUT: # Keep this one
  178. map = self.mapFuncs
  179. name = "Set"+name
  180. else: # Existing becomes a func.
  181. existing.wasProperty = 1
  182. self.mapFuncs["Set"+name]=existing
  183. map = self.propMapPut # existing gets overwritten below.
  184. else:
  185. map = self.propMapPut # first time weve seen it.
  186. elif invkind == pythoncom.INVOKE_FUNC:
  187. map = self.mapFuncs
  188. else:
  189. map = None
  190. if not map is None:
  191. # if map.has_key(name):
  192. # sys.stderr.write("Warning - overwriting existing method/attribute %s\n" % name)
  193. map[name] = MapEntry(tuple(fdesc), names, doc, resultCLSID, resultDoc, hidden)
  194. # any methods that can't be reached via DISPATCH we return None
  195. # for, so dynamic dispatch doesnt see it.
  196. if fdesc.funckind != pythoncom.FUNC_DISPATCH:
  197. return None
  198. return (name,map)
  199. return None
  200. def _AddVar_(self,typeinfo,fdesc,bForUser):
  201. ### need pythoncom.VARFLAG_FRESTRICTED ...
  202. ### then check it
  203. if fdesc.varkind == pythoncom.VAR_DISPATCH:
  204. id = fdesc.memid
  205. names = typeinfo.GetNames(id)
  206. # Translate any Alias or Enums in result.
  207. typerepr, flags, defval = fdesc.elemdescVar
  208. typerepr, resultCLSID, resultDoc = _ResolveType(typerepr, typeinfo)
  209. fdesc.elemdescVar = typerepr, flags, defval
  210. doc = None
  211. try:
  212. if bForUser: doc = typeinfo.GetDocumentation(id)
  213. except pythoncom.ole_error:
  214. pass
  215. # handle the enumerator specially
  216. map = self.propMap
  217. # Check if the element is hidden.
  218. hidden = 0
  219. if hasattr(fdesc,"wVarFlags"):
  220. hidden = (fdesc.wVarFlags & 0x40) != 0 # VARFLAG_FHIDDEN
  221. map[names[0]] = MapEntry(tuple(fdesc), names, doc, resultCLSID, resultDoc, hidden)
  222. return (names[0],map)
  223. else:
  224. return None
  225. def Build(self, typeinfo, attr, bForUser = 1):
  226. self.clsid = attr[0]
  227. self.bIsDispatch = (attr.wTypeFlags & pythoncom.TYPEFLAG_FDISPATCHABLE) != 0
  228. if typeinfo is None: return
  229. # Loop over all methods
  230. for j in range(attr[6]):
  231. fdesc = typeinfo.GetFuncDesc(j)
  232. self._AddFunc_(typeinfo,fdesc,bForUser)
  233. # Loop over all variables (ie, properties)
  234. for j in range(attr[7]):
  235. fdesc = typeinfo.GetVarDesc(j)
  236. self._AddVar_(typeinfo,fdesc,bForUser)
  237. # Now post-process the maps. For any "Get" or "Set" properties
  238. # that have arguments, we must turn them into methods. If a method
  239. # of the same name already exists, change the name.
  240. for key, item in list(self.propMapGet.items()):
  241. self._propMapGetCheck_(key,item)
  242. for key, item in list(self.propMapPut.items()):
  243. self._propMapPutCheck_(key,item)
  244. def CountInOutOptArgs(self, argTuple):
  245. "Return tuple counting in/outs/OPTS. Sum of result may not be len(argTuple), as some args may be in/out."
  246. ins = out = opts = 0
  247. for argCheck in argTuple:
  248. inOut = argCheck[1]
  249. if inOut==0:
  250. ins = ins + 1
  251. out = out + 1
  252. else:
  253. if inOut & pythoncom.PARAMFLAG_FIN:
  254. ins = ins + 1
  255. if inOut & pythoncom.PARAMFLAG_FOPT:
  256. opts = opts + 1
  257. if inOut & pythoncom.PARAMFLAG_FOUT:
  258. out = out + 1
  259. return ins, out, opts
  260. def MakeFuncMethod(self, entry, name, bMakeClass = 1):
  261. # If we have a type description, and not varargs...
  262. if entry.desc is not None and (len(entry.desc) < 6 or entry.desc[6]!=-1):
  263. return self.MakeDispatchFuncMethod(entry, name, bMakeClass)
  264. else:
  265. return self.MakeVarArgsFuncMethod(entry, name, bMakeClass)
  266. def MakeDispatchFuncMethod(self, entry, name, bMakeClass = 1):
  267. fdesc = entry.desc
  268. doc = entry.doc
  269. names = entry.names
  270. ret = []
  271. if bMakeClass:
  272. linePrefix = "\t"
  273. defNamedOptArg = "defaultNamedOptArg"
  274. defNamedNotOptArg = "defaultNamedNotOptArg"
  275. defUnnamedArg = "defaultUnnamedArg"
  276. else:
  277. linePrefix = ""
  278. defNamedOptArg = "pythoncom.Missing"
  279. defNamedNotOptArg = "pythoncom.Missing"
  280. defUnnamedArg = "pythoncom.Missing"
  281. defOutArg = "pythoncom.Missing"
  282. id = fdesc[0]
  283. s = linePrefix + 'def ' + name + '(self' + BuildCallList(fdesc, names, defNamedOptArg, defNamedNotOptArg, defUnnamedArg, defOutArg) + '):'
  284. ret.append(s)
  285. if doc and doc[1]:
  286. ret.append(linePrefix + '\t' + _makeDocString(doc[1]))
  287. # print "fdesc is ", fdesc
  288. resclsid = entry.GetResultCLSID()
  289. if resclsid:
  290. resclsid = "'%s'" % resclsid
  291. else:
  292. resclsid = 'None'
  293. # Strip the default values from the arg desc
  294. retDesc = fdesc[8][:2]
  295. argsDesc = tuple([what[:2] for what in fdesc[2]])
  296. # The runtime translation of the return types is expensive, so when we know the
  297. # return type of the function, there is no need to check the type at runtime.
  298. # To qualify, this function must return a "simple" type, and have no byref args.
  299. # Check if we have byrefs or anything in the args which mean we still need a translate.
  300. param_flags = [what[1] for what in fdesc[2]]
  301. bad_params = [flag for flag in param_flags if flag & (pythoncom.PARAMFLAG_FOUT | pythoncom.PARAMFLAG_FRETVAL)!=0]
  302. s = None
  303. if len(bad_params)==0 and len(retDesc)==2 and retDesc[1]==0:
  304. rd = retDesc[0]
  305. if rd in NoTranslateMap:
  306. s = '%s\treturn self._oleobj_.InvokeTypes(%d, LCID, %s, %s, %s%s)' % (linePrefix, id, fdesc[4], retDesc, argsDesc, _BuildArgList(fdesc, names))
  307. elif rd in [pythoncom.VT_DISPATCH, pythoncom.VT_UNKNOWN]:
  308. s = '%s\tret = self._oleobj_.InvokeTypes(%d, LCID, %s, %s, %s%s)\n' % (linePrefix, id, fdesc[4], retDesc, repr(argsDesc), _BuildArgList(fdesc, names))
  309. s = s + '%s\tif ret is not None:\n' % (linePrefix,)
  310. if rd == pythoncom.VT_UNKNOWN:
  311. s = s + "%s\t\t# See if this IUnknown is really an IDispatch\n" % (linePrefix,)
  312. s = s + "%s\t\ttry:\n" % (linePrefix,)
  313. s = s + "%s\t\t\tret = ret.QueryInterface(pythoncom.IID_IDispatch)\n" % (linePrefix,)
  314. s = s + "%s\t\texcept pythoncom.error:\n" % (linePrefix,)
  315. s = s + "%s\t\t\treturn ret\n" % (linePrefix,)
  316. s = s + '%s\t\tret = Dispatch(ret, %s, %s)\n' % (linePrefix,repr(name), resclsid)
  317. s = s + '%s\treturn ret' % (linePrefix)
  318. elif rd == pythoncom.VT_BSTR:
  319. s = "%s\t# Result is a Unicode object\n" % (linePrefix,)
  320. s = s + '%s\treturn self._oleobj_.InvokeTypes(%d, LCID, %s, %s, %s%s)' % (linePrefix, id, fdesc[4], retDesc, repr(argsDesc), _BuildArgList(fdesc, names))
  321. # else s remains None
  322. if s is None:
  323. s = '%s\treturn self._ApplyTypes_(%d, %s, %s, %s, %s, %s%s)' % (linePrefix, id, fdesc[4], retDesc, argsDesc, repr(name), resclsid, _BuildArgList(fdesc, names))
  324. ret.append(s)
  325. ret.append("")
  326. return ret
  327. def MakeVarArgsFuncMethod(self, entry, name, bMakeClass = 1):
  328. fdesc = entry.desc
  329. names = entry.names
  330. doc = entry.doc
  331. ret = []
  332. argPrefix = "self"
  333. if bMakeClass:
  334. linePrefix = "\t"
  335. else:
  336. linePrefix = ""
  337. ret.append(linePrefix + 'def ' + name + '(' + argPrefix + ', *args):')
  338. if doc and doc[1]: ret.append(linePrefix + '\t' + _makeDocString(doc[1]))
  339. if fdesc:
  340. invoketype = fdesc[4]
  341. else:
  342. invoketype = pythoncom.DISPATCH_METHOD
  343. s = linePrefix + '\treturn self._get_good_object_(self._oleobj_.Invoke(*(('
  344. ret.append(s + str(entry.dispid) + ",0,%d,1)+args)),'%s')" % (invoketype, names[0]))
  345. ret.append("")
  346. return ret
  347. # Note - "DispatchItem" poorly named - need a new intermediate class.
  348. class VTableItem(DispatchItem):
  349. def Build(self, typeinfo, attr, bForUser = 1):
  350. DispatchItem.Build(self, typeinfo, attr, bForUser)
  351. assert typeinfo is not None, "Cant build vtables without type info!"
  352. meth_list = list(self.mapFuncs.values()) + list(self.propMapGet.values()) + list(self.propMapPut.values())
  353. meth_list.sort(key=lambda m: m.desc[7])
  354. # Now turn this list into the run-time representation
  355. # (ready for immediate use or writing to gencache)
  356. self.vtableFuncs = []
  357. for entry in meth_list:
  358. self.vtableFuncs.append( (entry.names, entry.dispid, entry.desc) )
  359. # A Lazy dispatch item - builds an item on request using info from
  360. # an ITypeComp. The dynamic module makes the called to build each item,
  361. # and also holds the references to the typeinfo and typecomp.
  362. class LazyDispatchItem(DispatchItem):
  363. typename = "LazyDispatchItem"
  364. def __init__(self, attr, doc):
  365. self.clsid = attr[0]
  366. DispatchItem.__init__(self, None, attr, doc, 0)
  367. typeSubstMap = {
  368. pythoncom.VT_INT: pythoncom.VT_I4,
  369. pythoncom.VT_UINT: pythoncom.VT_UI4,
  370. pythoncom.VT_HRESULT: pythoncom.VT_I4,
  371. }
  372. def _ResolveType(typerepr, itypeinfo):
  373. # Resolve VT_USERDEFINED (often aliases or typed IDispatches)
  374. if type(typerepr)==tuple:
  375. indir_vt, subrepr = typerepr
  376. if indir_vt == pythoncom.VT_PTR:
  377. # If it is a VT_PTR to a VT_USERDEFINED that is an IDispatch/IUnknown,
  378. # then it resolves to simply the object.
  379. # Otherwise, it becomes a ByRef of the resolved type
  380. # We need to drop an indirection level on pointer to user defined interfaces.
  381. # eg, (VT_PTR, (VT_USERDEFINED, somehandle)) needs to become VT_DISPATCH
  382. # only when "somehandle" is an object.
  383. # but (VT_PTR, (VT_USERDEFINED, otherhandle)) doesnt get the indirection dropped.
  384. was_user = type(subrepr)==tuple and subrepr[0]==pythoncom.VT_USERDEFINED
  385. subrepr, sub_clsid, sub_doc = _ResolveType(subrepr, itypeinfo)
  386. if was_user and subrepr in [pythoncom.VT_DISPATCH, pythoncom.VT_UNKNOWN, pythoncom.VT_RECORD]:
  387. # Drop the VT_PTR indirection
  388. return subrepr, sub_clsid, sub_doc
  389. # Change PTR indirection to byref
  390. return subrepr | pythoncom.VT_BYREF, sub_clsid, sub_doc
  391. if indir_vt == pythoncom.VT_SAFEARRAY:
  392. # resolve the array element, and convert to VT_ARRAY
  393. subrepr, sub_clsid, sub_doc = _ResolveType(subrepr, itypeinfo)
  394. return pythoncom.VT_ARRAY | subrepr, sub_clsid, sub_doc
  395. if indir_vt == pythoncom.VT_CARRAY: # runtime has no support for this yet.
  396. # resolve the array element, and convert to VT_CARRAY
  397. # sheesh - return _something_
  398. return pythoncom.VT_CARRAY, None, None
  399. if indir_vt == pythoncom.VT_USERDEFINED:
  400. try:
  401. resultTypeInfo = itypeinfo.GetRefTypeInfo(subrepr)
  402. except pythoncom.com_error, details:
  403. if details.hresult in [winerror.TYPE_E_CANTLOADLIBRARY, winerror.TYPE_E_LIBNOTREGISTERED]:
  404. # an unregistered interface
  405. return pythoncom.VT_UNKNOWN, None, None
  406. raise
  407. resultAttr = resultTypeInfo.GetTypeAttr()
  408. typeKind = resultAttr.typekind
  409. if typeKind == pythoncom.TKIND_ALIAS:
  410. tdesc = resultAttr.tdescAlias
  411. return _ResolveType(tdesc, resultTypeInfo)
  412. elif typeKind in [pythoncom.TKIND_ENUM, pythoncom.TKIND_MODULE]:
  413. # For now, assume Long
  414. return pythoncom.VT_I4, None, None
  415. elif typeKind == pythoncom.TKIND_DISPATCH:
  416. clsid = resultTypeInfo.GetTypeAttr()[0]
  417. retdoc = resultTypeInfo.GetDocumentation(-1)
  418. return pythoncom.VT_DISPATCH, clsid, retdoc
  419. elif typeKind in [pythoncom.TKIND_INTERFACE,
  420. pythoncom.TKIND_COCLASS]:
  421. # XXX - should probably get default interface for CO_CLASS???
  422. clsid = resultTypeInfo.GetTypeAttr()[0]
  423. retdoc = resultTypeInfo.GetDocumentation(-1)
  424. return pythoncom.VT_UNKNOWN, clsid, retdoc
  425. elif typeKind == pythoncom.TKIND_RECORD:
  426. return pythoncom.VT_RECORD, None, None
  427. raise NotSupportedException("Can not resolve alias or user-defined type")
  428. return typeSubstMap.get(typerepr,typerepr), None, None
  429. def _BuildArgList(fdesc, names):
  430. "Builds list of args to the underlying Invoke method."
  431. # Word has TypeInfo for Insert() method, but says "no args"
  432. numArgs = max(fdesc[6], len(fdesc[2]))
  433. names = list(names)
  434. while None in names:
  435. i = names.index(None)
  436. names[i] = "arg%d" % (i,)
  437. # We've seen 'source safe' libraries offer the name of 'ret' params in
  438. # 'names' - although we can't reproduce this, it would be insane to offer
  439. # more args than we have arg infos for - hence the upper limit on names...
  440. names = list(map(MakePublicAttributeName, names[1:(numArgs + 1)]))
  441. name_num = 0
  442. while len(names) < numArgs:
  443. names.append("arg%d" % (len(names),))
  444. # As per BuildCallList(), avoid huge lines.
  445. # Hack a "\n" at the end of every 5th name - "strides" would be handy
  446. # here but don't exist in 2.2
  447. for i in range(0, len(names), 5):
  448. names[i] = names[i] + "\n\t\t\t"
  449. return "," + ", ".join(names)
  450. valid_identifier_chars = string.ascii_letters + string.digits + "_"
  451. def demunge_leading_underscores(className):
  452. i = 0
  453. while className[i] == "_":
  454. i += 1
  455. assert i >= 2, "Should only be here with names starting with '__'"
  456. return className[i-1:] + className[:i-1]
  457. # Given a "public name" (eg, the name of a class, function, etc)
  458. # make sure it is a legal (and reasonable!) Python name.
  459. def MakePublicAttributeName(className, is_global = False):
  460. # Given a class attribute that needs to be public, convert it to a
  461. # reasonable name.
  462. # Also need to be careful that the munging doesnt
  463. # create duplicates - eg, just removing a leading "_" is likely to cause
  464. # a clash.
  465. # if is_global is True, then the name is a global variable that may
  466. # overwrite a builtin - eg, "None"
  467. if className[:2]=='__':
  468. return demunge_leading_underscores(className)
  469. elif className == 'None':
  470. # assign to None is evil (and SyntaxError in 2.4, even though
  471. # iskeyword says False there) - note that if it was a global
  472. # it would get picked up below
  473. className = 'NONE'
  474. elif iskeyword(className):
  475. # most keywords are lower case (except True, False etc in py3k)
  476. ret = className.capitalize()
  477. # but those which aren't get forced upper.
  478. if ret == className:
  479. ret = ret.upper()
  480. return ret
  481. elif is_global and hasattr(__builtins__, className):
  482. # builtins may be mixed case. If capitalizing it doesn't change it,
  483. # force to all uppercase (eg, "None", "True" become "NONE", "TRUE"
  484. ret = className.capitalize()
  485. if ret==className: # didn't change - force all uppercase.
  486. ret = ret.upper()
  487. return ret
  488. # Strip non printable chars
  489. return ''.join([char for char in className if char in valid_identifier_chars])
  490. # Given a default value passed by a type library, return a string with
  491. # an appropriate repr() for the type.
  492. # Takes a raw ELEMDESC and returns a repr string, or None
  493. # (NOTE: The string itself may be '"None"', which is valid, and different to None.
  494. # XXX - To do: Dates are probably screwed, but can they come in?
  495. def MakeDefaultArgRepr(defArgVal):
  496. try:
  497. inOut = defArgVal[1]
  498. except IndexError:
  499. # something strange - assume is in param.
  500. inOut = pythoncom.PARAMFLAG_FIN
  501. if inOut & pythoncom.PARAMFLAG_FHASDEFAULT:
  502. # times need special handling...
  503. val = defArgVal[2]
  504. if isinstance(val, datetime.datetime):
  505. # VARIANT <-> SYSTEMTIME conversions always lose any sub-second
  506. # resolution, so just use a 'timetuple' here.
  507. return repr(tuple(val.utctimetuple()))
  508. if type(val) is TimeType:
  509. # must be the 'old' pywintypes time object...
  510. year=val.year; month=val.month; day=val.day; hour=val.hour; minute=val.minute; second=val.second; msec=val.msec
  511. return "pywintypes.Time((%(year)d, %(month)d, %(day)d, %(hour)d, %(minute)d, %(second)d,0,0,0,%(msec)d))" % locals()
  512. return repr(val)
  513. return None
  514. def BuildCallList(fdesc, names, defNamedOptArg, defNamedNotOptArg, defUnnamedArg, defOutArg, is_comment = False):
  515. "Builds a Python declaration for a method."
  516. # Names[0] is the func name - param names are from 1.
  517. numArgs = len(fdesc[2])
  518. numOptArgs = fdesc[6]
  519. strval = ''
  520. if numOptArgs==-1: # Special value that says "var args after here"
  521. firstOptArg = numArgs
  522. numArgs = numArgs - 1
  523. else:
  524. firstOptArg = numArgs - numOptArgs
  525. for arg in xrange(numArgs):
  526. try:
  527. argName = names[arg+1]
  528. namedArg = argName is not None
  529. except IndexError:
  530. namedArg = 0
  531. if not namedArg: argName = "arg%d" % (arg)
  532. thisdesc = fdesc[2][arg]
  533. # See if the IDL specified a default value
  534. defArgVal = MakeDefaultArgRepr(thisdesc)
  535. if defArgVal is None:
  536. # Out params always get their special default
  537. if thisdesc[1] & (pythoncom.PARAMFLAG_FOUT | pythoncom.PARAMFLAG_FIN) == pythoncom.PARAMFLAG_FOUT:
  538. defArgVal = defOutArg
  539. else:
  540. # Unnamed arg - always allow default values.
  541. if namedArg:
  542. # Is a named argument
  543. if arg >= firstOptArg:
  544. defArgVal = defNamedOptArg
  545. else:
  546. defArgVal = defNamedNotOptArg
  547. else:
  548. defArgVal = defUnnamedArg
  549. argName = MakePublicAttributeName(argName)
  550. # insanely long lines with an 'encoding' flag crashes python 2.4.0
  551. # keep 5 args per line
  552. # This may still fail if the arg names are insane, but that seems
  553. # unlikely. See also _BuildArgList()
  554. if (arg+1) % 5 == 0:
  555. strval = strval + "\n"
  556. if is_comment:
  557. strval = strval + "#"
  558. strval = strval + "\t\t\t"
  559. strval = strval + ", " + argName
  560. if defArgVal:
  561. strval = strval + "=" + defArgVal
  562. if numOptArgs==-1:
  563. strval = strval + ", *" + names[-1]
  564. return strval
  565. if __name__=='__main__':
  566. print "Use 'makepy.py' to generate Python code - this module is just a helper"