Skip to content
This repository has been archived by the owner on Oct 1, 2020. It is now read-only.

replace load_blueprints to a better version #151

Closed
rochacbruno opened this issue Jun 5, 2014 · 0 comments
Closed

replace load_blueprints to a better version #151

rochacbruno opened this issue Jun 5, 2014 · 0 comments

Comments

@rochacbruno
Copy link
Collaborator

# coding: utf-8
import os
import random
import logging

from werkzeug.utils import import_string

logger = logging.getLogger()


def load_blueprints_from_folder(app, folder_path=None, object_name=None):
    folder_path = (
        folder_path or
        os.path.join(app.root_path, 'blueprints')
    )
    object_name = object_name or app.config.get(
        'BLUEPRINTS_OBJECT_NAME', 'blueprint'
    )
    dir_list = os.listdir(folder_path)
    base_module_name = app.name.replace(".app", "")
    loaded_blueprints = {}
    for fname in dir_list:
        is_valid_module = all([
            not os.path.exists(os.path.join(folder_path, fname, 'DISABLED')),
            os.path.isdir(os.path.join(folder_path, fname)),
            os.path.exists(os.path.join(folder_path, fname, '__init__.py'))
        ])
        if is_valid_module:
            import_name = ".".join([base_module_name, fname, object_name])
            try:
                blueprint = import_string(import_name)
            except ImportError:
                app.logger.error("Cannot load %s blueprint", fname)
            else:
                loaded_blueprints[fname] = blueprint
                if blueprint.name not in app.blueprints:
                    app.register_blueprint(blueprint)
                else:
                    blueprint.name += str(random.getrandbits(8))
                    app.register_blueprint(blueprint)
    logger.info("{0} modules loaded".format(loaded_blueprints.keys()))
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant