Compare commits

...

7 Commits

3 changed files with 17 additions and 14 deletions

View File

@ -23,7 +23,7 @@ class Userconfig:
:return: str
"""
content = []
self._cfg.debug.stdout(" ================ build_file ===============", 1)
self._cfg.debug.stdout(f" ================ build_file {destfile} ===============", 1)
if commentstring != "":
self._cfg.debug.stdout(" +++ commentstring found, adding header.", 3)
content.append(commentstring + " " + self._cfg.get("stamp") + " " + time.strftime("%+") + "\n")
@ -54,7 +54,7 @@ class Userconfig:
fp.write(block)
fp.write("\n")
fp.close()
self._cfg.debug.stdout(" ================ build_file ===============", 1)
self._cfg.debug.stdout(f" ================ done: build_file {destfile} ===============", 1)
return tempfilename
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"""
dirs = os.listdir(directory)
ret = []
self._cfg.debug.stdout(" ================ workconf ===============", 1)
self._cfg.debug.stdout(" Finding files in directory %s." % directory, 4)
self._cfg.debug.stdout(f" ================ workconf {directory} ===============", 1)
for d in dirs:
name = directory + "/" + d
if os.path.isdir(name):
@ -112,7 +111,7 @@ class Userconfig:
continue
ret.append(name)
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
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
directory
"""
self._cfg.debug.stdout(" ================ workdir ===============", 1)
self._cfg.debug.stdout(" Working on directory %s" % directory, 3)
self._cfg.debug.stdout(f" ================ workdir {directory} ===============", 1)
# skip directory if no CONFIGFILE present
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)
@ -143,7 +141,7 @@ class Userconfig:
reverse_sort = dir_config.get(section="Main", option="reverse", boolean=True)
except ValueError:
reverse_sort = False
self._cfg.debug.stdout(f' +++ reverse_sort is {reverse_sort}', 3)
if os.access(directory + "/install.sh", os.X_OK):
subprocess.call([directory + "/install.sh"])
@ -171,5 +169,5 @@ class Userconfig:
f"{destfiles[destname]}", 4)
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

View File

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

View File

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