dictionary.py :  » Windows » pyExcelerator » pywin32-214 » com » win32com » servers » 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 » servers » dictionary.py
"""Python.Dictionary COM Server.

This module implements a simple COM server that acts much like a Python
dictionary or as a standard string-keyed VB Collection.  The keys of
the dictionary are strings and are case-insensitive.

It uses a highly customized policy to fine-tune the behavior exposed to
the COM client.

The object exposes the following properties:

    int Count                       (readonly)
    VARIANT Item(BSTR key)          (propget for Item)
    Item(BSTR key, VARIANT value)   (propput for Item)

    Note that 'Item' is the default property, so the following forms of
    VB code are acceptable:

        set ob = CreateObject("Python.Dictionary")
        ob("hello") = "there"
        ob.Item("hi") = ob("HELLO")

All keys are defined, returning VT_NULL (None) if a value has not been
stored.  To delete a key, simply assign VT_NULL to the key.

The object responds to the _NewEnum method by returning an enumerator over
the dictionary's keys. This allows for the following type of VB code:

    for each name in ob
        debug.print name, ob(name)
    next
"""

import pythoncom
from win32com.server import util,policy
from win32com.server.exception import COMException
import winerror
import types
import pywintypes

from pythoncom import DISPATCH_METHOD,DISPATCH_PROPERTYGET
from winerror import S_OK

class DictionaryPolicy(policy.BasicWrapPolicy):
  ### BasicWrapPolicy looks for this
  _com_interfaces_ = [ ]

  ### BasicWrapPolicy looks for this
  _name_to_dispid_ = {
    'item' : pythoncom.DISPID_VALUE,
    '_newenum' : pythoncom.DISPID_NEWENUM,
    'count' : 1,
    }

  ### Auto-Registration process looks for these...
  _reg_desc_ = 'Python Dictionary'
  _reg_clsid_ = '{39b61048-c755-11d0-86fa-00c04fc2e03e}'
  _reg_progid_ = 'Python.Dictionary'
  _reg_verprogid_ = 'Python.Dictionary.1'
  _reg_policy_spec_ = 'win32com.servers.dictionary.DictionaryPolicy'

  def _CreateInstance_(self, clsid, reqIID):
    self._wrap_({ })
    return pythoncom.WrapObject(self, reqIID)

  def _wrap_(self, ob):
    self._obj_ = ob  # ob should be a dictionary

  def _invokeex_(self, dispid, lcid, wFlags, args, kwargs, serviceProvider):
    if dispid == 0:  # item
      l = len(args)
      if l < 1:
        raise COMException(desc="not enough parameters", scode=winerror.DISP_E_BADPARAMCOUNT)

      key = args[0]
      if type(key) not in [str, unicode]:
        ### the nArgErr thing should be 0-based, not reversed... sigh
        raise COMException(desc="Key must be a string", scode=winerror.DISP_E_TYPEMISMATCH)

      key = key.lower()

      if wFlags & (DISPATCH_METHOD | DISPATCH_PROPERTYGET):
        if l > 1:
            raise COMException(scode=winerror.DISP_E_BADPARAMCOUNT)
        try:
          return self._obj_[key]
        except KeyError:
          return None  # unknown keys return None (VT_NULL)

      if l != 2:
        raise COMException(scode=winerror.DISP_E_BADPARAMCOUNT)
      if args[1] is None:
        # delete a key when None is assigned to it
        try:
          del self._obj_[key]
        except KeyError:
          pass
      else:
        self._obj_[key] = args[1]
      return S_OK

    if dispid == 1:  # count
      if not wFlags & DISPATCH_PROPERTYGET:
        raise COMException(scode=winerror.DISP_E_MEMBERNOTFOUND)  # not found
      if len(args) != 0:
        raise COMException(scode=winerror.DISP_E_BADPARAMCOUNT)
      return len(self._obj_)

    if dispid == pythoncom.DISPID_NEWENUM:
      return util.NewEnum(self._obj_.keys())

    raise COMException(scode=winerror.DISP_E_MEMBERNOTFOUND)

  def _getidsofnames_(self, names, lcid):
    ### this is a copy of MappedWrapPolicy._getidsofnames_ ...

    name = names[0].lower()
    try:
      return (self._name_to_dispid_[name],)
    except KeyError:
      raise COMException(scode=winerror.DISP_E_MEMBERNOTFOUND,
                                desc="Member not found")


def Register():
  from win32com.server.register import UseCommandLine
  return UseCommandLine(DictionaryPolicy)

if __name__ == '__main__':
  Register()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.