"""basic Nitpo class""" import configparser from datetime import datetime 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' name = __name__ conf_fufi = etc_dir + '/' + name + '.ini' c['name'] = name c['conf_fufi'] = conf_fufi c['tf'] = '%Y-%m-%dT%H:%M:%SZ' c['ns'] = f"http://{name}.openlib.org" c['prefix'] = 'n' c['xslt_ext'] = '.xslt.xml' c['nsmap'] = {c['prefix']: c['ns']} self.const = c return 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}].") sys.exit() if(subsect not in self.conf[sect]): print(f"I need a Ф[{sect}][{subsect}].") 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 def is_on_terminal(self): """checks whether nitpo runs interactively""" if sys.stdin and sys.stdin.isatty(): return True return False def now(self): return datetime.now().strftime(self.const['tf'])