1 | # -*- coding: utf-8 -*-
|
---|
2 |
|
---|
3 | """
|
---|
4 | The postman project - config.py
|
---|
5 |
|
---|
6 | This file is released under the BSD license, see LICENSE for
|
---|
7 | more information.
|
---|
8 |
|
---|
9 | Francisco de Borja Lopez Rio - <borja@codigo23.net>
|
---|
10 | Soluciones Informaticas Codigo23 S.L.U. - http://codigo23.net
|
---|
11 | """
|
---|
12 |
|
---|
13 | import os
|
---|
14 | from ConfigParser import SafeConfigParser
|
---|
15 |
|
---|
16 | def get_config_parameters(section=None, configfile=None):
|
---|
17 | if not configfile:
|
---|
18 | # If there is no config file defined, try with the usual places
|
---|
19 | # for config files, if there is no config file there, try to load
|
---|
20 | # the one provided with postman sources
|
---|
21 | default_paths = ['/usr/local/etc/postman/postman.conf',
|
---|
22 | '/usr/local/etc/postman.conf',
|
---|
23 | '/etc/postman/postman.conf',
|
---|
24 | '/etc/postman.conf',
|
---|
25 | os.path.join(os.path.dirname(__file__),
|
---|
26 | '../conf/postman.conf')]
|
---|
27 | for path in default_paths:
|
---|
28 | if os.path.exists(path):
|
---|
29 | configfile = path
|
---|
30 | break
|
---|
31 |
|
---|
32 | # if there is no config file now, raise an exception, as we need one
|
---|
33 | if not configfile:
|
---|
34 | raise IOError('ERROR - Can not find postman.conf in your environment')
|
---|
35 |
|
---|
36 | available_sections = ['xmlrpc_server', 'storage', 'archive',
|
---|
37 | 'mailing_lists', 'members']
|
---|
38 |
|
---|
39 | config = {}
|
---|
40 |
|
---|
41 | parser = SafeConfigParser()
|
---|
42 | # FIXME: we should check here if the config file has been read correctly,
|
---|
43 | # instead of letting an exception to pass through
|
---|
44 | parser.read(configfile)
|
---|
45 |
|
---|
46 | if section in available_sections:
|
---|
47 | for name, value in parser.items(section):
|
---|
48 | config[name] = value
|
---|
49 | return config
|
---|
50 |
|
---|
51 | # if no section (or an invalid section) is provided, return an empty config
|
---|
52 | # set
|
---|
53 | return config
|
---|
54 |
|
---|