[0] | 1 | # -*- coding: utf-8 -*-
|
---|
| 2 |
|
---|
[11] | 3 | """
|
---|
[16] | 4 | The mailjam project - config.py
|
---|
[11] | 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 |
|
---|
[0] | 13 | import os
|
---|
[9] | 14 | from ConfigParser import SafeConfigParser
|
---|
| 15 |
|
---|
[14] | 16 | class DaemonConfig():
|
---|
| 17 | def __init__(self, configfile=None):
|
---|
| 18 | self.configfile = configfile
|
---|
[27] | 19 | self.default_paths = ['/etc/mailjam', '/etc',
|
---|
| 20 | '/usr/local/etc/mailjam', '/usr/local/etc',
|
---|
[14] | 21 | os.path.join(os.path.dirname(__file__),'../conf')]
|
---|
| 22 | self.config = {}
|
---|
| 23 |
|
---|
| 24 | @property
|
---|
| 25 | def default_filename(self):
|
---|
| 26 | """
|
---|
| 27 | Method to get the default configuration filename. It is a method instead
|
---|
| 28 | of an attribute for easier subclassing
|
---|
| 29 | """
|
---|
[16] | 30 | return 'mailjam.conf'
|
---|
[14] | 31 |
|
---|
| 32 | @property
|
---|
| 33 | def sections(self):
|
---|
| 34 | """
|
---|
| 35 | Method to get the list of available sections in the configuration file.
|
---|
| 36 | It is a method instead of an attribute for easier subclassing.
|
---|
[9] | 37 |
|
---|
[14] | 38 | FIXME: Perhaps it would be better to dinamically get the list of
|
---|
| 39 | sections from the configuration file itself.
|
---|
| 40 | """
|
---|
| 41 | return ['xmlrpc_server', 'storage', 'archive',
|
---|
| 42 | 'mailing_lists', 'members']
|
---|
| 43 |
|
---|
[20] | 44 | def validate_configfile(self):
|
---|
[14] | 45 | if self.configfile:
|
---|
[20] | 46 | # ensure we have the full path to the provided file
|
---|
| 47 | self.configfile = os.path.join(os.path.dirname(__file__),
|
---|
| 48 | self.configfile)
|
---|
[14] | 49 | if not os.path.exists(self.configfile):
|
---|
| 50 | # the file does not exist, so we override it with None,
|
---|
| 51 | # allowing the next few lines to search for a valid
|
---|
| 52 | # config file on the usual places
|
---|
| 53 | self.configfile = None
|
---|
[20] | 54 | else:
|
---|
| 55 | return self.configfile
|
---|
[14] | 56 |
|
---|
| 57 | if not self.configfile:
|
---|
| 58 | for path in self.default_paths:
|
---|
| 59 | full_path = os.path.join(path, self.default_filename)
|
---|
| 60 | if os.path.exists(full_path):
|
---|
| 61 | self.configfile = full_path
|
---|
| 62 | return full_path
|
---|
[20] | 63 |
|
---|
[14] | 64 | # if we reach here, self.configfile will be still None, no valid
|
---|
| 65 | # config files were found, and so we raise an exception
|
---|
| 66 | raise IOError('ERROR - Can not find ' + self.default_filename + \
|
---|
| 67 | ' in your environment')
|
---|
[9] | 68 |
|
---|
[14] | 69 | def load(self):
|
---|
| 70 | if not self.configfile:
|
---|
| 71 | self.validate_configfile()
|
---|
| 72 | parser = SafeConfigParser()
|
---|
| 73 | parser.read(self.configfile)
|
---|
| 74 | for section in self.sections:
|
---|
| 75 | self.config[section] = {}
|
---|
| 76 | for name, value in parser.items(section):
|
---|
| 77 | self.config[section][name] = value
|
---|
| 78 | return self.config
|
---|
| 79 |
|
---|
| 80 | def get_section_parameters(self, section):
|
---|
| 81 | if section not in self.sections:
|
---|
| 82 | raise IndexError('ERROR - ' + section + \
|
---|
| 83 | ' is not one of the available sections: ' + \
|
---|
| 84 | ', '.join(self.sections))
|
---|
| 85 | if section not in self.config.keys():
|
---|
| 86 | # perhaps the config hasn't been loaded yet
|
---|
| 87 | self.load()
|
---|
[9] | 88 |
|
---|
[14] | 89 | return self.config[section]
|
---|
| 90 |
|
---|
[23] | 91 |
|
---|
[14] | 92 | class MTAClientConfig(DaemonConfig):
|
---|
| 93 | @property
|
---|
[15] | 94 | def default_filename(self):
|
---|
[16] | 95 | return 'mailjam-mta.conf'
|
---|
[14] | 96 |
|
---|
| 97 | @property
|
---|
| 98 | def sections(self):
|
---|
[15] | 99 | return ['server', 'archive']
|
---|
[9] | 100 |
|
---|
[14] | 101 |
|
---|
[23] | 102 | class CLIClientConfig(DaemonConfig):
|
---|
| 103 | @property
|
---|
| 104 | def default_filename(self):
|
---|
| 105 | return 'mailjam-cli.conf'
|
---|
| 106 |
|
---|
| 107 | @property
|
---|
| 108 | def sections(self):
|
---|
[27] | 109 | return ['server', 'history']
|
---|
[23] | 110 |
|
---|
| 111 |
|
---|
[14] | 112 | def get_config_parameters(section=None, configfile=None):
|
---|
| 113 | """
|
---|
| 114 | This function is kept here for backwards compatibility.
|
---|
| 115 | By default all the daemon code will use this function to get
|
---|
| 116 | configuration parameters from the default configuration file.
|
---|
| 117 | """
|
---|
| 118 | config = DaemonConfig(configfile)
|
---|
| 119 | return config.get_section_parameters(section)
|
---|
| 120 |
|
---|
| 121 |
|
---|