Skip to content

Instantly share code, notes, and snippets.

@webknjaz
Forked from Giancarlos/smtest.py
Last active November 15, 2018 12:30
Show Gist options
  • Save webknjaz/208f68c0ca1a17df467b3879d23e2da8 to your computer and use it in GitHub Desktop.
Save webknjaz/208f68c0ca1a17df467b3879d23e2da8 to your computer and use it in GitHub Desktop.
import functools
import collections
import cherrypy
__metaclass__ = type # enable new-style classes by default
class SelectedMethod:
"""
Descriptor allowing a series of handler methods to satisfy
a variety of behaviors based on the request method.
Works similar to the MethodDispatcher, but doesn't
require replacing the whole dispatch mechanism, so
may be deployed selectively.
First, decorate your method using SelectedMethod::
class App:
@SelectedMethod
def index(self):
return "Hello World"
Then, add separate handlers for specific methods::
@index.GET
def get(self):
return "Your method was GET"
@index.POST
def delete(self):
db.delete(self.id)
The first handler passed will always handle any methods
not specified. The app may wish to
raise 405 in this case::
@SelectedMethod
def index(self):
raise cherrypy.HTTPError("405 Method Not Allowed")
"""
__isabstractmethod__ = False
def __init__(self, orig):
self.methods = collections.defaultdict(lambda: orig)
def __get__(self, obj, type=None):
method = self.methods[cherrypy.request.method]
bound = functools.partial(method, obj)
bound._cp_config = getattr(method, '_cp_config', {})
bound.exposed = True
return bound
def __getattr__(self, method):
"""
Return a decorator suitable for wrapping another
"""
def wrapper(func):
self.methods[method] = func
return func
return wrapper
class People:
@SelectedMethod
def index(self):
return "Default Index"
@index.GET
def get(self):
return "GET"
@index.POST
def post(self):
return "POST"
class WebUi:
def __init__(self):
self.people = People()
@cherrypy.expose()
def index(self):
raise cherrypy.HTTPRedirect('/ui/people/')
class WebRoot:
def __init__(self):
# Front-end API end-points loaded from '/web/routes/ui.py'
self.ui = WebUi()
@cherrypy.expose()
def index(self):
raise cherrypy.HTTPRedirect('/ui/')
conf = {} # For purposes of this code sample let's just assume this is properly defined with all the rules it needs....
cherrypy.tree.mount(WebRoot(), '/', conf)
cherrypy.engine.start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment