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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import traceback
  5. import string
  6. import new
  7. import pythoncom
  8. import winerror
  9. import build
  10. from types import StringType, IntType, TupleType, ListType
  11. from pywintypes import UnicodeType, IIDType
  12. import win32com.client as win32com
  13. from win32com.client import NeedUnicodeConversions
  14. debugging = 0
  15. debugging_attr = 0
  16. LCID = 0
  17. ERRORS_BAD_CONTEXT = [
  18.     winerror.DISP_E_MEMBERNOTFOUND,
  19.     winerror.DISP_E_BADPARAMCOUNT,
  20.     winerror.DISP_E_PARAMNOTOPTIONAL,
  21.     winerror.DISP_E_TYPEMISMATCH,
  22.     winerror.E_INVALIDARG]
  23. ALL_INVOKE_TYPES = [
  24.     pythoncom.INVOKE_PROPERTYGET,
  25.     pythoncom.INVOKE_PROPERTYPUT,
  26.     pythoncom.INVOKE_PROPERTYPUTREF,
  27.     pythoncom.INVOKE_FUNC]
  28.  
  29. def debug_print(*args):
  30.     if debugging:
  31.         for arg in args:
  32.             print arg,
  33.         
  34.         print 
  35.     
  36.  
  37.  
  38. def debug_attr_print(*args):
  39.     if debugging_attr:
  40.         for arg in args:
  41.             print arg,
  42.         
  43.         print 
  44.     
  45.  
  46. dispatchType = pythoncom.TypeIIDs[pythoncom.IID_IDispatch]
  47. iunkType = pythoncom.TypeIIDs[pythoncom.IID_IUnknown]
  48. _GoodDispatchType = [
  49.     StringType,
  50.     IIDType,
  51.     UnicodeType]
  52. _defaultDispatchItem = build.DispatchItem
  53.  
  54. def _GetGoodDispatch(IDispatch, clsctx = pythoncom.CLSCTX_SERVER):
  55.     if type(IDispatch) in _GoodDispatchType:
  56.         
  57.         try:
  58.             IDispatch = pythoncom.connect(IDispatch)
  59.         except pythoncom.ole_error:
  60.             IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx, pythoncom.IID_IDispatch)
  61.         except:
  62.             None<EXCEPTION MATCH>pythoncom.ole_error
  63.         
  64.  
  65.     None<EXCEPTION MATCH>pythoncom.ole_error
  66.     IDispatch = getattr(IDispatch, '_oleobj_', IDispatch)
  67.     return IDispatch
  68.  
  69.  
  70. def _GetGoodDispatchAndUserName(IDispatch, userName, clsctx):
  71.     if userName is None:
  72.         if type(IDispatch) == StringType:
  73.             userName = IDispatch
  74.         elif type(IDispatch) == UnicodeType:
  75.             userName = IDispatch.encode('ascii', 'replace')
  76.         
  77.     elif type(userName) == UnicodeType:
  78.         userName = userName.encode('ascii', 'replace')
  79.     else:
  80.         userName = str(userName)
  81.     return (_GetGoodDispatch(IDispatch, clsctx), userName)
  82.  
  83.  
  84. def _GetDescInvokeType(entry, default_invoke_type):
  85.     if not entry or not (entry.desc):
  86.         return default_invoke_type
  87.     return entry.desc[4]
  88.  
  89.  
  90. def Dispatch(IDispatch, userName = None, createClass = None, typeinfo = None, UnicodeToString = NeedUnicodeConversions, clsctx = pythoncom.CLSCTX_SERVER):
  91.     (IDispatch, userName) = _GetGoodDispatchAndUserName(IDispatch, userName, clsctx)
  92.     if createClass is None:
  93.         createClass = CDispatch
  94.     
  95.     lazydata = None
  96.     
  97.     try:
  98.         if typeinfo is None:
  99.             typeinfo = IDispatch.GetTypeInfo()
  100.         
  101.         
  102.         try:
  103.             typecomp = typeinfo.GetTypeComp()
  104.             lazydata = (typeinfo, typecomp)
  105.         except pythoncom.com_error:
  106.             pass
  107.  
  108.     except pythoncom.com_error:
  109.         typeinfo = None
  110.  
  111.     olerepr = MakeOleRepr(IDispatch, typeinfo, lazydata)
  112.     return createClass(IDispatch, olerepr, userName, UnicodeToString, lazydata)
  113.  
  114.  
  115. def MakeOleRepr(IDispatch, typeinfo, typecomp):
  116.     olerepr = None
  117.     if typeinfo is not None:
  118.         
  119.         try:
  120.             attr = typeinfo.GetTypeAttr()
  121.             if attr[5] == pythoncom.TKIND_INTERFACE and attr[11] & pythoncom.TYPEFLAG_FDUAL:
  122.                 href = typeinfo.GetRefTypeOfImplType(-1)
  123.                 typeinfo = typeinfo.GetRefTypeInfo(href)
  124.                 attr = typeinfo.GetTypeAttr()
  125.             
  126.             if typecomp is None:
  127.                 olerepr = build.DispatchItem(typeinfo, attr, None, 0)
  128.             else:
  129.                 olerepr = build.LazyDispatchItem(attr, None)
  130.         except pythoncom.ole_error:
  131.             pass
  132.         except:
  133.             None<EXCEPTION MATCH>pythoncom.ole_error
  134.         
  135.  
  136.     None<EXCEPTION MATCH>pythoncom.ole_error
  137.     if olerepr is None:
  138.         olerepr = build.DispatchItem()
  139.     
  140.     return olerepr
  141.  
  142.  
  143. def DumbDispatch(IDispatch, userName = None, createClass = None, UnicodeToString = NeedUnicodeConversions, clsctx = pythoncom.CLSCTX_SERVER):
  144.     (IDispatch, userName) = _GetGoodDispatchAndUserName(IDispatch, userName, clsctx)
  145.     if createClass is None:
  146.         createClass = CDispatch
  147.     
  148.     return createClass(IDispatch, build.DispatchItem(), userName, UnicodeToString)
  149.  
  150.  
  151. class CDispatch:
  152.     
  153.     def __init__(self, IDispatch, olerepr, userName = None, UnicodeToString = NeedUnicodeConversions, lazydata = None):
  154.         if userName is None:
  155.             userName = '<unknown>'
  156.         
  157.         self.__dict__['_oleobj_'] = IDispatch
  158.         self.__dict__['_username_'] = userName
  159.         self.__dict__['_olerepr_'] = olerepr
  160.         self.__dict__['_mapCachedItems_'] = { }
  161.         self.__dict__['_builtMethods_'] = { }
  162.         self.__dict__['_enum_'] = None
  163.         self.__dict__['_unicode_to_string_'] = UnicodeToString
  164.         self.__dict__['_lazydata_'] = lazydata
  165.  
  166.     
  167.     def __call__(self, *args):
  168.         if self._olerepr_.defaultDispatchName:
  169.             (invkind, dispid) = self._find_dispatch_type_(self._olerepr_.defaultDispatchName)
  170.         else:
  171.             invkind = pythoncom.DISPATCH_METHOD | pythoncom.DISPATCH_PROPERTYGET
  172.             dispid = pythoncom.DISPID_VALUE
  173.         if invkind is not None:
  174.             allArgs = (dispid, LCID, invkind, 1) + args
  175.             return self._get_good_object_(self._oleobj_.Invoke(*allArgs), self._olerepr_.defaultDispatchName, None)
  176.         raise TypeError, 'This dispatch object does not define a default method'
  177.  
  178.     
  179.     def __nonzero__(self):
  180.         return 1
  181.  
  182.     
  183.     def __repr__(self):
  184.         return '<COMObject %s>' % self._username_
  185.  
  186.     
  187.     def __str__(self):
  188.         
  189.         try:
  190.             return str(self.__call__())
  191.         except pythoncom.com_error:
  192.             details = None
  193.             if details[0] not in ERRORS_BAD_CONTEXT:
  194.                 raise 
  195.             details[0] not in ERRORS_BAD_CONTEXT
  196.             return self.__repr__()
  197.  
  198.  
  199.     
  200.     def __cmp__(self, other):
  201.         other = getattr(other, '_oleobj_', other)
  202.         return cmp(self._oleobj_, other)
  203.  
  204.     
  205.     def __int__(self):
  206.         return int(self.__call__())
  207.  
  208.     
  209.     def __len__(self):
  210.         (invkind, dispid) = self._find_dispatch_type_('Count')
  211.         if invkind:
  212.             return self._oleobj_.Invoke(dispid, LCID, invkind, 1)
  213.         raise TypeError, 'This dispatch object does not define a Count method'
  214.  
  215.     
  216.     def _NewEnum(self):
  217.         
  218.         try:
  219.             invkind = pythoncom.DISPATCH_METHOD | pythoncom.DISPATCH_PROPERTYGET
  220.             enum = self._oleobj_.InvokeTypes(pythoncom.DISPID_NEWENUM, LCID, invkind, (13, 10), ())
  221.         except pythoncom.com_error:
  222.             return None
  223.  
  224.         import util
  225.         return util.WrapEnum(enum, None)
  226.  
  227.     
  228.     def __getitem__(self, index):
  229.         if isinstance(index, IntType):
  230.             if self.__dict__['_enum_'] is None:
  231.                 self.__dict__['_enum_'] = self._NewEnum()
  232.             
  233.             if self.__dict__['_enum_'] is not None:
  234.                 return self._get_good_object_(self._enum_.__getitem__(index))
  235.         
  236.         (invkind, dispid) = self._find_dispatch_type_('Item')
  237.         if invkind is not None:
  238.             return self._get_good_object_(self._oleobj_.Invoke(dispid, LCID, invkind, 1, index))
  239.         raise TypeError, 'This object does not support enumeration'
  240.  
  241.     
  242.     def __setitem__(self, index, *args):
  243.         if self._olerepr_.defaultDispatchName:
  244.             (invkind, dispid) = self._find_dispatch_type_(self._olerepr_.defaultDispatchName)
  245.         else:
  246.             invkind = pythoncom.DISPATCH_PROPERTYPUT | pythoncom.DISPATCH_PROPERTYPUTREF
  247.             dispid = pythoncom.DISPID_VALUE
  248.         if invkind is not None:
  249.             allArgs = (dispid, LCID, invkind, 0, index) + args
  250.             return self._get_good_object_(self._oleobj_.Invoke(*allArgs), self._olerepr_.defaultDispatchName, None)
  251.         raise TypeError, 'This dispatch object does not define a default method'
  252.  
  253.     
  254.     def _find_dispatch_type_(self, methodName):
  255.         if self._olerepr_.mapFuncs.has_key(methodName):
  256.             item = self._olerepr_.mapFuncs[methodName]
  257.             return (item.desc[4], item.dispid)
  258.         if self._olerepr_.propMapGet.has_key(methodName):
  259.             item = self._olerepr_.propMapGet[methodName]
  260.             return (item.desc[4], item.dispid)
  261.         
  262.         try:
  263.             dispid = self._oleobj_.GetIDsOfNames(0, methodName)
  264.         except:
  265.             self._olerepr_.propMapGet.has_key(methodName)
  266.             self._olerepr_.mapFuncs.has_key(methodName)
  267.             return (None, None)
  268.  
  269.         return (pythoncom.DISPATCH_METHOD | pythoncom.DISPATCH_PROPERTYGET, dispid)
  270.  
  271.     
  272.     def _ApplyTypes_(self, dispid, wFlags, retType, argTypes, user, resultCLSID, *args):
  273.         result = self._oleobj_.InvokeTypes(*(dispid, LCID, wFlags, retType, argTypes) + args)
  274.         return self._get_good_object_(result, user, resultCLSID)
  275.  
  276.     
  277.     def _wrap_dispatch_(self, ob, userName = None, returnCLSID = None, UnicodeToString = NeedUnicodeConversions):
  278.         return Dispatch(ob, userName, UnicodeToString = UnicodeToString)
  279.  
  280.     
  281.     def _get_good_single_object_(self, ob, userName = None, ReturnCLSID = None):
  282.         if iunkType == type(ob):
  283.             
  284.             try:
  285.                 ob = ob.QueryInterface(pythoncom.IID_IDispatch)
  286.             except pythoncom.com_error:
  287.                 pass
  288.             except:
  289.                 None<EXCEPTION MATCH>pythoncom.com_error
  290.             
  291.  
  292.         None<EXCEPTION MATCH>pythoncom.com_error
  293.         if dispatchType == type(ob):
  294.             return self._wrap_dispatch_(ob, userName, ReturnCLSID)
  295.         if self._unicode_to_string_ and UnicodeType == type(ob):
  296.             return str(ob)
  297.         return ob
  298.  
  299.     
  300.     def _get_good_object_(self, ob, userName = None, ReturnCLSID = None):
  301.         if ob is None:
  302.             return None
  303.         if type(ob) == TupleType:
  304.             return tuple(map((lambda o, s = self, oun = userName, rc = ReturnCLSID: s._get_good_single_object_(o, oun, rc)), ob))
  305.         return self._get_good_single_object_(ob)
  306.  
  307.     
  308.     def _make_method_(self, name):
  309.         methodName = build.MakePublicAttributeName(name)
  310.         methodCodeList = self._olerepr_.MakeFuncMethod(self._olerepr_.mapFuncs[name], methodName, 0)
  311.         methodCode = string.join(methodCodeList, '\n')
  312.         
  313.         try:
  314.             codeObject = compile(methodCode, '<COMObject %s>' % self._username_, 'exec')
  315.             tempNameSpace = { }
  316.             globNameSpace = globals().copy()
  317.             globNameSpace['Dispatch'] = win32com.client.Dispatch
  318.             exec codeObject in globNameSpace, tempNameSpace
  319.             name = methodName
  320.             fn = self._builtMethods_[name] = tempNameSpace[name]
  321.             newMeth = new.instancemethod(fn, self, self.__class__)
  322.             return newMeth
  323.         except:
  324.             debug_print('Error building OLE definition for code ', methodCode)
  325.             traceback.print_exc()
  326.  
  327.  
  328.     
  329.     def _Release_(self):
  330.         for childCont in self._mapCachedItems_.values():
  331.             childCont._Release_()
  332.         
  333.         self._mapCachedItems_ = { }
  334.         if self._oleobj_:
  335.             self._oleobj_.Release()
  336.             self.__dict__['_oleobj_'] = None
  337.         
  338.         if self._olerepr_:
  339.             self.__dict__['_olerepr_'] = None
  340.         
  341.         self._enum_ = None
  342.  
  343.     
  344.     def _proc_(self, name, *args):
  345.         
  346.         try:
  347.             item = self._olerepr_.mapFuncs[name]
  348.             dispId = item.dispid
  349.             return self._get_good_object_(self._oleobj_.Invoke(*(dispId, LCID, item.desc[4], 0) + args))
  350.         except KeyError:
  351.             raise AttributeError, name
  352.  
  353.  
  354.     
  355.     def _print_details_(self):
  356.         print 'AxDispatch container', self._username_
  357.         
  358.         try:
  359.             print 'Methods:'
  360.             for method in self._olerepr_.mapFuncs.keys():
  361.                 print '\t', method
  362.             
  363.             print 'Props:'
  364.             for prop, entry in self._olerepr_.propMap.items():
  365.                 print '\t%s = 0x%x - %s' % (prop, entry.dispid, `entry`)
  366.             
  367.             print 'Get Props:'
  368.             for prop, entry in self._olerepr_.propMapGet.items():
  369.                 print '\t%s = 0x%x - %s' % (prop, entry.dispid, `entry`)
  370.             
  371.             print 'Put Props:'
  372.             for prop, entry in self._olerepr_.propMapPut.items():
  373.                 print '\t%s = 0x%x - %s' % (prop, entry.dispid, `entry`)
  374.         except:
  375.             traceback.print_exc()
  376.  
  377.  
  378.     
  379.     def __LazyMap__(self, attr):
  380.         
  381.         try:
  382.             if self._LazyAddAttr_(attr):
  383.                 debug_attr_print('%s.__LazyMap__(%s) added something' % (self._username_, attr))
  384.                 return 1
  385.         except AttributeError:
  386.             return 0
  387.  
  388.  
  389.     
  390.     def _LazyAddAttr_(self, attr):
  391.         if self._lazydata_ is None:
  392.             return 0
  393.         res = 0
  394.         (typeinfo, typecomp) = self._lazydata_
  395.         olerepr = self._olerepr_
  396.         for i in ALL_INVOKE_TYPES:
  397.             
  398.             try:
  399.                 (x, t) = typecomp.Bind(attr, i)
  400.                 if x == 0 and attr[:3] in ('Set', 'Get'):
  401.                     (x, t) = typecomp.Bind(attr[3:], i)
  402.                 
  403.                 if x == 1:
  404.                     r = olerepr._AddFunc_(typeinfo, t, 0)
  405.                 elif x == 2:
  406.                     r = olerepr._AddVar_(typeinfo, t, 0)
  407.                 else:
  408.                     r = None
  409.                 if r is not None:
  410.                     key = r[0]
  411.                     map = r[1]
  412.                     item = map[key]
  413.                     if map == olerepr.propMapPut:
  414.                         olerepr._propMapPutCheck_(key, item)
  415.                     elif map == olerepr.propMapGet:
  416.                         olerepr._propMapGetCheck_(key, item)
  417.                     
  418.                     res = 1
  419.             continue
  420.             continue
  421.  
  422.         
  423.         return res
  424.  
  425.     
  426.     def _FlagAsMethod(self, *methodNames):
  427.         for name in methodNames:
  428.             details = build.MapEntry(self.__AttrToID__(name), (name,))
  429.             self._olerepr_.mapFuncs[name] = details
  430.         
  431.  
  432.     
  433.     def __AttrToID__(self, attr):
  434.         debug_attr_print('Calling GetIDsOfNames for property %s in Dispatch container %s' % (attr, self._username_))
  435.         return self._oleobj_.GetIDsOfNames(0, attr)
  436.  
  437.     
  438.     def __getattr__(self, attr):
  439.         if attr == '__iter__':
  440.             
  441.             try:
  442.                 invkind = pythoncom.DISPATCH_METHOD | pythoncom.DISPATCH_PROPERTYGET
  443.                 enum = self._oleobj_.InvokeTypes(pythoncom.DISPID_NEWENUM, LCID, invkind, (13, 10), ())
  444.             except pythoncom.com_error:
  445.                 raise AttributeError, 'This object can not function as an iterator'
  446.  
  447.             
  448.             class Factory:
  449.                 
  450.                 def __init__(self, ob):
  451.                     self.ob = ob
  452.  
  453.                 
  454.                 def __call__(self):
  455.                     import win32com.client.util as win32com
  456.                     return win32com.client.util.Iterator(self.ob)
  457.  
  458.  
  459.             return Factory(enum)
  460.         if attr[0] == '_' and attr[-1] == '_':
  461.             raise AttributeError, attr
  462.         attr[-1] == '_'
  463.         
  464.         try:
  465.             return new.instancemethod(self._builtMethods_[attr], self, self.__class__)
  466.         except KeyError:
  467.             attr == '__iter__'
  468.             attr == '__iter__'
  469.         except:
  470.             attr == '__iter__'
  471.  
  472.         if self._olerepr_.mapFuncs.has_key(attr):
  473.             return self._make_method_(attr)
  474.         retEntry = None
  475.         if retEntry is not None:
  476.             
  477.             try:
  478.                 ret = self._mapCachedItems_[retEntry.dispid]
  479.                 debug_attr_print('Cached items has attribute!', ret)
  480.                 return ret
  481.             except (KeyError, AttributeError):
  482.                 None if self._olerepr_ and self._oleobj_ else None if retEntry is None else attr == '__iter__'
  483.                 None if self._olerepr_ and self._oleobj_ else None if retEntry is None else attr == '__iter__'
  484.                 debug_attr_print('Attribute %s not in cache' % attr)
  485.             except:
  486.                 None if self._olerepr_ and self._oleobj_ else None if retEntry is None else attr == '__iter__'<EXCEPTION MATCH>(KeyError, AttributeError)
  487.             
  488.  
  489.         None if self._olerepr_ and self._oleobj_ else None if retEntry is None else attr == '__iter__'
  490.         if retEntry is not None:
  491.             invoke_type = _GetDescInvokeType(retEntry, pythoncom.INVOKE_PROPERTYGET)
  492.             debug_attr_print('Getting property Id 0x%x from OLE object' % retEntry.dispid)
  493.             
  494.             try:
  495.                 ret = self._oleobj_.Invoke(retEntry.dispid, 0, invoke_type, 1)
  496.             except pythoncom.com_error:
  497.                 None if self._olerepr_ and self._oleobj_ else None if retEntry is None else attr == '__iter__'
  498.                 details = None if self._olerepr_ and self._oleobj_ else None if retEntry is None else attr == '__iter__'
  499.                 if details[0] in ERRORS_BAD_CONTEXT:
  500.                     self._olerepr_.mapFuncs[attr] = retEntry
  501.                     return self._make_method_(attr)
  502.                 raise pythoncom.com_error, details
  503.             except:
  504.                 details[0] in ERRORS_BAD_CONTEXT
  505.  
  506.             debug_attr_print('OLE returned ', ret)
  507.             return self._get_good_object_(ret)
  508.         raise AttributeError, '%s.%s' % (self._username_, attr)
  509.  
  510.     
  511.     def __setattr__(self, attr, value):
  512.         if self.__dict__.has_key(attr):
  513.             self.__dict__[attr] = value
  514.             return None
  515.         debug_attr_print('SetAttr called for %s.%s=%s on DispatchContainer' % (self._username_, attr, `value`))
  516.         raise AttributeError, "Property '%s.%s' can not be set." % (self._username_, attr)
  517.  
  518.  
  519.