From 6738f73db794cfcfc3a0cdfe2004b968b82fa986 Mon Sep 17 00:00:00 2001 From: cerberus Date: Sun, 28 Sep 2025 13:53:10 +0200 Subject: [PATCH] create class --- refit/src/modules/refit_create.py | 92 +++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 refit/src/modules/refit_create.py diff --git a/refit/src/modules/refit_create.py b/refit/src/modules/refit_create.py new file mode 100644 index 0000000..6ee7bab --- /dev/null +++ b/refit/src/modules/refit_create.py @@ -0,0 +1,92 @@ +import os +import pathlib +import sys + +from .refit_logger import logger + +logger.debug("Initiated refit_create.py") +# ---------------------------------------------------------------------- + + +class Refit_Create: + """A class to create folders and files. + + It first calls the decider which lets the create_input_valid() function + check if the input argument exists. If create_input_valid() returns + 'True' it continues to execute the command as per the given arguments. + + default folder name: directory""" + + def __init__(self, args): + """Initiating variables for creation""" + self.args = args + + self.name = args.name + self.input = args.input + self.n = args.n + + def create_input_valid(self): + """Checks if the input is valid and returns either True or + throws an error in log and terminal.""" + + logger.debug("in create_input_valid()") + if self.input is None: + logger.warning(f"{self.input} cannot be None") + print("Input argument missing. Use 'refit create -h' for help") + raise ValueError("Input missing") + return True + + def create_n_folders(self, n, input, name): + """Creates an set ammount of folders. Using the default directory + name if no other is provided.""" + + logger.debug("in create_n_folders()") + + # Sets the default directory name. + if name is None: + logger.debug("name argument is 'None', applying default name") + name = "directory" + else: + logger.debug(f"{name}, Type: {type(name)}, Length: {len(name)}") + + # Checks if multiple names are passed to the --name argument + # if so, only the first one gets used. + if len(name) > 1: + _NAMES_WARNING = f"{len(name)} names given, only one required.\nContinuing with '{name[0]}' as name." + logger.warning(_NAMES_WARNING) + print(_NAMES_WARNING) + name = name[0] + + logger.debug(f"{name}, Type: {type(name)}") + + # Creating the length of the suffix. + length_n = len(str(n)) + logger.debug(f"Length of numbering string:{length_n}") + + while n > 0: + # Creating the suffix string, filling the numbers to len of arg.n + i = str(n - 1) + number_string = str.zfill(i, length_n) + + # Creating path for the folder + temp_name = f"{name}_{number_string}" + rfc_path = os.path.join(input, temp_name) + + # Creating folder and subtracting n by one for the number_string + os.mkdir(rfc_path) + n -= 1 + + def rf_create_decider(self): + """Coordination of the 'create' sub command""" + + if self.create_input_valid(): + logger.debug("valid input -> continue") + + self.create_n_folders(self.n, self.input, self.name) + logger.debug( + "End of run---------------------------------------------------------" + ) + + def __call__(self): + """Gets called when the object is treated as an function""" + self.rf_create_decider()