def before_wiki(): """ Setup environment for wiki requests, start timers. """ logging.debug("running before_wiki") flaskg.clock = Clock() flaskg.clock.start('total') flaskg.clock.start('init') try: flaskg.unprotected_storage = app.storage flaskg.user = setup_user() flaskg.storage = protecting.ProtectingMiddleware(app.storage, flaskg.user, app.cfg.acl_mapping) flaskg.dicts = app.cfg.dicts() flaskg.groups = app.cfg.groups() flaskg.content_lang = app.cfg.language_default flaskg.current_lang = app.cfg.language_default setup_jinja_env() # request.user_agent == '' if this is pytest flaskg.add_lineno_attr = request.user_agent and flaskg.user.edit_on_doubleclick finally: flaskg.clock.stop('init')
def create_app_ext(flask_config_file=None, flask_config_dict=None, moin_config_class=None, warn_default=True, **kwargs): """ Factory for moin wsgi apps :param flask_config_file: a flask config file name (may have a MOINCFG class), if not given, a config pointed to by MOINCFG env var will be loaded (if possible). :param flask_config_dict: a dict used to update flask config (applied after flask_config_file was loaded [if given]) :param moin_config_class: if you give this, it'll be instantiated as app.cfg, otherwise it'll use MOINCFG from flask config. If that also is not there, it'll use the DefaultConfig built into MoinMoin. :param warn_default: emit a warning if moin falls back to its builtin default config (maybe user forgot to specify MOINCFG?) :param kwargs: if you give additional keyword args, the keys/values will get patched into the moin configuration class (before its instance is created) """ clock = Clock() clock.start('create_app total') app = Flask('moin') clock.start('create_app load config') if flask_config_file: app.config.from_pyfile(path.abspath(flask_config_file)) else: if not app.config.from_envvar('MOINCFG', silent=True): # no MOINCFG env variable set, try stuff in cwd: flask_config_file = path.abspath('wikiconfig_local.py') if not path.exists(flask_config_file): flask_config_file = path.abspath('wikiconfig.py') if not path.exists(flask_config_file): # we should be here only because wiki admin is performing `moin create-instance` if 'create-instance' in sys.argv: config_path = path.dirname(config.__file__) flask_config_file = path.join(config_path, 'wikiconfig.py') else: flask_config_file = None if flask_config_file: app.config.from_pyfile(path.abspath(flask_config_file)) if flask_config_dict: app.config.update(flask_config_dict) Config = moin_config_class if not Config: Config = app.config.get('MOINCFG') if not Config: if warn_default: logging.warning("using builtin default configuration") from moin.config.default import DefaultConfig as Config for key, value in kwargs.items(): setattr(Config, key, value) if Config.secrets is None: # reuse the secret configured for flask (which is required for sessions) Config.secrets = app.config.get('SECRET_KEY') app.cfg = Config() clock.stop('create_app load config') clock.start('create_app register') # register converters from werkzeug.routing import BaseConverter class ItemNameConverter(BaseConverter): """Like the default :class:`UnicodeConverter`, but it also matches slashes (except at the beginning AND end). This is useful for wikis and similar applications:: Rule('/<itemname:wikipage>') Rule('/<itemname:wikipage>/edit') """ regex = '[^/]+?(/[^/]+?)*' weight = 200 app.url_map.converters['itemname'] = ItemNameConverter # register modules, before/after request functions from moin.apps.frontend import frontend frontend.before_request(before_wiki) frontend.teardown_request(teardown_wiki) app.register_blueprint(frontend) from moin.apps.admin import admin admin.before_request(before_wiki) admin.teardown_request(teardown_wiki) app.register_blueprint(admin, url_prefix='/+admin') from moin.apps.feed import feed feed.before_request(before_wiki) feed.teardown_request(teardown_wiki) app.register_blueprint(feed, url_prefix='/+feed') from moin.apps.misc import misc misc.before_request(before_wiki) misc.teardown_request(teardown_wiki) app.register_blueprint(misc, url_prefix='/+misc') from moin.apps.serve import serve app.register_blueprint(serve, url_prefix='/+serve') clock.stop('create_app register') clock.start('create_app flask-cache') # the 'simple' caching uses a dict and is not thread safe according to the docs. cache = Cache(config={'CACHE_TYPE': 'simple'}) cache.init_app(app) app.cache = cache clock.stop('create_app flask-cache') # init storage clock.start('create_app init backends') init_backends(app) clock.stop('create_app init backends') clock.start('create_app flask-babel') i18n_init(app) clock.stop('create_app flask-babel') # configure templates clock.start('create_app flask-theme') setup_themes(app) if app.cfg.template_dirs: app.jinja_env.loader = ChoiceLoader([ FileSystemLoader(app.cfg.template_dirs), app.jinja_env.loader, ]) app.register_error_handler(403, themed_error) clock.stop('create_app flask-theme') clock.stop('create_app total') del clock return app