home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- import sys
- import string
- import types
- from keyword import iskeyword
- from win32com.client import NeedUnicodeConversions
- import pythoncom
- from pywintypes import UnicodeType, TimeType
- import winerror
-
- def _safeQuotedString(s):
- if s[-1] == '"':
- s = s[:-1] + '\\"'
-
- return '"""%s"""' % s
-
- error = 'PythonCOM.Client.Build error'
-
- class NotSupportedException(Exception):
- pass
-
- DropIndirection = 'DropIndirection'
- NoTranslateTypes = [
- pythoncom.VT_BOOL,
- pythoncom.VT_CLSID,
- pythoncom.VT_CY,
- pythoncom.VT_DATE,
- pythoncom.VT_DECIMAL,
- pythoncom.VT_EMPTY,
- pythoncom.VT_ERROR,
- pythoncom.VT_FILETIME,
- pythoncom.VT_HRESULT,
- pythoncom.VT_I1,
- pythoncom.VT_I2,
- pythoncom.VT_I4,
- pythoncom.VT_I8,
- pythoncom.VT_INT,
- pythoncom.VT_NULL,
- pythoncom.VT_R4,
- pythoncom.VT_R8,
- pythoncom.VT_NULL,
- pythoncom.VT_STREAM,
- pythoncom.VT_UI1,
- pythoncom.VT_UI2,
- pythoncom.VT_UI4,
- pythoncom.VT_UI8,
- pythoncom.VT_UINT,
- pythoncom.VT_VOID]
- NoTranslateMap = { }
- for v in NoTranslateTypes:
- NoTranslateMap[v] = None
-
-
- class MapEntry:
-
- def __init__(self, desc_or_id, names = None, doc = None, resultCLSID = pythoncom.IID_NULL, resultDoc = None, hidden = 0):
- if type(desc_or_id) == type(0):
- self.dispid = desc_or_id
- self.desc = None
- else:
- self.dispid = desc_or_id[0]
- self.desc = desc_or_id
- self.names = names
- self.doc = doc
- self.resultCLSID = resultCLSID
- self.resultDocumentation = resultDoc
- self.wasProperty = 0
- self.hidden = hidden
-
-
- def GetResultCLSID(self):
- rc = self.resultCLSID
- if rc == pythoncom.IID_NULL:
- return None
- return rc
-
-
- def GetResultCLSIDStr(self):
- rc = self.GetResultCLSID()
- if rc is None:
- return 'None'
- return repr(str(rc))
-
-
- def GetResultName(self):
- if self.resultDocumentation is None:
- return None
- return self.resultDocumentation[0]
-
-
-
- class OleItem:
- typename = 'OleItem'
-
- def __init__(self, doc = None):
- self.doc = doc
- if self.doc:
- self.python_name = MakePublicAttributeName(self.doc[0])
- else:
- self.python_name = None
- self.bWritten = 0
- self.bIsDispatch = 0
- self.bIsSink = 0
- self.clsid = None
- self.co_class = None
-
-
-
- class DispatchItem(OleItem):
- typename = 'DispatchItem'
-
- def __init__(self, typeinfo = None, attr = None, doc = None, bForUser = 1):
- OleItem.__init__(self, doc)
- self.propMap = { }
- self.propMapGet = { }
- self.propMapPut = { }
- self.mapFuncs = { }
- self.defaultDispatchName = None
- self.hidden = 0
- if typeinfo:
- self.Build(typeinfo, attr, bForUser)
-
-
-
- def _propMapPutCheck_(self, key, item):
- (ins, outs, opts) = self.CountInOutOptArgs(item.desc[2])
- if ins > 1:
- if opts + 1 == ins or ins == item.desc[6] + 1:
- newKey = 'Set' + key
- deleteExisting = 0
- else:
- deleteExisting = 1
- if self.mapFuncs.has_key(key) or self.propMapGet.has_key(key):
- newKey = 'Set' + key
- else:
- newKey = key
- item.wasProperty = 1
- self.mapFuncs[newKey] = item
- if deleteExisting:
- del self.propMapPut[key]
-
-
-
-
- def _propMapGetCheck_(self, key, item):
- (ins, outs, opts) = self.CountInOutOptArgs(item.desc[2])
- if ins > 0:
- if item.desc[6] == ins or ins == opts:
- newKey = 'Get' + key
- deleteExisting = 0
- else:
- deleteExisting = 1
- if self.mapFuncs.has_key(key):
- newKey = 'Get' + key
- else:
- newKey = key
- item.wasProperty = 1
- self.mapFuncs[newKey] = item
- if deleteExisting:
- del self.propMapGet[key]
-
-
-
-
- def _AddFunc_(self, typeinfo, fdesc, bForUser):
- id = fdesc.memid
- funcflags = fdesc.wFuncFlags
-
- try:
- names = typeinfo.GetNames(id)
- name = names[0]
- except pythoncom.ole_error:
- name = ''
- names = None
-
- doc = None
-
- try:
- if bForUser:
- doc = typeinfo.GetDocumentation(id)
- except pythoncom.ole_error:
- pass
-
- if id == 0 and name:
- self.defaultDispatchName = name
-
- invkind = fdesc.invkind
- (typerepr, flag, defval) = fdesc.rettype
- (typerepr, resultCLSID, resultDoc) = _ResolveType(typerepr, typeinfo)
- fdesc.rettype = (typerepr, flag, defval, resultCLSID)
- argList = []
- for argDesc in fdesc.args:
- (typerepr, flag, defval) = argDesc
- (arg_type, arg_clsid, arg_doc) = _ResolveType(typerepr, typeinfo)
- argDesc = (arg_type, flag, defval, arg_clsid)
- argList.append(argDesc)
-
- fdesc.args = tuple(argList)
- hidden = funcflags & pythoncom.FUNCFLAG_FHIDDEN != 0
- if invkind == pythoncom.INVOKE_PROPERTYGET:
- map = self.propMapGet
- elif invkind in (pythoncom.INVOKE_PROPERTYPUT, pythoncom.INVOKE_PROPERTYPUTREF):
- existing = self.propMapPut.get(name, None)
- if existing is not None:
- if existing.desc[4] == pythoncom.INVOKE_PROPERTYPUT:
- map = self.mapFuncs
- name = 'Set' + name
- else:
- existing.wasProperty = 1
- self.mapFuncs['Set' + name] = existing
- map = self.propMapPut
- else:
- map = self.propMapPut
- elif invkind == pythoncom.INVOKE_FUNC:
- map = self.mapFuncs
- else:
- map = None
- if map is not None:
- map[name] = MapEntry(tuple(fdesc), names, doc, resultCLSID, resultDoc, hidden)
- if fdesc.funckind != pythoncom.FUNC_DISPATCH:
- return None
- return (name, map)
-
-
- def _AddVar_(self, typeinfo, fdesc, bForUser):
- if fdesc.varkind == pythoncom.VAR_DISPATCH:
- id = fdesc.memid
- names = typeinfo.GetNames(id)
- (typerepr, flags, defval) = fdesc.elemdescVar
- (typerepr, resultCLSID, resultDoc) = _ResolveType(typerepr, typeinfo)
- fdesc.elemdescVar = (typerepr, flags, defval)
- doc = None
-
- try:
- if bForUser:
- doc = typeinfo.GetDocumentation(id)
- except pythoncom.ole_error:
- pass
-
- map = self.propMap
- hidden = 0
- if hasattr(fdesc, 'wVarFlags'):
- hidden = fdesc.wVarFlags & 64 != 0
-
- map[names[0]] = MapEntry(tuple(fdesc), names, doc, resultCLSID, resultDoc, hidden)
- return (names[0], map)
- return None
-
-
- def Build(self, typeinfo, attr, bForUser = 1):
- self.clsid = attr[0]
- self.bIsDispatch = attr.wTypeFlags & pythoncom.TYPEFLAG_FDISPATCHABLE != 0
- if typeinfo is None:
- return None
- for j in xrange(attr[6]):
- fdesc = typeinfo.GetFuncDesc(j)
- self._AddFunc_(typeinfo, fdesc, bForUser)
-
- for j in xrange(attr[7]):
- fdesc = typeinfo.GetVarDesc(j)
- self._AddVar_(typeinfo, fdesc, bForUser)
-
- for key, item in self.propMapGet.items():
- self._propMapGetCheck_(key, item)
-
- for key, item in self.propMapPut.items():
- self._propMapPutCheck_(key, item)
-
-
-
- def CountInOutOptArgs(self, argTuple):
- ins = out = opts = 0
- for argCheck in argTuple:
- inOut = argCheck[1]
- if inOut == 0:
- ins = ins + 1
- out = out + 1
- continue
- if inOut & pythoncom.PARAMFLAG_FIN:
- ins = ins + 1
-
- if inOut & pythoncom.PARAMFLAG_FOPT:
- opts = opts + 1
-
- if inOut & pythoncom.PARAMFLAG_FOUT:
- out = out + 1
- continue
-
- return (ins, out, opts)
-
-
- def MakeFuncMethod(self, entry, name, bMakeClass = 1):
- if entry.desc is not None:
- if len(entry.desc) < 6 or entry.desc[6] != -1:
- return self.MakeDispatchFuncMethod(entry, name, bMakeClass)
- return self.MakeVarArgsFuncMethod(entry, name, bMakeClass)
- return entry.desc is not None
-
-
- def MakeDispatchFuncMethod(self, entry, name, bMakeClass = 1):
- fdesc = entry.desc
- doc = entry.doc
- names = entry.names
- ret = []
- if bMakeClass:
- linePrefix = '\t'
- defNamedOptArg = 'defaultNamedOptArg'
- defNamedNotOptArg = 'defaultNamedNotOptArg'
- defUnnamedArg = 'defaultUnnamedArg'
- else:
- linePrefix = ''
- defNamedOptArg = 'pythoncom.Missing'
- defNamedNotOptArg = 'pythoncom.Missing'
- defUnnamedArg = 'pythoncom.Missing'
- defOutArg = 'pythoncom.Missing'
- id = fdesc[0]
- s = linePrefix + 'def ' + name + '(self' + BuildCallList(fdesc, names, defNamedOptArg, defNamedNotOptArg, defUnnamedArg, defOutArg) + '):'
- ret.append(s)
- if doc and doc[1]:
- ret.append(linePrefix + '\t' + _safeQuotedString(doc[1]))
-
- resclsid = entry.GetResultCLSID()
- if resclsid:
- resclsid = "'%s'" % resclsid
- else:
- resclsid = 'None'
- retDesc = fdesc[8][:2]
- argsDesc = tuple(map((lambda what: what[:2]), fdesc[2]))
- param_flags = map((lambda what: what[1]), fdesc[2])
- bad_params = filter((lambda flag: flag & (pythoncom.PARAMFLAG_FOUT | pythoncom.PARAMFLAG_FRETVAL) != 0), param_flags)
- s = None
- if len(bad_params) == 0 and len(retDesc) == 2 and retDesc[1] == 0:
- rd = retDesc[0]
- if NoTranslateMap.has_key(rd):
- s = '%s\treturn self._oleobj_.InvokeTypes(%d, LCID, %s, %s, %s%s)' % (linePrefix, id, fdesc[4], retDesc, argsDesc, _BuildArgList(fdesc, names))
- elif rd in [
- pythoncom.VT_DISPATCH,
- pythoncom.VT_UNKNOWN]:
- s = '%s\tret = self._oleobj_.InvokeTypes(%d, LCID, %s, %s, %s%s)\n' % (linePrefix, id, fdesc[4], retDesc, `argsDesc`, _BuildArgList(fdesc, names))
- s = s + '%s\tif ret is not None:\n' % (linePrefix,)
- if rd == pythoncom.VT_UNKNOWN:
- s = s + '%s\t\t# See if this IUnknown is really an IDispatch\n' % (linePrefix,)
- s = s + '%s\t\ttry:\n' % (linePrefix,)
- s = s + '%s\t\t\tret = ret.QueryInterface(pythoncom.IID_IDispatch)\n' % (linePrefix,)
- s = s + '%s\t\texcept pythoncom.error:\n' % (linePrefix,)
- s = s + '%s\t\t\treturn ret\n' % (linePrefix,)
-
- s = s + '%s\t\tret = Dispatch(ret, %s, %s, UnicodeToString=%d)\n' % (linePrefix, `name`, resclsid, NeedUnicodeConversions)
- s = s + '%s\treturn ret' % linePrefix
- elif rd == pythoncom.VT_BSTR:
- if NeedUnicodeConversions:
- s = '%s\t# Result is a Unicode object - perform automatic string conversion\n' % (linePrefix,)
- s = s + '%s\treturn str(self._oleobj_.InvokeTypes(%d, LCID, %s, %s, %s%s))' % (linePrefix, id, fdesc[4], retDesc, `argsDesc`, _BuildArgList(fdesc, names))
- else:
- s = '%s\t# Result is a Unicode object - return as-is for this version of Python\n' % (linePrefix,)
- s = s + '%s\treturn self._oleobj_.InvokeTypes(%d, LCID, %s, %s, %s%s)' % (linePrefix, id, fdesc[4], retDesc, `argsDesc`, _BuildArgList(fdesc, names))
-
-
- if s is None:
- s = '%s\treturn self._ApplyTypes_(%d, %s, %s, %s, %s, %s%s)' % (linePrefix, id, fdesc[4], retDesc, argsDesc, `name`, resclsid, _BuildArgList(fdesc, names))
-
- ret.append(s)
- ret.append('')
- return ret
-
-
- def MakeVarArgsFuncMethod(self, entry, name, bMakeClass = 1):
- fdesc = entry.desc
- names = entry.names
- doc = entry.doc
- ret = []
- argPrefix = 'self'
- if bMakeClass:
- linePrefix = '\t'
- else:
- linePrefix = ''
- ret.append(linePrefix + 'def ' + name + '(' + argPrefix + ', *args):')
- if doc and doc[1]:
- ret.append(linePrefix + '\t' + _safeQuotedString(doc[1]))
-
- if fdesc:
- invoketype = fdesc[4]
- else:
- invoketype = pythoncom.DISPATCH_METHOD
- s = linePrefix + '\treturn self._get_good_object_(self._oleobj_.Invoke(*(('
- ret.append(s + str(entry.dispid) + ",0,%d,1)+args)),'%s')" % (invoketype, names[0]))
- ret.append('')
- return ret
-
-
-
- class VTableItem(DispatchItem):
-
- def Build(self, typeinfo, attr, bForUser = 1):
- DispatchItem.Build(self, typeinfo, attr, bForUser)
-
- def cmp_vtable_off(m1, m2):
- return cmp(m1.desc[7], m2.desc[7])
-
- meth_list = self.mapFuncs.values() + self.propMapGet.values() + self.propMapPut.values()
- meth_list.sort(cmp_vtable_off)
- self.vtableFuncs = []
- for entry in meth_list:
- self.vtableFuncs.append((entry.names, entry.dispid, entry.desc))
-
-
-
-
- class LazyDispatchItem(DispatchItem):
- typename = 'LazyDispatchItem'
-
- def __init__(self, attr, doc):
- self.clsid = attr[0]
- DispatchItem.__init__(self, None, attr, doc, 0)
-
-
- typeSubstMap = {
- pythoncom.VT_INT: pythoncom.VT_I4,
- pythoncom.VT_UINT: pythoncom.VT_I4,
- pythoncom.VT_HRESULT: pythoncom.VT_I4 }
-
- def _ResolveType(typerepr, itypeinfo):
- return (typeSubstMap.get(typerepr, typerepr), None, None)
-
-
- def _BuildArgList(fdesc, names):
- numArgs = max(fdesc[6], len(fdesc[2]))
- names = list(names)
- while None in names:
- i = names.index(None)
- names[i] = 'arg%d' % (i,)
- names = map(MakePublicAttributeName, names[1:])
- name_num = 0
- while len(names) < numArgs:
- names.append('arg%d' % (len(names),))
- for i in range(0, len(names), 5):
- names[i] = names[i] + '\n\t\t\t'
-
- return ',' + string.join(names, ', ')
-
- valid_identifier_chars = string.ascii_letters + string.digits + '_'
-
- def demunge_leading_underscores(className):
- i = 0
- while className[i] == '_':
- i += 1
- return className[i - 1:] + className[:i - 1]
-
-
- def MakePublicAttributeName(className, is_global = False):
- if className[:2] == '__':
- return demunge_leading_underscores(className)
- if iskeyword(className):
- return string.capitalize(className)
- if className == 'None':
- className = 'NONE'
- elif is_global and __builtins__.has_key(className):
- ret = className.capitalize()
- if ret == className:
- ret = ret.upper()
-
- return ret
- return filter((lambda char: char in valid_identifier_chars), className)
-
-
- def MakeDefaultArgRepr(defArgVal):
-
- try:
- inOut = defArgVal[1]
- except IndexError:
- inOut = pythoncom.PARAMFLAG_FIN
-
- if inOut & pythoncom.PARAMFLAG_FHASDEFAULT:
- val = defArgVal[2]
- if type(val) is UnicodeType:
- return repr(str(val))
- if type(val) is TimeType:
- year = val.year
- month = val.month
- day = val.day
- hour = val.hour
- minute = val.minute
- second = val.second
- msec = val.msec
- return 'pythoncom.MakeTime((%(year)d, %(month)d, %(day)d, %(hour)d, %(minute)d, %(second)d,0,0,0,%(msec)d))' % locals()
- return repr(val)
- inOut & pythoncom.PARAMFLAG_FHASDEFAULT
-
-
- def BuildCallList(fdesc, names, defNamedOptArg, defNamedNotOptArg, defUnnamedArg, defOutArg, is_comment = False):
- numArgs = len(fdesc[2])
- numOptArgs = fdesc[6]
- strval = ''
- if numOptArgs == -1:
- firstOptArg = numArgs
- numArgs = numArgs - 1
- else:
- firstOptArg = numArgs - numOptArgs
- for arg in xrange(numArgs):
-
- try:
- argName = names[arg + 1]
- namedArg = argName is not None
- except IndexError:
- namedArg = 0
-
- if not namedArg:
- argName = 'arg%d' % arg
-
- thisdesc = fdesc[2][arg]
- defArgVal = MakeDefaultArgRepr(thisdesc)
- if defArgVal is None:
- if thisdesc[1] & (pythoncom.PARAMFLAG_FOUT | pythoncom.PARAMFLAG_FIN) == pythoncom.PARAMFLAG_FOUT:
- defArgVal = defOutArg
- elif namedArg:
- if arg >= firstOptArg:
- defArgVal = defNamedOptArg
- else:
- defArgVal = defNamedNotOptArg
- else:
- defArgVal = defUnnamedArg
-
- argName = MakePublicAttributeName(argName)
- if (arg + 1) % 5 == 0:
- strval = strval + '\n'
- if is_comment:
- strval = strval + '#'
-
- strval = strval + '\t\t\t'
-
- strval = strval + ', ' + argName
- if defArgVal:
- strval = strval + '=' + defArgVal
- continue
-
- if numOptArgs == -1:
- strval = strval + ', *' + names[-1]
-
- return strval
-
- if __name__ == '__main__':
- print "Use 'makepy.py' to generate Python code - this module is just a helper"
-
-