Created
December 11, 2011 03:26
-
-
Save fhats/1458068 to your computer and use it in GitHub Desktop.
Subclass of Flask to automatically store routes in the view handler's docstrings
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
class FlaskWithRouteDocs(Flask): | |
"""A subclass of Flask to allow using :route: and :methods: in docstrings, a la Sphinx.""" | |
def route(self, rule, **options): | |
def decorator(f): | |
route_dec = super(FlaskWithRouteDocs, self).route(rule, **options) | |
methods_str = ','.join(options.get('methods', ['GET'])) | |
if f.__doc__ is not None and ":route:" in f.__doc__: | |
f.__doc__ = f.__doc__.replace(":route:", rule) | |
else: | |
f.__doc__ = "%s\n\n **Route:** ``%s``" % (f.__doc__, rule) | |
if f.__doc__ is not None and ":methods:" in f.__doc__: | |
f.__doc__ = f.__doc__.replace(":methods:", methods_str) | |
else: | |
f.__doc__ = "%s\n\n **Methods:** ``%s``" % (f.__doc__, methods_str) | |
return route_dec(f) | |
return decorator |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment