We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
sqlalchemy setting "pool_pre_ping"
**CKAN version**

Introduced in commit https://github.com/ckan/ckan/commit/ae9eb1360028a77b35506716eed2768349827806 which is related at least to 2.9.0, 2.9.1, 2.9.2. 

**Describe the bug**

This is the ckan code:
```python
    # Enable pessimistic disconnect handling (added in SQLAlchemy 1.2)
    # to eliminate database errors due to stale pooled connections
    config.setdefault('pool_pre_ping', True)

    # Initialize SQLAlchemy
    engine = sqlalchemy.engine_from_config(config)
```

This is the sqlalchemy function:
```python
    def engine_from_config(configuration, prefix="sqlalchemy.", **kwargs):
```

I guess the `pool_pre_ping` config is not taken into account by sqlalchemy since it's missing the prefix.


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `ckan/config/environment.py`
Content:
```
1 # encoding: utf-8
2 
3 '''CKAN environment configuration'''
4 import os
5 import logging
6 import warnings
7 import pytz
8 
9 import six
10 import sqlalchemy
11 
12 from six.moves.urllib.parse import urlparse
13 
14 import ckan.model as model
15 import ckan.plugins as p
16 import ckan.lib.plugins as lib_plugins
17 import ckan.lib.helpers as helpers
18 import ckan.lib.app_globals as app_globals
19 from ckan.lib.redis import is_redis_available
20 import ckan.lib.render as render
21 import ckan.lib.search as search
22 import ckan.logic as logic
23 import ckan.authz as authz
24 import ckan.lib.jinja_extensions as jinja_extensions
25 from ckan.lib.webassets_tools import webassets_init
26 from ckan.lib.i18n import build_js_translations
27 
28 from ckan.common import _, ungettext, config
29 from ckan.exceptions import CkanConfigurationException
30 
31 if six.PY2:
32     from pylons import config as pylons_config
33 
34 
35 log = logging.getLogger(__name__)
36 
37 
38 # Suppress benign warning 'Unbuilt egg for setuptools'
39 warnings.simplefilter('ignore', UserWarning)
40 
41 
42 def load_environment(conf):
43     """
44     Configure the Pylons environment via the ``pylons.config`` object. This
45     code should only need to be run once.
46     """
47     if six.PY2:
48         # this must be run at a time when the env is semi-setup, thus inlined
49         # here. Required by the deliverance plugin and iATI
50         from pylons.wsgiapp import PylonsApp
51         import pkg_resources
52         find_controller_generic = getattr(
53             PylonsApp.find_controller,
54             '_old_find_controller',
55             PylonsApp.find_controller)
56 
57         # This is from pylons 1.0 source, will monkey-patch into 0.9.7
58         def find_controller(self, controller):
59             if controller in self.controller_classes:
60                 return self.controller_classes[controller]
61             # Check to see if its a dotted name
62             if '.' in controller or ':' in controller:
63                 ep = pkg_resources.EntryPoint.parse('x={0}'.format(controller))
64 
65                 if hasattr(ep, 'resolve'):
66                     # setuptools >= 10.2
67                     mycontroller = ep.resolve()
68                 else:
69                     # setuptools >= 11.3
70                     mycontroller = ep.load(False)
71 
72                 self.controller_classes[controller] = mycontroller
73                 return mycontroller
74             return find_controller_generic(self, controller)
75         find_controller._old_find_controller = find_controller_generic
76         PylonsApp.find_controller = find_controller
77 
78     os.environ['CKAN_CONFIG'] = conf['__file__']
79 
80     # Pylons paths
81     root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
82 
83     valid_base_public_folder_names = ['public']
84     static_files = conf.get('ckan.base_public_folder', 'public')
85     conf['ckan.base_public_folder'] = static_files
86 
87     if static_files not in valid_base_public_folder_names:
88         raise CkanConfigurationException(
89             'You provided an invalid value for ckan.base_public_folder. '
90             'Possible values are: "public".'
91         )
92 
93     log.info('Loading static files from %s' % static_files)
94     paths = dict(root=root,
95                  controllers=os.path.join(root, 'controllers'),
96                  static_files=os.path.join(root, static_files),
97                  templates=[])
98 
99     # Initialize main CKAN config object
100     config.update(conf)
101 
102     if six.PY2:
103         # Initialize Pylons own config object
104         pylons_config.init_app(
105             conf['global_conf'], conf, package='ckan', paths=paths)
106 
107         # Update the main CKAN config object with the Pylons specific stuff,
108         # as it is quite hard to keep them separated. This should be removed
109         # once Pylons support is dropped
110         config.update(pylons_config)
111 
112     # Setup the SQLAlchemy database engine
113     # Suppress a couple of sqlalchemy warnings
114     msgs = ['^Unicode type received non-unicode bind param value',
115             "^Did not recognize type 'BIGINT' of column 'size'",
116             "^Did not recognize type 'tsvector' of column 'search_vector'"
117             ]
118     for msg in msgs:
119         warnings.filterwarnings('ignore', msg, sqlalchemy.exc.SAWarning)
120 
121     # load all CKAN plugins
122     p.load_all()
123 
124     # Check Redis availability
125     if not is_redis_available():
126         log.critical('Could not connect to Redis.')
127 
128     app_globals.reset()
129 
130     # issue #3260: remove idle transaction
131     # Session that was used for getting all config params nor committed,
132     # neither removed and we have idle connection as result
133     model.Session.commit()
134 
135     # Build JavaScript translations. Must be done after plugins have
136     # been loaded.
137     build_js_translations()
138 
139 
140 # A mapping of config settings that can be overridden by env vars.
141 # Note: Do not remove the following lines, they are used in the docs
142 # Start CONFIG_FROM_ENV_VARS
143 CONFIG_FROM_ENV_VARS = {
144     'sqlalchemy.url': 'CKAN_SQLALCHEMY_URL',
145     'ckan.datastore.write_url': 'CKAN_DATASTORE_WRITE_URL',
146     'ckan.datastore.read_url': 'CKAN_DATASTORE_READ_URL',
147     'ckan.redis.url': 'CKAN_REDIS_URL',
148     'solr_url': 'CKAN_SOLR_URL',
149     'solr_user': 'CKAN_SOLR_USER',
150     'solr_password': 'CKAN_SOLR_PASSWORD',
151     'ckan.site_id': 'CKAN_SITE_ID',
152     'ckan.site_url': 'CKAN_SITE_URL',
153     'ckan.storage_path': 'CKAN_STORAGE_PATH',
154     'ckan.datapusher.url': 'CKAN_DATAPUSHER_URL',
155     'smtp.server': 'CKAN_SMTP_SERVER',
156     'smtp.starttls': 'CKAN_SMTP_STARTTLS',
157     'smtp.user': 'CKAN_SMTP_USER',
158     'smtp.password': 'CKAN_SMTP_PASSWORD',
159     'smtp.mail_from': 'CKAN_SMTP_MAIL_FROM',
160     'ckan.max_resource_size': 'CKAN_MAX_UPLOAD_SIZE_MB'
161 }
162 # End CONFIG_FROM_ENV_VARS
163 
164 
165 def update_config():
166     ''' This code needs to be run when the config is changed to take those
167     changes into account. It is called whenever a plugin is loaded as the
168     plugin might have changed the config values (for instance it might
169     change ckan.site_url) '''
170 
171     webassets_init()
172 
173     for plugin in p.PluginImplementations(p.IConfigurer):
174         # must do update in place as this does not work:
175         # config = plugin.update_config(config)
176         plugin.update_config(config)
177 
178     # Set whitelisted env vars on config object
179     # This is set up before globals are initialized
180 
181     ckan_db = os.environ.get('CKAN_DB', None)
182     if ckan_db:
183         msg = 'Setting CKAN_DB as an env var is deprecated and will be' \
184             ' removed in a future release. Use CKAN_SQLALCHEMY_URL instead.'
185         log.warn(msg)
186         config['sqlalchemy.url'] = ckan_db
187 
188     for option in CONFIG_FROM_ENV_VARS:
189         from_env = os.environ.get(CONFIG_FROM_ENV_VARS[option], None)
190         if from_env:
191             config[option] = from_env
192 
193     root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
194 
195     site_url = config.get('ckan.site_url', '')
196     if not site_url:
197         raise RuntimeError(
198             'ckan.site_url is not configured and it must have a value.'
199             ' Please amend your .ini file.')
200     if not site_url.lower().startswith('http'):
201         raise RuntimeError(
202             'ckan.site_url should be a full URL, including the schema '
203             '(http or https)')
204 
205     display_timezone = config.get('ckan.display_timezone', '')
206     if (display_timezone and
207             display_timezone != 'server' and
208             display_timezone not in pytz.all_timezones):
209         raise CkanConfigurationException(
210             "ckan.display_timezone is not 'server' or a valid timezone"
211         )
212 
213     # Remove backslash from site_url if present
214     config['ckan.site_url'] = config['ckan.site_url'].rstrip('/')
215 
216     ckan_host = config['ckan.host'] = urlparse(site_url).netloc
217     if config.get('ckan.site_id') is None:
218         if ':' in ckan_host:
219             ckan_host, port = ckan_host.split(':')
220         assert ckan_host, 'You need to configure ckan.site_url or ' \
221                           'ckan.site_id for SOLR search-index rebuild to work.'
222         config['ckan.site_id'] = ckan_host
223 
224     # ensure that a favicon has been set
225     favicon = config.get('ckan.favicon', '/base/images/ckan.ico')
226     config['ckan.favicon'] = favicon
227 
228     # Init SOLR settings and check if the schema is compatible
229     # from ckan.lib.search import SolrSettings, check_solr_schema_version
230 
231     # lib.search is imported here as we need the config enabled and parsed
232     search.SolrSettings.init(config.get('solr_url'),
233                              config.get('solr_user'),
234                              config.get('solr_password'))
235     search.check_solr_schema_version()
236 
237     lib_plugins.reset_package_plugins()
238     lib_plugins.register_package_plugins()
239     lib_plugins.reset_group_plugins()
240     lib_plugins.register_group_plugins()
241 
242     # initialise the globals
243     app_globals.app_globals._init()
244 
245     helpers.load_plugin_helpers()
246     config['pylons.h'] = helpers.helper_functions
247 
248     # Templates and CSS loading from configuration
249     valid_base_templates_folder_names = ['templates']
250     templates = config.get('ckan.base_templates_folder', 'templates')
251     config['ckan.base_templates_folder'] = templates
252 
253     if templates not in valid_base_templates_folder_names:
254         raise CkanConfigurationException(
255             'You provided an invalid value for ckan.base_templates_folder. '
256             'Possible values are: "templates".'
257         )
258 
259     jinja2_templates_path = os.path.join(root, templates)
260     log.info('Loading templates from %s' % jinja2_templates_path)
261     template_paths = [jinja2_templates_path]
262 
263     extra_template_paths = config.get('extra_template_paths', '')
264     if extra_template_paths:
265         # must be first for them to override defaults
266         template_paths = extra_template_paths.split(',') + template_paths
267     config['computed_template_paths'] = template_paths
268 
269     # Markdown ignores the logger config, so to get rid of excessive
270     # markdown debug messages in the log, set it to the level of the
271     # root logger.
272     logging.getLogger("MARKDOWN").setLevel(logging.getLogger().level)
273 
274     if six.PY2:
275         # Create Jinja2 environment
276         env = jinja_extensions.Environment(
277             **jinja_extensions.get_jinja_env_options())
278         env.install_gettext_callables(_, ungettext, newstyle=True)
279         # custom filters
280         env.policies['ext.i18n.trimmed'] = True
281         env.filters['empty_and_escape'] = jinja_extensions.empty_and_escape
282         config['pylons.app_globals'].jinja_env = env
283 
284     # CONFIGURATION OPTIONS HERE (note: all config options will override
285     # any Pylons config options)
286 
287     # Enable pessimistic disconnect handling (added in SQLAlchemy 1.2)
288     # to eliminate database errors due to stale pooled connections
289     config.setdefault('pool_pre_ping', True)
290 
291     # Initialize SQLAlchemy
292     engine = sqlalchemy.engine_from_config(config)
293     model.init_model(engine)
294 
295     for plugin in p.PluginImplementations(p.IConfigurable):
296         plugin.configure(config)
297 
298     # reset the template cache - we do this here so that when we load the
299     # environment it is clean
300     render.reset_template_info_cache()
301 
302     # clear other caches
303     logic.clear_actions_cache()
304     logic.clear_validators_cache()
305     authz.clear_auth_functions_cache()
306 
307     # Here we create the site user if they are not already in the database
308     try:
309         logic.get_action('get_site_user')({'ignore_auth': True}, None)
310     except (sqlalchemy.exc.ProgrammingError, sqlalchemy.exc.OperationalError):
311         # (ProgrammingError for Postgres, OperationalError for SQLite)
312         # The database is not initialised.  This is a bit dirty.  This occurs
313         # when running tests.
314         pass
315     except sqlalchemy.exc.InternalError:
316         # The database is not initialised.  Travis hits this
317         pass
318 
319     # Close current session and open database connections to ensure a clean
320     # clean environment even if an error occurs later on
321     model.Session.remove()
322     model.Session.bind.dispose()
323
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/ckan/config/environment.py b/ckan/config/environment.py
--- a/ckan/config/environment.py
+++ b/ckan/config/environment.py
@@ -286,7 +286,7 @@
 
     # Enable pessimistic disconnect handling (added in SQLAlchemy 1.2)
     # to eliminate database errors due to stale pooled connections
-    config.setdefault('pool_pre_ping', True)
+    config.setdefault('sqlalchemy.pool_pre_ping', True)
 
     # Initialize SQLAlchemy
     engine = sqlalchemy.engine_from_config(config)

{"golden_diff": "diff --git a/ckan/config/environment.py b/ckan/config/environment.py\n--- a/ckan/config/environment.py\n+++ b/ckan/config/environment.py\n@@ -286,7 +286,7 @@\n \n     # Enable pessimistic disconnect handling (added in SQLAlchemy 1.2)\n     # to eliminate database errors due to stale pooled connections\n-    config.setdefault('pool_pre_ping', True)\n+    config.setdefault('sqlalchemy.pool_pre_ping', True)\n \n     # Initialize SQLAlchemy\n     engine = sqlalchemy.engine_from_config(config)\n", "issue": "sqlalchemy setting \"pool_pre_ping\"\n**CKAN version**\r\n\r\nIntroduced in commit https://github.com/ckan/ckan/commit/ae9eb1360028a77b35506716eed2768349827806 which is related at least to 2.9.0, 2.9.1, 2.9.2. \r\n\r\n**Describe the bug**\r\n\r\nThis is the ckan code:\r\n```python\r\n    # Enable pessimistic disconnect handling (added in SQLAlchemy 1.2)\r\n    # to eliminate database errors due to stale pooled connections\r\n    config.setdefault('pool_pre_ping', True)\r\n\r\n    # Initialize SQLAlchemy\r\n    engine = sqlalchemy.engine_from_config(config)\r\n```\r\n\r\nThis is the sqlalchemy function:\r\n```python\r\n    def engine_from_config(configuration, prefix=\"sqlalchemy.\", **kwargs):\r\n```\r\n\r\nI guess the `pool_pre_ping` config is not taken into account by sqlalchemy since it's missing the prefix.\r\n\n", "before_files": [{"content": "# encoding: utf-8\n\n'''CKAN environment configuration'''\nimport os\nimport logging\nimport warnings\nimport pytz\n\nimport six\nimport sqlalchemy\n\nfrom six.moves.urllib.parse import urlparse\n\nimport ckan.model as model\nimport ckan.plugins as p\nimport ckan.lib.plugins as lib_plugins\nimport ckan.lib.helpers as helpers\nimport ckan.lib.app_globals as app_globals\nfrom ckan.lib.redis import is_redis_available\nimport ckan.lib.render as render\nimport ckan.lib.search as search\nimport ckan.logic as logic\nimport ckan.authz as authz\nimport ckan.lib.jinja_extensions as jinja_extensions\nfrom ckan.lib.webassets_tools import webassets_init\nfrom ckan.lib.i18n import build_js_translations\n\nfrom ckan.common import _, ungettext, config\nfrom ckan.exceptions import CkanConfigurationException\n\nif six.PY2:\n    from pylons import config as pylons_config\n\n\nlog = logging.getLogger(__name__)\n\n\n# Suppress benign warning 'Unbuilt egg for setuptools'\nwarnings.simplefilter('ignore', UserWarning)\n\n\ndef load_environment(conf):\n    \"\"\"\n    Configure the Pylons environment via the ``pylons.config`` object. This\n    code should only need to be run once.\n    \"\"\"\n    if six.PY2:\n        # this must be run at a time when the env is semi-setup, thus inlined\n        # here. Required by the deliverance plugin and iATI\n        from pylons.wsgiapp import PylonsApp\n        import pkg_resources\n        find_controller_generic = getattr(\n            PylonsApp.find_controller,\n            '_old_find_controller',\n            PylonsApp.find_controller)\n\n        # This is from pylons 1.0 source, will monkey-patch into 0.9.7\n        def find_controller(self, controller):\n            if controller in self.controller_classes:\n                return self.controller_classes[controller]\n            # Check to see if its a dotted name\n            if '.' in controller or ':' in controller:\n                ep = pkg_resources.EntryPoint.parse('x={0}'.format(controller))\n\n                if hasattr(ep, 'resolve'):\n                    # setuptools >= 10.2\n                    mycontroller = ep.resolve()\n                else:\n                    # setuptools >= 11.3\n                    mycontroller = ep.load(False)\n\n                self.controller_classes[controller] = mycontroller\n                return mycontroller\n            return find_controller_generic(self, controller)\n        find_controller._old_find_controller = find_controller_generic\n        PylonsApp.find_controller = find_controller\n\n    os.environ['CKAN_CONFIG'] = conf['__file__']\n\n    # Pylons paths\n    root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))\n\n    valid_base_public_folder_names = ['public']\n    static_files = conf.get('ckan.base_public_folder', 'public')\n    conf['ckan.base_public_folder'] = static_files\n\n    if static_files not in valid_base_public_folder_names:\n        raise CkanConfigurationException(\n            'You provided an invalid value for ckan.base_public_folder. '\n            'Possible values are: \"public\".'\n        )\n\n    log.info('Loading static files from %s' % static_files)\n    paths = dict(root=root,\n                 controllers=os.path.join(root, 'controllers'),\n                 static_files=os.path.join(root, static_files),\n                 templates=[])\n\n    # Initialize main CKAN config object\n    config.update(conf)\n\n    if six.PY2:\n        # Initialize Pylons own config object\n        pylons_config.init_app(\n            conf['global_conf'], conf, package='ckan', paths=paths)\n\n        # Update the main CKAN config object with the Pylons specific stuff,\n        # as it is quite hard to keep them separated. This should be removed\n        # once Pylons support is dropped\n        config.update(pylons_config)\n\n    # Setup the SQLAlchemy database engine\n    # Suppress a couple of sqlalchemy warnings\n    msgs = ['^Unicode type received non-unicode bind param value',\n            \"^Did not recognize type 'BIGINT' of column 'size'\",\n            \"^Did not recognize type 'tsvector' of column 'search_vector'\"\n            ]\n    for msg in msgs:\n        warnings.filterwarnings('ignore', msg, sqlalchemy.exc.SAWarning)\n\n    # load all CKAN plugins\n    p.load_all()\n\n    # Check Redis availability\n    if not is_redis_available():\n        log.critical('Could not connect to Redis.')\n\n    app_globals.reset()\n\n    # issue #3260: remove idle transaction\n    # Session that was used for getting all config params nor committed,\n    # neither removed and we have idle connection as result\n    model.Session.commit()\n\n    # Build JavaScript translations. Must be done after plugins have\n    # been loaded.\n    build_js_translations()\n\n\n# A mapping of config settings that can be overridden by env vars.\n# Note: Do not remove the following lines, they are used in the docs\n# Start CONFIG_FROM_ENV_VARS\nCONFIG_FROM_ENV_VARS = {\n    'sqlalchemy.url': 'CKAN_SQLALCHEMY_URL',\n    'ckan.datastore.write_url': 'CKAN_DATASTORE_WRITE_URL',\n    'ckan.datastore.read_url': 'CKAN_DATASTORE_READ_URL',\n    'ckan.redis.url': 'CKAN_REDIS_URL',\n    'solr_url': 'CKAN_SOLR_URL',\n    'solr_user': 'CKAN_SOLR_USER',\n    'solr_password': 'CKAN_SOLR_PASSWORD',\n    'ckan.site_id': 'CKAN_SITE_ID',\n    'ckan.site_url': 'CKAN_SITE_URL',\n    'ckan.storage_path': 'CKAN_STORAGE_PATH',\n    'ckan.datapusher.url': 'CKAN_DATAPUSHER_URL',\n    'smtp.server': 'CKAN_SMTP_SERVER',\n    'smtp.starttls': 'CKAN_SMTP_STARTTLS',\n    'smtp.user': 'CKAN_SMTP_USER',\n    'smtp.password': 'CKAN_SMTP_PASSWORD',\n    'smtp.mail_from': 'CKAN_SMTP_MAIL_FROM',\n    'ckan.max_resource_size': 'CKAN_MAX_UPLOAD_SIZE_MB'\n}\n# End CONFIG_FROM_ENV_VARS\n\n\ndef update_config():\n    ''' This code needs to be run when the config is changed to take those\n    changes into account. It is called whenever a plugin is loaded as the\n    plugin might have changed the config values (for instance it might\n    change ckan.site_url) '''\n\n    webassets_init()\n\n    for plugin in p.PluginImplementations(p.IConfigurer):\n        # must do update in place as this does not work:\n        # config = plugin.update_config(config)\n        plugin.update_config(config)\n\n    # Set whitelisted env vars on config object\n    # This is set up before globals are initialized\n\n    ckan_db = os.environ.get('CKAN_DB', None)\n    if ckan_db:\n        msg = 'Setting CKAN_DB as an env var is deprecated and will be' \\\n            ' removed in a future release. Use CKAN_SQLALCHEMY_URL instead.'\n        log.warn(msg)\n        config['sqlalchemy.url'] = ckan_db\n\n    for option in CONFIG_FROM_ENV_VARS:\n        from_env = os.environ.get(CONFIG_FROM_ENV_VARS[option], None)\n        if from_env:\n            config[option] = from_env\n\n    root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))\n\n    site_url = config.get('ckan.site_url', '')\n    if not site_url:\n        raise RuntimeError(\n            'ckan.site_url is not configured and it must have a value.'\n            ' Please amend your .ini file.')\n    if not site_url.lower().startswith('http'):\n        raise RuntimeError(\n            'ckan.site_url should be a full URL, including the schema '\n            '(http or https)')\n\n    display_timezone = config.get('ckan.display_timezone', '')\n    if (display_timezone and\n            display_timezone != 'server' and\n            display_timezone not in pytz.all_timezones):\n        raise CkanConfigurationException(\n            \"ckan.display_timezone is not 'server' or a valid timezone\"\n        )\n\n    # Remove backslash from site_url if present\n    config['ckan.site_url'] = config['ckan.site_url'].rstrip('/')\n\n    ckan_host = config['ckan.host'] = urlparse(site_url).netloc\n    if config.get('ckan.site_id') is None:\n        if ':' in ckan_host:\n            ckan_host, port = ckan_host.split(':')\n        assert ckan_host, 'You need to configure ckan.site_url or ' \\\n                          'ckan.site_id for SOLR search-index rebuild to work.'\n        config['ckan.site_id'] = ckan_host\n\n    # ensure that a favicon has been set\n    favicon = config.get('ckan.favicon', '/base/images/ckan.ico')\n    config['ckan.favicon'] = favicon\n\n    # Init SOLR settings and check if the schema is compatible\n    # from ckan.lib.search import SolrSettings, check_solr_schema_version\n\n    # lib.search is imported here as we need the config enabled and parsed\n    search.SolrSettings.init(config.get('solr_url'),\n                             config.get('solr_user'),\n                             config.get('solr_password'))\n    search.check_solr_schema_version()\n\n    lib_plugins.reset_package_plugins()\n    lib_plugins.register_package_plugins()\n    lib_plugins.reset_group_plugins()\n    lib_plugins.register_group_plugins()\n\n    # initialise the globals\n    app_globals.app_globals._init()\n\n    helpers.load_plugin_helpers()\n    config['pylons.h'] = helpers.helper_functions\n\n    # Templates and CSS loading from configuration\n    valid_base_templates_folder_names = ['templates']\n    templates = config.get('ckan.base_templates_folder', 'templates')\n    config['ckan.base_templates_folder'] = templates\n\n    if templates not in valid_base_templates_folder_names:\n        raise CkanConfigurationException(\n            'You provided an invalid value for ckan.base_templates_folder. '\n            'Possible values are: \"templates\".'\n        )\n\n    jinja2_templates_path = os.path.join(root, templates)\n    log.info('Loading templates from %s' % jinja2_templates_path)\n    template_paths = [jinja2_templates_path]\n\n    extra_template_paths = config.get('extra_template_paths', '')\n    if extra_template_paths:\n        # must be first for them to override defaults\n        template_paths = extra_template_paths.split(',') + template_paths\n    config['computed_template_paths'] = template_paths\n\n    # Markdown ignores the logger config, so to get rid of excessive\n    # markdown debug messages in the log, set it to the level of the\n    # root logger.\n    logging.getLogger(\"MARKDOWN\").setLevel(logging.getLogger().level)\n\n    if six.PY2:\n        # Create Jinja2 environment\n        env = jinja_extensions.Environment(\n            **jinja_extensions.get_jinja_env_options())\n        env.install_gettext_callables(_, ungettext, newstyle=True)\n        # custom filters\n        env.policies['ext.i18n.trimmed'] = True\n        env.filters['empty_and_escape'] = jinja_extensions.empty_and_escape\n        config['pylons.app_globals'].jinja_env = env\n\n    # CONFIGURATION OPTIONS HERE (note: all config options will override\n    # any Pylons config options)\n\n    # Enable pessimistic disconnect handling (added in SQLAlchemy 1.2)\n    # to eliminate database errors due to stale pooled connections\n    config.setdefault('pool_pre_ping', True)\n\n    # Initialize SQLAlchemy\n    engine = sqlalchemy.engine_from_config(config)\n    model.init_model(engine)\n\n    for plugin in p.PluginImplementations(p.IConfigurable):\n        plugin.configure(config)\n\n    # reset the template cache - we do this here so that when we load the\n    # environment it is clean\n    render.reset_template_info_cache()\n\n    # clear other caches\n    logic.clear_actions_cache()\n    logic.clear_validators_cache()\n    authz.clear_auth_functions_cache()\n\n    # Here we create the site user if they are not already in the database\n    try:\n        logic.get_action('get_site_user')({'ignore_auth': True}, None)\n    except (sqlalchemy.exc.ProgrammingError, sqlalchemy.exc.OperationalError):\n        # (ProgrammingError for Postgres, OperationalError for SQLite)\n        # The database is not initialised.  This is a bit dirty.  This occurs\n        # when running tests.\n        pass\n    except sqlalchemy.exc.InternalError:\n        # The database is not initialised.  Travis hits this\n        pass\n\n    # Close current session and open database connections to ensure a clean\n    # clean environment even if an error occurs later on\n    model.Session.remove()\n    model.Session.bind.dispose()\n", "path": "ckan/config/environment.py"}], "after_files": [{"content": "# encoding: utf-8\n\n'''CKAN environment configuration'''\nimport os\nimport logging\nimport warnings\nimport pytz\n\nimport six\nimport sqlalchemy\n\nfrom six.moves.urllib.parse import urlparse\n\nimport ckan.model as model\nimport ckan.plugins as p\nimport ckan.lib.plugins as lib_plugins\nimport ckan.lib.helpers as helpers\nimport ckan.lib.app_globals as app_globals\nfrom ckan.lib.redis import is_redis_available\nimport ckan.lib.render as render\nimport ckan.lib.search as search\nimport ckan.logic as logic\nimport ckan.authz as authz\nimport ckan.lib.jinja_extensions as jinja_extensions\nfrom ckan.lib.webassets_tools import webassets_init\nfrom ckan.lib.i18n import build_js_translations\n\nfrom ckan.common import _, ungettext, config\nfrom ckan.exceptions import CkanConfigurationException\n\nif six.PY2:\n    from pylons import config as pylons_config\n\n\nlog = logging.getLogger(__name__)\n\n\n# Suppress benign warning 'Unbuilt egg for setuptools'\nwarnings.simplefilter('ignore', UserWarning)\n\n\ndef load_environment(conf):\n    \"\"\"\n    Configure the Pylons environment via the ``pylons.config`` object. This\n    code should only need to be run once.\n    \"\"\"\n    if six.PY2:\n        # this must be run at a time when the env is semi-setup, thus inlined\n        # here. Required by the deliverance plugin and iATI\n        from pylons.wsgiapp import PylonsApp\n        import pkg_resources\n        find_controller_generic = getattr(\n            PylonsApp.find_controller,\n            '_old_find_controller',\n            PylonsApp.find_controller)\n\n        # This is from pylons 1.0 source, will monkey-patch into 0.9.7\n        def find_controller(self, controller):\n            if controller in self.controller_classes:\n                return self.controller_classes[controller]\n            # Check to see if its a dotted name\n            if '.' in controller or ':' in controller:\n                ep = pkg_resources.EntryPoint.parse('x={0}'.format(controller))\n\n                if hasattr(ep, 'resolve'):\n                    # setuptools >= 10.2\n                    mycontroller = ep.resolve()\n                else:\n                    # setuptools >= 11.3\n                    mycontroller = ep.load(False)\n\n                self.controller_classes[controller] = mycontroller\n                return mycontroller\n            return find_controller_generic(self, controller)\n        find_controller._old_find_controller = find_controller_generic\n        PylonsApp.find_controller = find_controller\n\n    os.environ['CKAN_CONFIG'] = conf['__file__']\n\n    # Pylons paths\n    root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))\n\n    valid_base_public_folder_names = ['public']\n    static_files = conf.get('ckan.base_public_folder', 'public')\n    conf['ckan.base_public_folder'] = static_files\n\n    if static_files not in valid_base_public_folder_names:\n        raise CkanConfigurationException(\n            'You provided an invalid value for ckan.base_public_folder. '\n            'Possible values are: \"public\".'\n        )\n\n    log.info('Loading static files from %s' % static_files)\n    paths = dict(root=root,\n                 controllers=os.path.join(root, 'controllers'),\n                 static_files=os.path.join(root, static_files),\n                 templates=[])\n\n    # Initialize main CKAN config object\n    config.update(conf)\n\n    if six.PY2:\n        # Initialize Pylons own config object\n        pylons_config.init_app(\n            conf['global_conf'], conf, package='ckan', paths=paths)\n\n        # Update the main CKAN config object with the Pylons specific stuff,\n        # as it is quite hard to keep them separated. This should be removed\n        # once Pylons support is dropped\n        config.update(pylons_config)\n\n    # Setup the SQLAlchemy database engine\n    # Suppress a couple of sqlalchemy warnings\n    msgs = ['^Unicode type received non-unicode bind param value',\n            \"^Did not recognize type 'BIGINT' of column 'size'\",\n            \"^Did not recognize type 'tsvector' of column 'search_vector'\"\n            ]\n    for msg in msgs:\n        warnings.filterwarnings('ignore', msg, sqlalchemy.exc.SAWarning)\n\n    # load all CKAN plugins\n    p.load_all()\n\n    # Check Redis availability\n    if not is_redis_available():\n        log.critical('Could not connect to Redis.')\n\n    app_globals.reset()\n\n    # issue #3260: remove idle transaction\n    # Session that was used for getting all config params nor committed,\n    # neither removed and we have idle connection as result\n    model.Session.commit()\n\n    # Build JavaScript translations. Must be done after plugins have\n    # been loaded.\n    build_js_translations()\n\n\n# A mapping of config settings that can be overridden by env vars.\n# Note: Do not remove the following lines, they are used in the docs\n# Start CONFIG_FROM_ENV_VARS\nCONFIG_FROM_ENV_VARS = {\n    'sqlalchemy.url': 'CKAN_SQLALCHEMY_URL',\n    'ckan.datastore.write_url': 'CKAN_DATASTORE_WRITE_URL',\n    'ckan.datastore.read_url': 'CKAN_DATASTORE_READ_URL',\n    'ckan.redis.url': 'CKAN_REDIS_URL',\n    'solr_url': 'CKAN_SOLR_URL',\n    'solr_user': 'CKAN_SOLR_USER',\n    'solr_password': 'CKAN_SOLR_PASSWORD',\n    'ckan.site_id': 'CKAN_SITE_ID',\n    'ckan.site_url': 'CKAN_SITE_URL',\n    'ckan.storage_path': 'CKAN_STORAGE_PATH',\n    'ckan.datapusher.url': 'CKAN_DATAPUSHER_URL',\n    'smtp.server': 'CKAN_SMTP_SERVER',\n    'smtp.starttls': 'CKAN_SMTP_STARTTLS',\n    'smtp.user': 'CKAN_SMTP_USER',\n    'smtp.password': 'CKAN_SMTP_PASSWORD',\n    'smtp.mail_from': 'CKAN_SMTP_MAIL_FROM',\n    'ckan.max_resource_size': 'CKAN_MAX_UPLOAD_SIZE_MB'\n}\n# End CONFIG_FROM_ENV_VARS\n\n\ndef update_config():\n    ''' This code needs to be run when the config is changed to take those\n    changes into account. It is called whenever a plugin is loaded as the\n    plugin might have changed the config values (for instance it might\n    change ckan.site_url) '''\n\n    webassets_init()\n\n    for plugin in p.PluginImplementations(p.IConfigurer):\n        # must do update in place as this does not work:\n        # config = plugin.update_config(config)\n        plugin.update_config(config)\n\n    # Set whitelisted env vars on config object\n    # This is set up before globals are initialized\n\n    ckan_db = os.environ.get('CKAN_DB', None)\n    if ckan_db:\n        msg = 'Setting CKAN_DB as an env var is deprecated and will be' \\\n            ' removed in a future release. Use CKAN_SQLALCHEMY_URL instead.'\n        log.warn(msg)\n        config['sqlalchemy.url'] = ckan_db\n\n    for option in CONFIG_FROM_ENV_VARS:\n        from_env = os.environ.get(CONFIG_FROM_ENV_VARS[option], None)\n        if from_env:\n            config[option] = from_env\n\n    root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))\n\n    site_url = config.get('ckan.site_url', '')\n    if not site_url:\n        raise RuntimeError(\n            'ckan.site_url is not configured and it must have a value.'\n            ' Please amend your .ini file.')\n    if not site_url.lower().startswith('http'):\n        raise RuntimeError(\n            'ckan.site_url should be a full URL, including the schema '\n            '(http or https)')\n\n    display_timezone = config.get('ckan.display_timezone', '')\n    if (display_timezone and\n            display_timezone != 'server' and\n            display_timezone not in pytz.all_timezones):\n        raise CkanConfigurationException(\n            \"ckan.display_timezone is not 'server' or a valid timezone\"\n        )\n\n    # Remove backslash from site_url if present\n    config['ckan.site_url'] = config['ckan.site_url'].rstrip('/')\n\n    ckan_host = config['ckan.host'] = urlparse(site_url).netloc\n    if config.get('ckan.site_id') is None:\n        if ':' in ckan_host:\n            ckan_host, port = ckan_host.split(':')\n        assert ckan_host, 'You need to configure ckan.site_url or ' \\\n                          'ckan.site_id for SOLR search-index rebuild to work.'\n        config['ckan.site_id'] = ckan_host\n\n    # ensure that a favicon has been set\n    favicon = config.get('ckan.favicon', '/base/images/ckan.ico')\n    config['ckan.favicon'] = favicon\n\n    # Init SOLR settings and check if the schema is compatible\n    # from ckan.lib.search import SolrSettings, check_solr_schema_version\n\n    # lib.search is imported here as we need the config enabled and parsed\n    search.SolrSettings.init(config.get('solr_url'),\n                             config.get('solr_user'),\n                             config.get('solr_password'))\n    search.check_solr_schema_version()\n\n    lib_plugins.reset_package_plugins()\n    lib_plugins.register_package_plugins()\n    lib_plugins.reset_group_plugins()\n    lib_plugins.register_group_plugins()\n\n    # initialise the globals\n    app_globals.app_globals._init()\n\n    helpers.load_plugin_helpers()\n    config['pylons.h'] = helpers.helper_functions\n\n    # Templates and CSS loading from configuration\n    valid_base_templates_folder_names = ['templates']\n    templates = config.get('ckan.base_templates_folder', 'templates')\n    config['ckan.base_templates_folder'] = templates\n\n    if templates not in valid_base_templates_folder_names:\n        raise CkanConfigurationException(\n            'You provided an invalid value for ckan.base_templates_folder. '\n            'Possible values are: \"templates\".'\n        )\n\n    jinja2_templates_path = os.path.join(root, templates)\n    log.info('Loading templates from %s' % jinja2_templates_path)\n    template_paths = [jinja2_templates_path]\n\n    extra_template_paths = config.get('extra_template_paths', '')\n    if extra_template_paths:\n        # must be first for them to override defaults\n        template_paths = extra_template_paths.split(',') + template_paths\n    config['computed_template_paths'] = template_paths\n\n    # Markdown ignores the logger config, so to get rid of excessive\n    # markdown debug messages in the log, set it to the level of the\n    # root logger.\n    logging.getLogger(\"MARKDOWN\").setLevel(logging.getLogger().level)\n\n    if six.PY2:\n        # Create Jinja2 environment\n        env = jinja_extensions.Environment(\n            **jinja_extensions.get_jinja_env_options())\n        env.install_gettext_callables(_, ungettext, newstyle=True)\n        # custom filters\n        env.policies['ext.i18n.trimmed'] = True\n        env.filters['empty_and_escape'] = jinja_extensions.empty_and_escape\n        config['pylons.app_globals'].jinja_env = env\n\n    # CONFIGURATION OPTIONS HERE (note: all config options will override\n    # any Pylons config options)\n\n    # Enable pessimistic disconnect handling (added in SQLAlchemy 1.2)\n    # to eliminate database errors due to stale pooled connections\n    config.setdefault('sqlalchemy.pool_pre_ping', True)\n\n    # Initialize SQLAlchemy\n    engine = sqlalchemy.engine_from_config(config)\n    model.init_model(engine)\n\n    for plugin in p.PluginImplementations(p.IConfigurable):\n        plugin.configure(config)\n\n    # reset the template cache - we do this here so that when we load the\n    # environment it is clean\n    render.reset_template_info_cache()\n\n    # clear other caches\n    logic.clear_actions_cache()\n    logic.clear_validators_cache()\n    authz.clear_auth_functions_cache()\n\n    # Here we create the site user if they are not already in the database\n    try:\n        logic.get_action('get_site_user')({'ignore_auth': True}, None)\n    except (sqlalchemy.exc.ProgrammingError, sqlalchemy.exc.OperationalError):\n        # (ProgrammingError for Postgres, OperationalError for SQLite)\n        # The database is not initialised.  This is a bit dirty.  This occurs\n        # when running tests.\n        pass\n    except sqlalchemy.exc.InternalError:\n        # The database is not initialised.  Travis hits this\n        pass\n\n    # Close current session and open database connections to ensure a clean\n    # clean environment even if an error occurs later on\n    model.Session.remove()\n    model.Session.bind.dispose()\n", "path": "ckan/config/environment.py"}]}