testDynamic.py :  » Windows » pyExcelerator » pywin32-214 » com » win32com » test » 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 » test » testDynamic.py
# Test dynamic policy, and running object table.

import pythoncom
import winerror

from win32com.server.exception import Exception

error = "testDynamic error"

iid = pythoncom.MakeIID("{b48969a0-784b-11d0-ae71-d23f56000000}")

class VeryPermissive:
    def _dynamic_(self, name, lcid, wFlags, args):
        if wFlags & pythoncom.DISPATCH_METHOD:
            return getattr(self,name)(*args)

        if wFlags & pythoncom.DISPATCH_PROPERTYGET:
            try:
                # to avoid problems with byref param handling, tuple results are converted to lists.
                ret = self.__dict__[name]
                if type(ret)==type(()):
                    ret = list(ret)
                return ret
            except KeyError: # Probably a method request.
                raise Exception(scode=winerror.DISP_E_MEMBERNOTFOUND)

        if wFlags & (pythoncom.DISPATCH_PROPERTYPUT | pythoncom.DISPATCH_PROPERTYPUTREF):
            setattr(self, name, args[0])
            return

        raise Exception(scode=winerror.E_INVALIDARG, desc="invalid wFlags")

    def write(self, *args):
        if len(args)==0:
            raise Exception(scode=winerror.DISP_E_BADPARAMCOUNT) # Probably call as PROPGET.

        for arg in args[:-1]:
            print str(arg),
        print str(args[-1])

def Test():
    import win32com.server.util, win32com.server.policy
#       import win32dbg;win32dbg.brk()
    ob = win32com.server.util.wrap(VeryPermissive(),usePolicy=win32com.server.policy.DynamicPolicy)
    try:
        handle = pythoncom.RegisterActiveObject(ob, iid, 0)
    except pythoncom.com_error, details:
        print "Warning - could not register the object in the ROT:", details
        handle = None
    try:
        import win32com.client.dynamic
        client = win32com.client.dynamic.Dispatch(iid)
        client.ANewAttr = "Hello"
        if client.ANewAttr != "Hello":
            raise error("Could not set dynamic property")

        v = ["Hello","From","Python",1.4]
        client.TestSequence = v
        if v != list(client.TestSequence):
            raise error("Dynamic sequences not working! %r/%r" % (repr(v), repr(client.testSequence)))

        client.write("This","output","has","come","via","COM")
        # Check our new "_FlagAsMethod" works (kinda!)
        client._FlagAsMethod("NotReallyAMethod")
        if not callable(client.NotReallyAMethod):
            raise error("Method I flagged as callable isn't!")


        client = None
    finally:
        if handle is not None:
            pythoncom.RevokeActiveObject(handle)

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