[Zope] Wrapping ZSQL method for XML-RPC access

Erik Myllymaki Erik Myllymaki <erik.myllymaki@starpointe.ca>
Fri, 02 Aug 2002 12:09:16 -0700


Great - thanks for the tip:

------begin python script---------------
items=[]
zsql = getattr(context, sql_method)
rs = zsql(emp_no=10095)
names = rs.names()
for item in rs:
  new_dict = {}
  k=0
  while k < len(item):
    new_dict[names[k]] = item[k]
    k=k+1
  items.append(new_dict)

return items
----------end python script----------------

Seems I have to explicitely name the parameters I am passing into the
zsql_method though - any *magic* to get around this?

My goal is a generic script I can wrap around any zsql method so I will need
to pass the parameters in via a ist and parse...

Thanks again.

----- Original Message -----
From: "peter sabaini" <sabaini@inode.at>
To: "Erik Myllymaki" <erik.myllymaki@starpointe.ca>
Cc: <zope@zope.org>
Sent: Friday, August 02, 2002 11:30 AM
Subject: Re: [Zope] Wrapping ZSQL method for XML-RPC access


>
> use the magic of python, specifically the getattr() builtin :-)
>
> ie. you'd use for example
>
> zsql = getattr(context, 'getSalesmen')
> # 'getSalesmen' could be any string you want
> # now call your zsql method in the usual way
> rs = zsql(param='val')
> # do whatever youre going to do to your salesmen
>
> hth
> peter.
>
> Erik Myllymaki wrote:
> > So, thanks to the archives, I see that ZSQL methods need to be wrapped
to
> > get at them via XML-RPC. Modifying a snippet from a previous post, I got
the
> > following Python Script:
> >
> > -------Begin script-------------------
> > items=[]
> > names = context.getSalesmen().names()
> > for item in getSaleman():
> >   new_dict = {}
> >   k=0
> >   while k < len(item):
> >     new_dict[names[k]] = item[k]
> >     k=k+1
> >   items.append(new_dict)
> >
> > return items
> > --------End script-----------------
> >
> > this gives me what I need for the "getSalesmen" ZSQL method.
> >
> > But I would like to use the script with any ZSQL Method. So,  i will
need to
> > pass in two parameters - the first a ZSQL Method name, and the second, a
> > list of parameters.
> >
> > Problem is, I don't know how I would go about building and executing the
> > statement:
> >
> >  for item in getSalemen():
> >
> > if all I have is the string "getSalesmen" ?
> >
> > exec(), eval() ...
> >
> > TIA
> >
> >
> > _______________________________________________
> > Zope maillist  -  Zope@zope.org
> > http://lists.zope.org/mailman/listinfo/zope
> > **   No cross posts or HTML encoding!  **
> > (Related lists -
> >  http://lists.zope.org/mailman/listinfo/zope-announce
> >  http://lists.zope.org/mailman/listinfo/zope-dev )
> >
> >
>
>