Created
May 9, 2017 08:21
-
-
Save reox/e2955cafa93198ed544d27621f7d5340 to your computer and use it in GitHub Desktop.
Bottle as a class. Need to define the routing yourself, then you are fine
This file contains hidden or 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
from bottle import Bottle | |
# From http://stackoverflow.com/a/21112077 | |
def routemethod(route, **kwargs): | |
def decorator(f): | |
f.route = route | |
for arg in kwargs: | |
setattr(f, arg, kwargs[arg]) | |
return f | |
return decorator | |
class Bla(Bottle): | |
def __init__(self): | |
super(Bla, self).__init__() | |
self.routeapp() | |
def routeapp(self): | |
for kw in dir(self): | |
# It seems we do not like the config method here... | |
if kw in ['config']: | |
continue | |
attr = getattr(self, kw) | |
if hasattr(attr, "route"): | |
if hasattr(attr, "method"): | |
method = getattr(attr, "method") | |
else: | |
method = "GET" | |
if hasattr(attr, "callback"): | |
callback = getattr(attr, "callback") | |
else: | |
callback = None | |
if hasattr(attr, "name"): | |
name = getattr(attr, "name") | |
else: | |
name = None | |
if hasattr(attr, "apply"): | |
aply = getattr(attr, "apply") | |
else: | |
aply = None | |
if hasattr(attr, "skip"): | |
skip = getattr(attr, "skip") | |
else: | |
skip = None | |
print(kw, attr) | |
self.route(attr.route, method, callback, name, aply, skip)(attr) | |
@routemethod('/') | |
def test(self): | |
return "XXX" | |
if __name__ == "__main__": | |
b = Bla() | |
b.run(host='0.0.0.0', debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment