##// END OF EJS Templates
i18n: remove explicit formencode language setting...
Thomas De Schampheleire -
r6628:3b291036 default
parent child Browse files
Show More
@@ -1,195 +1,193 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 # This program is free software: you can redistribute it and/or modify
2 # This program is free software: you can redistribute it and/or modify
3 # it under the terms of the GNU General Public License as published by
3 # it under the terms of the GNU General Public License as published by
4 # the Free Software Foundation, either version 3 of the License, or
4 # the Free Software Foundation, either version 3 of the License, or
5 # (at your option) any later version.
5 # (at your option) any later version.
6 #
6 #
7 # This program is distributed in the hope that it will be useful,
7 # This program is distributed in the hope that it will be useful,
8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 # GNU General Public License for more details.
10 # GNU General Public License for more details.
11 #
11 #
12 # You should have received a copy of the GNU General Public License
12 # You should have received a copy of the GNU General Public License
13 # along with this program. If not, see <http://www.gnu.org/licenses/>.
13 # along with this program. If not, see <http://www.gnu.org/licenses/>.
14 """
14 """
15 Global configuration file for TurboGears2 specific settings in Kallithea.
15 Global configuration file for TurboGears2 specific settings in Kallithea.
16
16
17 This file complements the .ini file.
17 This file complements the .ini file.
18 """
18 """
19
19
20 import platform
20 import platform
21 import os, sys, logging
21 import os, sys, logging
22
22
23 import tg
23 import tg
24 from tg import hooks
24 from tg import hooks
25 from tg.configuration import AppConfig
25 from tg.configuration import AppConfig
26 from tg.support.converters import asbool
26 from tg.support.converters import asbool
27 import alembic.config
27 import alembic.config
28 from alembic.script.base import ScriptDirectory
28 from alembic.script.base import ScriptDirectory
29 from alembic.migration import MigrationContext
29 from alembic.migration import MigrationContext
30 from sqlalchemy import create_engine
30 from sqlalchemy import create_engine
31
31
32 from kallithea.lib.middleware.https_fixup import HttpsFixup
32 from kallithea.lib.middleware.https_fixup import HttpsFixup
33 from kallithea.lib.middleware.simplegit import SimpleGit
33 from kallithea.lib.middleware.simplegit import SimpleGit
34 from kallithea.lib.middleware.simplehg import SimpleHg
34 from kallithea.lib.middleware.simplehg import SimpleHg
35 from kallithea.lib.auth import set_available_permissions
35 from kallithea.lib.auth import set_available_permissions
36 from kallithea.lib.utils import load_rcextensions, make_ui, set_app_settings, set_vcs_config, \
36 from kallithea.lib.utils import load_rcextensions, make_ui, set_app_settings, set_vcs_config, \
37 set_indexer_config, check_git_version, repo2db_mapper
37 set_indexer_config, check_git_version, repo2db_mapper
38 from kallithea.lib.utils2 import str2bool
38 from kallithea.lib.utils2 import str2bool
39 import kallithea.model.base
39 import kallithea.model.base
40 from kallithea.model.scm import ScmModel
40 from kallithea.model.scm import ScmModel
41
41
42 import formencode
42 import formencode
43
43
44 log = logging.getLogger(__name__)
44 log = logging.getLogger(__name__)
45
45
46
46
47 class KallitheaAppConfig(AppConfig):
47 class KallitheaAppConfig(AppConfig):
48 # Note: AppConfig has a misleading name, as it's not the application
48 # Note: AppConfig has a misleading name, as it's not the application
49 # configuration, but the application configurator. The AppConfig values are
49 # configuration, but the application configurator. The AppConfig values are
50 # used as a template to create the actual configuration, which might
50 # used as a template to create the actual configuration, which might
51 # overwrite or extend the one provided by the configurator template.
51 # overwrite or extend the one provided by the configurator template.
52
52
53 # To make it clear, AppConfig creates the config and sets into it the same
53 # To make it clear, AppConfig creates the config and sets into it the same
54 # values that AppConfig itself has. Then the values from the config file and
54 # values that AppConfig itself has. Then the values from the config file and
55 # gearbox options are loaded and merged into the configuration. Then an
55 # gearbox options are loaded and merged into the configuration. Then an
56 # after_init_config(conf) method of AppConfig is called for any change that
56 # after_init_config(conf) method of AppConfig is called for any change that
57 # might depend on options provided by configuration files.
57 # might depend on options provided by configuration files.
58
58
59 def __init__(self):
59 def __init__(self):
60 super(KallitheaAppConfig, self).__init__()
60 super(KallitheaAppConfig, self).__init__()
61
61
62 self['package'] = kallithea
62 self['package'] = kallithea
63
63
64 self['prefer_toscawidgets2'] = False
64 self['prefer_toscawidgets2'] = False
65 self['use_toscawidgets'] = False
65 self['use_toscawidgets'] = False
66
66
67 self['renderers'] = []
67 self['renderers'] = []
68
68
69 # Enable json in expose
69 # Enable json in expose
70 self['renderers'].append('json')
70 self['renderers'].append('json')
71
71
72 # Configure template rendering
72 # Configure template rendering
73 self['renderers'].append('mako')
73 self['renderers'].append('mako')
74 self['default_renderer'] = 'mako'
74 self['default_renderer'] = 'mako'
75 self['use_dotted_templatenames'] = False
75 self['use_dotted_templatenames'] = False
76
76
77 # Configure Sessions, store data as JSON to avoid pickle security issues
77 # Configure Sessions, store data as JSON to avoid pickle security issues
78 self['session.enabled'] = True
78 self['session.enabled'] = True
79 self['session.data_serializer'] = 'json'
79 self['session.data_serializer'] = 'json'
80
80
81 # Configure the base SQLALchemy Setup
81 # Configure the base SQLALchemy Setup
82 self['use_sqlalchemy'] = True
82 self['use_sqlalchemy'] = True
83 self['model'] = kallithea.model.base
83 self['model'] = kallithea.model.base
84 self['DBSession'] = kallithea.model.meta.Session
84 self['DBSession'] = kallithea.model.meta.Session
85
85
86 # Configure App without an authentication backend.
86 # Configure App without an authentication backend.
87 self['auth_backend'] = None
87 self['auth_backend'] = None
88
88
89 # Use custom error page for these errors. By default, Turbogears2 does not add
89 # Use custom error page for these errors. By default, Turbogears2 does not add
90 # 400 in this list.
90 # 400 in this list.
91 # Explicitly listing all is considered more robust than appending to defaults,
91 # Explicitly listing all is considered more robust than appending to defaults,
92 # in light of possible future framework changes.
92 # in light of possible future framework changes.
93 self['errorpage.status_codes'] = [400, 401, 403, 404]
93 self['errorpage.status_codes'] = [400, 401, 403, 404]
94
94
95 # Disable transaction manager -- currently Kallithea takes care of transactions itself
95 # Disable transaction manager -- currently Kallithea takes care of transactions itself
96 self['tm.enabled'] = False
96 self['tm.enabled'] = False
97
97
98 base_config = KallitheaAppConfig()
98 base_config = KallitheaAppConfig()
99
99
100 # TODO still needed as long as we use pylonslib
100 # TODO still needed as long as we use pylonslib
101 sys.modules['pylons'] = tg
101 sys.modules['pylons'] = tg
102
102
103 # DebugBar, a debug toolbar for TurboGears2.
103 # DebugBar, a debug toolbar for TurboGears2.
104 # (https://github.com/TurboGears/tgext.debugbar)
104 # (https://github.com/TurboGears/tgext.debugbar)
105 # To enable it, install 'tgext.debugbar' and 'kajiki', and run Kallithea with
105 # To enable it, install 'tgext.debugbar' and 'kajiki', and run Kallithea with
106 # 'debug = true' (not in production!)
106 # 'debug = true' (not in production!)
107 # See the Kallithea documentation for more information.
107 # See the Kallithea documentation for more information.
108 try:
108 try:
109 from tgext.debugbar import enable_debugbar
109 from tgext.debugbar import enable_debugbar
110 import kajiki # only to check its existence
110 import kajiki # only to check its existence
111 except ImportError:
111 except ImportError:
112 pass
112 pass
113 else:
113 else:
114 base_config['renderers'].append('kajiki')
114 base_config['renderers'].append('kajiki')
115 enable_debugbar(base_config)
115 enable_debugbar(base_config)
116
116
117
117
118 def setup_configuration(app):
118 def setup_configuration(app):
119 config = app.config
119 config = app.config
120
120
121 if config.get('ignore_alembic_revision', False):
121 if config.get('ignore_alembic_revision', False):
122 log.warn('database alembic revision checking is disabled')
122 log.warn('database alembic revision checking is disabled')
123 else:
123 else:
124 dbconf = config['sqlalchemy.url']
124 dbconf = config['sqlalchemy.url']
125 alembic_cfg = alembic.config.Config()
125 alembic_cfg = alembic.config.Config()
126 alembic_cfg.set_main_option('script_location', 'kallithea:alembic')
126 alembic_cfg.set_main_option('script_location', 'kallithea:alembic')
127 alembic_cfg.set_main_option('sqlalchemy.url', dbconf)
127 alembic_cfg.set_main_option('sqlalchemy.url', dbconf)
128 script_dir = ScriptDirectory.from_config(alembic_cfg)
128 script_dir = ScriptDirectory.from_config(alembic_cfg)
129 available_heads = sorted(script_dir.get_heads())
129 available_heads = sorted(script_dir.get_heads())
130
130
131 engine = create_engine(dbconf)
131 engine = create_engine(dbconf)
132 with engine.connect() as conn:
132 with engine.connect() as conn:
133 context = MigrationContext.configure(conn)
133 context = MigrationContext.configure(conn)
134 current_heads = sorted(str(s) for s in context.get_current_heads())
134 current_heads = sorted(str(s) for s in context.get_current_heads())
135 if current_heads != available_heads:
135 if current_heads != available_heads:
136 log.error('Failed to run Kallithea:\n\n'
136 log.error('Failed to run Kallithea:\n\n'
137 'The database version does not match the Kallithea version.\n'
137 'The database version does not match the Kallithea version.\n'
138 'Please read the documentation on how to upgrade or downgrade the database.\n'
138 'Please read the documentation on how to upgrade or downgrade the database.\n'
139 'Current database version id(s): %s\n'
139 'Current database version id(s): %s\n'
140 'Expected database version id(s): %s\n'
140 'Expected database version id(s): %s\n'
141 'If you are a developer and you know what you are doing, you can add `ignore_alembic_revision = True` '
141 'If you are a developer and you know what you are doing, you can add `ignore_alembic_revision = True` '
142 'to your .ini file to skip the check.\n' % (' '.join(current_heads), ' '.join(available_heads)))
142 'to your .ini file to skip the check.\n' % (' '.join(current_heads), ' '.join(available_heads)))
143 sys.exit(1)
143 sys.exit(1)
144
144
145 # store some globals into kallithea
145 # store some globals into kallithea
146 kallithea.CELERY_ON = str2bool(config['app_conf'].get('use_celery'))
146 kallithea.CELERY_ON = str2bool(config['app_conf'].get('use_celery'))
147 kallithea.CELERY_EAGER = str2bool(config['app_conf'].get('celery.always.eager'))
147 kallithea.CELERY_EAGER = str2bool(config['app_conf'].get('celery.always.eager'))
148 kallithea.CONFIG = config
148 kallithea.CONFIG = config
149
149
150 load_rcextensions(root_path=config['here'])
150 load_rcextensions(root_path=config['here'])
151
151
152 set_available_permissions(config)
152 set_available_permissions(config)
153 repos_path = make_ui('db').configitems('paths')[0][1]
153 repos_path = make_ui('db').configitems('paths')[0][1]
154 config['base_path'] = repos_path
154 config['base_path'] = repos_path
155 set_app_settings(config)
155 set_app_settings(config)
156
156
157 instance_id = kallithea.CONFIG.get('instance_id', '*')
157 instance_id = kallithea.CONFIG.get('instance_id', '*')
158 if instance_id == '*':
158 if instance_id == '*':
159 instance_id = '%s-%s' % (platform.uname()[1], os.getpid())
159 instance_id = '%s-%s' % (platform.uname()[1], os.getpid())
160 kallithea.CONFIG['instance_id'] = instance_id
160 kallithea.CONFIG['instance_id'] = instance_id
161
161
162 # update kallithea.CONFIG with the meanwhile changed 'config'
162 # update kallithea.CONFIG with the meanwhile changed 'config'
163 kallithea.CONFIG.update(config)
163 kallithea.CONFIG.update(config)
164
164
165 # configure vcs and indexer libraries (they are supposed to be independent
165 # configure vcs and indexer libraries (they are supposed to be independent
166 # as much as possible and thus avoid importing tg.config or
166 # as much as possible and thus avoid importing tg.config or
167 # kallithea.CONFIG).
167 # kallithea.CONFIG).
168 set_vcs_config(kallithea.CONFIG)
168 set_vcs_config(kallithea.CONFIG)
169 set_indexer_config(kallithea.CONFIG)
169 set_indexer_config(kallithea.CONFIG)
170
170
171 check_git_version()
171 check_git_version()
172
172
173 if str2bool(config.get('initial_repo_scan', True)):
173 if str2bool(config.get('initial_repo_scan', True)):
174 repo2db_mapper(ScmModel().repo_scan(repos_path),
174 repo2db_mapper(ScmModel().repo_scan(repos_path),
175 remove_obsolete=False, install_git_hooks=False)
175 remove_obsolete=False, install_git_hooks=False)
176
176
177 formencode.api.set_stdtranslation(languages=[config.get('lang')])
178
179 hooks.register('configure_new_app', setup_configuration)
177 hooks.register('configure_new_app', setup_configuration)
180
178
181
179
182 def setup_application(app):
180 def setup_application(app):
183 config = app.config
181 config = app.config
184
182
185 # we want our low level middleware to get to the request ASAP. We don't
183 # we want our low level middleware to get to the request ASAP. We don't
186 # need any stack middleware in them - especially no StatusCodeRedirect buffering
184 # need any stack middleware in them - especially no StatusCodeRedirect buffering
187 app = SimpleHg(app, config)
185 app = SimpleHg(app, config)
188 app = SimpleGit(app, config)
186 app = SimpleGit(app, config)
189
187
190 # Enable https redirects based on HTTP_X_URL_SCHEME set by proxy
188 # Enable https redirects based on HTTP_X_URL_SCHEME set by proxy
191 if any(asbool(config.get(x)) for x in ['https_fixup', 'force_https', 'use_htsts']):
189 if any(asbool(config.get(x)) for x in ['https_fixup', 'force_https', 'use_htsts']):
192 app = HttpsFixup(app, config)
190 app = HttpsFixup(app, config)
193 return app
191 return app
194
192
195 hooks.register('before_config', setup_application)
193 hooks.register('before_config', setup_application)
General Comments 0
You need to be logged in to leave comments. Login now