home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 January / maximum-cd-2011-01.iso / DiscContents / calibre-0.7.26.msi / file_2914 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-10-31  |  6.7 KB  |  197 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import types
  5. import pythoncom
  6. from win32com.client import gencache
  7. com_error = pythoncom.com_error
  8. _univgw = pythoncom._univgw
  9.  
  10. def RegisterInterfaces(typelibGUID, lcid, major, minor, interface_names = None):
  11.     ret = []
  12.     
  13.     try:
  14.         mod = gencache.GetModuleForTypelib(typelibGUID, lcid, major, minor)
  15.     except ImportError:
  16.         mod = None
  17.  
  18.     if mod is None:
  19.         import win32com.client.build as win32com
  20.         tlb = pythoncom.LoadRegTypeLib(typelibGUID, major, minor, lcid)
  21.         typecomp_lib = tlb.GetTypeComp()
  22.         if interface_names is None:
  23.             interface_names = []
  24.             for i in range(tlb.GetTypeInfoCount()):
  25.                 info = tlb.GetTypeInfo(i)
  26.                 doc = tlb.GetDocumentation(i)
  27.                 attr = info.GetTypeAttr()
  28.                 if (attr.typekind == pythoncom.TKIND_INTERFACE or attr.typekind == pythoncom.TKIND_DISPATCH) and attr.wTypeFlags & pythoncom.TYPEFLAG_FDUAL:
  29.                     interface_names.append(doc[0])
  30.                     continue
  31.             
  32.         
  33.         for name in interface_names:
  34.             (type_info, type_comp) = typecomp_lib.BindType(name)
  35.             if type_info is None:
  36.                 raise ValueError, "The interface '%s' can not be located" % (name,)
  37.             type_info is None
  38.             attr = type_info.GetTypeAttr()
  39.             if attr.typekind == pythoncom.TKIND_DISPATCH:
  40.                 refhtype = type_info.GetRefTypeOfImplType(-1)
  41.                 type_info = type_info.GetRefTypeInfo(refhtype)
  42.                 attr = type_info.GetTypeAttr()
  43.             
  44.             item = win32com.client.build.VTableItem(type_info, attr, type_info.GetDocumentation(-1))
  45.             _doCreateVTable(item.clsid, item.python_name, item.bIsDispatch, item.vtableFuncs)
  46.             for info in item.vtableFuncs:
  47.                 (names, dispid, desc) = info
  48.                 invkind = desc[4]
  49.                 ret.append((dispid, invkind, names[0]))
  50.             
  51.         
  52.     elif not interface_names:
  53.         interface_names = mod.VTablesToClassMap.values()
  54.     
  55.     for name in interface_names:
  56.         
  57.         try:
  58.             iid = mod.NamesToIIDMap[name]
  59.         except KeyError:
  60.             raise ValueError, "Interface '%s' does not exist in this cached typelib" % (name,)
  61.  
  62.         sub_mod = gencache.GetModuleForCLSID(iid)
  63.         is_dispatch = getattr(sub_mod, name + '_vtables_dispatch_', None)
  64.         method_defs = getattr(sub_mod, name + '_vtables_', None)
  65.         if is_dispatch is None or method_defs is None:
  66.             raise ValueError, "Interface '%s' is IDispatch only" % (name,)
  67.         method_defs is None
  68.         _doCreateVTable(iid, name, is_dispatch, method_defs)
  69.         for info in method_defs:
  70.             (names, dispid, desc) = info
  71.             invkind = desc[4]
  72.             ret.append((dispid, invkind, names[0]))
  73.         
  74.     
  75.     return ret
  76.  
  77.  
  78. def _doCreateVTable(iid, interface_name, is_dispatch, method_defs):
  79.     defn = Definition(iid, is_dispatch, method_defs)
  80.     vtbl = _univgw.CreateVTable(defn, is_dispatch)
  81.     _univgw.RegisterVTable(vtbl, iid, interface_name)
  82.  
  83.  
  84. def _CalcTypeSize(typeTuple):
  85.     t = typeTuple[0]
  86.     if t & (pythoncom.VT_BYREF | pythoncom.VT_ARRAY):
  87.         cb = _univgw.SizeOfVT(pythoncom.VT_PTR)[1]
  88.     elif t == pythoncom.VT_RECORD:
  89.         cb = _univgw.SizeOfVT(pythoncom.VT_PTR)[1]
  90.     else:
  91.         cb = _univgw.SizeOfVT(t)[1]
  92.     return cb
  93.  
  94.  
  95. class Arg:
  96.     
  97.     def __init__(self, arg_info, name = None):
  98.         self.name = name
  99.         (self.vt, self.inOut, self.default, self.clsid) = arg_info
  100.         self.size = _CalcTypeSize(arg_info)
  101.         self.offset = 0
  102.  
  103.  
  104.  
  105. class Method:
  106.     
  107.     def __init__(self, method_info, isEventSink = 0):
  108.         (all_names, dispid, desc) = method_info
  109.         name = all_names[0]
  110.         names = all_names[1:]
  111.         invkind = desc[4]
  112.         arg_defs = desc[2]
  113.         ret_def = desc[8]
  114.         self.dispid = dispid
  115.         self.invkind = invkind
  116.         if isEventSink and name[:2] != 'On':
  117.             name = 'On%s' % name
  118.         
  119.         self.name = name
  120.         cbArgs = 0
  121.         self.args = []
  122.         for argDesc in arg_defs:
  123.             arg = Arg(argDesc)
  124.             arg.offset = cbArgs
  125.             cbArgs = cbArgs + arg.size
  126.             self.args.append(arg)
  127.         
  128.         self.cbArgs = cbArgs
  129.         self._gw_in_args = self._GenerateInArgTuple()
  130.         self._gw_out_args = self._GenerateOutArgTuple()
  131.  
  132.     
  133.     def _GenerateInArgTuple(self):
  134.         l = []
  135.         for arg in self.args:
  136.             if arg.inOut & pythoncom.PARAMFLAG_FIN or arg.inOut == 0:
  137.                 l.append((arg.vt, arg.offset, arg.size))
  138.                 continue
  139.         
  140.         return tuple(l)
  141.  
  142.     
  143.     def _GenerateOutArgTuple(self):
  144.         l = []
  145.         for arg in self.args:
  146.             if arg.inOut & pythoncom.PARAMFLAG_FOUT and arg.inOut & pythoncom.PARAMFLAG_FRETVAL or arg.inOut == 0:
  147.                 l.append((arg.vt, arg.offset, arg.size, arg.clsid))
  148.                 continue
  149.         
  150.         return tuple(l)
  151.  
  152.  
  153.  
  154. class Definition:
  155.     
  156.     def __init__(self, iid, is_dispatch, method_defs):
  157.         self._iid = iid
  158.         self._methods = []
  159.         self._is_dispatch = is_dispatch
  160.         for info in method_defs:
  161.             entry = Method(info)
  162.             self._methods.append(entry)
  163.         
  164.  
  165.     
  166.     def iid(self):
  167.         return self._iid
  168.  
  169.     
  170.     def vtbl_argsizes(self):
  171.         return map((lambda m: m.cbArgs), self._methods)
  172.  
  173.     
  174.     def dispatch(self, ob, index, argPtr, ReadFromInTuple = _univgw.ReadFromInTuple, WriteFromOutTuple = _univgw.WriteFromOutTuple):
  175.         meth = self._methods[index]
  176.         hr = 0
  177.         args = ReadFromInTuple(meth._gw_in_args, argPtr)
  178.         ob = getattr(ob, 'policy', ob)
  179.         ob._dispid_to_func_[meth.dispid] = meth.name
  180.         retVal = ob._InvokeEx_(meth.dispid, 0, meth.invkind, args, None, None)
  181.         if type(retVal) == types.TupleType:
  182.             if len(retVal) == len(meth._gw_out_args) + 1:
  183.                 hr = retVal[0]
  184.                 retVal = retVal[1:]
  185.             else:
  186.                 raise TypeError, 'Expected %s return values, got: %s' % (len(meth._gw_out_args) + 1, len(retVal))
  187.         len(retVal) == len(meth._gw_out_args) + 1
  188.         retVal = [
  189.             retVal]
  190.         retVal.extend([
  191.             None] * (len(meth._gw_out_args) - 1))
  192.         retVal = tuple(retVal)
  193.         WriteFromOutTuple(retVal, meth._gw_out_args, argPtr)
  194.         return hr
  195.  
  196.  
  197.