logging improvements

This commit is contained in:
2025-09-29 20:11:16 +02:00
parent 9b86007e34
commit 5469a01c09
9 changed files with 124 additions and 56 deletions

0
refit/src/__init__.py Normal file
View File

View File

@@ -3,9 +3,14 @@ from .refit_logger import logger
def get_standard_name_number(current_number, number_str_length):
"""returns a number string filled to the length of the input number"""
logger.debug(
f"FUNC: get_standard_name_number() index={current_number} string_length={number_str_length}"
)
current_number = str(current_number)
standard_name_number = str.zfill(current_number, number_str_length)
logger.debug(f"standard_name_number={standard_name_number}")
logger.debug(
f"FUNC: get_standard_name_number() return value= '{standard_name_number}'"
)
return standard_name_number
@@ -14,36 +19,46 @@ def get_standard_folder_name(name):
given name to the argument --name as astring"""
logger.debug("get_standard_folder_name() call")
if name is None:
logger.warning("No name assigned, continue with default.")
standard_folder_name = "directory"
logger.info(
f"No name for folder assigned, continue with default value '{standard_folder_name}'."
)
else:
logger.debug(f"{name}, Type: {type(name)}, Length: {len(name)}")
logger.debug(
f"FUNC: get_standard_folder_name() Properties of name='{name}': Type: {type(name)}, Length: {len(name)}"
)
# Checks if multiple names are passed to the --name argument
# if so, only the first one gets used.
if len(name) > 1:
_NAMES_WARNING = f"{len(name)} names given, only one required.\nContinuing with '{name[0]}' as name."
logger.warning(_NAMES_WARNING)
logger.warning("FUNC: get_standard_folder_name()" + _NAMES_WARNING)
print(_NAMES_WARNING)
standard_folder_name = name[0]
logger.debug(f"{standard_folder_name}, Type: {type(standard_folder_name)}")
logger.debug(
f"FUNC: get_standard_folder_name() Properties of folder name '{standard_folder_name}': Type: {type(standard_folder_name)}"
)
else:
standard_folder_name = name[0]
logger.debug("Continuing with expected input")
logger.debug("get_standard_folder_name() exit.")
logger.debug("FUNC: get_standard_folder_name() exit.")
return standard_folder_name
def get_standard_file_name(name):
"""Returns the default file name if none is provided."""
logger.debug("running get_standard_file_name()")
logger.debug(
f"FUNC: get_standard_file_name() MSG: entered function with name='{name}'"
)
if name is None:
logger.warning("No name assigned, continue with default")
standard_file_name = "file"
logger.info(
f"No file name assigned, continue with default value '{standard_file_name}'"
)
else:
if len(name) > 1:
_NAMES_WARNING = f"{len(name)} names given, only one required.\nContinuing with '{name[0]}' as name."
logger.warning(_NAMES_WARNING)
@@ -53,7 +68,9 @@ def get_standard_file_name(name):
logger.debug(f"{standard_file_name}, Type: {type(standard_file_name)}")
else:
standard_file_name = name[0]
logger.debug("Continuing with expected input")
logger.debug("exiting get_standard_file_name()")
logger.debug(
f"FUNC: get_standard_file_name MSG: Continuing with expected input: '{standard_file_name}'"
)
logger.debug("FUNC get_standard_file_name() MSG: Exit")
return standard_file_name

View File

@@ -1,9 +1,10 @@
import argparse
import os
from .refit_logger import logger
from . import librefit
logger.debug("Initiated refit_create.py")
# logger.debug("Initiated refit_create.py")
# ----------------------------------------------------------------------
@@ -29,27 +30,38 @@ class Refit_Create:
"""Checks if the input is valid and returns either True or
throws an error in log and terminal."""
logger.debug("in create_input_valid()")
logger.debug(f"Start create_input_valid() value= {self.input}")
if self.input is None:
logger.warning(f"{self.input} cannot be None")
print("Input argument missing. Use 'refit create -h' for help")
raise ValueError("Input missing")
return True
else:
logger.debug(
f"Exiting create_input_valid() with valid input. value= {self.input}"
)
return True
def create_n_folders(self, n, input, name):
"""Creates an set ammount of folders. Using the default directory
name if no other is provided."""
logger.debug("in create_n_folders()")
logger.debug(f"FUNC: create_n_folders() ARGS: n={n} input={input} name={name}")
# Creating the length of the suffix number_string.
length_n = len(str(n))
logger.debug(
f"FUNC: create_n_folders() MSG: Length of numbering string added to the name:{length_n}"
)
# Get either the default folder name or the input name as string.
logger.debug(
f"FUNC: create_n_folders() MSG: Type of name value: {type(name)} name={name}"
)
folder_name = librefit.get_standard_folder_name(name)
logger.debug(f"Length of numbering string:{length_n}")
logger.debug(f"Folder name: {folder_name}")
logger.debug(
f"FUNC: create_n_folders() MSG: Folder name: {folder_name} post get_standard_folder_name() call"
)
while n > 0:
# iterating down for the files number.
@@ -60,26 +72,25 @@ class Refit_Create:
# Creating path for the folder
temp_name = f"{folder_name}_{number_string}"
logger.debug(f"temp_name= {temp_name}")
folder_creation_path = os.path.join(input, temp_name)
logger.debug(f"Created: {folder_creation_path}")
# Creating folder and subtracting n by one for the number_string
os.mkdir(folder_creation_path)
n -= 1
def rf_create_decider(self):
"""Coordination of the 'create' sub command"""
logger.debug("FUNC: rf_create_decider() MSG: Entered decider function")
if self.create_input_valid():
logger.debug("valid input -> continue")
logger.debug(f"Valid input. value={self.input}")
if self.filemode:
logger.debug("filemode active")
logger.debug("DECISION if filemode")
else:
logger.debug(
f"DIR-MODE | ARGS: n={self.n} input={self.input} name={self.name}"
)
self.create_n_folders(self.n, self.input, self.name)
logger.debug(
"End of run---------------------------------------------------------"
)
def __call__(self):
"""Gets called when the object is treated as an function"""

View File

@@ -1,16 +0,0 @@
import librefit
def test_get_default_file_name():
"""Tests if the function returns the correct value"""
default_name = librefit.get_standard_file_name()
assert "file" in default_name
def test_get_default_folder_name():
"""Test if the default directory name gets returned"""
default_name = librefit.get_standard_folder_name()
assert "directory" in default_name
def test_get_standard_name_number():
"""Tests if the number function returns the correctly formatted string."""
name_number = librefit.get_standard_name_number(20,3)
assert "020" in name_number

View File

@@ -11,29 +11,30 @@ CONFIG_FILE = "version.cfg"
def read_version_config():
logger.debug(f"Start read_version_config() with config file: {CONFIG_FILE}")
config = configparser.ConfigParser()
logger.debug("reading config file")
config.read(CONFIG_FILE)
logger.debug("config file read")
if not os.path.exists(CONFIG_FILE):
logger.error("Could not find path")
return ""
logger.error(
f"Func: read_version_config() MSG: Could not find config file '{CONFIG_FILE}'"
)
return "x.x.x"
if "VERSION" not in config:
logger.error("Could not find VERSION in config file")
return ""
logger.error(f"Could not find VERSION-variable in config file '{CONFIG_FILE}'")
return "x.x.x"
try:
v = config["VERSION"]
major = v.get("major", "0")
minor = v.get("minor", "0")
patch = v.get("patch", "0")
logger.debug(f"output: {major}.{minor}.{patch}")
logger.debug(f"Config file read successfully. Version: {major}.{minor}.{patch}")
return f"{major}.{minor}.{patch}"
except Exception:
logger.warning("Couldnt read version")
return ""
logger.warning("Couldn not read version from config file")
return "x.x.x"
REFIT_VERSION = f"Refit Beta {read_version_config()}"
@@ -78,11 +79,11 @@ args = parser.parse_args()
# Dispatcher
# determines what code gets addressed based of the users choosen flags.
if hasattr(args, "command_class"):
logger.debug("In hasattr()")
logger.debug(f"In dispatcher with args: {args}")
Refit_Create = args.command_class
create_command_instance = Refit_Create(args)
create_command_instance()
else:
parser.print_help()
logger.info("No input, exiting with error:1")
logger.info("No input, exiting with exit code: 1")
sys.exit(1)

View File

@@ -1,5 +1,5 @@
[VERSION]
major = 0
minor = 2
patch = 3
patch = 4