  | | | Python introspection... | Python introspection... 2005-04-06 - By Jerry Gamache
Back Once you know the function name, you sometimes want to get some very terse documentation:
def FormatDocumentation(typeInfo, funDesc): nameAndParms = typeInfo.GetNames(funDesc.memid) docum = typeInfo.GetDocumentation(funDesc.memid) import pythoncom # This one differentiates between "functions" and "accessors" if funDesc.invkind == pythoncom.INVOKE_FUNC: docStr = nameAndParms[0] + "(" + ",".join(nameAndParms[1:]) + ")\n" else: docStr = nameAndParms[0] + "\n" if docum[1]: docStr += "\t" + docum[1] return docStr
def GetDocumentation( dynDisp, funcName = None ): allTypeInfoDoc = [] for iTI in xrange(0,dynDisp._oleobj_.GetTypeInfoCount()): typeInfo = dynDisp._oleobj_.GetTypeInfo(iTI) typeAttr = typeInfo.GetTypeAttr() if funcName: for iFun in xrange(0,typeAttr.cFuncs): funDesc = typeInfo.GetFuncDesc(iFun) name = typeInfo.GetNames(funDesc.memid)[0] if name.upper() == funcName.upper(): return FormatDocumentation(typeInfo, funDesc) else: dict = {} className = dynDisp._oleobj_.GetTypeInfo(0).GetDocumentation(-1) classDoc = "class %s \n\t%s\n\n"%(className[0],className[1]) allFuncDoc = [] for iFun in xrange(0,typeAttr.cFuncs): funDesc = typeInfo.GetFuncDesc(iFun) name = typeInfo.GetNames(funDesc.memid)[0] if not dict.has_key(name): dict[name] = 1 allFuncDoc.append(FormatDocumentation(typeInfo, funDesc)) allFuncDoc.sort() allTypeInfoDoc.append(classDoc + "\n".join(allFuncDoc)) if funcName: return "Documentation not found for %s"%(funcName,) else: return "\n".join(allTypeInfoDoc)
# 2 ways to call this one: without a function name it retrieves doc for all functions Application.LogMessage(GetDocumentation(Application)) # And with a name, it tries to find the doc for that function: Application.LogMessage(GetDocumentation(Application, "StatusBar")) Application.LogMessage(GetDocumentation(Application, "NotThere"))
-- --Original Message-- -- From: owner-xsi@(protected) [mailto:owner-xsi@(protected)]On Behalf Of kim aldis Posted At: Tuesday, April 05, 2005 5:06 PM Posted To: xsi Conversation: Python introspection... Subject: RE: Python introspection...
That was fun. Even more fun when you start running on a selection. Thanks Jerry.
> -- --Original Message-- -- > From: owner-xsi@(protected) > [mailto:owner-xsi@(protected)] On Behalf Of Jerry Gamache > Sent: 05 April 2005 21:26 > To: XSI@(protected) > Subject: Python introspection... > > Ever wondered what function an XSI object exposes at runtime? > > def GetFunctions( dynDisp ): > """returns a sorted and unique list of all functions > defined in a dynamic dispatch""" > dict = {} > try: > for iTI in > xrange(0,dynDisp._oleobj_.GetTypeInfoCount()): > typeInfo = dynDisp._oleobj_.GetTypeInfo(iTI) > typeAttr = typeInfo.GetTypeAttr() > for iFun in xrange(0,typeAttr.cFuncs): > funDesc = typeInfo.GetFuncDesc(iFun) > name = > typeInfo.GetNames(funDesc.memid)[0] > dict[name] = 1 > except: > pass # Object is not the dynamic dispatch I knew > ret = dict.keys() > ret.sort() > return ret > > import pprint > > funcs = GetFunctions(Application) > Application.LogMessage(pprint.pformat(funcs)) > > funcs = GetFunctions(Application.ActiveSceneRoot) > Application.LogMessage(pprint.pformat(funcs)) > > --- > Unsubscribe? Mail Majordomo@(protected) with the following > text in body: > unsubscribe xsi > >
--- Unsubscribe? Mail Majordomo@(protected) with the following text in body: unsubscribe xsi
--- Unsubscribe? Mail Majordomo@(protected) with the following text in body: unsubscribe xsi
|
|
 |