[Tutor] setting program configuration for all files and modules of a program
Tim Michelsen
timmichelsen at gmx-topmail.de
Tue Apr 15 00:29:53 CEST 2008
Hello,
I am building a simple GUI for a calculation program. I am using config
files (*.cfg) and reading them in with ConfigParser. This works well
because I have nearly all code in 1 text file.
But I would like to modularize my code and separate the GUI code from
the functional code that provides the calculation operations. This will
help to expand the functionality at a later stage. I want to achieve
this through splitting the code into different modules.
The problem is now that I would always have to write the configration
file specific code in each module that uses the configration settings
(see code below).
How can I provide the settings stored in the configuration file
throughout my program to all functions without needing to initiate the
ConfigParser object in each module?
I through of having a special module "settings.py" which I could use to
read in the configuration from the file and then import this in each module.
Is there a more decent and elegant way?
What is the state of the art in storing and parsing configuraions in
python programs?
Kind regards,
Timmie
##### CODE #####
import sys
import locale
import ConfigParser
import gettext
## Path to configuration file
configuration_file_name = './config/program_settings.cfg' # name of
the configuration file
## read configuration from config file
try:
program_config = ConfigParser.ConfigParser()
program_config.readfp(open(configuration_file_name))
except IOError, (errno, strerror):
error_msg_exception = _("I/O error(%s): %s") % (errno, strerror)
error_msg_no_config = _("No configuration file with name "),
configuration_file_name, _("found in the program directory. Please copy
your configuration there. Values will be replaced by defaults.")
print error_msg_exception
print error_msg_no_config
easygui.msgbox(error_msg_no_config)
#pass
output = program_config.get('output', 'file_extension')
#####
More information about the Tutor
mailing list