From e3d15c8a46fe607db7ff11952c28f7c5b6573c0f Mon Sep 17 00:00:00 2001 From: nietky Date: Mon, 15 Oct 2012 16:55:50 +1030 Subject: [PATCH 1/3] Add configuration module for MTpy Implement persistent configuration settings in INI files Changes made: * New module at MTpy/config.py (MTpy.config) The settings are kept in a SafeConfigParser object which you can get by doing: >>> from MTpy.config import config For more information on this object see: http://docs.python.org/library/configparser.html http://www.doughellmann.com/PyMOTW/ConfigParser/#accessing-configuration-settings The module attempts to load settings from files in four locations, in this order: 1. Github, but only if necessary 2. $SOURCEFOLDER/MTpy/MTpy.ini 3. $HOME/MTpy.ini 4. ./MTpy.ini Settings found in later files override those in earlier files, meaning that the user can set individual settings per directory, or per user, and the defaults are read from the system's file. An attempt is made to download from Github only if no INI files can be found locally at all. --- mtpy/config.py | 107 +++++++++++++++++++++++++++++++++++++++++++++++++ mtpy/mtpy.ini | 0 2 files changed, 107 insertions(+) create mode 100644 mtpy/config.py create mode 100644 mtpy/mtpy.ini diff --git a/mtpy/config.py b/mtpy/config.py new file mode 100644 index 00000000..8566ef81 --- /dev/null +++ b/mtpy/config.py @@ -0,0 +1,107 @@ +'''Keep configuration settings for mtpy in INI files. + +This module will try to load the ``mtpy.ini`` settings file from three locations: +the current working directory, the user's home directory, and the original +in the package source directory (SYSTEM_INI), in that priority. Each file +need not contain all settings, and settings from higher priority files take +precendence. + +Usage +----- + +For the configuration file containing this text:: + + [BIRRP] + bbfile = Z:\instruments\bbconv.txt + +you can access settings like so:: + + >>> from MTpy.config import config + >>> for section_name in config.sections(): + ... print 'Section:', section_name + ... for name, value in config.items(section_name): + ... print '\t', name, ':', value + Section: BIRRP + bbfile : Z:\instruments\bbconv.txt + +Format +------ + +The settings file should be in INI file format. See for more info: + +http://www.doughellmann.com/PyMOTW/ConfigParser/#accessing-configuration-settings + +Github +------ + +If for some reason the SYSTEM_INI file is missing, load will try to download the +original from Github, and write it as a backup 'github-mtpy.ini' configuration +file. + +''' +import ConfigParser +import os +import shutil + +INI_FN = 'mtpy.ini' + +# Possible locations for INI files + +GITHUB_INI = os.path.join(os.path.dirname(__file__), 'github-' + INI_FN) +SYSTEM_INI = os.path.join(os.path.dirname(__file__), INI_FN) +USER_INI = os.path.join(os.path.expanduser('~'), INI_FN) +WORKINGDIR_INI = os.path.join(os.getcwd(), INI_FN) + + +def load(): + '''Return config parser. + + Checks for GITHUB_INI, SYSTEM_INI, USER_INI, and WORKINGDIR_INI and updates + the parser object with the values found each time. + + ''' + def combine(parsers): + '''Return a new SafeConfigParser containing the options already set in + *parsers*. + + Works forwards through the list, so the higher priority parsers should go + at the end of *parsers*. + + ''' + new_parser = ConfigParser.SafeConfigParser() + for parser in parsers: + if not parser: + continue + for section in parser.sections(): + if not new_parser.has_section(section): + new_parser.add_section(section) + for name, value in parser.items(section): + new_parser.set(section, name, str(value)) + return new_parser + + def make_parser(fn): + parser = ConfigParser.SafeConfigParser() + parser.read(fn) + return parser + + parser = make_parser(GITHUB_INI) + read_at_least_one = False + for fn in [SYSTEM_INI, USER_INI, WORKINGDIR_INI]: + next_parser = make_parser(fn) + if next_parser.read(fn): + read_at_least_one = True + parser = combine([parser, next_parser]) + + if not read_at_least_one: + # Failed to read any INI file. This is bad, so try to retrieve the + # latest default from Github, and copy it to SYSTEM_INI. + + import urllib2 + github_ini_url = urllib2.url('http://raw.github.com/geophysics/mtpy/master/mtpy/' + INI_FN) + with open(GITHUB_INI, mode='w') as f: + f.write(github_ini_url.read()) + return parser.read([GITHUB_INI]) + return parser + + +config = load() \ No newline at end of file diff --git a/mtpy/mtpy.ini b/mtpy/mtpy.ini new file mode 100644 index 00000000..e69de29b From b8e55e7d146b3bed7549f3875b8d6c91fa66d8ab Mon Sep 17 00:00:00 2001 From: nietky Date: Mon, 15 Oct 2012 17:18:57 +1030 Subject: [PATCH 2/3] Fix incorrect location of import stmnt in config.py --- mtpy/config.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mtpy/config.py b/mtpy/config.py index 8566ef81..43e28343 100644 --- a/mtpy/config.py +++ b/mtpy/config.py @@ -42,6 +42,7 @@ import ConfigParser import os import shutil +import urllib2 INI_FN = 'mtpy.ini' @@ -96,7 +97,6 @@ def make_parser(fn): # Failed to read any INI file. This is bad, so try to retrieve the # latest default from Github, and copy it to SYSTEM_INI. - import urllib2 github_ini_url = urllib2.url('http://raw.github.com/geophysics/mtpy/master/mtpy/' + INI_FN) with open(GITHUB_INI, mode='w') as f: f.write(github_ini_url.read()) @@ -104,4 +104,4 @@ def make_parser(fn): return parser -config = load() \ No newline at end of file +config = load() From c56518f0e599cb9892e8bbb66c8a4a1ad711baf3 Mon Sep 17 00:00:00 2001 From: nietky Date: Sat, 20 Oct 2012 21:52:41 +1030 Subject: [PATCH 3/3] Change 'ini' to 'cfg' (see #15) The 'ini' file extension stands for 'initialization' which is not precisely what this code is intended for (configuration). Thus 'cfg' would be better. See issue #15 (https://github.com/geophysics/mtpy/pull/15) for discussion. --- mtpy/config.py | 42 ++++++++++++++++++------------------- mtpy/{mtpy.ini => mtpy.cfg} | 0 2 files changed, 21 insertions(+), 21 deletions(-) rename mtpy/{mtpy.ini => mtpy.cfg} (100%) diff --git a/mtpy/config.py b/mtpy/config.py index 43e28343..55c3756d 100644 --- a/mtpy/config.py +++ b/mtpy/config.py @@ -1,8 +1,8 @@ -'''Keep configuration settings for mtpy in INI files. +'''Keep configuration settings for mtpy in CFG files. -This module will try to load the ``mtpy.ini`` settings file from three locations: +This module will try to load the ``mtpy.cfg`` settings file from three locations: the current working directory, the user's home directory, and the original -in the package source directory (SYSTEM_INI), in that priority. Each file +in the package source directory (SYSTEM_CFG), in that priority. Each file need not contain all settings, and settings from higher priority files take precendence. @@ -27,15 +27,15 @@ Format ------ -The settings file should be in INI file format. See for more info: +The settings file should be in CFG file format. See for more info: http://www.doughellmann.com/PyMOTW/ConfigParser/#accessing-configuration-settings Github ------ -If for some reason the SYSTEM_INI file is missing, load will try to download the -original from Github, and write it as a backup 'github-mtpy.ini' configuration +If for some reason the SYSTEM_CFG file is missing, load will try to download the +original from Github, and write it as a backup 'github-mtpy.cfg' configuration file. ''' @@ -44,20 +44,20 @@ import shutil import urllib2 -INI_FN = 'mtpy.ini' +CFG_FN = 'mtpy.cfg' -# Possible locations for INI files +# Possible locations for CFG files -GITHUB_INI = os.path.join(os.path.dirname(__file__), 'github-' + INI_FN) -SYSTEM_INI = os.path.join(os.path.dirname(__file__), INI_FN) -USER_INI = os.path.join(os.path.expanduser('~'), INI_FN) -WORKINGDIR_INI = os.path.join(os.getcwd(), INI_FN) +GITHUB_CFG = os.path.join(os.path.dirname(__file__), 'github-' + CFG_FN) +SYSTEM_CFG = os.path.join(os.path.dirname(__file__), CFG_FN) +USER_CFG = os.path.join(os.path.expanduser('~'), CFG_FN) +WORKINGDIR_CFG = os.path.join(os.getcwd(), CFG_FN) def load(): '''Return config parser. - Checks for GITHUB_INI, SYSTEM_INI, USER_INI, and WORKINGDIR_INI and updates + Checks for GITHUB_CFG, SYSTEM_CFG, USER_CFG, and WORKINGDIR_CFG and updates the parser object with the values found each time. ''' @@ -85,22 +85,22 @@ def make_parser(fn): parser.read(fn) return parser - parser = make_parser(GITHUB_INI) + parser = make_parser(GITHUB_CFG) read_at_least_one = False - for fn in [SYSTEM_INI, USER_INI, WORKINGDIR_INI]: + for fn in [SYSTEM_CFG, USER_CFG, WORKINGDIR_CFG]: next_parser = make_parser(fn) if next_parser.read(fn): read_at_least_one = True parser = combine([parser, next_parser]) if not read_at_least_one: - # Failed to read any INI file. This is bad, so try to retrieve the - # latest default from Github, and copy it to SYSTEM_INI. + # Failed to read any CFG file. This is bad, so try to retrieve the + # latest default from Github, and copy it to SYSTEM_CFG. - github_ini_url = urllib2.url('http://raw.github.com/geophysics/mtpy/master/mtpy/' + INI_FN) - with open(GITHUB_INI, mode='w') as f: - f.write(github_ini_url.read()) - return parser.read([GITHUB_INI]) + github_cfg_url = urllib2.url('http://raw.github.com/geophysics/mtpy/master/mtpy/' + CFG_FN) + with open(GITHUB_CFG, mode='w') as f: + f.write(github_cfg_url.read()) + return parser.read([GITHUB_CFG]) return parser diff --git a/mtpy/mtpy.ini b/mtpy/mtpy.cfg similarity index 100% rename from mtpy/mtpy.ini rename to mtpy/mtpy.cfg