I agree that explicit is best, but this is the approach I use for my ajax
handler methods (so I don't have to mess with anything every time I add a
new web method!)

(Note: I originally wrote the following "FindAndCall" function in C#, and
when I was new to pythonI converted it, so don't make fun of me if it's not
pythonic!  It works, and I've not messed with it since.)

So, the LAST item in my urls list is:

   '/(.*)', 'wmHandler'


Then, here's the class to handle those requests:

class wmHandler:
    # the GET and POST methods here are hooked by web.py.
    # whatever method is requested, that function is called.

    def GET(self, method):
        return FindAndCall(method)


    def POST(self, method):
        return FindAndCall(method)


And the FindAndCall function:















*def FindAndCall(method, args=None):    """    Several rules here:    1) a
/ in the "method" denotes class/function.  This works if the source file
    is in the same directory as the executable.  All methods *should* have
a /.        If they don't, it's ok, it'll just look in the local namespace
for the function.    2) a . in the "method" is for a
package.module.class/function type lookup.        (This is when the class
is in a lib somewhere in the path.)        In this case, *we have hardcoded
that the class and module names must match*.        So, if the "method"
argument is foo.bar/baz, it will do "from foo import bar"        and then
the function being hooked would be "bar.baz()"    """    # does it have a /
?  if so let's look for another class.    # NOTE: this isn't recursive...
only the first value before a / is the class    *


































*    modname = ""    methodname = method    classname = ""    if "/" in
method:        modname, methodname = method.split('/', 1)        classname
= modname    if "." in modname:        modname, classname =
modname.split('.', 1)        modname = "%s.%s" % (modname, classname)    if
modname and classname and methodname:        try:            mod =
__import__(modname, globals(), locals(), classname)            cls =
getattr(mod, classname, None)            if cls:
methodToCall = getattr(cls(), methodname, None)            else:
    return "Class [%s] does not exist or could not be loaded." % modname
    except ImportError as ex:            logger.error(ex.__str__())
    return "Module [%s] does not exist." % modname    else:
methodToCall = getattr(globals, methodname, None)    if methodToCall:
  if callable(methodToCall):            if args:                return
methodToCall(args)            else:                return methodToCall()
return "Method [%s] does not exist or could not be called." % method*

So, the URL that would invoke this is:

http://localhost/wmHandler/module.class/function

Other variations will work, if you don't wanna have classes in your
modules, etc, but hopefully you get the idea and should definitely improve
this as you need.

Good luck!
NSC

-- 
You received this message because you are subscribed to the Google Groups 
"web.py" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/webpy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to