1

I'm exploring the use of python's bottle web framework. The web application must be extensible. That is, I will have a folder like "some/path/extensions/" into which future developers should be able to place extensions to this application. So for example: if the extension is called "treelayout" (with its own set of html files, js files etc, these files would be placed in .../.../extensions/treelayout/ folder. My goal is to have a url router that looks something like:

@GET("/some/path/extensions/:module/:func/:params")
def callmodule(module, func, params):
    #D = dictionary made out of variable number of params.
    #Then call module.func(D)  <== How can I do this given that module and func are strings?

Example, someone could call "h++p: //.../treelayout/create/maxnodes/100/fanout/10/depth/3" and this would call the new extension treelayout.create( {maxnodes:100, fanout:10, depth:3} ).

Any idea if this is possible to do with bottle or any other python framework?

G.A.
  • 1,443
  • 2
  • 22
  • 34

1 Answers1

0

The Python language can map string variables to loaded modules & methods.

import sys 

@GET("/some/path/extensions/:module/:func/:params")
def callmodule(module, func, params):
    D = dict(params)
    # Pull module instance from systems stack
    moduleInstance = sys.modules[module]
    # Grab method from module
    functionInstance = getattr(moduleInstance,func)
    # Pass "D" variable to function instance
    functionInstance(D)

Other great examples can be found in the answers to the following questions

Community
  • 1
  • 1
emcconville
  • 23,800
  • 4
  • 50
  • 66