How do I create a dynamic wrapper for another language?

has has.temp2 at virgin.net
Fri May 21 10:33:18 EDT 2004


> Whitney Battestilli wrote:
> 
> > I'm trying to embed python into an application that already contains a 
> > scripting language.  This scripting language contains thousands of 
> > commands and I have the ability to query the script engine and get 
> > syntax information regarding valid arguments and such. 
> > 
> > Rather than writing explicate wrappers for each command (which will be 
> > very time consuming), I would like to extend python by creating a module 
> > that allows any function name to be executed with any number of keyword 
> > arguments..  I would then like to take the function name and keyword 
> > arguments and pass these to the apps script engine in the format required.
> > 
> > Is there any way to do this or any way to do something like this?

It's pretty easy using Python's introspection features to supply the
syntactic sugar. Simple example:

class _Callable:
    def __init__(self, name):
        self._name = name
		
    def __call__(self, **args):
        # [Do argument name checking, etc. here]
        # [Dispatch command here]
        print 'Sending command %r with arguments: %r' % (self._name,
args) # TEST

class Bridge:
    def __getattr__(self, name):
        # [Do command name checking here]
        return _Callable(name)


bridge = Bridge()
bridge.foo(bar=1, baz=True) # -> Sending command 'foo' with arguments
{'baz': True, 'bar': 1}


Also see my Python-to-Apple Event Manager bridge for ideas and/or
code: http://freespace.virgin.net/hamish.sanderson/appscript.html



More information about the Python-list mailing list