diff --git a/.gitignore b/.gitignore index d123a62472dd92ae9d213c1dfd577e91fdad3b40..b59df5c30a75add3fb5155c1e392d91c87ae7149 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ /dist /mutalyzer.egg-info /static +/mutalyzer.conf diff --git a/extras/config.example b/extras/config.example index 62bf3ce30edb865a31f1a21c174f5c0fc99497ae..251ca14333ac6cf18b68d62ccb632fc47947aa1c 100644 --- a/extras/config.example +++ b/extras/config.example @@ -1,7 +1,9 @@ # # Mutalyzer config file. # -# Copy this file to /etc/mutalyzer/config and modify to suit your preferences. +# Specify the location of this file in the MUTALYZER_SETTINGS environment +# variable. + # # These settings are used by the Retriever module. diff --git a/extras/config.user.example b/extras/config.user.example index fe0887d72c6b17bc0c0e00f51a435ef9cb46ed29..fd3f1428a8ee37d248bed33a19316948cb342a1d 100644 --- a/extras/config.user.example +++ b/extras/config.user.example @@ -1,8 +1,8 @@ # # Mutalyzer config file. # -# Copy this file to ~/.config/mutalyzer/config to overwrite definitions from -# /etc/mutalyzer.config. +# Specify the location of this file in the MUTALYZER_SETTINGS environment +# variable. # Use this email address for retrieval of records at the NCBI. email = "mutalyzer@humgen.nl" diff --git a/mutalyzer/config.py b/mutalyzer/config.py index 0627927cacee381a91033a666f8f8ba17c477779..dc8b5a83ebf14e62110050b9cad3af6806657eef 100644 --- a/mutalyzer/config.py +++ b/mutalyzer/config.py @@ -1,5 +1,5 @@ """ -Module for reading the configuration values from configuration files. +Module for reading the configuration values from a configuration file. All communication with this module should be done by using the get function which returns a configuration value, given a name. @@ -7,28 +7,19 @@ which returns a configuration value, given a name. Reading the configuration file is implemented lazily and as such done upon the first call to the get function. -Configuration values are read from two locations, in this order: -1) /etc/mutalyzer/config -2) $XDG_CONFIG_HOME/mutalyzer/config - -If both files exist, values defined in the second overwrite values defined in -the first. +Configuration is read from the file specified by the `MUTALYZER_SETTINGS` +environment variable, or `mutalyzer.conf` in the current directory if it is +not set. """ import os + from configobj import ConfigObj from mutalyzer.util import singleton -SYSTEM_CONFIGURATION = '/etc/mutalyzer/config' -USER_CONFIGURATION = os.path.join( - os.environ.get('XDG_CONFIG_HOME', None) or \ - os.path.join(os.path.expanduser('~'), '.config'), - 'mutalyzer', 'config') - - class ConfigurationError(Exception): """ Raised when a configuration file cannot be read. @@ -40,14 +31,10 @@ def get(name): """ Get a configuration value by name. - @arg name: Name for the configuration value. - @type name: string + :arg name: Name for the configuration value. + :type name: string - @raise ConfigurationError: If configuration value could not be read. - Reasons are: - - Configuration file could not be parsed. - - Not all variables are present in configuration file. - - Given configuration value name does not exist. + :raise ConfigurationError: If configuration value could not be read. """ return _Config().get(name) @@ -60,51 +47,19 @@ class _Config(): Please note the limitations from the use of the @singleton decorator as described in its docstring. """ - def __init__(self, filename=None): + def __init__(self): """ Initialise the class with variables read from the configuration - file. In principle, this is the only place in the code where a - hard coded constant is used (the name and path to the configuration - file). - - Configuration values are read from two locations, in this order: - 1) /etc/mutalyzer/config - 2) $XDG_CONFIG_HOME/mutalyzer/config - - If both files exist, values defined in the second overwrite values - defined in the first. - - An exception to this system is when the optional {filename} argument - is set. In that case, the locations listed above are ignored and the - configuration is read from {filename}. - - @kwarg filename: Optional filename to read configuration from. If - present, this overrides automatic detection of configuration file - location. - @type filename: string - - @raise ConfigurationError: If configuration could not be read. - Reasons are: - - Supplied argument {filename} could not be opened. - - Configuration file could not be parsed. - - Not all variables are present in configuration file. + file. + + Configuration values are read from the file specified by the + `MUTALYZER_SETTINGS` environment variable, or `mutalyzer.conf` in the + current directory if it is not set. + + :raises ConfigurationError: If configuration could not be read. """ - config = None - - if filename: - config = self._load_config(filename) - else: - if os.path.isfile(SYSTEM_CONFIGURATION): - config = self._load_config(SYSTEM_CONFIGURATION) - if os.path.isfile(USER_CONFIGURATION): - user_config = self._load_config(USER_CONFIGURATION) - if config: - config.merge(user_config) - else: - config = user_config - - if not config: - raise ConfigurationError('Could not locate configuration.') + filename = os.environ.get('MUTALYZER_SETTINGS', 'mutalyzer.conf') + config = self._load_config(filename) # We define default values for many configuration settings (except for # some that are mandatory for the user to define, i.e. those in the @@ -191,35 +146,31 @@ class _Config(): except KeyError as e: raise ConfigurationError('Missing configuration value: %s' % e) - #__init__ def get(self, name): """ Get a configuration value by name. - @arg name: Name for the configuration value. - @type name: string + :arg name: Name for the configuration value. + :type name: string - @raise ConfigurationError: If given configuration value name does not - exist. + :raises ConfigurationError: If given configuration value name does not + exist. """ try: return self._values[name] except KeyError: raise ConfigurationError('No such configuration value: %s' % name) - #get def _load_config(self, filename): """ - Create a ConfigObj from the configuration in {filename}. + Create a `ConfigObj` from the configuration in `filename`. """ try: return ConfigObj(filename) except IOError: - raise ConfigurationError('Could not open configuration file: %s' \ + raise ConfigurationError('Could not open configuration file: %s' % filename) except SyntaxError: - raise ConfigurationError('Could not parse configuration file: %s' \ + raise ConfigurationError('Could not parse configuration file: %s' % filename) - #_load_config -#_Config