##// END OF EJS Templates
py3: rename all existing safe_unicode to safe_str
py3: rename all existing safe_unicode to safe_str

File last commit:

r8040:a5e719bc default
r8078:08eec03c default
Show More
app_cfg.py
213 lines | 8.0 KiB | text/x-python | PythonLexer
Thomas De Schampheleire
config: move bulk of load_environment to new file app_cfg.py...
r6514 # -*- coding: utf-8 -*-
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
Alessandro Molina
backend: replace Pylons with TurboGears2...
r6522 """
Global configuration file for TurboGears2 specific settings in Kallithea.
Thomas De Schampheleire
config: move bulk of load_environment to new file app_cfg.py...
r6514
Alessandro Molina
backend: replace Pylons with TurboGears2...
r6522 This file complements the .ini file.
"""
Mads Kiilerich
scripts: initial run of import cleanup using isort
r7718 import logging
import os
Thomas De Schampheleire
config: move bulk of load_environment to new file app_cfg.py...
r6514 import platform
Mads Kiilerich
scripts: initial run of import cleanup using isort
r7718 import sys
Thomas De Schampheleire
config: move bulk of load_environment to new file app_cfg.py...
r6514
Mads Kiilerich
scripts: initial run of import cleanup using isort
r7718 import alembic.config
import mercurial
Alessandro Molina
backend: replace Pylons with TurboGears2...
r6522 import tg
Mads Kiilerich
scripts: initial run of import cleanup using isort
r7718 from alembic.migration import MigrationContext
from alembic.script.base import ScriptDirectory
from sqlalchemy import create_engine
Alessandro Molina
backend: replace Pylons with TurboGears2...
r6522 from tg.configuration import AppConfig
from tg.support.converters import asbool
Thomas De Schampheleire
config: move bulk of load_environment to new file app_cfg.py...
r6514
Thomas De Schampheleire
lib/locale: move locale check to separate file...
r7657 import kallithea.lib.locale
Mads Kiilerich
scripts: initial run of import cleanup using isort
r7718 import kallithea.model.base
Mads Kiilerich
app: let app instance creation remove its SA Session when done...
r7876 import kallithea.model.meta
Mads Kiilerich
scripts: initial run of import cleanup using isort
r7718 from kallithea.lib.middleware.https_fixup import HttpsFixup
Mads Kiilerich
middleware: move handling of permanent repo URLs to separate middleware...
r7620 from kallithea.lib.middleware.permanent_repo_url import PermanentRepoUrl
Alessandro Molina
backend: replace Pylons with TurboGears2...
r6522 from kallithea.lib.middleware.simplegit import SimpleGit
from kallithea.lib.middleware.simplehg import SimpleHg
Mads Kiilerich
wsgi: reintroduce the "wrapper" middleware for logging request timing, but guarded by optional use_wsgi_wrapper=true
r7793 from kallithea.lib.middleware.wrapper import RequestWrapper
Mads Kiilerich
flake8: fix some F401 '...' imported but unused
r7719 from kallithea.lib.utils import check_git_version, load_rcextensions, make_ui, set_app_settings, set_indexer_config, set_vcs_config
Alessandro Molina
backend: replace Pylons with TurboGears2...
r6522 from kallithea.lib.utils2 import str2bool
Thomas De Schampheleire
config: move bulk of load_environment to new file app_cfg.py...
r6514
Alessandro Molina
backend: replace Pylons with TurboGears2...
r6522
domruf
alembic: check database version before starting kallithea...
r6556 log = logging.getLogger(__name__)
Alessandro Molina
backend: replace Pylons with TurboGears2...
r6522
class KallitheaAppConfig(AppConfig):
# Note: AppConfig has a misleading name, as it's not the application
# configuration, but the application configurator. The AppConfig values are
# used as a template to create the actual configuration, which might
# overwrite or extend the one provided by the configurator template.
# To make it clear, AppConfig creates the config and sets into it the same
# values that AppConfig itself has. Then the values from the config file and
# gearbox options are loaded and merged into the configuration. Then an
# after_init_config(conf) method of AppConfig is called for any change that
# might depend on options provided by configuration files.
def __init__(self):
super(KallitheaAppConfig, self).__init__()
self['package'] = kallithea
self['prefer_toscawidgets2'] = False
self['use_toscawidgets'] = False
self['renderers'] = []
# Enable json in expose
self['renderers'].append('json')
Thomas De Schampheleire
config: move bulk of make_app to app_cfg.py...
r6515
Alessandro Molina
backend: replace Pylons with TurboGears2...
r6522 # Configure template rendering
self['renderers'].append('mako')
self['default_renderer'] = 'mako'
self['use_dotted_templatenames'] = False
# Configure Sessions, store data as JSON to avoid pickle security issues
self['session.enabled'] = True
self['session.data_serializer'] = 'json'
# Configure the base SQLALchemy Setup
self['use_sqlalchemy'] = True
self['model'] = kallithea.model.base
self['DBSession'] = kallithea.model.meta.Session
# Configure App without an authentication backend.
self['auth_backend'] = None
Thomas De Schampheleire
config: move bulk of make_app to app_cfg.py...
r6515
Alessandro Molina
backend: replace Pylons with TurboGears2...
r6522 # Use custom error page for these errors. By default, Turbogears2 does not add
# 400 in this list.
# Explicitly listing all is considered more robust than appending to defaults,
# in light of possible future framework changes.
self['errorpage.status_codes'] = [400, 401, 403, 404]
Thomas De Schampheleire
config: move bulk of make_app to app_cfg.py...
r6515
Alessandro Molina
backend: replace Pylons with TurboGears2...
r6522 # Disable transaction manager -- currently Kallithea takes care of transactions itself
self['tm.enabled'] = False
Mads Kiilerich
i18n: make sure 'en' in Accept-Language is recognized as having 100% coverage - i18n.lang is for source language...
r8012 # Set the i18n source language so TG doesn't search beyond 'en' in Accept-Language.
# Don't force the default here if configuration force something else.
if not self.get('i18n.lang'):
self['i18n.lang'] = 'en'
Lars Kruse
codingstyle: trivial whitespace fixes...
r6789
Alessandro Molina
backend: replace Pylons with TurboGears2...
r6522 base_config = KallitheaAppConfig()
Thomas De Schampheleire
backend: add TurboGears2 DebugBar support...
r6524 # DebugBar, a debug toolbar for TurboGears2.
# (https://github.com/TurboGears/tgext.debugbar)
# To enable it, install 'tgext.debugbar' and 'kajiki', and run Kallithea with
# 'debug = true' (not in production!)
# See the Kallithea documentation for more information.
try:
from tgext.debugbar import enable_debugbar
import kajiki # only to check its existence
except ImportError:
pass
else:
base_config['renderers'].append('kajiki')
enable_debugbar(base_config)
Alessandro Molina
backend: replace Pylons with TurboGears2...
r6522 def setup_configuration(app):
config = app.config
Thomas De Schampheleire
config: move bulk of load_environment to new file app_cfg.py...
r6514
Thomas De Schampheleire
lib/locale: move locale check to separate file...
r7657 if not kallithea.lib.locale.current_locale_is_valid():
Mads Kiilerich
config: abort early if the environment doesn't allow Python to pass Unicode strings to the file system layer...
r7299 log.error("Terminating ...")
sys.exit(1)
Mads Kiilerich
hg: set encoding to utf-8 by default to always show unicode characters correctly...
r7249 # Mercurial sets encoding at module import time, so we have to monkey patch it
hgencoding = config.get('hgencoding')
if hgencoding:
mercurial.encoding.encoding = hgencoding
domruf
alembic: check database version before starting kallithea...
r6556 if config.get('ignore_alembic_revision', False):
log.warn('database alembic revision checking is disabled')
else:
dbconf = config['sqlalchemy.url']
alembic_cfg = alembic.config.Config()
alembic_cfg.set_main_option('script_location', 'kallithea:alembic')
alembic_cfg.set_main_option('sqlalchemy.url', dbconf)
script_dir = ScriptDirectory.from_config(alembic_cfg)
available_heads = sorted(script_dir.get_heads())
engine = create_engine(dbconf)
with engine.connect() as conn:
context = MigrationContext.configure(conn)
current_heads = sorted(str(s) for s in context.get_current_heads())
if current_heads != available_heads:
log.error('Failed to run Kallithea:\n\n'
'The database version does not match the Kallithea version.\n'
'Please read the documentation on how to upgrade or downgrade the database.\n'
'Current database version id(s): %s\n'
'Expected database version id(s): %s\n'
'If you are a developer and you know what you are doing, you can add `ignore_alembic_revision = True` '
'to your .ini file to skip the check.\n' % (' '.join(current_heads), ' '.join(available_heads)))
sys.exit(1)
Thomas De Schampheleire
config: move bulk of load_environment to new file app_cfg.py...
r6514 # store some globals into kallithea
Mads Kiilerich
config: stop using the app_conf sub key - TurboGears2 2.4 will drop it...
r7713 kallithea.CELERY_ON = str2bool(config.get('use_celery'))
kallithea.CELERY_EAGER = str2bool(config.get('celery.always.eager'))
Alessandro Molina
backend: replace Pylons with TurboGears2...
r6522 kallithea.CONFIG = config
Thomas De Schampheleire
config: move bulk of load_environment to new file app_cfg.py...
r6514
load_rcextensions(root_path=config['here'])
Mads Kiilerich
py3: add b'' annotations in some places where they will be needed later...
r7958 repos_path = make_ui().configitems(b'paths')[0][1]
Thomas De Schampheleire
config: move bulk of load_environment to new file app_cfg.py...
r6514 config['base_path'] = repos_path
set_app_settings(config)
instance_id = kallithea.CONFIG.get('instance_id', '*')
if instance_id == '*':
instance_id = '%s-%s' % (platform.uname()[1], os.getpid())
kallithea.CONFIG['instance_id'] = instance_id
Alessandro Molina
backend: replace Pylons with TurboGears2...
r6522 # update kallithea.CONFIG with the meanwhile changed 'config'
kallithea.CONFIG.update(config)
Thomas De Schampheleire
config: move bulk of load_environment to new file app_cfg.py...
r6514
Alessandro Molina
backend: replace Pylons with TurboGears2...
r6522 # configure vcs and indexer libraries (they are supposed to be independent
# as much as possible and thus avoid importing tg.config or
# kallithea.CONFIG).
Thomas De Schampheleire
config: move bulk of load_environment to new file app_cfg.py...
r6514 set_vcs_config(kallithea.CONFIG)
set_indexer_config(kallithea.CONFIG)
check_git_version()
Mads Kiilerich
app: let app instance creation remove its SA Session when done...
r7876 kallithea.model.meta.Session.remove()
Lars Kruse
codingstyle: trivial whitespace fixes...
r6789
Mads Kiilerich
config: fix pyflakes warning about unused tg import...
r8040 tg.hooks.register('configure_new_app', setup_configuration)
Thomas De Schampheleire
config: move bulk of make_app to app_cfg.py...
r6515
Alessandro Molina
backend: replace Pylons with TurboGears2...
r6522 def setup_application(app):
config = app.config
Thomas De Schampheleire
config: move bulk of make_app to app_cfg.py...
r6515
Alessandro Molina
backend: replace Pylons with TurboGears2...
r6522 # we want our low level middleware to get to the request ASAP. We don't
# need any stack middleware in them - especially no StatusCodeRedirect buffering
app = SimpleHg(app, config)
app = SimpleGit(app, config)
Thomas De Schampheleire
config: move bulk of make_app to app_cfg.py...
r6515
Alessandro Molina
backend: replace Pylons with TurboGears2...
r6522 # Enable https redirects based on HTTP_X_URL_SCHEME set by proxy
if any(asbool(config.get(x)) for x in ['https_fixup', 'force_https', 'use_htsts']):
app = HttpsFixup(app, config)
Mads Kiilerich
middleware: move handling of permanent repo URLs to separate middleware...
r7620
app = PermanentRepoUrl(app, config)
Mads Kiilerich
wsgi: reintroduce the "wrapper" middleware for logging request timing, but guarded by optional use_wsgi_wrapper=true
r7793
# Optional and undocumented wrapper - gives more verbose request/response logging, but has a slight overhead
if str2bool(config.get('use_wsgi_wrapper')):
app = RequestWrapper(app, config)
Alessandro Molina
backend: replace Pylons with TurboGears2...
r6522 return app
Thomas De Schampheleire
config: move bulk of make_app to app_cfg.py...
r6515
Lars Kruse
codingstyle: trivial whitespace fixes...
r6789
Mads Kiilerich
config: fix pyflakes warning about unused tg import...
r8040 tg.hooks.register('before_config', setup_application)