Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dictionary.py 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. import pywintypes
  26. import winerror
  27. from pythoncom import DISPATCH_METHOD, DISPATCH_PROPERTYGET
  28. from win32com.server import policy, util
  29. from win32com.server.exception import COMException
  30. from winerror import S_OK
  31. class DictionaryPolicy(policy.BasicWrapPolicy):
  32. ### BasicWrapPolicy looks for this
  33. _com_interfaces_ = []
  34. ### BasicWrapPolicy looks for this
  35. _name_to_dispid_ = {
  36. "item": pythoncom.DISPID_VALUE,
  37. "_newenum": pythoncom.DISPID_NEWENUM,
  38. "count": 1,
  39. }
  40. ### Auto-Registration process looks for these...
  41. _reg_desc_ = "Python Dictionary"
  42. _reg_clsid_ = "{39b61048-c755-11d0-86fa-00c04fc2e03e}"
  43. _reg_progid_ = "Python.Dictionary"
  44. _reg_verprogid_ = "Python.Dictionary.1"
  45. _reg_policy_spec_ = "win32com.servers.dictionary.DictionaryPolicy"
  46. def _CreateInstance_(self, clsid, reqIID):
  47. self._wrap_({})
  48. return pythoncom.WrapObject(self, reqIID)
  49. def _wrap_(self, ob):
  50. self._obj_ = ob # ob should be a dictionary
  51. def _invokeex_(self, dispid, lcid, wFlags, args, kwargs, serviceProvider):
  52. if dispid == 0: # item
  53. l = len(args)
  54. if l < 1:
  55. raise COMException(
  56. desc="not enough parameters", scode=winerror.DISP_E_BADPARAMCOUNT
  57. )
  58. key = args[0]
  59. if type(key) not in [str, str]:
  60. ### the nArgErr thing should be 0-based, not reversed... sigh
  61. raise COMException(
  62. desc="Key must be a string", scode=winerror.DISP_E_TYPEMISMATCH
  63. )
  64. key = key.lower()
  65. if wFlags & (DISPATCH_METHOD | DISPATCH_PROPERTYGET):
  66. if l > 1:
  67. raise COMException(scode=winerror.DISP_E_BADPARAMCOUNT)
  68. try:
  69. return self._obj_[key]
  70. except KeyError:
  71. return None # unknown keys return None (VT_NULL)
  72. if l != 2:
  73. raise COMException(scode=winerror.DISP_E_BADPARAMCOUNT)
  74. if args[1] is None:
  75. # delete a key when None is assigned to it
  76. try:
  77. del self._obj_[key]
  78. except KeyError:
  79. pass
  80. else:
  81. self._obj_[key] = args[1]
  82. return S_OK
  83. if dispid == 1: # count
  84. if not wFlags & DISPATCH_PROPERTYGET:
  85. raise COMException(scode=winerror.DISP_E_MEMBERNOTFOUND) # not found
  86. if len(args) != 0:
  87. raise COMException(scode=winerror.DISP_E_BADPARAMCOUNT)
  88. return len(self._obj_)
  89. if dispid == pythoncom.DISPID_NEWENUM:
  90. return util.NewEnum(list(self._obj_.keys()))
  91. raise COMException(scode=winerror.DISP_E_MEMBERNOTFOUND)
  92. def _getidsofnames_(self, names, lcid):
  93. ### this is a copy of MappedWrapPolicy._getidsofnames_ ...
  94. name = names[0].lower()
  95. try:
  96. return (self._name_to_dispid_[name],)
  97. except KeyError:
  98. raise COMException(
  99. scode=winerror.DISP_E_MEMBERNOTFOUND, desc="Member not found"
  100. )
  101. def Register():
  102. from win32com.server.register import UseCommandLine
  103. return UseCommandLine(DictionaryPolicy)
  104. if __name__ == "__main__":
  105. Register()