import os import sys from .refit_logger import logger # TODO: Make a standard function for reading config files, so it is # reusable 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: 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}" # ) 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}'" # ) return standard_name_number 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' """ logger.debug( f"FUNC: get_standard_folder_name() MSG: entered function with value: '{name}'" ) standard_folder_name = name[0] if name is not None else "directory" logger.debug( f"FUNC: get_standard_folder_name() MSG: exiting function with folder name: '{standard_folder_name}'" ) return standard_folder_name 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' """ logger.debug( f"FUNC: get_standard_file_name() MSG: entered function with name='{name}'" ) standard_file_name = name[0] if name is not None else "file" 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 def get_current_path(path) -> str: """Checks if the path argument is emty and applies the current directory as working path. This function takes an list with strings as an input. If the input is `None` the current directory is taken as the working directory. If the path is passed, a check for its existence takes place. Args: path (str): _The current working directory._ Returns: str: _Returns the path of the current directory after check for existence_ """ 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. path = "." logger.warning( f"FUNC: {get_current_path.__name__}() MSG: Path now has the value: '{path}'" ) return path else: # Checks if the path, entered by the user, exists. if os.path.exists(path) is True: logger.debug( f"FUNC: {get_current_path.__name__} MSG: Path '{path}' exists, continue...." ) return path else: ERROR_MESSAGE = ( f"FUNC: {get_current_path.__name__} MSG: '{path}' does not exist" ) logger.warning(ERROR_MESSAGE) print(ERROR_MESSAGE) sys.exit(1) # FIXME: the assigning of the number to the folders is not working # TODO: - add logging # - add docstring def create_recursive_folders(input_path, target_depth, current_depth, width): """Creates a recursive directory structure with y as depth and x as width""" if current_depth >= target_depth: return length_width = len(str(width)) logger.debug( f"FUNC: create_recursive_folders(length_width) MSG: length_width='{length_width}'" ) for i in range(width): folder_index = i + 1 new_directory_name = "sub_dir" + get_standard_name_number( length_width, folder_index ) new_directory_path = os.path.join(input_path, new_directory_name) print(new_directory_path) logger.debug(f"New Path: path={new_directory_path}") create_recursive_folders( new_directory_path, target_depth, current_depth + 1, width, ) # create_recursive_folders(".", 2, 0, 3)