2025-09-28 13:53:10 +02:00
|
|
|
import os
|
2025-09-29 21:38:42 +02:00
|
|
|
from pathlib import Path
|
2025-09-30 20:56:27 +02:00
|
|
|
import sys
|
2025-09-28 13:53:10 +02:00
|
|
|
|
|
|
|
|
from .refit_logger import logger
|
2025-09-28 17:19:44 +02:00
|
|
|
from . import librefit
|
2025-09-28 13:53:10 +02:00
|
|
|
|
2025-09-29 20:11:16 +02:00
|
|
|
# logger.debug("Initiated refit_create.py")
|
2025-09-28 13:53:10 +02:00
|
|
|
# ----------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.name = args.name
|
|
|
|
|
self.input = args.input
|
|
|
|
|
self.n = args.n
|
2025-09-28 19:51:06 +02:00
|
|
|
self.filemode = args.filemode
|
2025-09-30 20:56:27 +02:00
|
|
|
self.recursive = args.recursive
|
|
|
|
|
self.args = args
|
2025-09-28 13:53:10 +02:00
|
|
|
|
2025-10-01 08:06:26 +02:00
|
|
|
# def create_input_valid(self, args):
|
|
|
|
|
# """Checks if the input is argument for its existence. If no input
|
|
|
|
|
# argument with a value is passed, the current directory is used and
|
|
|
|
|
# passed to the input"""
|
|
|
|
|
|
|
|
|
|
# logger.debug(f"Start create_input_valid() value= {self.input}")
|
|
|
|
|
# logger.debug(f"FUNC: create_input_valid() MSG: arguments: {args}")
|
|
|
|
|
# if self.input is None:
|
|
|
|
|
# input = "."
|
|
|
|
|
# self.input = input
|
|
|
|
|
# logger.info(
|
|
|
|
|
# f"FUNC: create_input_valid MSG: Usingsing current directory as input. value={self.input} "
|
|
|
|
|
# )
|
|
|
|
|
# return self.input
|
|
|
|
|
# else:
|
|
|
|
|
# logger.debug(
|
|
|
|
|
# f"FUNC: create_input_valid() MSG: Exit with valid input. value= {self.input}"
|
|
|
|
|
# )
|
|
|
|
|
# return True
|
2025-09-28 13:53:10 +02:00
|
|
|
|
|
|
|
|
def create_n_folders(self, n, input, name):
|
|
|
|
|
"""Creates an set ammount of folders. Using the default directory
|
|
|
|
|
name if no other is provided."""
|
|
|
|
|
|
2025-09-29 20:11:16 +02:00
|
|
|
logger.debug(f"FUNC: create_n_folders() ARGS: n={n} input={input} name={name}")
|
2025-09-28 13:53:10 +02:00
|
|
|
|
2025-09-28 17:19:44 +02:00
|
|
|
# Creating the length of the suffix number_string.
|
|
|
|
|
length_n = len(str(n))
|
2025-09-29 20:11:16 +02:00
|
|
|
logger.debug(
|
|
|
|
|
f"FUNC: create_n_folders() MSG: Length of numbering string added to the name:{length_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.
|
2025-09-29 20:11:16 +02:00
|
|
|
logger.debug(
|
|
|
|
|
f"FUNC: create_n_folders() MSG: Type of name value: {type(name)} name={name}"
|
|
|
|
|
)
|
2025-09-28 17:19:44 +02:00
|
|
|
folder_name = librefit.get_standard_folder_name(name)
|
2025-09-28 13:53:10 +02:00
|
|
|
|
2025-09-29 20:11:16 +02:00
|
|
|
logger.debug(
|
|
|
|
|
f"FUNC: create_n_folders() MSG: Folder name: {folder_name} post get_standard_folder_name() call"
|
|
|
|
|
)
|
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-29 21:38:42 +02:00
|
|
|
folder_number = str(n - 1)
|
2025-09-28 13:53:10 +02:00
|
|
|
|
2025-09-28 17:19:44 +02:00
|
|
|
# Passing the number and the length of the string to get the string back.
|
2025-09-29 21:38:42 +02:00
|
|
|
number_string = librefit.get_standard_name_number(folder_number, 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-29 20:11:16 +02:00
|
|
|
|
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
|
|
|
|
|
|
2025-09-29 21:38:42 +02:00
|
|
|
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}"
|
|
|
|
|
)
|
|
|
|
|
|
2025-09-30 07:59:02 +02:00
|
|
|
# Get the name from the input argument.
|
2025-09-29 21:38:42 +02:00
|
|
|
file_name = librefit.get_standard_file_name(name)
|
2025-09-30 07:59:02 +02:00
|
|
|
|
|
|
|
|
# Get the length of the entered number to fill it with 0
|
2025-09-29 21:38:42 +02:00
|
|
|
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)
|
2025-09-30 20:56:27 +02:00
|
|
|
|
2025-09-30 07:59:02 +02:00
|
|
|
# Get the name of the file, either applying default or using first list item.
|
2025-09-29 21:38:42 +02:00
|
|
|
temp_name = f"{file_name}_{number_string}"
|
|
|
|
|
|
2025-09-30 20:56:27 +02:00
|
|
|
file_path = Path(os.path.join(input, temp_name)) # Build file path
|
|
|
|
|
file_path.touch(exist_ok=True) # creating file
|
|
|
|
|
|
2025-09-29 21:38:42 +02:00
|
|
|
logger.debug(
|
|
|
|
|
f"FUNC: create_n_files MSG: created file at {os.path.join(input, temp_name)}"
|
|
|
|
|
)
|
2025-09-30 07:59:02 +02:00
|
|
|
# Counting down n for the next ieration of the while-loop
|
2025-09-29 21:38:42 +02:00
|
|
|
n -= 1
|
|
|
|
|
|
2025-09-28 13:53:10 +02:00
|
|
|
def rf_create_decider(self):
|
|
|
|
|
"""Coordination of the 'create' sub command"""
|
2025-09-29 20:11:16 +02:00
|
|
|
logger.debug("FUNC: rf_create_decider() MSG: Entered decider function")
|
2025-09-30 20:56:27 +02:00
|
|
|
|
2025-10-01 16:07:08 +02:00
|
|
|
self.input = librefit.get_current_path(self.input)
|
|
|
|
|
print(self.input)
|
|
|
|
|
|
|
|
|
|
# if self.input is None:
|
|
|
|
|
# self.input = "."
|
|
|
|
|
# logger.info(
|
|
|
|
|
# f"FUNC: rf_create_decider() MSG: Usingsing current directory as input. value={self.input} "
|
|
|
|
|
# )
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# if self.filemode:
|
|
|
|
|
# logger.debug("DECISION if filemode")
|
|
|
|
|
# self.create_n_files(self.n, self.input, self.name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# if self.recursive is False:
|
|
|
|
|
# logger.debug(
|
|
|
|
|
# f"FUNC: {self.rf_create_decider.__name__} DIR-MODE | ARGS: n={self.n} input={self.input} name={self.name}"
|
|
|
|
|
# )
|
|
|
|
|
# if self.n is not None: self.create_n_folders(self.n, self.input, self.name)
|
|
|
|
|
# else:
|
|
|
|
|
# ERROR_MESSAGE="Please input the number on how many objects you want to create"
|
|
|
|
|
|
|
|
|
|
# else:
|
|
|
|
|
# logger.debug(f"FUNC: rf_create_decider() MSG: given arguments: {self.args}")
|
|
|
|
|
# print(
|
|
|
|
|
# "Use '-n' argument to create directories.\nPlease use 'refit create -h' for help"
|
|
|
|
|
# )
|
|
|
|
|
# sys.exit(1)
|
2025-09-28 13:53:10 +02:00
|
|
|
|
|
|
|
|
def __call__(self):
|
2025-09-30 08:05:00 +02:00
|
|
|
"""Gets called when the 'create' subcommand is used."""
|
2025-09-28 13:53:10 +02:00
|
|
|
self.rf_create_decider()
|