Created
December 24, 2015 09:20
-
-
Save kacieh80/92816f117cfee934a7f5 to your computer and use it in GitHub Desktop.
Pretty Flask like routes in Falcon python framework
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
import os | |
import pkgutil | |
class route(object): | |
""" Decorates RequestHandlers and builds a list of routes """ | |
_routes = [] | |
def __init__(self, uri): | |
""" Initializes route object | |
Inputs: | |
uri: uri of the Request Handler | |
""" | |
self._uri = uri | |
def __call__(self, _handler): | |
""" Adds Handler instance and URI and append to the list of routes | |
Input: | |
_handler: falcon Request Handler object | |
""" | |
self._routes.append([self._uri, _handler()]) | |
return _handler | |
@classmethod | |
def get_routes(cls): | |
""" Returns list of routes """ | |
return cls._routes | |
def add_routes(app): | |
""" Get all routes from handler decorator and add them to the app """ | |
routes = route.get_routes() | |
for r in routes: | |
app.add_route(r[0], r[1]) | |
def autoload(dirname): | |
""" Autoload all modules in a directory """ | |
for path, directories, files in os.walk(dirname): | |
for importer, package_name, _ in pkgutil.iter_modules([path]): | |
# Supposedly, this means the module is already loaded, but that is | |
# not the case for tests. It shouldn't hurt to reload them anyways. | |
# if package_name not in sys.modules or True: | |
importer.find_module(package_name).load_module(package_name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment