home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 July / CMCD0704.ISO / Software / Shareware / Comunicatii / jyte / jyte.exe / dictionary.py < prev    next >
Encoding:
Python Source  |  2000-10-16  |  4.3 KB  |  141 lines

  1. """Python.Dictionary COM Server.
  2.  
  3. This module implements a simple COM server that acts much like a Python
  4. dictionary or as a standard string-keyed VB Collection.  The keys of
  5. the dictionary are strings and are case-insensitive.
  6.  
  7. It uses a highly customized policy to fine-tune the behavior exposed to
  8. the COM client.
  9.  
  10. The object exposes the following properties:
  11.  
  12.     int Count                       (readonly)
  13.     VARIANT Item(BSTR key)          (propget for Item)
  14.     Item(BSTR key, VARIANT value)   (propput for Item)
  15.  
  16.     Note that 'Item' is the default property, so the following forms of
  17.     VB code are acceptable:
  18.  
  19.         set ob = CreateObject("Python.Dictionary")
  20.         ob("hello") = "there"
  21.         ob.Item("hi") = ob("HELLO")
  22.  
  23. All keys are defined, returning VT_NULL (None) if a value has not been
  24. stored.  To delete a key, simply assign VT_NULL to the key.
  25.  
  26. The object responds to the _NewEnum method by returning an enumerator over
  27. the dictionary's keys. This allows for the following type of VB code:
  28.  
  29.     for each name in ob
  30.         debug.print name, ob(name)
  31.     next
  32. """
  33.  
  34. import string
  35. import pythoncom
  36. from win32com.server import util, policy
  37. from win32com.server.exception import COMException
  38. import winerror
  39. import types
  40. import pywintypes
  41.  
  42. from pythoncom import DISPATCH_METHOD, DISPATCH_PROPERTYGET
  43. from winerror import S_OK
  44.  
  45. UnicodeType = pywintypes.UnicodeType
  46. StringType = types.StringType
  47.  
  48.  
  49. class DictionaryPolicy(policy.BasicWrapPolicy):
  50.   ### BasicWrapPolicy looks for this
  51.   _com_interfaces_ = [ ]
  52.  
  53.   ### BasicWrapPolicy looks for this
  54.   _name_to_dispid_ = {
  55.     'item' : pythoncom.DISPID_VALUE,
  56.     '_newenum' : pythoncom.DISPID_NEWENUM,
  57.     'count' : 1,
  58.     }
  59.  
  60.   ### Auto-Registration process looks for these...
  61.   _reg_desc_ = 'Python Dictionary'
  62.   _reg_clsid_ = '{39b61048-c755-11d0-86fa-00c04fc2e03e}'
  63.   _reg_progid_ = 'Python.Dictionary'
  64.   _reg_verprogid_ = 'Python.Dictionary.1'
  65.   _reg_policy_spec_ = 'win32com.servers.dictionary.DictionaryPolicy'
  66.  
  67.   def _CreateInstance_(self, clsid, reqIID):
  68.     self._wrap_({ })
  69.     return pythoncom.WrapObject(self, reqIID)
  70.  
  71.   def _wrap_(self, ob):
  72.     self._obj_ = ob    # ob should be a dictionary
  73.  
  74.   def _invokeex_(self, dispid, lcid, wFlags, args, kwargs, serviceProvider):
  75.     if dispid == 0:    # item
  76.       l = len(args)
  77.       if l < 1:
  78.         raise COMException(desc="not enough parameters", scode=winerror.DISP_E_BADPARAMCOUNT)
  79.  
  80.       key = args[0]
  81.       if type(key) == UnicodeType:
  82.         pass
  83.       elif type(key) == StringType:
  84.         key = pywintypes.Unicode(key)
  85.       else:
  86.         ### the nArgErr thing should be 0-based, not reversed... sigh
  87.         raise COMException(desc="Key must be a string", scode=winerror.DISP_E_TYPEMISMATCH)
  88.  
  89.       key = key.lower()
  90.  
  91.       if wFlags & (DISPATCH_METHOD | DISPATCH_PROPERTYGET):
  92.         if l > 1:
  93.             raise COMException(scode=winerror.DISP_E_BADPARAMCOUNT)
  94.         try:
  95.           return self._obj_[key]
  96.         except KeyError:
  97.           return None    # unknown keys return None (VT_NULL)
  98.  
  99.       if l <> 2:
  100.         raise COMException(scode=winerror.DISP_E_BADPARAMCOUNT)
  101.       if args[1] is None:
  102.         # delete a key when None is assigned to it
  103.         try:
  104.           del self._obj_[key]
  105.         except KeyError:
  106.           pass
  107.       else:
  108.         self._obj_[key] = args[1]
  109.       return S_OK
  110.  
  111.     if dispid == 1:    # count
  112.       if not wFlags & DISPATCH_PROPERTYGET:
  113.         raise COMException(scode=winerror.DISP_E_MEMBERNOTFOUND)    # not found
  114.       if len(args) != 0:
  115.         raise COMException(scode=winerror.DISP_E_BADPARAMCOUNT)
  116.       return len(self._obj_)
  117.  
  118.     if dispid == pythoncom.DISPID_NEWENUM:
  119.       return util.NewEnum(self._obj_.keys())
  120.  
  121.     raise COMException(scode=winerror.DISP_E_MEMBERNOTFOUND)
  122.  
  123.   def _getidsofnames_(self, names, lcid):
  124.     ### this is a copy of MappedWrapPolicy._getidsofnames_ ...
  125.  
  126.     # Note: these names will always be StringType
  127.     name = string.lower(names[0])
  128.     try:
  129.       return (self._name_to_dispid_[name],)
  130.     except KeyError:
  131.       raise COMException(scode=winerror.DISP_E_MEMBERNOTFOUND,
  132.                                 desc="Member not found")
  133.  
  134.  
  135. def Register():
  136.   from win32com.server.register import UseCommandLine
  137.   return UseCommandLine(DictionaryPolicy)
  138.  
  139. if __name__ == '__main__':
  140.   Register()
  141.