added recursion, needs debugging

This commit is contained in:
2025-10-03 19:13:53 +02:00
parent 0e84c959ce
commit acd3f5fe39
4 changed files with 78 additions and 40 deletions

View File

@@ -7,7 +7,7 @@ from .refit_logger import logger
# reusable # reusable
def get_standard_name_number(current_number: str, number_str_length: int) -> str: 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 """Returns a number string filled to the length of the input number
This function returns the number in a standartized way as a string. This function returns the number in a standartized way as a string.
@@ -27,8 +27,8 @@ def get_standard_name_number(current_number: str, number_str_length: int) -> str
# logger.debug( # logger.debug(
# f"FUNC: get_standard_name_number() index={current_number} string_length={number_str_length}" # f"FUNC: get_standard_name_number() index={current_number} string_length={number_str_length}"
# ) # )
current_number = str(current_number) temp_current_number = str(current_number)
standard_name_number = str.zfill(current_number, number_str_length) standard_name_number = str.zfill(temp_current_number, number_str_length)
# logger.debug( # logger.debug(
# f"FUNC: get_standard_name_number() return value= '{standard_name_number}'" # f"FUNC: get_standard_name_number() return value= '{standard_name_number}'"
# ) # )
@@ -121,13 +121,6 @@ def get_current_path(path) -> str:
str: _Returns the path of the current directory after check for existence_ str: _Returns the path of the current directory after check for existence_
""" """
# TODO: write test for the current path function, test for:
# - None
# - for existing path
# - for non existing path
# NOTE: research how to check for paths in test functions...
logger.debug(f"FUNC: get_current_path() MSG: entered function with path = '{path}'") logger.debug(f"FUNC: get_current_path() MSG: entered function with path = '{path}'")
if path is None: if path is None:
# Set the current directory if none is passed with the command. # Set the current directory if none is passed with the command.
@@ -150,3 +143,38 @@ def get_current_path(path) -> str:
logger.warning(ERROR_MESSAGE) logger.warning(ERROR_MESSAGE)
print(ERROR_MESSAGE) print(ERROR_MESSAGE)
sys.exit(1) sys.exit(1)
# FIXME: th function needs some attention
# TODO:
# - add proper debugging
# - make it walk down one directory.
def create_recursive_folders(
input_path, target_depth, current_depth, width, curret_width
):
"""Creates a recursive directory structure with y as depth and x as width"""
if current_depth == target_depth:
sys.exit(1)
# length_width = str(len(width))
length_width = len(str(width))
logger.debug(
f"FUNC: create_recursive_folders(length_width) MSG: length_width='{length_width}'"
)
while curret_width < width:
# TODO: fix the issue, that the number gets appended to an existing number
new_directory_name = "sub_dir" + get_standard_name_number(length_width, width)
new_directory_path = os.path.join(input_path, new_directory_name)
print(new_directory_path)
curret_width += 1
create_recursive_folders(
new_directory_path,
target_depth,
width,
current_depth + 1,
curret_width,
)
# create_recursive_folders(".", 2, 0, 3)

View File

@@ -52,7 +52,7 @@ class Refit_Create:
while n > 0: while n > 0:
# iterating down for the files number. # iterating down for the files number.
folder_number = str(n - 1) folder_number = n - 1
# Passing the number and the length of the string to get the string back. # Passing the number and the length of the string to get the string back.
number_string = librefit.get_standard_name_number(folder_number, length_n) number_string = librefit.get_standard_name_number(folder_number, length_n)
@@ -85,7 +85,7 @@ class Refit_Create:
while n > 0: while n > 0:
# Get number of the file(s) to create # Get number of the file(s) to create
file_number = str(n - 1) file_number = n - 1
number_string = librefit.get_standard_name_number(file_number, length_n) number_string = librefit.get_standard_name_number(file_number, length_n)
# Get the name of the file, either applying default or using first list item. # Get the name of the file, either applying default or using first list item.
@@ -100,6 +100,19 @@ class Refit_Create:
# Counting down n for the next ieration of the while-loop # Counting down n for the next ieration of the while-loop
n -= 1 n -= 1
def create_recursive(self, recursive, name, input, n):
"""Creating directories recursively"""
logger.debug(
f"FUNC: create_recursive(beginning) MSG: entered function with following arguments: recursive='{recursive}' name='{name}' input='{input}' n='{n}'"
)
librefit.create_recursive_folders(
input,
recursive[0],
0,
recursive[1],
0,
)
def input_validator(self): def input_validator(self):
"""Function, which checks if the user input is valid""" """Function, which checks if the user input is valid"""
@@ -110,16 +123,21 @@ class Refit_Create:
"FUNC: input_validator() MSG: No directory passed to the command, continue with current directory" "FUNC: input_validator() MSG: No directory passed to the command, continue with current directory"
) )
# Check for conflicting flags
if self.recursive is not None and self.filemode:
logger.error("Filemode and recursive do not work together.")
print("Filemode and recursive do not work together.")
sys.exit(1)
# Check if recursive input is an empty list # Check if recursive input is an empty list
if self.recursive is not None: if self.recursive is not None:
if len(self.recursive) < 3: if len(self.recursive) < 2:
logger.error("Recursive flag cannot be set without values.") logger.error("Recursive flag cannot be set without values.")
sys.exit(1) sys.exit(1)
# Exit the program if the -n argument is not passed # Exit the program if the -n argument is not passed
if self.n is None: if self.n is None:
logger.error( logger.error(
f"FUNC rf_create_decider(n=None ?) MSG: the number value cannot be '{self.n}'" f"FUNC create_dispatcher(n=None ?) MSG: the number value cannot be '{self.n}'"
) )
print("Use the '-n' flag for the create command.") print("Use the '-n' flag for the create command.")
sys.exit(1) sys.exit(1)
@@ -129,44 +147,36 @@ class Refit_Create:
) )
return True return True
def rf_create_decider(self): def create_dispatcher(self):
"""Coordination of the 'create' sub command""" """Coordination of the 'create' sub command"""
logger.debug("FUNC: rf_create_decider() MSG: Entered decider function") logger.debug("FUNC: create_dispatcher() MSG: Entered decider function")
if self.input_validator(): if self.input_validator():
logger.debug( logger.debug(
f"FUNC: rf_create_decider() MSG: n={self.n} after input validating" f"FUNC: create_dispatcher() MSG: n={self.n} after input validating"
) )
# Exits the program if recursive and filemode flags are set at the same time
if self.filemode: if self.filemode:
logger.debug( logger.debug(
f"FUNC: rf_create_decider(filemode) MSG: given arguments: {self.n} {self.input} {self.name}" f"FUNC: create_dispatcher(filemode) MSG: given arguments: n={self.n} input={self.input} name={self.name}"
) )
if self.recursive is None: self.create_n_files(self.n, self.input, self.name)
self.create_n_files(self.n, self.input, self.name)
else:
logger.error("Recursive and filemode don´t work together.")
print("Recursive and filemode don´t work together.")
if self.recursive is not None: elif self.recursive is not None:
logger.debug( logger.debug(
f"FUNC: rf_create_decider(recursive) MSG: given arguments: n={self.n} input={self.input} name={self.name} recursive={self.recursive}" f"FUNC: create_dispatcher(recursive) MSG: given arguments: n={self.n} input={self.input} name={self.name} recursive={self.recursive}"
) )
self.create_recursive(self.recursive, self.name, self.input, self.n)
# OPTIMIZE:Enters the standard creation mode and crerates directories. elif not self.recursive and not self.filemode:
if self.recursive is None: logger.debug(
if not self.filemode: f"FUNC: create_dispatcher(n_folder) MSG: given arguments: n={self.n} input={self.input} name={self.name}"
logger.debug( )
f"FUNC: rf_create_decider(n_folder) MSG: given arguments: n={self.n} input={self.input} name={self.name}" self.create_n_folders(self.n, self.input, self.name)
)
self.create_n_folders(self.n, self.input, self.name)
else:
print("How did we end up here?????")
else: else:
logger.debug( logger.debug(
f"FUNC: rf_create_decider(exit no input) MSG: given arguments: n={self.n} input={self.input} name={self.name} recursive={self.recursive}" f"FUNC: create_dispatcher(exit no input) MSG: given arguments: n={self.n} input={self.input} name={self.name} recursive={self.recursive}"
) )
print( print(
"Use '-n' argument to create directories.\nPlease use 'refit create -h' for help" "Use '-n' argument to create directories.\nPlease use 'refit create -h' for help"
@@ -175,4 +185,4 @@ class Refit_Create:
def __call__(self): def __call__(self):
"""Gets called when the 'create' subcommand is used.""" """Gets called when the 'create' subcommand is used."""
self.rf_create_decider() self.create_dispatcher()

View File

@@ -82,8 +82,8 @@ create_parser.add_argument(
create_parser.add_argument( create_parser.add_argument(
"-r", "-r",
"--recursive", "--recursive",
# action="store_true", type=int,
nargs="*", nargs=2,
help="Sets the recursive mode for folders to true. First argumet\n is for the depth and the second for the width.", help="Sets the recursive mode for folders to true. First argumet\n is for the depth and the second for the width.",
) )
create_parser.set_defaults(command_class=Refit_Create) create_parser.set_defaults(command_class=Refit_Create)

View File

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