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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __author__ = 'Greg Stein and Mark Hammond'
  5. import win32api
  6. import winerror
  7. import string
  8. import sys
  9. import types
  10. import win32con
  11. import pythoncom
  12. from pythoncom import DISPATCH_METHOD, DISPATCH_PROPERTYGET, DISPATCH_PROPERTYPUT, DISPATCH_PROPERTYPUTREF, DISPID_UNKNOWN, DISPID_VALUE, DISPID_PROPERTYPUT, DISPID_NEWENUM, DISPID_EVALUATE, DISPID_CONSTRUCTOR, DISPID_DESTRUCTOR, DISPID_COLLECT, DISPID_STARTENUM
  13. S_OK = 0
  14. from pywintypes import UnicodeType
  15. IDispatchType = pythoncom.TypeIIDs[pythoncom.IID_IDispatch]
  16. IUnknownType = pythoncom.TypeIIDs[pythoncom.IID_IUnknown]
  17. core_has_unicode = hasattr(__builtins__, 'unicode')
  18. from exception import COMException
  19. error = __name__ + ' error'
  20. regSpec = 'CLSID\\%s\\PythonCOM'
  21. regPolicy = 'CLSID\\%s\\PythonCOMPolicy'
  22. regDispatcher = 'CLSID\\%s\\PythonCOMDispatcher'
  23. regAddnPath = 'CLSID\\%s\\PythonCOMPath'
  24. from sys import exc_info
  25.  
  26. def CreateInstance(clsid, reqIID):
  27.     
  28.     try:
  29.         addnPaths = string.split(win32api.RegQueryValue(win32con.HKEY_CLASSES_ROOT, regAddnPath % clsid), ';')
  30.         for newPath in addnPaths:
  31.             if newPath not in sys.path:
  32.                 sys.path.insert(0, newPath)
  33.                 continue
  34.     except win32api.error:
  35.         pass
  36.  
  37.     
  38.     try:
  39.         policy = win32api.RegQueryValue(win32con.HKEY_CLASSES_ROOT, regPolicy % clsid)
  40.         policy = resolve_func(policy)
  41.     except win32api.error:
  42.         policy = DefaultPolicy
  43.  
  44.     
  45.     try:
  46.         dispatcher = win32api.RegQueryValue(win32con.HKEY_CLASSES_ROOT, regDispatcher % clsid)
  47.         if dispatcher:
  48.             dispatcher = resolve_func(dispatcher)
  49.     except win32api.error:
  50.         dispatcher = None
  51.  
  52.     sys.exc_type = None
  53.     sys.exc_value = None
  54.     sys.exc_traceback = None
  55.     if dispatcher:
  56.         retObj = dispatcher(policy, None)
  57.     else:
  58.         retObj = policy(None)
  59.     return retObj._CreateInstance_(clsid, reqIID)
  60.  
  61.  
  62. class BasicWrapPolicy:
  63.     
  64.     def __init__(self, object):
  65.         if object is not None:
  66.             self._wrap_(object)
  67.         
  68.  
  69.     
  70.     def _CreateInstance_(self, clsid, reqIID):
  71.         
  72.         try:
  73.             classSpec = win32api.RegQueryValue(win32con.HKEY_CLASSES_ROOT, regSpec % clsid)
  74.         except win32api.error:
  75.             raise error, 'The object is not correctly registered - %s key can not be read' % regSpec % clsid
  76.  
  77.         myob = call_func(classSpec)
  78.         self._wrap_(myob)
  79.         
  80.         try:
  81.             return pythoncom.WrapObject(self, reqIID)
  82.         except pythoncom.com_error:
  83.             (hr, desc, exc, arg) = None
  84.             IIDToInterfaceName = IIDToInterfaceName
  85.             import win32com.util
  86.             desc = "The object '%r' was created, but does not support the interface '%s'(%s): %s" % (myob, IIDToInterfaceName(reqIID), reqIID, desc)
  87.             raise pythoncom.com_error, (hr, desc, exc, arg)
  88.  
  89.  
  90.     
  91.     def _wrap_(self, object):
  92.         self._name_to_dispid_ = { }
  93.         ob = self._obj_ = object
  94.         if hasattr(ob, '_query_interface_'):
  95.             self._query_interface_ = ob._query_interface_
  96.         
  97.         if hasattr(ob, '_invoke_'):
  98.             self._invoke_ = ob._invoke_
  99.         
  100.         if hasattr(ob, '_invokeex_'):
  101.             self._invokeex_ = ob._invokeex_
  102.         
  103.         if hasattr(ob, '_getidsofnames_'):
  104.             self._getidsofnames_ = ob._getidsofnames_
  105.         
  106.         if hasattr(ob, '_getdispid_'):
  107.             self._getdispid_ = ob._getdispid_
  108.         
  109.         if hasattr(ob, '_com_interfaces_'):
  110.             self._com_interfaces_ = []
  111.             for i in ob._com_interfaces_:
  112.                 if type(i) != pythoncom.PyIIDType:
  113.                     if i[0] != '{':
  114.                         i = pythoncom.InterfaceNames[i]
  115.                     else:
  116.                         i = pythoncom.MakeIID(i)
  117.                 
  118.                 self._com_interfaces_.append(i)
  119.             
  120.         else:
  121.             self._com_interfaces_ = []
  122.  
  123.     
  124.     def _QueryInterface_(self, iid):
  125.         if iid in self._com_interfaces_:
  126.             return 1
  127.         return self._query_interface_(iid)
  128.  
  129.     
  130.     def _query_interface_(self, iid):
  131.         return 0
  132.  
  133.     
  134.     def _Invoke_(self, dispid, lcid, wFlags, args):
  135.         if type(dispid) == type(''):
  136.             
  137.             try:
  138.                 dispid = self._name_to_dispid_[string.lower(dispid)]
  139.             except KeyError:
  140.                 raise COMException(scode = winerror.DISP_E_MEMBERNOTFOUND, desc = 'Member not found')
  141.             except:
  142.                 None<EXCEPTION MATCH>KeyError
  143.             
  144.  
  145.         None<EXCEPTION MATCH>KeyError
  146.         return self._invoke_(dispid, lcid, wFlags, args)
  147.  
  148.     
  149.     def _invoke_(self, dispid, lcid, wFlags, args):
  150.         return (S_OK, -1, self._invokeex_(dispid, lcid, wFlags, args, None, None))
  151.  
  152.     
  153.     def _GetIDsOfNames_(self, names, lcid):
  154.         if len(names) > 1:
  155.             raise COMException(scode = winerror.DISP_E_INVALID, desc = 'Cannot support member argument names')
  156.         len(names) > 1
  157.         return self._getidsofnames_(names, lcid)
  158.  
  159.     
  160.     def _getidsofnames_(self, names, lcid):
  161.         return (self._getdispid_(names[0], 0),)
  162.  
  163.     
  164.     def _GetDispID_(self, name, fdex):
  165.         return self._getdispid_(name, fdex)
  166.  
  167.     
  168.     def _getdispid_(self, name, fdex):
  169.         
  170.         try:
  171.             return self._name_to_dispid_[string.lower(str(name))]
  172.         except KeyError:
  173.             raise COMException(scode = winerror.DISP_E_UNKNOWNNAME)
  174.  
  175.  
  176.     
  177.     def _InvokeEx_(self, dispid, lcid, wFlags, args, kwargs, serviceProvider):
  178.         if type(dispid) == type(''):
  179.             
  180.             try:
  181.                 dispid = self._name_to_dispid_[string.lower(dispid)]
  182.             except KeyError:
  183.                 raise COMException(scode = winerror.DISP_E_MEMBERNOTFOUND, desc = 'Member not found')
  184.             except:
  185.                 None<EXCEPTION MATCH>KeyError
  186.             
  187.  
  188.         None<EXCEPTION MATCH>KeyError
  189.         return self._invokeex_(dispid, lcid, wFlags, args, kwargs, serviceProvider)
  190.  
  191.     
  192.     def _invokeex_(self, dispid, lcid, wFlags, args, kwargs, serviceProvider):
  193.         raise error, 'This class does not provide _invokeex_ semantics'
  194.  
  195.     
  196.     def _DeleteMemberByName_(self, name, fdex):
  197.         return self._deletememberbyname_(name, fdex)
  198.  
  199.     
  200.     def _deletememberbyname_(self, name, fdex):
  201.         raise COMException(scode = winerror.E_NOTIMPL)
  202.  
  203.     
  204.     def _DeleteMemberByDispID_(self, id):
  205.         return self._deletememberbydispid(id)
  206.  
  207.     
  208.     def _deletememberbydispid_(self, id):
  209.         raise COMException(scode = winerror.E_NOTIMPL)
  210.  
  211.     
  212.     def _GetMemberProperties_(self, id, fdex):
  213.         return self._getmemberproperties_(id, fdex)
  214.  
  215.     
  216.     def _getmemberproperties_(self, id, fdex):
  217.         raise COMException(scode = winerror.E_NOTIMPL)
  218.  
  219.     
  220.     def _GetMemberName_(self, dispid):
  221.         return self._getmembername_(dispid)
  222.  
  223.     
  224.     def _getmembername_(self, dispid):
  225.         raise COMException(scode = winerror.E_NOTIMPL)
  226.  
  227.     
  228.     def _GetNextDispID_(self, fdex, dispid):
  229.         return self._getnextdispid_(fdex, dispid)
  230.  
  231.     
  232.     def _getnextdispid_(self, fdex, dispid):
  233.         ids = self._name_to_dispid_.values()
  234.         ids.sort()
  235.         if DISPID_STARTENUM in ids:
  236.             ids.remove(DISPID_STARTENUM)
  237.         
  238.         if dispid == DISPID_STARTENUM:
  239.             return ids[0]
  240.         
  241.         try:
  242.             return ids[ids.index(dispid) + 1]
  243.         except ValueError:
  244.             dispid == DISPID_STARTENUM
  245.             dispid == DISPID_STARTENUM
  246.             raise COMException(scode = winerror.E_UNEXPECTED)
  247.         except IndexError:
  248.             raise COMException(scode = winerror.S_FALSE)
  249.         except:
  250.             dispid == DISPID_STARTENUM
  251.  
  252.  
  253.     
  254.     def _GetNameSpaceParent_(self):
  255.         return self._getnamespaceparent()
  256.  
  257.     
  258.     def _getnamespaceparent_(self):
  259.         raise COMException(scode = winerror.E_NOTIMPL)
  260.  
  261.  
  262.  
  263. class MappedWrapPolicy(BasicWrapPolicy):
  264.     
  265.     def _wrap_(self, object):
  266.         BasicWrapPolicy._wrap_(self, object)
  267.         ob = self._obj_
  268.         if hasattr(ob, '_dispid_to_func_'):
  269.             self._dispid_to_func_ = ob._dispid_to_func_
  270.         else:
  271.             self._dispid_to_func_ = { }
  272.         if hasattr(ob, '_dispid_to_get_'):
  273.             self._dispid_to_get_ = ob._dispid_to_get_
  274.         else:
  275.             self._dispid_to_get_ = { }
  276.         if hasattr(ob, '_dispid_to_put_'):
  277.             self._dispid_to_put_ = ob._dispid_to_put_
  278.         else:
  279.             self._dispid_to_put_ = { }
  280.  
  281.     
  282.     def _getmembername_(self, dispid):
  283.         if self._dispid_to_func_.has_key(dispid):
  284.             return self._dispid_to_func_[dispid]
  285.         if self._dispid_to_get_.has_key(dispid):
  286.             return self._dispid_to_get_[dispid]
  287.         if self._dispid_to_put_.has_key(dispid):
  288.             return self._dispid_to_put_[dispid]
  289.         raise COMException(scode = winerror.DISP_E_MEMBERNOTFOUND)
  290.  
  291.  
  292.  
  293. class DesignatedWrapPolicy(MappedWrapPolicy):
  294.     
  295.     def _wrap_(self, ob):
  296.         tlb_guid = getattr(ob, '_typelib_guid_', None)
  297.         MappedWrapPolicy._wrap_(self, ob)
  298.         if not hasattr(ob, '_public_methods_') and not hasattr(ob, '_typelib_guid_'):
  299.             raise error, 'Object does not support DesignatedWrapPolicy, as it does not have either _public_methods_ or _typelib_guid_ attributes.'
  300.         not hasattr(ob, '_typelib_guid_')
  301.         for dispid, name in self._dispid_to_func_.items():
  302.             self._name_to_dispid_[string.lower(name)] = dispid
  303.         
  304.         for dispid, name in self._dispid_to_get_.items():
  305.             self._name_to_dispid_[string.lower(name)] = dispid
  306.         
  307.         for dispid, name in self._dispid_to_put_.items():
  308.             self._name_to_dispid_[string.lower(name)] = dispid
  309.         
  310.         for dispid, invkind, name in universal_data:
  311.             self._name_to_dispid_[string.lower(name)] = dispid
  312.             if invkind == DISPATCH_METHOD:
  313.                 self._dispid_to_func_[dispid] = name
  314.                 continue
  315.             None if tlb_guid is not None else []
  316.             if invkind in (DISPATCH_PROPERTYPUT, DISPATCH_PROPERTYPUTREF):
  317.                 self._dispid_to_put_[dispid] = name
  318.                 continue
  319.             if invkind == DISPATCH_PROPERTYGET:
  320.                 self._dispid_to_get_[dispid] = name
  321.                 continue
  322.             raise ValueError, 'unexpected invkind: %d (%s)' % (invkind, name)
  323.         
  324.         if hasattr(ob, '_value_'):
  325.             self._dispid_to_get_[DISPID_VALUE] = '_value_'
  326.             self._dispid_to_put_[DISPID_PROPERTYPUT] = '_value_'
  327.         
  328.         if hasattr(ob, '_NewEnum'):
  329.             self._name_to_dispid_['_newenum'] = DISPID_NEWENUM
  330.             self._dispid_to_func_[DISPID_NEWENUM] = '_NewEnum'
  331.         
  332.         if hasattr(ob, '_Evaluate'):
  333.             self._name_to_dispid_['_evaluate'] = DISPID_EVALUATE
  334.             self._dispid_to_func_[DISPID_EVALUATE] = '_Evaluate'
  335.         
  336.         next_dispid = self._allocnextdispid(999)
  337.         if hasattr(ob, '_public_attrs_'):
  338.             if hasattr(ob, '_readonly_attrs_'):
  339.                 readonly = ob._readonly_attrs_
  340.             else:
  341.                 readonly = []
  342.             for name in ob._public_attrs_:
  343.                 dispid = self._name_to_dispid_.get(name.lower())
  344.                 if dispid is None:
  345.                     dispid = next_dispid
  346.                     self._name_to_dispid_[name.lower()] = dispid
  347.                     next_dispid = self._allocnextdispid(next_dispid)
  348.                 
  349.                 self._dispid_to_get_[dispid] = name
  350.                 if name not in readonly:
  351.                     self._dispid_to_put_[dispid] = name
  352.                     continue
  353.             
  354.         
  355.         for name in getattr(ob, '_public_methods_', []):
  356.             dispid = self._name_to_dispid_.get(name.lower())
  357.             if dispid is None:
  358.                 dispid = next_dispid
  359.                 self._name_to_dispid_[name.lower()] = dispid
  360.                 next_dispid = self._allocnextdispid(next_dispid)
  361.             
  362.             self._dispid_to_func_[dispid] = name
  363.         
  364.         self._typeinfos_ = None
  365.  
  366.     
  367.     def _build_typeinfos_(self):
  368.         tlb_guid = getattr(self._obj_, '_typelib_guid_', None)
  369.         if tlb_guid is None:
  370.             return []
  371.         (tlb_major, tlb_minor) = getattr(self._obj_, '_typelib_version_', (1, 0))
  372.         tlb = pythoncom.LoadRegTypeLib(tlb_guid, tlb_major, tlb_minor)
  373.         typecomp = tlb.GetTypeComp()
  374.         for iname in self._obj_._com_interfaces_:
  375.             
  376.             try:
  377.                 (type_info, type_comp) = typecomp.BindType(iname)
  378.                 if type_info is not None:
  379.                     return [
  380.                         type_info]
  381.             continue
  382.             except pythoncom.com_error:
  383.                 tlb_guid is None
  384.                 tlb_guid is None
  385.                 continue
  386.             
  387.  
  388.         
  389.         return []
  390.  
  391.     
  392.     def _GetTypeInfoCount_(self):
  393.         if self._typeinfos_ is None:
  394.             self._typeinfos_ = self._build_typeinfos_()
  395.         
  396.         return len(self._typeinfos_)
  397.  
  398.     
  399.     def _GetTypeInfo_(self, index, lcid):
  400.         if self._typeinfos_ is None:
  401.             self._typeinfos_ = self._build_typeinfos_()
  402.         
  403.         if index < 0 or index >= len(self._typeinfos_):
  404.             raise COMException(scode = winerror.DISP_E_BADINDEX)
  405.         index >= len(self._typeinfos_)
  406.         return (0, self._typeinfos_[index])
  407.  
  408.     
  409.     def _allocnextdispid(self, last_dispid):
  410.         while None:
  411.             last_dispid = last_dispid + 1
  412.             if not self._dispid_to_func_.has_key(last_dispid) and not self._dispid_to_get_.has_key(last_dispid) and not self._dispid_to_put_.has_key(last_dispid):
  413.                 return last_dispid
  414.             continue
  415.             return None
  416.  
  417.     
  418.     def _invokeex_(self, dispid, lcid, wFlags, args, kwArgs, serviceProvider):
  419.         if wFlags & DISPATCH_METHOD:
  420.             
  421.             try:
  422.                 funcname = self._dispid_to_func_[dispid]
  423.             except KeyError:
  424.                 if not wFlags & DISPATCH_PROPERTYGET:
  425.                     raise COMException(scode = winerror.DISP_E_MEMBERNOTFOUND)
  426.                 wFlags & DISPATCH_PROPERTYGET
  427.  
  428.             
  429.             try:
  430.                 func = getattr(self._obj_, funcname)
  431.             except AttributeError:
  432.                 raise COMException(scode = winerror.DISP_E_MEMBERNOTFOUND)
  433.  
  434.             
  435.             try:
  436.                 return func(*args)
  437.             except TypeError:
  438.                 v = None
  439.                 if str(v).find('arguments') >= 0:
  440.                     print '** TypeError %s calling function %r(%r)' % (v, func, args)
  441.                 
  442.                 raise 
  443.             except:
  444.                 None<EXCEPTION MATCH>TypeError
  445.             
  446.  
  447.         None<EXCEPTION MATCH>TypeError
  448.         if wFlags & DISPATCH_PROPERTYGET:
  449.             
  450.             try:
  451.                 name = self._dispid_to_get_[dispid]
  452.             except KeyError:
  453.                 raise COMException(scode = winerror.DISP_E_MEMBERNOTFOUND)
  454.  
  455.             retob = getattr(self._obj_, name)
  456.             if type(retob) == types.MethodType:
  457.                 retob = retob(*args)
  458.             
  459.             return retob
  460.         if wFlags & (DISPATCH_PROPERTYPUT | DISPATCH_PROPERTYPUTREF):
  461.             
  462.             try:
  463.                 name = self._dispid_to_put_[dispid]
  464.             except KeyError:
  465.                 wFlags & DISPATCH_PROPERTYGET
  466.                 wFlags & DISPATCH_PROPERTYGET
  467.                 raise COMException(scode = winerror.DISP_E_MEMBERNOTFOUND)
  468.             except:
  469.                 wFlags & DISPATCH_PROPERTYGET
  470.  
  471.             if type(getattr(self._obj_, name, None)) == types.MethodType and type(getattr(self._obj_, 'Set' + name, None)) == types.MethodType:
  472.                 fn = getattr(self._obj_, 'Set' + name)
  473.                 fn(*args)
  474.             else:
  475.                 setattr(self._obj_, name, args[0])
  476.             return None
  477.         raise COMException(scode = winerror.E_INVALIDARG, desc = 'invalid wFlags')
  478.  
  479.  
  480.  
  481. class EventHandlerPolicy(DesignatedWrapPolicy):
  482.     
  483.     def _transform_args_(self, args, kwArgs, dispid, lcid, wFlags, serviceProvider):
  484.         ret = []
  485.         for arg in args:
  486.             arg_type = type(arg)
  487.             if arg_type == IDispatchType:
  488.                 import win32com.client as win32com
  489.                 arg = win32com.client.Dispatch(arg)
  490.             elif arg_type == IUnknownType:
  491.                 
  492.                 try:
  493.                     import win32com.client as win32com
  494.                     arg = win32com.client.Dispatch(arg.QueryInterface(pythoncom.IID_IDispatch))
  495.                 except pythoncom.error:
  496.                     pass
  497.                 except:
  498.                     None<EXCEPTION MATCH>pythoncom.error
  499.                 
  500.  
  501.             None<EXCEPTION MATCH>pythoncom.error
  502.             if not core_has_unicode and arg_type == UnicodeType:
  503.                 arg = str(arg)
  504.             
  505.             ret.append(arg)
  506.         
  507.         return (tuple(ret), kwArgs)
  508.  
  509.     
  510.     def _invokeex_(self, dispid, lcid, wFlags, args, kwArgs, serviceProvider):
  511.         (args, kwArgs) = self._transform_args_(args, kwArgs, dispid, lcid, wFlags, serviceProvider)
  512.         return DesignatedWrapPolicy._invokeex_(self, dispid, lcid, wFlags, args, kwArgs, serviceProvider)
  513.  
  514.  
  515.  
  516. class DynamicPolicy(BasicWrapPolicy):
  517.     
  518.     def _wrap_(self, object):
  519.         BasicWrapPolicy._wrap_(self, object)
  520.         if not hasattr(self._obj_, '_dynamic_'):
  521.             raise error, 'Object does not support Dynamic COM Policy'
  522.         hasattr(self._obj_, '_dynamic_')
  523.         self._next_dynamic_ = self._min_dynamic_ = 1000
  524.         self._dyn_dispid_to_name_ = {
  525.             DISPID_VALUE: '_value_',
  526.             DISPID_NEWENUM: '_NewEnum' }
  527.  
  528.     
  529.     def _getdispid_(self, name, fdex):
  530.         lname = string.lower(str(name))
  531.         
  532.         try:
  533.             return self._name_to_dispid_[lname]
  534.         except KeyError:
  535.             dispid = self._next_dynamic_ = self._next_dynamic_ + 1
  536.             self._name_to_dispid_[lname] = dispid
  537.             self._dyn_dispid_to_name_[dispid] = name
  538.             return dispid
  539.  
  540.  
  541.     
  542.     def _invoke_(self, dispid, lcid, wFlags, args):
  543.         return (S_OK, -1, self._invokeex_(dispid, lcid, wFlags, args, None, None))
  544.  
  545.     
  546.     def _invokeex_(self, dispid, lcid, wFlags, args, kwargs, serviceProvider):
  547.         
  548.         try:
  549.             name = str(self._dyn_dispid_to_name_[dispid])
  550.         except KeyError:
  551.             raise COMException(scode = winerror.DISP_E_MEMBERNOTFOUND, desc = 'Member not found')
  552.  
  553.         return self._obj_._dynamic_(name, lcid, wFlags, args)
  554.  
  555.  
  556. DefaultPolicy = DesignatedWrapPolicy
  557.  
  558. def resolve_func(spec):
  559.     
  560.     try:
  561.         idx = string.rindex(spec, '.')
  562.         mname = spec[:idx]
  563.         fname = spec[idx + 1:]
  564.         module = _import_module(mname)
  565.         return getattr(module, fname)
  566.     except ValueError:
  567.         return globals()[spec]
  568.  
  569.  
  570.  
  571. def call_func(spec, *args):
  572.     return resolve_func(spec)(*args)
  573.  
  574.  
  575. def _import_module(mname):
  576.     __import__(mname)
  577.     return sys.modules[mname]
  578.  
  579.  
  580. try:
  581.     from dispatcher import DispatcherTrace, DispatcherWin32trace
  582. except ImportError:
  583.     pass
  584.  
  585.