removed tools, moved functions into userconfig

This commit is contained in:
2024-03-31 23:28:11 +02:00
parent e43dd9013c
commit 1a32b4e096
5 changed files with 193 additions and 354 deletions

View File

@@ -1,11 +1,65 @@
import os
import argparse
from userconfig.cfgfile import Conf
from userconfig.tools import Debug
from userconfig import Userconfig
import sys
import subprocess
class Debug:
_verbose = 0
_COLOR_RED = '\033[91m'
_COLOR_BLUE = '\33[34m'
_COLOR_GREEN = '\33[32m'
_COLOR_YELLOW = '\033[93m'
_COLOR_END = '\33[0m'
_FORMAT = {'STANDARD': '',
'ERROR': f'{_COLOR_RED}ERROR:{_COLOR_END} ',
'NOTICE': f'{_COLOR_BLUE}NOTICE:{_COLOR_END} ',
'WARNING': f'{_COLOR_YELLOW}WARNING:{_COLOR_END} ',
'SUCCESS': f'{_COLOR_GREEN}SUCCESS:{_COLOR_END} '
}
def __init__(self, verbose=0):
self.set_verbose(verbose)
def set_verbose(self, verbose):
self._verbose = verbose
def add_verbose(self):
self._verbose += 1
def get_verbose(self):
return self._verbose
def stdout(self, out, verbose_level=0, category='STANDARD'):
if self._verbose >= verbose_level:
if category in self._FORMAT:
print(f'{self._FORMAT[category]}{out}')
else:
print(f'[category {category} unknown]:{out}')
def stderr(self, out, verbose_level=0, category='STANDARD'):
if self._verbose >= verbose_level:
if category in self._FORMAT:
print(f'{self._FORMAT[category]}{out}', file=sys.stderr)
else:
print(f'[category {category} unknown]:{out}', file=sys.stderr)
def red(self, out):
return f'{self._COLOR_RED}{out}{self._COLOR_END}'
def green(self, out):
return f'{self._COLOR_GREEN}{out}{self._COLOR_END}'
def blue(self, out):
return f'{self._COLOR_BLUE}{out}{self._COLOR_END}'
def yellow(self, out):
return f'{self._COLOR_YELLOW}{out}{self._COLOR_END}'
def main():
parser = argparse.ArgumentParser(prog='userconfig',
description='Manages configuration files, usually in the user home directory')
@@ -20,13 +74,12 @@ def main():
debug = Debug()
# debug.set_verbose(cmdline.verbose)
# cfg = Conf(filename=cmdline.file, debug=debug)
debug.set_verbose(4)
cfg = Conf(filename='/Users/lysis/userconfig2-test.conf', debug=debug)
# cfg = Conf(filename='/Users/lysis/etc/userconfig2.conf', debug=debug)
debug.set_verbose(1)
cfg = Conf(filename='/home/lysis/userconfig2-test.conf', debug=debug)
uc = Userconfig(cfg)
cfg.debug.stdout(f"Verbose level is {cfg.debug.get_verbose()}", 1, 'STANDARD')
configdir = cfg.get("configdir")
cfg.debug.stdout(f'Verbose level: {cfg.debug.get_verbose()}', 1, 'STANDARD')
configdir = cfg.get('configdir')
# configdir is the root of the userconfig files
# Directory structure:
# configdir/
@@ -49,7 +102,7 @@ def main():
# [Number]_[category]_[value]
# Number: can have leading zeros for better sorting in directory structure, will be used for sorting and priority
# category: which kind of match we are looking for. currently: Arch for `uname -s`, Host for `hostname -s`
# value: optional, if category requires input, for example: Arch_Linux
# value: optional, if category requires an input value, for example: Arch_Linux
# can be empty for example if category is all (no match needed, we want this always to be applied)
cfg.debug.stdout(f'configdir: {configdir}', 1)
@@ -62,7 +115,8 @@ def main():
if os.path.isfile(f'{package.path}/.ignore'):
cfg.debug.stdout(f'{package.path} contains .ignore, skipping', 2, 'WARNING')
continue
cfg.debug.stdout(f'============ start {package.path} ============', 2)
cfg.debug.stdout(f'Package: {package.path}', 1, 'NOTICE')
cfg.debug.stdout(f'============ start {cfg.debug.green(package.path)} ============', 2)
(category_dirs, dir_config) = uc.process_package_dir(package.path)
cfg.debug.stdout(f'Got categories: {category_dirs}', 2)
host_category_dirs = uc.filter_categories(category_dirs)
@@ -84,8 +138,9 @@ def main():
# Make sure all directories for destination file exist
uc.create_destination_directories(dir_config.get(section="Main", option="dest"))
temp_filename = uc.build_file(file_list[file], dest, comment_string)
uc.copy_file(temp_filename, dest, comment_string)
cfg.debug.stdout(f'============ end {package.path} ============\n\n', 2)
uc.diff_and_copy_file(temp_filename, dest, comment_string)
cfg.debug.stdout(f'============ end {cfg.debug.green(package.path)} ============\n\n', 2)
if __name__ == '__main__':
main()