From 4968ba27f4856e8b383af129cdcbc598b64dbe61 Mon Sep 17 00:00:00 2001 From: Martijn Vermaat <martijn@vermaat.name> Date: Fri, 13 Dec 2013 07:37:48 +0100 Subject: [PATCH] Specify configuration file in the MUTALYZER_SETTINGS environment variable We no longer look for /etc/mutalyzer/config and ~/.config/mutalyzer/config since it is too low level and inflexible. The user should now specify the location of the configuration file in the MUTALYZER_SETTINGS environment variable (or the file mutalyzer.conf in the current directory is tried). --- .gitignore | 1 + extras/config.example | 4 +- extras/config.user.example | 4 +- mutalyzer/config.py | 99 ++++++++++---------------------------- 4 files changed, 31 insertions(+), 77 deletions(-) diff --git a/.gitignore b/.gitignore index d123a624..b59df5c3 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 62bf3ce3..251ca143 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 fe0887d7..fd3f1428 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 0627927c..dc8b5a83 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 -- GitLab