Files
python/refit/src/modules/refit_create.py
2025-10-02 15:40:36 +02:00

155 lines
5.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import os
from pathlib import Path
import sys
from .refit_logger import logger
from . import librefit
# 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
self.filemode = args.filemode
self.recursive = args.recursive
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(f"FUNC: create_n_folders() ARGS: n={n} input={input} name={name}")
# Creating the length of the suffix number_string.
length_n = len(str(n))
logger.debug(
f"FUNC: create_n_folders() MSG: Length of numbering string added to the name:{length_n}"
)
# Get either the default folder name or the input name as string.
logger.debug(
f"FUNC: create_n_folders() MSG: Type of name value: {type(name)} name={name}"
)
folder_name = librefit.get_standard_folder_name(name)
logger.debug(
f"FUNC: create_n_folders() MSG: Folder name: {folder_name} post get_standard_folder_name() call"
)
while n > 0:
# iterating down for the files number.
folder_number = str(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)
# Creating path for the folder
temp_name = f"{folder_name}_{number_string}"
folder_creation_path = os.path.join(input, temp_name)
# Creating folder and subtracting n by one for the number_string
os.mkdir(folder_creation_path)
n -= 1
def create_n_files(self, n, input, name):
"""Creates an set ammount of files, using the default file name
if none is provided."""
logger.debug(
f"FUNC: create_n_files() MSG: Entered function VALUES: n={self.n} name={self.name} input={self.input}"
)
# Get the name from the input argument.
file_name = librefit.get_standard_file_name(name)
# Get the length of the entered number to fill it with 0
length_n = len(str(n))
logger.debug(
f"FUNC: create_n_files() MSG: file name='{file_name}' length_n={length_n}"
)
while n > 0:
# Get number of the file(s) to create
file_number = str(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.
temp_name = f"{file_name}_{number_string}"
file_path = Path(os.path.join(input, temp_name)) # Build file path
file_path.touch(exist_ok=True) # creating file
logger.debug(
f"FUNC: create_n_files MSG: created file at {os.path.join(input, temp_name)}"
)
# Counting down n for the next ieration of the while-loop
n -= 1
def input_validator(self):
"""Function, which checks if the user input is valid"""
# NOTE: find a way on how to return 'True' when the user input
# is valid
# Check working directory
if self.input is None:
self.input = librefit.get_current_path(self.input)
logger.info(
"FUNC: input_validator() MSG: No directory passed to the command, continue with current directory"
)
# Exit the program if the -n argument is not passed
if self.n is None:
logger.error(
f"FUNC rf_create_decider() MSG: the number value cannot be '{self.n}'"
)
print("Use the '-n' flag for the create command.")
sys.exit(1)
def rf_create_decider(self):
"""Coordination of the 'create' sub command"""
logger.debug("FUNC: rf_create_decider() MSG: Entered decider function")
# WARNING: implement the input validator and only continue with
# the decider if the validator returns true
if self.input_validator():
logger.debug("yippie")
# Exits the program if recursive and filemode flags are set at the same time
if self.filemode:
logger.debug("DECISION filemode set, creating files instead of folders")
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.")
if self.recursive is None:
self.create_n_folders(self.n, self.input, self.name)
else:
logger.debug(
f"FUNC: rf_create_decider(end) MSG: given arguments: {self.args}"
)
print(
"Use '-n' argument to create directories.\nPlease use 'refit create -h' for help"
)
sys.exit(1)
def __call__(self):
"""Gets called when the 'create' subcommand is used."""
self.rf_create_decider()