Changeset 14:0a6affee82cd in mailjam


Ignore:
Timestamp:
May 21, 2012, 4:50:48 PM (10 years ago)
Author:
Borja Lopez <borja@…>
Branch:
default
Phase:
public
Message:

Added a configuration file specific for the MTA client.

Replaced the postman.config module with a new version based on classes. Now,
instead the old get_config_parameters() function (a modified version of it is
still there, to ensure backwards compatibility) there is a DaemonConfig class,
which contains some methods to perform the config file parsing and configuration
parameters extraction. That class will be then subclassed in order to have
different configuration classes for the daemon and the different clients (mta,
cli, web, etc).

Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • conf/postman.conf

    r13 r14  
    11#
    2 # postman.conf - Postman configuration file
     2# postman.conf - Postman daemon configuration file
    33#
    44
  • postman/config.py

    r11 r14  
    1414from ConfigParser import SafeConfigParser
    1515
     16class DaemonConfig():
     17    def __init__(self, configfile=None):
     18        self.configfile = configfile
     19        self.default_paths = ['/usr/local/etc/postman', '/usr/local/etc',
     20                              '/etc/postman', '/etc',
     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        """
     30        return 'postman.conf'
     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.
     37
     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   
     44    def validate_configfile(self):       
     45        if self.configfile:
     46            if not os.path.exists(self.configfile):
     47                # the file does not exist, so we override it with None,
     48                # allowing the next few lines to search for a valid
     49                # config file on the usual places
     50                self.configfile = None
     51           
     52        if not self.configfile:
     53            for path in self.default_paths:
     54                full_path = os.path.join(path, self.default_filename)
     55                if os.path.exists(full_path):
     56                    self.configfile = full_path
     57                    return full_path
     58               
     59        # if we reach here, self.configfile will be still None, no valid
     60        # config files were found, and so we raise an exception
     61        raise IOError('ERROR - Can not find ' + self.default_filename + \
     62                      ' in your environment')
     63
     64    def load(self):
     65        if not self.configfile:
     66            self.validate_configfile()
     67        parser = SafeConfigParser()
     68        parser.read(self.configfile)
     69        for section in self.sections:
     70            self.config[section] = {}
     71            for name, value in parser.items(section):
     72                self.config[section][name] = value
     73        return self.config       
     74
     75    def get_section_parameters(self, section):
     76        if section not in self.sections:
     77            raise IndexError('ERROR - ' + section + \
     78                             ' is not one of the available sections: ' + \
     79                             ', '.join(self.sections))
     80        if section not in self.config.keys():
     81            # perhaps the config hasn't been loaded yet
     82            self.load()
     83
     84        return self.config[section]
     85
     86class MTAClientConfig(DaemonConfig):
     87    @property
     88    def default_filename():
     89        return 'postman-mta.conf'
     90
     91    @property
     92    def sections(self):
     93        return ['postman_server', ]
     94
     95
    1696def 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')
     97    """
     98    This function is kept here for backwards compatibility.
     99    By default all the daemon code will use this function to get
     100    configuration parameters from the default configuration file.
     101    """
     102    config = DaemonConfig(configfile)
     103    return config.get_section_parameters(section)
    35104
    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 
     105   
Note: See TracChangeset for help on using the changeset viewer.