import os import sys import json from .refit_logger import logger # TODO: Make a standard function for reading config files, so it is # reusable 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 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' """ standard_folder_name = name[0] if name is not None else "directory" 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' """ standard_file_name = name[0] if name is not None else "file" 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) def create_linear_directories( input_path: str, target_depth: int, current_depth: int, name ): """Creates the linear directories for the x*y pattern If no name is given the name of the level is defaulted to 'level'. Otherwise it takes the second input of the --name argument. In the end it appends the number of the folder. Args: input_path ( str ): _The current working directory. target_depth ( int ): _The depth on how deepo directories are created._ current_depth ( int ): _The current depth of the folder creation._ name ( list[str] | None ): _The name of the level directories._ """ # logger.debug( # f"FUNC: create_linear_directories(entered) VALUES: path='{input_path}', target_depth='{target_depth}', current_depth='{current_depth}'" # ) # TODO: Find a way on how to specify the type in the function call # and let the if statement pass. # Get base directory name if name is None: base_name = "level" else: base_name = name[1] if current_depth > target_depth: return # Create directory name directory_name = ( base_name + "_" + get_standard_name_number(current_depth, get_int_length(target_depth)) ) # Create the path where to create directory path = os.path.join(input_path, directory_name) os.mkdir(path) # Recursive call of itself create_linear_directories(path, target_depth, current_depth + 1, name) def create_parallel_directories(input_path: str, target_depth: int, width: int, name): """Creates the branches which house the levels. As input it takes the input_path and the width from which it creates the branches of the structure. Afterwards it passes the target_depth to another function to create the levels of each branch. If 'None' is passed to the '--name' argument, the default name 'branch' gets used as base directory name. Args: input_path ( str ): _The current working directory. target_depth ( int ): _The depth on how deepo directories are created._ width ( int ): _The ammount of branches to create._ name ( list[str] | None ): _The name of the level directories._ """ # logger.debug( # f"FUNC: create_parallel_directories(entered) VALUES: path='{input_path}', target_depth='{target_depth}', width='{width}', name={name}" # ) # TODO: Find a way on how to specify the type in the function call # and let the if statement pass. # Get base directory name if name is None: base_name = "branch" else: base_name = name[0] for i in range(width): # Create directory name directory_name = ( base_name + "_" + get_standard_name_number(i, get_int_length(width)) ) # Create the path where to create directory path = os.path.join(input_path, directory_name) os.mkdir(path) # Recursive call of itself create_linear_directories( input_path=path, target_depth=target_depth, current_depth=0, name=name, ) def get_version_from_file(input_path) -> str: """Returns the version of the program. The function accepts an path to a version file as a string and it returns it version number formatted. Arg: input_path (str): The absolute or relative path to the version file. Example: >>>get_version_from_file("/path/to/file.json") '0.2.1'""" # Expands the input path expanded_path = os.path.expanduser(input_path) # Opening the file and reading its contents, saving as as an dict. file_path = open(expanded_path) prog_version = json.load(file_path) # Formatting the output before returning the string again pretty_version = ( f"{prog_version['major']}.{prog_version['minor']}.{prog_version['patch']}" ) return pretty_version