def GetTypeLibsForSpec(arg): """Given an argument on the command line (either a file name, library description, or ProgID of an object) return a list of actual typelibs to use. """ typelibs = [] try: try: tlb = pythoncom.LoadTypeLib(arg) spec = selecttlb.TypelibSpec(None, 0,0,0) spec.FromTypelib(tlb, arg) typelibs.append((tlb, spec)) except pythoncom.com_error: # See if it is a description tlbs = selecttlb.FindTlbsWithDescription(arg) if len(tlbs)==0: # Maybe it is the name of a COM object? try: ob = Dispatch(arg) # and if so, it must support typelib info tlb, index = ob._oleobj_.GetTypeInfo().GetContainingTypeLib() spec = selecttlb.TypelibSpec(None, 0,0,0) spec.FromTypelib(tlb) tlbs.append(spec) except pythoncom.com_error: pass if len(tlbs)==0: print "Could not locate a type library matching '%s'" % (arg) for spec in tlbs: # Version numbers not always reliable if enumerated from registry. # (as some libs use hex, other's dont. Both examples from MS, of course.) if spec.dll is None: tlb = pythoncom.LoadRegTypeLib(spec.clsid, spec.major, spec.minor, spec.lcid) else: tlb = pythoncom.LoadTypeLib(spec.dll) # We have a typelib, but it may not be exactly what we specified # (due to automatic version matching of COM). So we query what we really have! attr = tlb.GetLibAttr() spec.major = attr[3] spec.minor = attr[4] spec.lcid = attr[1] typelibs.append((tlb, spec)) return typelibs except pythoncom.com_error: t,v,tb=sys.exc_info() sys.stderr.write ("Unable to load type library from '%s' - %s\n" % (arg, v)) tb = None # Storing tb in a local is a cycle! sys.exit(1)
def GenerateChildFromTypeLibSpec(child, typelibInfo, verboseLevel = None, progressInstance = None, bUnicodeToString=NeedUnicodeConversions): if verboseLevel is None: verboseLevel = 0 # By default, we use no gui, and no verbose level for the children. if type(typelibInfo)==type(()): typelibCLSID, lcid, major, minor = typelibInfo tlb = pythoncom.LoadRegTypeLib(typelibCLSID, major, minor, lcid) else: tlb = typelibInfo tla = typelibInfo.GetLibAttr() typelibCLSID = tla[0] lcid = tla[1] major = tla[3] minor = tla[4] spec = selecttlb.TypelibSpec(typelibCLSID, lcid, major, minor) spec.FromTypelib(tlb, str(typelibCLSID)) typelibs = [(tlb, spec)] if progressInstance is None: progressInstance = SimpleProgress(verboseLevel) progress = progressInstance for typelib, info in typelibs: dir_name = gencache.GetGeneratedFileName(info.clsid, info.lcid, info.major, info.minor) dir_path_name = os.path.join(gencache.GetGeneratePath(), dir_name) progress.LogBeginGenerate(dir_path_name) gen = genpy.Generator(typelib, info.dll, progress, bUnicodeToString=bUnicodeToString) gen.generate_child(child, dir_path_name) progress.SetDescription("Importing module") __import__("win32com.gen_py." + dir_name + "." + child) progress.Close()
def GenerateFromTypeLibSpec(typelibInfo, file = None, verboseLevel = None, progressInstance = None, bUnicodeToString=NeedUnicodeConversions, bForDemand = bForDemandDefault, bBuildHidden = 1): if verboseLevel is None: verboseLevel = 0 # By default, we use no gui and no verbose level! if bForDemand and file is not None: raise RuntimeError, "You can only perform a demand-build when the output goes to the gen_py directory" if type(typelibInfo)==type(()): # Tuple typelibCLSID, lcid, major, minor = typelibInfo tlb = pythoncom.LoadRegTypeLib(typelibCLSID, major, minor, lcid) spec = selecttlb.TypelibSpec(typelibCLSID, lcid, major, minor) spec.FromTypelib(tlb, str(typelibCLSID)) typelibs = [(tlb, spec)] elif type(typelibInfo)==types.InstanceType: if typelibInfo.dll is None: # Version numbers not always reliable if enumerated from registry. tlb = pythoncom.LoadRegTypeLib(typelibInfo.clsid, typelibInfo.major, typelibInfo.minor, typelibInfo.lcid) else: tlb = pythoncom.LoadTypeLib(typelibInfo.dll) typelibs = [(tlb, typelibInfo)] elif hasattr(typelibInfo, "GetLibAttr"): # A real typelib object! tla = typelibInfo.GetLibAttr() guid = tla[0] lcid = tla[1] major = tla[3] minor = tla[4] spec = selecttlb.TypelibSpec(guid, lcid, major, minor) typelibs = [(typelibInfo, spec)] else: typelibs = GetTypeLibsForSpec(typelibInfo) if progressInstance is None: progressInstance = SimpleProgress(verboseLevel) progress = progressInstance bToGenDir = (file is None) for typelib, info in typelibs: if file is None: this_name = gencache.GetGeneratedFileName(info.clsid, info.lcid, info.major, info.minor) full_name = os.path.join(gencache.GetGeneratePath(), this_name) if bForDemand: try: os.unlink(full_name + ".py") except os.error: pass try: os.unlink(full_name + ".pyc") except os.error: pass try: os.unlink(full_name + ".pyo") except os.error: pass if not os.path.isdir(full_name): os.mkdir(full_name) outputName = os.path.join(full_name, "__init__.py") else: outputName = full_name + ".py" # generate to a temp file (so errors don't leave a 1/2 # generated file) and one which can handle unicode! try: os.unlink(outputName) except os.error: pass encoding = 'mbcs' # could make this a param. fileUse = codecs.open(outputName + ".temp", "wt", encoding) progress.LogBeginGenerate(outputName) else: fileUse = file gen = genpy.Generator(typelib, info.dll, progress, bUnicodeToString=bUnicodeToString, bBuildHidden=bBuildHidden) gen.generate(fileUse, bForDemand) if file is None: fileUse.close() os.rename(outputName + ".temp", outputName) if bToGenDir: progress.SetDescription("Importing module") gencache.AddModuleToCache(info.clsid, info.lcid, info.major, info.minor) progress.Close()