Files
python/refit/src/modules/librefit.py

197 lines
6.4 KiB
Python
Raw Normal View History

2025-10-01 16:07:08 +02:00
import os
import sys
2025-09-28 17:19:20 +02:00
from .refit_logger import logger
2025-10-02 15:40:36 +02:00
# TODO: Make a standard function for reading config files, so it is
# reusable
2025-09-28 17:19:20 +02:00
def get_int_length(number: int) -> int:
"""Takes an number as its input and returns the numbers diget amount.
This function takes an integer number, converts it into a string and
converts its digets. It returns the length of the number as an integer.
Args:
number (int): The number you need the length of
Examples:
>>> get_int_length(100)
'3'
>>> get_int_length(10)
'2'
>>> get_int_length(4000)
'4'
"""
number_string = str(number)
amount_didgets = len(number_string)
return amount_didgets
2025-10-03 19:13:53 +02:00
def get_standard_name_number(current_number: int, number_str_length: int) -> str:
"""Returns a number string filled to the length of the input number
This function returns the number in a standartized way as a string.
As input it takes the current number of the string to build and a
number which determines the length of the string.
Args:
2025-09-30 20:56:27 +02:00
current_number (str): The current number of the item.
number_str_length (int): The length of the string which gets returned.
Examples:
>>> get_standard_name_number(1, 2)
'01'
>>> get_standard_name_number(23, 4)
'0023'
"""
# logger.debug(
# f"FUNC: get_standard_name_number() index={current_number} string_length={number_str_length}"
# )
2025-10-03 19:13:53 +02:00
temp_current_number = str(current_number)
standard_name_number = str.zfill(temp_current_number, number_str_length)
# logger.debug(
# f"FUNC: get_standard_name_number() return value= '{standard_name_number}'"
# )
2025-09-28 17:19:20 +02:00
return standard_name_number
2025-09-30 20:56:27 +02:00
def get_standard_folder_name(name: str) -> str:
"""Returnes a standard name either from a list or the default value.
This function sanitizes the input, which gets passed as a list or None from
argparse. The function either chooses the first entry of the list, given to
the --name argument or returns the default value 'directory'
Args:
name (list[str] | None): A list of names if passed to the --name argument
or None if no name is passed.
Returns:
str: The file name. Returns 'file' as default value if name argument is 'None'
otherwise the first element of the list.
Examples:
>>> get_standard_file_name(None)
'file'
>>> get_standard_file_name(["example"])
'example'
>>> get_standard_file_name(["directory_name", "example"])
'directory_name'
"""
2025-09-28 17:19:20 +02:00
standard_folder_name = name[0] if name is not None else "directory"
2025-09-28 17:19:20 +02:00
return standard_folder_name
2025-09-29 20:11:16 +02:00
def get_standard_file_name(name) -> str:
"""Returnes a name either from a list or the default value.
This function sanitizes the input, which gets passed as a list or None from
argparse. The function either chooses the first entry of the list, given to
the --name argument or returns the default value 'file'
Args:
name (list[str] | None): A list of names if passed to the --name argument
or None if no name is passed.
Returns:
str: The file name. Returns 'file' as default value if name argument is 'None'
otherwise the first element of the list.
Examples:
>>> get_standard_file_name(None)
'file'
>>> get_standard_file_name(["example"])
'example'
>>> get_standard_file_name(["file_name", "example"])
'file_name'
"""
2025-09-29 20:11:16 +02:00
standard_file_name = name[0] if name is not None else "file"
2025-09-29 20:11:16 +02:00
return standard_file_name
2025-10-01 16:07:08 +02:00
2025-10-01 22:20:23 +02:00
def get_current_path(path) -> str:
2025-10-01 16:07:08 +02:00
"""Checks if the path argument is emty and applies the current directory as working path.
2025-10-01 22:20:23 +02:00
This function takes an list with strings as an input. If the input is `None` the current
2025-10-01 16:07:08 +02:00
directory is taken as the working directory.
If the path is passed, a check for its existence takes place.
2025-10-01 22:20:23 +02:00
2025-10-01 16:07:08 +02:00
Args:
path (str): _The current working directory._
Returns:
str: _Returns the path of the current directory after check for existence_
"""
2025-10-02 15:40:36 +02:00
2025-10-01 22:20:23 +02:00
logger.debug(f"FUNC: get_current_path() MSG: entered function with path = '{path}'")
2025-10-01 16:07:08 +02:00
if path is None:
2025-10-02 15:11:40 +02:00
# Set the current directory if none is passed with the command.
2025-10-01 16:07:08 +02:00
path = "."
# logger.warning(
# f"FUNC: {get_current_path.__name__}() MSG: Path now has the value: '{path}'"
# )
2025-10-01 16:07:08 +02:00
return path
else:
2025-10-02 15:11:40 +02:00
# Checks if the path, entered by the user, exists.
2025-10-01 16:07:08 +02:00
if os.path.exists(path) is True:
# logger.debug(
# f"FUNC: {get_current_path.__name__} MSG: Path '{path}' exists, continue...."
# )
2025-10-01 16:07:08 +02:00
return path
else:
2025-10-01 22:20:23 +02:00
ERROR_MESSAGE = (
f"FUNC: {get_current_path.__name__} MSG: '{path}' does not exist"
)
logger.warning(ERROR_MESSAGE)
2025-10-01 16:07:08 +02:00
print(ERROR_MESSAGE)
sys.exit(1)
2025-10-03 19:13:53 +02:00
# TODO: - add dynamic name input
2025-10-04 09:09:02 +02:00
# - add docstring
def create_linear_directories(input_path, target_depth, current_depth):
"""Creates the linear directories for the x*y pattern"""
2025-10-03 19:13:53 +02:00
logger.debug(
f"FUNC: create_linear_directories(entered) VALUES: path='{input_path}', target_depth='{target_depth}', current_depth='{current_depth}'"
)
if current_depth > target_depth:
2025-10-04 09:09:02 +02:00
return
2025-10-03 19:13:53 +02:00
directory_name = "level_" + get_standard_name_number(
current_depth,
get_int_length(target_depth),
)
path = os.path.join(input_path, directory_name)
os.mkdir(path)
create_linear_directories(path, target_depth, current_depth + 1)
# TODO: - add dynamic name input
# - add docstring
def create_parallel_directories(input_path, target_depth, width):
"""Creates directories after the pattern x*y"""
2025-10-03 19:13:53 +02:00
logger.debug(
f"FUNC: create_parallel_directories(entered) VALUES: path='{input_path}', target_depth='{target_depth}', width='{width}'"
2025-10-03 19:13:53 +02:00
)
2025-10-04 09:09:02 +02:00
# # Getting the lenth of the number string which gets addedto the dir name.
# length_width = get_int_length(width)
2025-10-04 09:09:02 +02:00
for i in range(width):
directory_name = "branch_" + get_standard_name_number(i, get_int_length(width))
path = os.path.join(input_path, directory_name)
os.mkdir(path)
create_linear_directories(path, target_depth, current_depth=0)