Files
python/refit/src/modules/refit_create.py

81 lines
2.7 KiB
Python
Raw Normal View History

2025-09-28 13:53:10 +02:00
import os
from .refit_logger import logger
2025-09-28 17:19:44 +02:00
from . import librefit
2025-09-28 13:53:10 +02:00
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()")
2025-09-28 17:19:44 +02:00
# Creating the length of the suffix number_string.
length_n = len(str(n))
2025-09-28 13:53:10 +02:00
2025-09-28 17:19:44 +02:00
# Get either the default folder name or the input name as string.
folder_name = librefit.get_standard_folder_name(name)
2025-09-28 13:53:10 +02:00
logger.debug(f"Length of numbering string:{length_n}")
2025-09-28 17:19:44 +02:00
logger.debug(f"Folder name: {folder_name}")
2025-09-28 13:53:10 +02:00
while n > 0:
2025-09-28 17:19:44 +02:00
# iterating down for the files number.
2025-09-28 13:53:10 +02:00
i = str(n - 1)
2025-09-28 17:19:44 +02:00
# Passing the number and the length of the string to get the string back.
number_string = librefit.get_standard_name_number(i, length_n)
2025-09-28 13:53:10 +02:00
2025-09-28 17:19:44 +02:00
# Creating path for the folder
temp_name = f"{folder_name}_{number_string}"
folder_creation_path = os.path.join(input, temp_name)
2025-09-28 13:53:10 +02:00
# Creating folder and subtracting n by one for the number_string
2025-09-28 17:19:44 +02:00
os.mkdir(folder_creation_path)
2025-09-28 13:53:10 +02:00
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()