2024-03-29 21:48:06 +01:00
|
|
|
import configparser
|
|
|
|
import os
|
2024-03-30 09:19:38 +01:00
|
|
|
import sys
|
2024-03-29 21:48:06 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Conf(object):
|
2024-03-30 21:42:06 +01:00
|
|
|
_confobj = None
|
2024-03-29 21:48:06 +01:00
|
|
|
_cfgfiles = []
|
|
|
|
debug = None
|
|
|
|
|
2024-03-30 00:24:55 +01:00
|
|
|
def __init__(self, filename=None, debug=None, force_filename=False):
|
2024-03-29 21:48:06 +01:00
|
|
|
if debug:
|
|
|
|
self.set_debug(debug)
|
2024-03-30 21:42:06 +01:00
|
|
|
self._confobj = configparser.ConfigParser(dict(HOME=os.environ.get('HOME')))
|
2024-03-29 21:48:06 +01:00
|
|
|
filenames = []
|
2024-03-30 00:24:55 +01:00
|
|
|
if not force_filename:
|
2024-03-30 20:40:30 +01:00
|
|
|
# default config files are $HOME/etc/userconfig2.conf and
|
|
|
|
# {sys.prefix}/etc/userconfig2.conf
|
2024-03-30 09:19:38 +01:00
|
|
|
if os.path.isfile(f'{os.environ.get("HOME")}/etc/userconfig2.conf'):
|
|
|
|
filenames.append(f'{os.environ.get("HOME")}/etc/userconfig2.conf')
|
|
|
|
if os.path.isfile(f'{sys.prefix}/etc/userconfig2.conf'):
|
|
|
|
filenames.append(f'{sys.prefix}/etc/userconfig2.conf')
|
2024-03-30 20:49:42 +01:00
|
|
|
# supplied filename will be read last, has highest priority
|
|
|
|
if filename:
|
|
|
|
filenames.append(filename)
|
2024-03-29 21:48:06 +01:00
|
|
|
ret = self.set_filenames(filenames)
|
|
|
|
if not ret:
|
|
|
|
raise ValueError(f'Cannot open either configuration file: {",".join(filenames)}')
|
|
|
|
|
|
|
|
def set_debug(self, debug):
|
|
|
|
self.debug = debug
|
|
|
|
|
|
|
|
def set_filenames(self, filenames):
|
|
|
|
ret = self._confobj.read(filenames)
|
|
|
|
if len(ret) == 0:
|
|
|
|
return None
|
2024-03-30 20:40:30 +01:00
|
|
|
self._cfgfiles = ret
|
2024-03-29 21:48:06 +01:00
|
|
|
if self.debug:
|
|
|
|
self.debug.stdout("Read config files: %s" % ", ".join(filenames), 2)
|
|
|
|
return ret
|
|
|
|
|
|
|
|
def get(self, option, boolean=False, section='userconfig'):
|
|
|
|
try:
|
|
|
|
if boolean:
|
|
|
|
return self._confobj.getboolean(section, option)
|
|
|
|
else:
|
|
|
|
return self._confobj.get(section, option)
|
2024-03-30 21:42:06 +01:00
|
|
|
except (configparser.NoOptionError, configparser.NoSectionError) as e:
|
2024-03-29 21:48:06 +01:00
|
|
|
raise ValueError(f'Option {option} does not exist in section {section}: {e}')
|
|
|
|
|
|
|
|
def set(self, option, value, section='userconfig'):
|
|
|
|
try:
|
|
|
|
self._confobj.set(section, option, value)
|
|
|
|
except configparser.NoSectionError as e:
|
|
|
|
raise ValueError(f'Section {section} does not exist: {e}')
|
|
|
|
|
|
|
|
def get_items(self, section='userconfig'):
|
|
|
|
try:
|
|
|
|
return self._confobj.items(section)
|
|
|
|
except configparser.NoSectionError as e:
|
|
|
|
raise ValueError(f'Section {section} does not exist: {e}')
|
|
|
|
|
|
|
|
def check(self, option, section='userconfig'):
|
|
|
|
return self._confobj.has_option(section, option)
|
|
|
|
|
|
|
|
def sections(self):
|
|
|
|
return self._confobj.sections()
|