util.py :  » Windows » pyExcelerator » pywin32-214 » com » win32com » client » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Windows » pyExcelerator 
pyExcelerator » pywin32 214 » com » win32com » client » util.py
"""General client side utilities.

This module contains utility functions, used primarily by advanced COM
programmers, or other COM modules.
"""
import pythoncom
from win32com.client import Dispatch,_get_good_object_

PyIDispatchType = pythoncom.TypeIIDs[pythoncom.IID_IDispatch]

def WrapEnum(ob, resultCLSID = None):
  """Wrap an object in a VARIANT enumerator.  

  All VT_DISPATCHs returned by the enumerator are converted to wrapper objects
  (which may be either a class instance, or a dynamic.Dispatch type object).

  """
  if type(ob) != pythoncom.TypeIIDs[pythoncom.IID_IEnumVARIANT]:
    ob = ob.QueryInterface(pythoncom.IID_IEnumVARIANT)
  return EnumVARIANT(ob, resultCLSID)

class Enumerator:
  """A class that provides indexed access into an Enumerator

  By wrapping a PyIEnum* object in this class, you can perform
  natural looping and indexing into the Enumerator.

  Looping is very efficient, but it should be noted that although random 
  access is supported, the underlying object is still an enumerator, so 
  this will force many reset-and-seek operations to find the requested index.

  """
  def __init__(self, enum):
    self._oleobj_ = enum # a PyIEnumVARIANT
    self.index = -1
  def __getitem__(self, index):
    return self.__GetIndex(index)
  def __call__(self, index):
    return self.__GetIndex(index)
  
  def __GetIndex(self, index):
    if type(index)!=type(0): raise TypeError("Only integer indexes are supported for enumerators")
    # NOTE
    # In this context, self.index is users purely as a flag to say 
    # "am I still in sequence".  The user may call Next() or Reset() if they
    # so choose, in which case self.index will not be correct (although we
    # still want to stay in sequence)
    if index != self.index + 1:
      # Index requested out of sequence.
      self._oleobj_.Reset()
      if index: self._oleobj_.Skip(index) # if asked for item 1, must skip 1, Python always zero based.
    self.index = index
    result = self._oleobj_.Next(1)
    if len(result):
      return self._make_retval_(result[0])
    raise IndexError("list index out of range")
  def Next(self, count=1):
    ret = self._oleobj_.Next(count)
    realRets = []
    for r in ret:
      realRets.append(self._make_retval_(r))
    return tuple(realRets) # Convert back to tuple.
  def Reset(self):
    return self._oleobj_.Reset()
  def Clone(self):
    return self.__class__( self._oleobj_.Clone(), self.resultCLSID)
  def _make_retval_(self, result):
    return result

class EnumVARIANT(Enumerator):
  def __init__(self, enum, resultCLSID = None):
    self.resultCLSID = resultCLSID
    Enumerator.__init__(self, enum)
  def _make_retval_(self, result):
    return _get_good_object_(result, resultCLSID = self.resultCLSID)

class Iterator:
  def __init__(self, enum, resultCLSID = None):
    self.resultCLSID = resultCLSID
    self._iter_ = iter(enum.QueryInterface(pythoncom.IID_IEnumVARIANT))
  def __iter__(self):
    return self
  def next(self):
    return _get_good_object_(self._iter_.next(), resultCLSID = self.resultCLSID)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.