From acd3f5fe3948a4e930e9ed21b9c4411248207c6b Mon Sep 17 00:00:00 2001 From: cerberus Date: Fri, 3 Oct 2025 19:13:53 +0200 Subject: [PATCH] added recursion, needs debugging --- refit/src/modules/librefit.py | 48 ++++++++++++++++++----- refit/src/modules/refit_create.py | 64 ++++++++++++++++++------------- refit/src/refit.py | 4 +- refit/src/version.cfg | 2 +- 4 files changed, 78 insertions(+), 40 deletions(-) diff --git a/refit/src/modules/librefit.py b/refit/src/modules/librefit.py index 31dcf29..c1abb8c 100644 --- a/refit/src/modules/librefit.py +++ b/refit/src/modules/librefit.py @@ -7,7 +7,7 @@ from .refit_logger import logger # 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 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( # 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) + 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}'" # ) @@ -121,13 +121,6 @@ def get_current_path(path) -> str: 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}'") if path is None: # 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) print(ERROR_MESSAGE) 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) diff --git a/refit/src/modules/refit_create.py b/refit/src/modules/refit_create.py index e5baef5..ff30a55 100644 --- a/refit/src/modules/refit_create.py +++ b/refit/src/modules/refit_create.py @@ -52,7 +52,7 @@ class Refit_Create: while n > 0: # 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. number_string = librefit.get_standard_name_number(folder_number, length_n) @@ -85,7 +85,7 @@ class Refit_Create: while n > 0: # 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) # 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 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): """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" ) + # 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 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.") sys.exit(1) # Exit the program if the -n argument is not passed if self.n is None: 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.") sys.exit(1) @@ -129,44 +147,36 @@ class Refit_Create: ) return True - def rf_create_decider(self): + def create_dispatcher(self): """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(): 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: 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) - else: - logger.error("Recursive and filemode don´t work together.") - print("Recursive and filemode don´t work together.") + self.create_n_files(self.n, self.input, self.name) - if self.recursive is not None: + elif self.recursive is not None: 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. - if self.recursive is None: - if not self.filemode: - 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) - else: - print("How did we end up here?????") + elif not self.recursive and not self.filemode: + logger.debug( + f"FUNC: create_dispatcher(n_folder) MSG: given arguments: n={self.n} input={self.input} name={self.name}" + ) + self.create_n_folders(self.n, self.input, self.name) else: 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( "Use '-n' argument to create directories.\nPlease use 'refit create -h' for help" @@ -175,4 +185,4 @@ class Refit_Create: def __call__(self): """Gets called when the 'create' subcommand is used.""" - self.rf_create_decider() + self.create_dispatcher() diff --git a/refit/src/refit.py b/refit/src/refit.py index 0216905..a5adfb1 100644 --- a/refit/src/refit.py +++ b/refit/src/refit.py @@ -82,8 +82,8 @@ create_parser.add_argument( create_parser.add_argument( "-r", "--recursive", - # action="store_true", - nargs="*", + type=int, + nargs=2, 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) diff --git a/refit/src/version.cfg b/refit/src/version.cfg index 08baeb2..0517c75 100644 --- a/refit/src/version.cfg +++ b/refit/src/version.cfg @@ -1,5 +1,5 @@ [VERSION] major = 0 minor = 3 -patch = 2 +patch = 3