Compare commits

..

No commits in common. "a986a708000ca1948e3654abc5a71157d5a8b93e" and "015ea337f256030ed8634712f9743dbe24af43c8" have entirely different histories.

3 changed files with 14 additions and 17 deletions

View File

@ -23,7 +23,7 @@ class Userconfig:
:return: str :return: str
""" """
content = [] content = []
self._cfg.debug.stdout(f" ================ build_file {destfile} ===============", 1) self._cfg.debug.stdout(" ================ build_file ===============", 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(f" ================ done: build_file {destfile} ===============", 1) self._cfg.debug.stdout(" ================ build_file ===============", 1)
return tempfilename return tempfilename
def process_all_files(self, destfiles, dir_config): def process_all_files(self, destfiles, dir_config):
@ -100,7 +100,8 @@ 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(f" ================ workconf {directory} ===============", 1) self._cfg.debug.stdout(" ================ workconf ===============", 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):
@ -111,7 +112,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(f" ================ done: workconf {directory} ===============", 1) self._cfg.debug.stdout(" ================ workconf ===============", 1)
return ret return ret
def workdir(self, directory): def workdir(self, directory):
@ -119,7 +120,8 @@ 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(f" ================ workdir {directory} ===============", 1) self._cfg.debug.stdout(" ================ workdir ===============", 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)
@ -141,7 +143,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"])
@ -169,5 +171,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(f" ================ done: workdir {directory} ===============", 1) self._cfg.debug.stdout(" ================ workdir ===============", 1)
return destfiles, dir_config return destfiles, dir_config

View File

@ -4,25 +4,21 @@ import sys
class Conf(object): class Conf(object):
_confobj = None _confobj = configparser.ConfigParser(os.environ)
_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)}')
@ -34,7 +30,6 @@ 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
@ -45,7 +40,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.NoSectionError) as e: except (configparser.NoOptionError, configparser.NoOptionError) 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=filename, debug=cfg.debug, force_filename=True) ret = Conf(filename)
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