Last active
November 22, 2015 20:34
-
-
Save jesuscast/b2b1890b13d484a3aae0 to your computer and use it in GitHub Desktop.
Really good examples of exposing class decorators. Taken from bottle.py Nothing here makes sense it is just for me to remember how to write some code.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
+route = functools.wraps(Bottle.route)(lambda *a, **ka: app().route(*a, **ka)) | |
+get = functools.wraps(Bottle.get)(lambda *a, **ka: app().get(*a, **ka)) | |
+post = functools.wraps(Bottle.post)(lambda *a, **ka: app().post(*a, **ka)) | |
+put = functools.wraps(Bottle.put)(lambda *a, **ka: app().put(*a, **ka)) | |
+delete = functools.wraps(Bottle.delete)(lambda *a, **ka: app().delete(*a, **ka)) | |
+error = functools.wraps(Bottle.error)(lambda code: app().error(code)) | |
def route(self, path=None, method='GET', **kargs): | |
""" Decorator: Bind a function to a GET request path. | |
If the path parameter is None, the signature of the decorated | |
function is used to generate the path. See yieldroutes() | |
for details. | |
The method parameter (default: GET) specifies the HTTP request | |
method to listen to. You can specify a list of methods. | |
""" | |
if isinstance(method, str): #TODO: Test this | |
method = method.split(';') | |
def wrapper(callback): | |
paths = [] if path is None else [path.strip().lstrip('/')] | |
if not paths: # Lets generate the path automatically | |
paths = yieldroutes(callback) | |
for p in paths: | |
for m in method: | |
route = m.upper() + ';' + p | |
self.routes.add(route, callback, **kargs) | |
return callback | |
return wrapper | |
def make_default_app_wrapper(name): | |
""" Return a callable that relays calls to the current default app. """ | |
@functools.wraps(getattr(Bottle, name)) | |
def wrapper(*a, **ka): | |
return getattr(app(), name)(*a, **ka) | |
return wrapper | |
route = make_default_app_wrapper('route') | |
get = make_default_app_wrapper('get') | |
post = make_default_app_wrapper('post') | |
put = make_default_app_wrapper('put') | |
delete = make_default_app_wrapper('delete') | |
patch = make_default_app_wrapper('patch') | |
error = make_default_app_wrapper('error') | |
mount = make_default_app_wrapper('mount') | |
hook = make_default_app_wrapper('hook') | |
install = make_default_app_wrapper('install') | |
uninstall = make_default_app_wrapper('uninstall') | |
url = make_default_app_wrapper('get_url') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment