Under SCO there are a set of system calls, defread()/defopen(),
that let you centralize configuration files under a single directory.
Under SCO that directory is /etc/default.
I have created a simple module that does the same thing for any
platform and/or system.
The configuration file is formatted as one option per line with a key
before a '=' and the data after the '='. '#' is a coment line.
There is a single function call in this module: open().
It takes a single argument, the name of the configuration file (this is
usually the program's name).
This open returns a class that acts like a dictionary.
Enjoy!
- ---- CUT:
# default.py
#
# This module impliments the SCO defread() and defopen() system calls.
#
# open(filename) returns a default class or raises an exception
# It opens the file pointed to by _default_path/filename
#
# the default class works by defining a "dictionary"
__default_path = '/etc/default/'
class __default:
def __init__(self, filename):
from string import strip, index
from __builtin__ import open
fo = open(__default_path + filename,'r')
self.filename = filename
self.data = {}
line = fo.readline()
while line != '':
if line[0] == '#':
line = fo.readline()
continue
line = strip(line)
if line == '':
line = fo.readline()
continue
try:
ndx = index(line,'=')
key = line[:ndx]
dta = line[ndx+1:]
self.data[key] = dta
except ValueError:
pass
line = fo.readline()
fo.close()
def __str__(self):
return self.__repr__(self)
def __repr__(self):
return '<default file \'' + self.filename + '\'>'
def __getitem__(self, key):
if self.data.has_key(key):
return self.data[key]
else:
raise KeyError, key
def keys(self):
return self.data.keys()
def items(self):
return self.data.items()
def values(self):
return self.data.values()
def has_key(self, key):
return self.data.has_key(key)
def open(filename):
return __default(filename)
- ---- CUT:
- --
Lance Ellinghaus lance@fox.com
PGP 2.6 Key fingerprint = 94 A0 AA 66 2E 6D B1 DC 63 C6 FD F0 C1 66 1C 61
To get PGP 2.6 Public Key, send email to: pgp-key@fox.com
-----BEGIN PGP SIGNATURE-----
iQCVAwUBLxRJaSnWbbetXq5VAQHQsQQArdZJhRexp0NZY+nAsM5d5QNlNKSlnkUO
P+LV1L6b5TG8E67W1Td4YS9wybqtNh740OVLhpBQEBQz4ZoxgncVjDA9EaXqoSSF
IwnzqjhC38iTn0N5MMYREfLGVrOdwp92mKTQUeuEuLAr42ECz+NXJr4RrgQwJZaG
qWIqrRIVQZA=
-----END PGP SIGNATURE-----