Add automatic RAMDb update from MongoDB, using threading2 readers-writer lock instead...
[infos-pratiques:etalage.git] / etalage / environment.py
1 # -*- coding: utf-8 -*-
2
3
4 # Etalage -- Open Data POIs portal
5 # By: Emmanuel Raviart <eraviart@easter-eggs.com>
6 #
7 # Copyright (C) 2011 Easter-eggs
8 # http://gitorious.org/infos-pratiques/etalage
9 #
10 # This file is part of Etalage.
11 #
12 # Etalage is free software; you can redistribute it and/or modify
13 # it under the terms of the GNU Affero General Public License as
14 # published by the Free Software Foundation, either version 3 of the
15 # License, or (at your option) any later version.
16 #
17 # Etalage is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 # GNU Affero General Public License for more details.
21 #
22 # You should have received a copy of the GNU Affero General Public License
23 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
25
26 """Environment configuration"""
27
28
29 import logging
30 import os
31 import sys
32
33 import mako.lookup
34 import pymongo
35 from suq import monpyjama
36 from etalage import ramdb
37
38 import etalage
39 from . import conv, model, templates
40
41
42 app_dir = os.path.dirname(os.path.abspath(__file__))
43
44
45 def load_environment(global_conf, app_conf):
46     """Configure the application environment."""
47     conf = etalage.conf # Empty dictionary
48     conf.update({
49         'app_conf': app_conf,
50         'app_dir': app_dir,
51         'cache_dir': os.path.join(os.path.dirname(app_dir), 'cache'),
52         'categories_collection': 'categories',
53         'data_updates_collection': 'data_updates',
54         'database': 'souk',
55         'debug': False,
56         'global_conf': global_conf,
57         'i18n_dir': os.path.join(app_dir, 'i18n'),
58         'log_level': 'WARNING',
59         'organism_types_collection': 'organism_types',
60         'pois_collection': 'pois',
61         'package_name': 'etalage',
62         'static_files': True, # Whether this application serves its own static files
63         'static_files_dir': os.path.join(app_dir, 'static'),
64         'territories_collection': 'territories',
65         })
66     conf.update(global_conf)
67     conf.update(app_conf)
68     conf['debug'] = conv.check(conv.pipe(conv.guess_bool, conv.default(False)))(conf['debug'])
69     conf['log_level'] = getattr(logging, conf['log_level'].upper())
70     conf['static_files'] = conv.check(conv.pipe(conv.guess_bool, conv.default(False)))(conf['static_files'])
71
72     # Configure logging.
73     logging.basicConfig(level = conf['log_level'], stream = sys.stdout)
74
75     errorware = conf.setdefault('errorware', {})
76     errorware['debug'] = conf['debug']
77     if not errorware['debug']:
78         errorware['error_email'] = conf['email_to']
79         errorware['error_log'] = conf.get('error_log', None)
80         errorware['error_message'] = conf.get('error_message', 'An internal server error occurred')
81         errorware['error_subject_prefix'] = conf.get('error_subject_prefix', 'Etalage Error: ')
82         errorware['from_address'] = conf['from_address']
83         errorware['smtp_server'] = conf.get('smtp_server', 'localhost')
84
85     # Connect to MongoDB database.
86     monpyjama.Wrapper.db = model.db = pymongo.Connection()[conf['database']]
87     # Initialize ramdb database from MongoDB.
88     ramdb.load()
89
90     # Create the Mako TemplateLookup, with the default auto-escaping.
91     templates.lookup = mako.lookup.TemplateLookup(
92         default_filters = ['h'],
93         directories = [os.path.join(app_dir, 'templates')],
94 #        error_handler = handle_mako_error,
95         input_encoding = 'utf-8', 
96         module_directory = os.path.join(conf['cache_dir'], 'templates'),
97 #        strict_undefined = True,
98         )
99