import configparser import os import sys class Nitpo: def __init__(self): # # order is crucial here self.set_constants() self.read_config() def set_constants(self): """the location of the configuration file can not be changed""" c = {} home_dir = os.environ['HOME'] etc_dir = home_dir + '/etc' conf_fufi = etc_dir + '/' + __name__ + '.ini' c['conf_fufi'] = conf_fufi c['tf'] = '%Y-%m-%dT%H:%M:%SZ' c['ns'] = 'http://nitpo.openlib.org' c['nsmap'] = {None: c['ns']} self.const = c def read_config(self): """reads the configuration""" conf_fufi = self.const['conf_fufi'] if not os.path.isfile(conf_fufi): print("I need a file " + conf_fufi) sys.exit() config = configparser.ConfigParser() config.read(conf_fufi) self.conf = config return self.conf def check_conf(self, sect, subsect): if(sect not in self.conf): print(f"I need a section {sect} in the configuration.") sys.exit() if(subsect not in self.conf[sect]): print(f"I need a {subsect} in the [{sect}] configuration.") sys.exit() def has_conf(self, sect, subsect): if(sect not in self.conf): return False if(subsect not in self.conf[sect]): return False return True def is_dev(self): """I'm on my development machine if I have /etc/wpa_supplicant.conf""" if os.path.isfile('/etc/wpa_supplicant.conf'): return True return False