Add configuration for ignored fields.
[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, 2012 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 from biryani import strings
34 import mako.lookup
35 import pymongo
36 from suq import monpyjama
37 from etalage import ramdb
38
39 import etalage
40 from . import conv, model, templates
41
42
43 app_dir = os.path.dirname(os.path.abspath(__file__))
44
45
46 def load_environment(global_conf, app_conf):
47     """Configure the application environment."""
48     conf = etalage.conf # Empty dictionary
49     conf.update(strings.deep_decode(global_conf))
50     conf.update(strings.deep_decode(app_conf))
51     conf.update(conv.check(conv.struct(
52         {
53             'app_conf': conv.set_value(app_conf),
54             'app_dir': conv.set_value(app_dir),
55             'cache_dir': conv.default(os.path.join(os.path.dirname(app_dir), 'cache')),
56             'categories_collection': conv.default('categories'),
57             'custom_templates_dir': conv.default(None),
58             'data_updates_collection': conv.default('data_updates'),
59             'database': conv.default('souk'),
60             'debug': conv.pipe(conv.guess_bool, conv.default(False)),
61             'global_conf': conv.set_value(global_conf),
62             'i18n_dir': conv.default(os.path.join(app_dir, 'i18n')),
63             'ignored_fields': conv.pipe(
64                 conv.function(lambda lines: lines.split(u'\n')),
65                 conv.uniform_sequence(conv.pipe(
66                     conv.function(lambda line: line.split(None, 1)),
67                     conv.uniform_sequence(conv.str_to_slug),
68                     conv.function(lambda seq: dict(zip(['id', 'name'], seq))),
69                     )),
70                 conv.id_name_dict_list_to_ignored_fields,
71                 ),
72             'log_level': conv.pipe(
73                 conv.default('WARNING'),
74                 conv.function(lambda log_level: getattr(logging, log_level.upper())),
75                 ),
76             'organism_types_collection': conv.default('organism_types'),
77             'pois_collection': conv.default('pois'),
78             'package_name': conv.default('etalage'),
79             'realm': conv.default(u'Etalage'),
80             # Whether this application serves its own static files.
81             'static_files': conv.pipe(conv.guess_bool, conv.default(True)),
82             'static_files_dir': conv.default(os.path.join(app_dir, 'static')),
83             'territories_collection': conv.default('territories'),
84             },
85         default = 'ignore',
86         keep_missing_values = True,
87         ))(conf))
88
89     # Configure logging.
90     logging.basicConfig(level = conf['log_level'], stream = sys.stdout)
91
92     errorware = conf.setdefault('errorware', {})
93     errorware['debug'] = conf['debug']
94     if not errorware['debug']:
95         errorware['error_email'] = conf['email_to']
96         errorware['error_log'] = conf.get('error_log', None)
97         errorware['error_message'] = conf.get('error_message', 'An internal server error occurred')
98         errorware['error_subject_prefix'] = conf.get('error_subject_prefix', 'Etalage Error: ')
99         errorware['from_address'] = conf['from_address']
100         errorware['smtp_server'] = conf.get('smtp_server', 'localhost')
101
102     # Connect to MongoDB database.
103     monpyjama.Wrapper.db = model.db = pymongo.Connection()[conf['database']]
104     # Initialize ramdb database from MongoDB.
105     ramdb.load()
106
107     # Create the Mako TemplateLookup, with the default auto-escaping.
108     templates_dirs = []
109     if conf['custom_templates_dir']:
110         templates_dirs.append(conf['custom_templates_dir'])
111     templates_dirs.append(os.path.join(app_dir, 'templates'))
112     templates.lookup = mako.lookup.TemplateLookup(
113         default_filters = ['h'],
114         directories = templates_dirs,
115 #        error_handler = handle_mako_error,
116         input_encoding = 'utf-8', 
117         module_directory = os.path.join(conf['cache_dir'], 'templates'),
118 #        strict_undefined = True,
119         )
120