Created
February 8, 2014 03:23
-
-
Save hersonls/8876217 to your computer and use it in GitHub Desktop.
Flask: Load blueprints from given path
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 | |
from importlib import import_module | |
from flask.blueprints import Blueprint | |
def load_blueprints_from_path(app, packages_path, blueprint_name='bp'): | |
blueprints = [] | |
for name in os.listdir(packages_path): | |
if os.path.isdir(os.path.join(packages_path, name)): | |
blueprint_name = blueprint_name | |
path, base_name = os.path.split(packages_path) | |
package_name = name | |
try: | |
package = import_module('{}.{}'.format(base_name, package_name)) | |
module = getattr(package, blueprint_name) | |
if isinstance(module, Blueprint): | |
blueprints.append(module) | |
# Register blueprint | |
app.register_blueprint(module) | |
print "Blueprint Installed: {}".format(name) | |
except ImportError, AttributeError: | |
pass | |
return blueprints |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment