dictionary.py 4.0 KB

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