Compare commits

...

7 Commits

3 changed files with 17 additions and 14 deletions

View File

@ -23,7 +23,7 @@ class Userconfig:
:return: str :return: str
""" """
content = [] content = []
self._cfg.debug.stdout(" ================ build_file ===============", 1) self._cfg.debug.stdout(f" ================ build_file {destfile} ===============", 1)
if commentstring != "": if commentstring != "":
self._cfg.debug.stdout(" +++ commentstring found, adding header.", 3) self._cfg.debug.stdout(" +++ commentstring found, adding header.", 3)
content.append(commentstring + " " + self._cfg.get("stamp") + " " + time.strftime("%+") + "\n") content.append(commentstring + " " + self._cfg.get("stamp") + " " + time.strftime("%+") + "\n")
@ -54,7 +54,7 @@ class Userconfig:
fp.write(block) fp.write(block)
fp.write("\n") fp.write("\n")
fp.close() fp.close()
self._cfg.debug.stdout(" ================ build_file ===============", 1) self._cfg.debug.stdout(f" ================ done: build_file {destfile} ===============", 1)
return tempfilename return tempfilename
def process_all_files(self, destfiles, dir_config): def process_all_files(self, destfiles, dir_config):
@ -100,8 +100,7 @@ class Userconfig:
"""walks through directory, collecting all filenames, returns list of all filenames""" """walks through directory, collecting all filenames, returns list of all filenames"""
dirs = os.listdir(directory) dirs = os.listdir(directory)
ret = [] ret = []
self._cfg.debug.stdout(" ================ workconf ===============", 1) self._cfg.debug.stdout(f" ================ workconf {directory} ===============", 1)
self._cfg.debug.stdout(" Finding files in directory %s." % directory, 4)
for d in dirs: for d in dirs:
name = directory + "/" + d name = directory + "/" + d
if os.path.isdir(name): if os.path.isdir(name):
@ -112,7 +111,7 @@ class Userconfig:
continue continue
ret.append(name) ret.append(name)
self._cfg.debug.stdout(" +++ Found file %s in directory %s" % (name, directory), 4) self._cfg.debug.stdout(" +++ Found file %s in directory %s" % (name, directory), 4)
self._cfg.debug.stdout(" ================ workconf ===============", 1) self._cfg.debug.stdout(f" ================ done: workconf {directory} ===============", 1)
return ret return ret
def workdir(self, directory): def workdir(self, directory):
@ -120,8 +119,7 @@ class Userconfig:
then collect all filenames within this directory and return a dict of all files for this then collect all filenames within this directory and return a dict of all files for this
directory directory
""" """
self._cfg.debug.stdout(" ================ workdir ===============", 1) self._cfg.debug.stdout(f" ================ workdir {directory} ===============", 1)
self._cfg.debug.stdout(" Working on directory %s" % directory, 3)
# skip directory if no CONFIGFILE present # skip directory if no CONFIGFILE present
if not os.path.isfile(directory+"/"+self._cfg.get("configfile")): if not os.path.isfile(directory+"/"+self._cfg.get("configfile")):
self._cfg.debug.stdout(f' --- No file {self._cfg.get("configfile")} in {directory}, skipping.', 1) self._cfg.debug.stdout(f' --- No file {self._cfg.get("configfile")} in {directory}, skipping.', 1)
@ -143,7 +141,7 @@ class Userconfig:
reverse_sort = dir_config.get(section="Main", option="reverse", boolean=True) reverse_sort = dir_config.get(section="Main", option="reverse", boolean=True)
except ValueError: except ValueError:
reverse_sort = False reverse_sort = False
self._cfg.debug.stdout(f' +++ reverse_sort is {reverse_sort}', 3)
if os.access(directory + "/install.sh", os.X_OK): if os.access(directory + "/install.sh", os.X_OK):
subprocess.call([directory + "/install.sh"]) subprocess.call([directory + "/install.sh"])
@ -171,5 +169,5 @@ class Userconfig:
f"{destfiles[destname]}", 4) f"{destfiles[destname]}", 4)
self._cfg.debug.stdout(" === workdir: %s, Files: %s" % (directory, str(destfiles)), 3) self._cfg.debug.stdout(" === workdir: %s, Files: %s" % (directory, str(destfiles)), 3)
self._cfg.debug.stdout(" ================ workdir ===============", 1) self._cfg.debug.stdout(f" ================ done: workdir {directory} ===============", 1)
return destfiles, dir_config return destfiles, dir_config

View File

@ -4,21 +4,25 @@ import sys
class Conf(object): class Conf(object):
_confobj = configparser.ConfigParser(os.environ) _confobj = None
_cfgfiles = [] _cfgfiles = []
debug = None debug = None
def __init__(self, filename=None, debug=None, force_filename=False): def __init__(self, filename=None, debug=None, force_filename=False):
if debug: if debug:
self.set_debug(debug) self.set_debug(debug)
self._confobj = configparser.ConfigParser(dict(HOME=os.environ.get('HOME')))
filenames = [] filenames = []
if filename:
filenames.append(filename)
if not force_filename: if not force_filename:
# default config files are $HOME/etc/userconfig2.conf and
# {sys.prefix}/etc/userconfig2.conf
if os.path.isfile(f'{os.environ.get("HOME")}/etc/userconfig2.conf'): if os.path.isfile(f'{os.environ.get("HOME")}/etc/userconfig2.conf'):
filenames.append(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'): if os.path.isfile(f'{sys.prefix}/etc/userconfig2.conf'):
filenames.append(f'{sys.prefix}/etc/userconfig2.conf') filenames.append(f'{sys.prefix}/etc/userconfig2.conf')
# supplied filename will be read last, has highest priority
if filename:
filenames.append(filename)
ret = self.set_filenames(filenames) ret = self.set_filenames(filenames)
if not ret: if not ret:
raise ValueError(f'Cannot open either configuration file: {",".join(filenames)}') raise ValueError(f'Cannot open either configuration file: {",".join(filenames)}')
@ -30,6 +34,7 @@ class Conf(object):
ret = self._confobj.read(filenames) ret = self._confobj.read(filenames)
if len(ret) == 0: if len(ret) == 0:
return None return None
self._cfgfiles = ret
if self.debug: if self.debug:
self.debug.stdout("Read config files: %s" % ", ".join(filenames), 2) self.debug.stdout("Read config files: %s" % ", ".join(filenames), 2)
return ret return ret
@ -40,7 +45,7 @@ class Conf(object):
return self._confobj.getboolean(section, option) return self._confobj.getboolean(section, option)
else: else:
return self._confobj.get(section, option) return self._confobj.get(section, option)
except (configparser.NoOptionError, configparser.NoOptionError) as e: except (configparser.NoOptionError, configparser.NoSectionError) as e:
raise ValueError(f'Option {option} does not exist in section {section}: {e}') raise ValueError(f'Option {option} does not exist in section {section}: {e}')
def set(self, option, value, section='userconfig'): def set(self, option, value, section='userconfig'):

View File

@ -33,7 +33,7 @@ class Debug:
def get_config(filename, cfg): def get_config(filename, cfg):
"""reads filename as config, checks for DEST parameter and returns cfgfile object""" """reads filename as config, checks for DEST parameter and returns cfgfile object"""
try: try:
ret = Conf(filename) ret = Conf(filename=filename, debug=cfg.debug, force_filename=True)
except ValueError: except ValueError:
cfg.debug.stderr("Error reading config file %s" % filename) cfg.debug.stderr("Error reading config file %s" % filename)
return False return False