Global Configuration Variables

Rob Hooft rob at hooft.net
Wed Oct 18 03:46:31 EDT 2000


I use a module, not a class. That way I have a singlet without any
special coding. The module contains all the default values, and reads
up to 3 configuration files to change these values (system wide, login
directory, current directory). There are also some sanity checks.

It looks something like:

-------------------------------------------------------
#
# Configurable parameters
#
acceptdenzosign=0
adcfilter=1
airabsorptionfilter=0
# [...and so on all the default values...]
xtalsnapformat=(".ppm","PPM")
xtalsnapinterval=60*60

# System-level and User-level changes.
_gl=globals().keys()
import os
for _filename in ["/usr/local/lib/nonius-config.py",
		  os.environ["HOME"]+"/.nonius-config",
		  "nonius-config"]:
    if os.path.exists(_filename):
	_filestatus=os.stat(_filename)
	_filemode=_filestatus[0]
	_fileuid=_filestatus[4]
	if (_fileuid==os.getuid() or _fileuid==0):
	    if _filemode&002==0:
		execfile(_filename)
	    else:
		print "WARNING: Skipped '%s': writeable by others."%_filename
	else:
	    print "WARNING: Skipped '%s': owned by someone else."%_filename
	del _filemode,_fileuid,_filestatus
del os,_filename

# Test for unknown variables.
for _key in globals().keys():
    if _key[0]!='_' and _key not in _gl:
        print "WARNING: Configuration variable %s not known. Misspelled?"%_key
del _gl

if __name__=="__main__":
    k=globals().keys()
    k.sort()
    for name in k:
        if name[0]!='_':
            print name,"=",repr(globals()[name])
-------------------------------------------------------

-- 
=====   rob at hooft.net          http://www.hooft.net/people/rob/  =====
=====   R&D, Nonius BV, Delft  http://www.nonius.nl/             =====
===== PGPid 0xFA19277D ========================== Use Linux! =========



More information about the Python-list mailing list