home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_2637 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  17.8 KB  |  546 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import sys
  5. import string
  6. import types
  7. from keyword import iskeyword
  8. from win32com.client import NeedUnicodeConversions
  9. import pythoncom
  10. from pywintypes import UnicodeType, TimeType
  11. import winerror
  12.  
  13. def _safeQuotedString(s):
  14.     if s[-1] == '"':
  15.         s = s[:-1] + '\\"'
  16.     
  17.     return '"""%s"""' % s
  18.  
  19. error = 'PythonCOM.Client.Build error'
  20.  
  21. class NotSupportedException(Exception):
  22.     pass
  23.  
  24. DropIndirection = 'DropIndirection'
  25. NoTranslateTypes = [
  26.     pythoncom.VT_BOOL,
  27.     pythoncom.VT_CLSID,
  28.     pythoncom.VT_CY,
  29.     pythoncom.VT_DATE,
  30.     pythoncom.VT_DECIMAL,
  31.     pythoncom.VT_EMPTY,
  32.     pythoncom.VT_ERROR,
  33.     pythoncom.VT_FILETIME,
  34.     pythoncom.VT_HRESULT,
  35.     pythoncom.VT_I1,
  36.     pythoncom.VT_I2,
  37.     pythoncom.VT_I4,
  38.     pythoncom.VT_I8,
  39.     pythoncom.VT_INT,
  40.     pythoncom.VT_NULL,
  41.     pythoncom.VT_R4,
  42.     pythoncom.VT_R8,
  43.     pythoncom.VT_NULL,
  44.     pythoncom.VT_STREAM,
  45.     pythoncom.VT_UI1,
  46.     pythoncom.VT_UI2,
  47.     pythoncom.VT_UI4,
  48.     pythoncom.VT_UI8,
  49.     pythoncom.VT_UINT,
  50.     pythoncom.VT_VOID]
  51. NoTranslateMap = { }
  52. for v in NoTranslateTypes:
  53.     NoTranslateMap[v] = None
  54.  
  55.  
  56. class MapEntry:
  57.     
  58.     def __init__(self, desc_or_id, names = None, doc = None, resultCLSID = pythoncom.IID_NULL, resultDoc = None, hidden = 0):
  59.         if type(desc_or_id) == type(0):
  60.             self.dispid = desc_or_id
  61.             self.desc = None
  62.         else:
  63.             self.dispid = desc_or_id[0]
  64.             self.desc = desc_or_id
  65.         self.names = names
  66.         self.doc = doc
  67.         self.resultCLSID = resultCLSID
  68.         self.resultDocumentation = resultDoc
  69.         self.wasProperty = 0
  70.         self.hidden = hidden
  71.  
  72.     
  73.     def GetResultCLSID(self):
  74.         rc = self.resultCLSID
  75.         if rc == pythoncom.IID_NULL:
  76.             return None
  77.         return rc
  78.  
  79.     
  80.     def GetResultCLSIDStr(self):
  81.         rc = self.GetResultCLSID()
  82.         if rc is None:
  83.             return 'None'
  84.         return repr(str(rc))
  85.  
  86.     
  87.     def GetResultName(self):
  88.         if self.resultDocumentation is None:
  89.             return None
  90.         return self.resultDocumentation[0]
  91.  
  92.  
  93.  
  94. class OleItem:
  95.     typename = 'OleItem'
  96.     
  97.     def __init__(self, doc = None):
  98.         self.doc = doc
  99.         if self.doc:
  100.             self.python_name = MakePublicAttributeName(self.doc[0])
  101.         else:
  102.             self.python_name = None
  103.         self.bWritten = 0
  104.         self.bIsDispatch = 0
  105.         self.bIsSink = 0
  106.         self.clsid = None
  107.         self.co_class = None
  108.  
  109.  
  110.  
  111. class DispatchItem(OleItem):
  112.     typename = 'DispatchItem'
  113.     
  114.     def __init__(self, typeinfo = None, attr = None, doc = None, bForUser = 1):
  115.         OleItem.__init__(self, doc)
  116.         self.propMap = { }
  117.         self.propMapGet = { }
  118.         self.propMapPut = { }
  119.         self.mapFuncs = { }
  120.         self.defaultDispatchName = None
  121.         self.hidden = 0
  122.         if typeinfo:
  123.             self.Build(typeinfo, attr, bForUser)
  124.         
  125.  
  126.     
  127.     def _propMapPutCheck_(self, key, item):
  128.         (ins, outs, opts) = self.CountInOutOptArgs(item.desc[2])
  129.         if ins > 1:
  130.             if opts + 1 == ins or ins == item.desc[6] + 1:
  131.                 newKey = 'Set' + key
  132.                 deleteExisting = 0
  133.             else:
  134.                 deleteExisting = 1
  135.                 if self.mapFuncs.has_key(key) or self.propMapGet.has_key(key):
  136.                     newKey = 'Set' + key
  137.                 else:
  138.                     newKey = key
  139.             item.wasProperty = 1
  140.             self.mapFuncs[newKey] = item
  141.             if deleteExisting:
  142.                 del self.propMapPut[key]
  143.             
  144.         
  145.  
  146.     
  147.     def _propMapGetCheck_(self, key, item):
  148.         (ins, outs, opts) = self.CountInOutOptArgs(item.desc[2])
  149.         if ins > 0:
  150.             if item.desc[6] == ins or ins == opts:
  151.                 newKey = 'Get' + key
  152.                 deleteExisting = 0
  153.             else:
  154.                 deleteExisting = 1
  155.                 if self.mapFuncs.has_key(key):
  156.                     newKey = 'Get' + key
  157.                 else:
  158.                     newKey = key
  159.             item.wasProperty = 1
  160.             self.mapFuncs[newKey] = item
  161.             if deleteExisting:
  162.                 del self.propMapGet[key]
  163.             
  164.         
  165.  
  166.     
  167.     def _AddFunc_(self, typeinfo, fdesc, bForUser):
  168.         id = fdesc.memid
  169.         funcflags = fdesc.wFuncFlags
  170.         
  171.         try:
  172.             names = typeinfo.GetNames(id)
  173.             name = names[0]
  174.         except pythoncom.ole_error:
  175.             name = ''
  176.             names = None
  177.  
  178.         doc = None
  179.         
  180.         try:
  181.             if bForUser:
  182.                 doc = typeinfo.GetDocumentation(id)
  183.         except pythoncom.ole_error:
  184.             pass
  185.  
  186.         if id == 0 and name:
  187.             self.defaultDispatchName = name
  188.         
  189.         invkind = fdesc.invkind
  190.         (typerepr, flag, defval) = fdesc.rettype
  191.         (typerepr, resultCLSID, resultDoc) = _ResolveType(typerepr, typeinfo)
  192.         fdesc.rettype = (typerepr, flag, defval, resultCLSID)
  193.         argList = []
  194.         for argDesc in fdesc.args:
  195.             (typerepr, flag, defval) = argDesc
  196.             (arg_type, arg_clsid, arg_doc) = _ResolveType(typerepr, typeinfo)
  197.             argDesc = (arg_type, flag, defval, arg_clsid)
  198.             argList.append(argDesc)
  199.         
  200.         fdesc.args = tuple(argList)
  201.         hidden = funcflags & pythoncom.FUNCFLAG_FHIDDEN != 0
  202.         if invkind == pythoncom.INVOKE_PROPERTYGET:
  203.             map = self.propMapGet
  204.         elif invkind in (pythoncom.INVOKE_PROPERTYPUT, pythoncom.INVOKE_PROPERTYPUTREF):
  205.             existing = self.propMapPut.get(name, None)
  206.             if existing is not None:
  207.                 if existing.desc[4] == pythoncom.INVOKE_PROPERTYPUT:
  208.                     map = self.mapFuncs
  209.                     name = 'Set' + name
  210.                 else:
  211.                     existing.wasProperty = 1
  212.                     self.mapFuncs['Set' + name] = existing
  213.                     map = self.propMapPut
  214.             else:
  215.                 map = self.propMapPut
  216.         elif invkind == pythoncom.INVOKE_FUNC:
  217.             map = self.mapFuncs
  218.         else:
  219.             map = None
  220.         if map is not None:
  221.             map[name] = MapEntry(tuple(fdesc), names, doc, resultCLSID, resultDoc, hidden)
  222.             if fdesc.funckind != pythoncom.FUNC_DISPATCH:
  223.                 return None
  224.             return (name, map)
  225.  
  226.     
  227.     def _AddVar_(self, typeinfo, fdesc, bForUser):
  228.         if fdesc.varkind == pythoncom.VAR_DISPATCH:
  229.             id = fdesc.memid
  230.             names = typeinfo.GetNames(id)
  231.             (typerepr, flags, defval) = fdesc.elemdescVar
  232.             (typerepr, resultCLSID, resultDoc) = _ResolveType(typerepr, typeinfo)
  233.             fdesc.elemdescVar = (typerepr, flags, defval)
  234.             doc = None
  235.             
  236.             try:
  237.                 if bForUser:
  238.                     doc = typeinfo.GetDocumentation(id)
  239.             except pythoncom.ole_error:
  240.                 pass
  241.  
  242.             map = self.propMap
  243.             hidden = 0
  244.             if hasattr(fdesc, 'wVarFlags'):
  245.                 hidden = fdesc.wVarFlags & 64 != 0
  246.             
  247.             map[names[0]] = MapEntry(tuple(fdesc), names, doc, resultCLSID, resultDoc, hidden)
  248.             return (names[0], map)
  249.         return None
  250.  
  251.     
  252.     def Build(self, typeinfo, attr, bForUser = 1):
  253.         self.clsid = attr[0]
  254.         self.bIsDispatch = attr.wTypeFlags & pythoncom.TYPEFLAG_FDISPATCHABLE != 0
  255.         if typeinfo is None:
  256.             return None
  257.         for j in xrange(attr[6]):
  258.             fdesc = typeinfo.GetFuncDesc(j)
  259.             self._AddFunc_(typeinfo, fdesc, bForUser)
  260.         
  261.         for j in xrange(attr[7]):
  262.             fdesc = typeinfo.GetVarDesc(j)
  263.             self._AddVar_(typeinfo, fdesc, bForUser)
  264.         
  265.         for key, item in self.propMapGet.items():
  266.             self._propMapGetCheck_(key, item)
  267.         
  268.         for key, item in self.propMapPut.items():
  269.             self._propMapPutCheck_(key, item)
  270.         
  271.  
  272.     
  273.     def CountInOutOptArgs(self, argTuple):
  274.         ins = out = opts = 0
  275.         for argCheck in argTuple:
  276.             inOut = argCheck[1]
  277.             if inOut == 0:
  278.                 ins = ins + 1
  279.                 out = out + 1
  280.                 continue
  281.             if inOut & pythoncom.PARAMFLAG_FIN:
  282.                 ins = ins + 1
  283.             
  284.             if inOut & pythoncom.PARAMFLAG_FOPT:
  285.                 opts = opts + 1
  286.             
  287.             if inOut & pythoncom.PARAMFLAG_FOUT:
  288.                 out = out + 1
  289.                 continue
  290.         
  291.         return (ins, out, opts)
  292.  
  293.     
  294.     def MakeFuncMethod(self, entry, name, bMakeClass = 1):
  295.         if entry.desc is not None:
  296.             if len(entry.desc) < 6 or entry.desc[6] != -1:
  297.                 return self.MakeDispatchFuncMethod(entry, name, bMakeClass)
  298.             return self.MakeVarArgsFuncMethod(entry, name, bMakeClass)
  299.         return entry.desc is not None
  300.  
  301.     
  302.     def MakeDispatchFuncMethod(self, entry, name, bMakeClass = 1):
  303.         fdesc = entry.desc
  304.         doc = entry.doc
  305.         names = entry.names
  306.         ret = []
  307.         if bMakeClass:
  308.             linePrefix = '\t'
  309.             defNamedOptArg = 'defaultNamedOptArg'
  310.             defNamedNotOptArg = 'defaultNamedNotOptArg'
  311.             defUnnamedArg = 'defaultUnnamedArg'
  312.         else:
  313.             linePrefix = ''
  314.             defNamedOptArg = 'pythoncom.Missing'
  315.             defNamedNotOptArg = 'pythoncom.Missing'
  316.             defUnnamedArg = 'pythoncom.Missing'
  317.         defOutArg = 'pythoncom.Missing'
  318.         id = fdesc[0]
  319.         s = linePrefix + 'def ' + name + '(self' + BuildCallList(fdesc, names, defNamedOptArg, defNamedNotOptArg, defUnnamedArg, defOutArg) + '):'
  320.         ret.append(s)
  321.         if doc and doc[1]:
  322.             ret.append(linePrefix + '\t' + _safeQuotedString(doc[1]))
  323.         
  324.         resclsid = entry.GetResultCLSID()
  325.         if resclsid:
  326.             resclsid = "'%s'" % resclsid
  327.         else:
  328.             resclsid = 'None'
  329.         retDesc = fdesc[8][:2]
  330.         argsDesc = tuple(map((lambda what: what[:2]), fdesc[2]))
  331.         param_flags = map((lambda what: what[1]), fdesc[2])
  332.         bad_params = filter((lambda flag: flag & (pythoncom.PARAMFLAG_FOUT | pythoncom.PARAMFLAG_FRETVAL) != 0), param_flags)
  333.         s = None
  334.         if len(bad_params) == 0 and len(retDesc) == 2 and retDesc[1] == 0:
  335.             rd = retDesc[0]
  336.             if NoTranslateMap.has_key(rd):
  337.                 s = '%s\treturn self._oleobj_.InvokeTypes(%d, LCID, %s, %s, %s%s)' % (linePrefix, id, fdesc[4], retDesc, argsDesc, _BuildArgList(fdesc, names))
  338.             elif rd in [
  339.                 pythoncom.VT_DISPATCH,
  340.                 pythoncom.VT_UNKNOWN]:
  341.                 s = '%s\tret = self._oleobj_.InvokeTypes(%d, LCID, %s, %s, %s%s)\n' % (linePrefix, id, fdesc[4], retDesc, `argsDesc`, _BuildArgList(fdesc, names))
  342.                 s = s + '%s\tif ret is not None:\n' % (linePrefix,)
  343.                 if rd == pythoncom.VT_UNKNOWN:
  344.                     s = s + '%s\t\t# See if this IUnknown is really an IDispatch\n' % (linePrefix,)
  345.                     s = s + '%s\t\ttry:\n' % (linePrefix,)
  346.                     s = s + '%s\t\t\tret = ret.QueryInterface(pythoncom.IID_IDispatch)\n' % (linePrefix,)
  347.                     s = s + '%s\t\texcept pythoncom.error:\n' % (linePrefix,)
  348.                     s = s + '%s\t\t\treturn ret\n' % (linePrefix,)
  349.                 
  350.                 s = s + '%s\t\tret = Dispatch(ret, %s, %s, UnicodeToString=%d)\n' % (linePrefix, `name`, resclsid, NeedUnicodeConversions)
  351.                 s = s + '%s\treturn ret' % linePrefix
  352.             elif rd == pythoncom.VT_BSTR:
  353.                 if NeedUnicodeConversions:
  354.                     s = '%s\t# Result is a Unicode object - perform automatic string conversion\n' % (linePrefix,)
  355.                     s = s + '%s\treturn str(self._oleobj_.InvokeTypes(%d, LCID, %s, %s, %s%s))' % (linePrefix, id, fdesc[4], retDesc, `argsDesc`, _BuildArgList(fdesc, names))
  356.                 else:
  357.                     s = '%s\t# Result is a Unicode object - return as-is for this version of Python\n' % (linePrefix,)
  358.                     s = s + '%s\treturn self._oleobj_.InvokeTypes(%d, LCID, %s, %s, %s%s)' % (linePrefix, id, fdesc[4], retDesc, `argsDesc`, _BuildArgList(fdesc, names))
  359.             
  360.         
  361.         if s is None:
  362.             s = '%s\treturn self._ApplyTypes_(%d, %s, %s, %s, %s, %s%s)' % (linePrefix, id, fdesc[4], retDesc, argsDesc, `name`, resclsid, _BuildArgList(fdesc, names))
  363.         
  364.         ret.append(s)
  365.         ret.append('')
  366.         return ret
  367.  
  368.     
  369.     def MakeVarArgsFuncMethod(self, entry, name, bMakeClass = 1):
  370.         fdesc = entry.desc
  371.         names = entry.names
  372.         doc = entry.doc
  373.         ret = []
  374.         argPrefix = 'self'
  375.         if bMakeClass:
  376.             linePrefix = '\t'
  377.         else:
  378.             linePrefix = ''
  379.         ret.append(linePrefix + 'def ' + name + '(' + argPrefix + ', *args):')
  380.         if doc and doc[1]:
  381.             ret.append(linePrefix + '\t' + _safeQuotedString(doc[1]))
  382.         
  383.         if fdesc:
  384.             invoketype = fdesc[4]
  385.         else:
  386.             invoketype = pythoncom.DISPATCH_METHOD
  387.         s = linePrefix + '\treturn self._get_good_object_(self._oleobj_.Invoke(*(('
  388.         ret.append(s + str(entry.dispid) + ",0,%d,1)+args)),'%s')" % (invoketype, names[0]))
  389.         ret.append('')
  390.         return ret
  391.  
  392.  
  393.  
  394. class VTableItem(DispatchItem):
  395.     
  396.     def Build(self, typeinfo, attr, bForUser = 1):
  397.         DispatchItem.Build(self, typeinfo, attr, bForUser)
  398.         
  399.         def cmp_vtable_off(m1, m2):
  400.             return cmp(m1.desc[7], m2.desc[7])
  401.  
  402.         meth_list = self.mapFuncs.values() + self.propMapGet.values() + self.propMapPut.values()
  403.         meth_list.sort(cmp_vtable_off)
  404.         self.vtableFuncs = []
  405.         for entry in meth_list:
  406.             self.vtableFuncs.append((entry.names, entry.dispid, entry.desc))
  407.         
  408.  
  409.  
  410.  
  411. class LazyDispatchItem(DispatchItem):
  412.     typename = 'LazyDispatchItem'
  413.     
  414.     def __init__(self, attr, doc):
  415.         self.clsid = attr[0]
  416.         DispatchItem.__init__(self, None, attr, doc, 0)
  417.  
  418.  
  419. typeSubstMap = {
  420.     pythoncom.VT_INT: pythoncom.VT_I4,
  421.     pythoncom.VT_UINT: pythoncom.VT_I4,
  422.     pythoncom.VT_HRESULT: pythoncom.VT_I4 }
  423.  
  424. def _ResolveType(typerepr, itypeinfo):
  425.     return (typeSubstMap.get(typerepr, typerepr), None, None)
  426.  
  427.  
  428. def _BuildArgList(fdesc, names):
  429.     numArgs = max(fdesc[6], len(fdesc[2]))
  430.     names = list(names)
  431.     while None in names:
  432.         i = names.index(None)
  433.         names[i] = 'arg%d' % (i,)
  434.     names = map(MakePublicAttributeName, names[1:])
  435.     name_num = 0
  436.     while len(names) < numArgs:
  437.         names.append('arg%d' % (len(names),))
  438.     for i in range(0, len(names), 5):
  439.         names[i] = names[i] + '\n\t\t\t'
  440.     
  441.     return ',' + string.join(names, ', ')
  442.  
  443. valid_identifier_chars = string.ascii_letters + string.digits + '_'
  444.  
  445. def demunge_leading_underscores(className):
  446.     i = 0
  447.     while className[i] == '_':
  448.         i += 1
  449.     return className[i - 1:] + className[:i - 1]
  450.  
  451.  
  452. def MakePublicAttributeName(className, is_global = False):
  453.     if className[:2] == '__':
  454.         return demunge_leading_underscores(className)
  455.     if iskeyword(className):
  456.         return string.capitalize(className)
  457.     if className == 'None':
  458.         className = 'NONE'
  459.     elif is_global and __builtins__.has_key(className):
  460.         ret = className.capitalize()
  461.         if ret == className:
  462.             ret = ret.upper()
  463.         
  464.         return ret
  465.     return filter((lambda char: char in valid_identifier_chars), className)
  466.  
  467.  
  468. def MakeDefaultArgRepr(defArgVal):
  469.     
  470.     try:
  471.         inOut = defArgVal[1]
  472.     except IndexError:
  473.         inOut = pythoncom.PARAMFLAG_FIN
  474.  
  475.     if inOut & pythoncom.PARAMFLAG_FHASDEFAULT:
  476.         val = defArgVal[2]
  477.         if type(val) is UnicodeType:
  478.             return repr(str(val))
  479.         if type(val) is TimeType:
  480.             year = val.year
  481.             month = val.month
  482.             day = val.day
  483.             hour = val.hour
  484.             minute = val.minute
  485.             second = val.second
  486.             msec = val.msec
  487.             return 'pythoncom.MakeTime((%(year)d, %(month)d, %(day)d, %(hour)d, %(minute)d, %(second)d,0,0,0,%(msec)d))' % locals()
  488.         return repr(val)
  489.     inOut & pythoncom.PARAMFLAG_FHASDEFAULT
  490.  
  491.  
  492. def BuildCallList(fdesc, names, defNamedOptArg, defNamedNotOptArg, defUnnamedArg, defOutArg, is_comment = False):
  493.     numArgs = len(fdesc[2])
  494.     numOptArgs = fdesc[6]
  495.     strval = ''
  496.     if numOptArgs == -1:
  497.         firstOptArg = numArgs
  498.         numArgs = numArgs - 1
  499.     else:
  500.         firstOptArg = numArgs - numOptArgs
  501.     for arg in xrange(numArgs):
  502.         
  503.         try:
  504.             argName = names[arg + 1]
  505.             namedArg = argName is not None
  506.         except IndexError:
  507.             namedArg = 0
  508.  
  509.         if not namedArg:
  510.             argName = 'arg%d' % arg
  511.         
  512.         thisdesc = fdesc[2][arg]
  513.         defArgVal = MakeDefaultArgRepr(thisdesc)
  514.         if defArgVal is None:
  515.             if thisdesc[1] & (pythoncom.PARAMFLAG_FOUT | pythoncom.PARAMFLAG_FIN) == pythoncom.PARAMFLAG_FOUT:
  516.                 defArgVal = defOutArg
  517.             elif namedArg:
  518.                 if arg >= firstOptArg:
  519.                     defArgVal = defNamedOptArg
  520.                 else:
  521.                     defArgVal = defNamedNotOptArg
  522.             else:
  523.                 defArgVal = defUnnamedArg
  524.         
  525.         argName = MakePublicAttributeName(argName)
  526.         if (arg + 1) % 5 == 0:
  527.             strval = strval + '\n'
  528.             if is_comment:
  529.                 strval = strval + '#'
  530.             
  531.             strval = strval + '\t\t\t'
  532.         
  533.         strval = strval + ', ' + argName
  534.         if defArgVal:
  535.             strval = strval + '=' + defArgVal
  536.             continue
  537.     
  538.     if numOptArgs == -1:
  539.         strval = strval + ', *' + names[-1]
  540.     
  541.     return strval
  542.  
  543. if __name__ == '__main__':
  544.     print "Use 'makepy.py' to generate Python code - this module is just a helper"
  545.  
  546.