<commit_before>from functools import reduce
import logging

from flask import Blueprint, current_app, url_for

from . import __version__
from .api.manager import OpenApiManager
from .babel.manager import BabelManager
from .const import (
    LOGMSG_ERR_FAB_ADD_PERMISSION_MENU,
    LOGMSG_ERR_FAB_ADD_PERMISSION_VIEW,
    LOGMSG_ERR_FAB_ADDON_IMPORT,
    LOGMSG_ERR_FAB_ADDON_PROCESS,
    LOGMSG_INF_FAB_ADD_VIEW,
    LOGMSG_INF_FAB_ADDON_ADDED,
    LOGMSG_WAR_FAB_VIEW_EXISTS
)
from .filters import TemplateFilters
from .menu import Menu
from .views import IndexView, UtilView

log = logging.getLogger(__name__)


def dynamic_class_import(class_path):
    """
        Will dynamically import a class from a string path
        :param class_path: string with class path
        :return: class
    """
    # Split first occurrence of path
    try:
        tmp = class_path.split(".")
        module_path = ".".join(tmp[0:-1])
        package = __import__(module_path)
        return reduce(getattr, tmp[1:], package)
    except Exception as e:
        log.exception(e)
        log.error(LOGMSG_ERR_FAB_ADDON_IMPORT.format(class_path, e))


class AppBuilder(object):
    """


        This is the base class for all the framework.
        This is were you will register all your views
        and create the menu structure.
        Will hold your flask app object, all your views, and security classes.

        initialize your application like this for SQLAlchemy::

            from flask import Flask
            from flask_appbuilder import SQLA, AppBuilder

            app = Flask(__name__)
            app.config.from_object('config')
            db = SQLA(app)
            appbuilder = AppBuilder(app, db.session)

        When using MongoEngine::

            from flask import Flask
            from flask_appbuilder import AppBuilder
            from flask_appbuilder.security.mongoengine.manager import SecurityManager
            from flask_mongoengine import MongoEngine

            app = Flask(__name__)
            app.config.from_object('config')
            dbmongo = MongoEngine(app)
            appbuilder = AppBuilder(app, security_manager_class=SecurityManager)

        You can also create everything as an application factory.
    """

    baseviews = []
    security_manager_class = None
    # Flask app
    app = None
    # Database Session
    session = None
    # Security Manager Class
    sm = None
    # Babel Manager Class
    bm = None
    # OpenAPI Manager Class
    openapi_manager = None
    # dict with addon name has key and intantiated class has value
    addon_managers = None
    # temporary list that hold addon_managers config key
    _addon_managers = None

    menu = None
    indexview = None

    static_folder = None
    static_url_path = None

    template_filters = None

    def __init__(
        self,
        app=None,
        session=None,
        menu=None,
        indexview=None,
        base_template="appbuilder/baselayout.html",
        static_folder="static/appbuilder",
        static_url_path="/appbuilder",
        security_manager_class=None,
    ):
        """
            AppBuilder constructor

            :param app:
                The flask app object
            :param session:
                The SQLAlchemy session object
            :param menu:
                optional, a previous contructed menu
            :param indexview:
                optional, your customized indexview
            :param static_folder:
                optional, your override for the global static folder
            :param static_url_path:
                optional, your override for the global static url path
            :param security_manager_class:
                optional, pass your own security manager class
        """
        self.baseviews = []
        self._addon_managers = []
        self.addon_managers = {}
        self.menu = menu or Menu()
        self.base_template = base_template
        self.security_manager_class = security_manager_class
        self.indexview = indexview or IndexView
        self.static_folder = static_folder
        self.static_url_path = static_url_path
        self.app = app
        self.update_perms = False

        if app is not None:
            self.init_app(app, session)

    def init_app(self, app, session):
        """
            Will initialize the Flask app, supporting the app factory pattern.

            :param app:
            :param session: The SQLAlchemy session

        """
        app.config.setdefault("APP_NAME", "F.A.B.")
        app.config.setdefault("APP_THEME", "")
        app.config.setdefault("APP_ICON", "")
        app.config.setdefault("LANGUAGES", {"en": {"flag": "gb", "name": "English"}})
        app.config.setdefault("ADDON_MANAGERS", [])
        app.config.setdefault("FAB_API_MAX_PAGE_SIZE", 20)
        self.update_perms = app.config.get('FAB_UPDATE_PERMS', True)
        _security_manager_class_name = app.config.get('FAB_SECURITY_MANAGER_CLASS', None)
        if _security_manager_class_name is not None:
            self.security_manager_class = dynamic_class_import(
                _security_manager_class_name
            )
        if self.security_manager_class is None:
            from flask_appbuilder.security.sqla.manager import SecurityManager
            self.security_manager_class = SecurityManager

        self._addon_managers = app.config["ADDON_MANAGERS"]
        self.session = session
        self.sm = self.security_manager_class(self)
        self.bm = BabelManager(self)
        self.openapi_manager = OpenApiManager(self)
        self._add_global_static()
        self._add_global_filters()
        app.before_request(self.sm.before_request)<commit_msg>[base] Fix, AppBuilder app on app factory not set<commit_after>from functools import reduce
import logging

from flask import Blueprint, current_app, url_for

from . import __version__
from .api.manager import OpenApiManager
from .babel.manager import BabelManager
from .const import (
    LOGMSG_ERR_FAB_ADD_PERMISSION_MENU,
    LOGMSG_ERR_FAB_ADD_PERMISSION_VIEW,
    LOGMSG_ERR_FAB_ADDON_IMPORT,
    LOGMSG_ERR_FAB_ADDON_PROCESS,
    LOGMSG_INF_FAB_ADD_VIEW,
    LOGMSG_INF_FAB_ADDON_ADDED,
    LOGMSG_WAR_FAB_VIEW_EXISTS
)
from .filters import TemplateFilters
from .menu import Menu
from .views import IndexView, UtilView

log = logging.getLogger(__name__)


def dynamic_class_import(class_path):
    """
        Will dynamically import a class from a string path
        :param class_path: string with class path
        :return: class
    """
    # Split first occurrence of path
    try:
        tmp = class_path.split(".")
        module_path = ".".join(tmp[0:-1])
        package = __import__(module_path)
        return reduce(getattr, tmp[1:], package)
    except Exception as e:
        log.exception(e)
        log.error(LOGMSG_ERR_FAB_ADDON_IMPORT.format(class_path, e))


class AppBuilder(object):
    """


        This is the base class for all the framework.
        This is were you will register all your views
        and create the menu structure.
        Will hold your flask app object, all your views, and security classes.

        initialize your application like this for SQLAlchemy::

            from flask import Flask
            from flask_appbuilder import SQLA, AppBuilder

            app = Flask(__name__)
            app.config.from_object('config')
            db = SQLA(app)
            appbuilder = AppBuilder(app, db.session)

        When using MongoEngine::

            from flask import Flask
            from flask_appbuilder import AppBuilder
            from flask_appbuilder.security.mongoengine.manager import SecurityManager
            from flask_mongoengine import MongoEngine

            app = Flask(__name__)
            app.config.from_object('config')
            dbmongo = MongoEngine(app)
            appbuilder = AppBuilder(app, security_manager_class=SecurityManager)

        You can also create everything as an application factory.
    """

    baseviews = []
    security_manager_class = None
    # Flask app
    app = None
    # Database Session
    session = None
    # Security Manager Class
    sm = None
    # Babel Manager Class
    bm = None
    # OpenAPI Manager Class
    openapi_manager = None
    # dict with addon name has key and intantiated class has value
    addon_managers = None
    # temporary list that hold addon_managers config key
    _addon_managers = None

    menu = None
    indexview = None

    static_folder = None
    static_url_path = None

    template_filters = None

    def __init__(
        self,
        app=None,
        session=None,
        menu=None,
        indexview=None,
        base_template="appbuilder/baselayout.html",
        static_folder="static/appbuilder",
        static_url_path="/appbuilder",
        security_manager_class=None,
    ):
        """
            AppBuilder constructor

            :param app:
                The flask app object
            :param session:
                The SQLAlchemy session object
            :param menu:
                optional, a previous contructed menu
            :param indexview:
                optional, your customized indexview
            :param static_folder:
                optional, your override for the global static folder
            :param static_url_path:
                optional, your override for the global static url path
            :param security_manager_class:
                optional, pass your own security manager class
        """
        self.baseviews = []
        self._addon_managers = []
        self.addon_managers = {}
        self.menu = menu or Menu()
        self.base_template = base_template
        self.security_manager_class = security_manager_class
        self.indexview = indexview or IndexView
        self.static_folder = static_folder
        self.static_url_path = static_url_path
        self.app = app
        self.update_perms = False

        if app is not None:
            self.init_app(app, session)

    def init_app(self, app, session):
        """
            Will initialize the Flask app, supporting the app factory pattern.

            :param app:
            :param session: The SQLAlchemy session

        """
        app.config.setdefault("APP_NAME", "F.A.B.")
        app.config.setdefault("APP_THEME", "")
        app.config.setdefault("APP_ICON", "")
        app.config.setdefault("LANGUAGES", {"en": {"flag": "gb", "name": "English"}})
        app.config.setdefault("ADDON_MANAGERS", [])
        app.config.setdefault("FAB_API_MAX_PAGE_SIZE", 20)
        self.app = app
        self.update_perms = app.config.get('FAB_UPDATE_PERMS', True)
        _security_manager_class_name = app.config.get('FAB_SECURITY_MANAGER_CLASS', None)
        if _security_manager_class_name is not None:
            self.security_manager_class = dynamic_class_import(
                _security_manager_class_name
            )
        if self.security_manager_class is None:
            from flask_appbuilder.security.sqla.manager import SecurityManager
            self.security_manager_class = SecurityManager

        self._addon_managers = app.config["ADDON_MANAGERS"]
        self.session = session
        self.sm = self.security_manager_class(self)
        self.bm = BabelManager(self)
        self.openapi_manager = OpenApiManager(self)
        self._add_global_static()
        self._add_global_filters()
        app.before_request(self.sm.before_request)
{'fraction_non_alphanumeric': 0.0700825010855406, 'fraction_numerical': 0.0008684324793747286, 'mean_word_length': 2.3486478627508, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '481428', 'n_tokens_mistral': 3252, 'n_tokens_neox': 3145, 'n_words': 837}