2025-09-27 20:53:07 +02:00
|
|
|
import argparse
|
|
|
|
|
import sys
|
2025-09-28 19:51:06 +02:00
|
|
|
import os
|
|
|
|
|
import configparser
|
2025-09-27 20:53:07 +02:00
|
|
|
|
|
|
|
|
from modules.refit_logger import logger
|
2025-09-28 13:51:53 +02:00
|
|
|
from modules.refit_create import Refit_Create
|
2025-09-27 20:53:07 +02:00
|
|
|
|
2025-09-28 18:05:03 +02:00
|
|
|
|
2025-09-28 19:51:06 +02:00
|
|
|
CONFIG_FILE = "version.cfg"
|
2025-09-28 18:05:03 +02:00
|
|
|
|
2025-09-28 19:51:06 +02:00
|
|
|
|
|
|
|
|
def read_version_config():
|
2025-09-29 20:11:16 +02:00
|
|
|
logger.debug(f"Start read_version_config() with config file: {CONFIG_FILE}")
|
2025-09-28 19:51:06 +02:00
|
|
|
config = configparser.ConfigParser()
|
|
|
|
|
config.read(CONFIG_FILE)
|
|
|
|
|
|
|
|
|
|
if not os.path.exists(CONFIG_FILE):
|
2025-09-29 20:11:16 +02:00
|
|
|
logger.error(
|
|
|
|
|
f"Func: read_version_config() MSG: Could not find config file '{CONFIG_FILE}'"
|
|
|
|
|
)
|
|
|
|
|
return "x.x.x"
|
2025-09-28 19:51:06 +02:00
|
|
|
|
|
|
|
|
if "VERSION" not in config:
|
2025-09-29 20:11:16 +02:00
|
|
|
logger.error(f"Could not find VERSION-variable in config file '{CONFIG_FILE}'")
|
|
|
|
|
return "x.x.x"
|
2025-09-28 19:51:06 +02:00
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
v = config["VERSION"]
|
|
|
|
|
major = v.get("major", "0")
|
|
|
|
|
minor = v.get("minor", "0")
|
|
|
|
|
patch = v.get("patch", "0")
|
2025-09-29 20:11:16 +02:00
|
|
|
logger.debug(f"Config file read successfully. Version: {major}.{minor}.{patch}")
|
2025-09-28 19:51:06 +02:00
|
|
|
return f"{major}.{minor}.{patch}"
|
|
|
|
|
except Exception:
|
2025-09-29 20:11:16 +02:00
|
|
|
logger.warning("Couldn not read version from config file")
|
|
|
|
|
return "x.x.x"
|
2025-09-28 19:51:06 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
REFIT_VERSION = f"Refit Beta {read_version_config()}"
|
2025-09-27 20:53:07 +02:00
|
|
|
|
|
|
|
|
# ---------------------------ARGPARSE START---------------------------
|
|
|
|
|
# Main Parser
|
|
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
|
prog="Refit",
|
|
|
|
|
description="This is a file and directory manipulation tool.\
|
|
|
|
|
it can create, move and delete files and directories as well as \
|
|
|
|
|
renaming them",
|
|
|
|
|
epilog=REFIT_VERSION,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# Main Parser Arguments
|
|
|
|
|
|
|
|
|
|
# Create Parser
|
|
|
|
|
subparser = parser.add_subparsers(
|
|
|
|
|
title="Commands",
|
|
|
|
|
dest="create",
|
|
|
|
|
required=False,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# Create Parser Arguments
|
2025-09-28 17:20:51 +02:00
|
|
|
create_parser = subparser.add_parser("create", help="creates a new file/folder")
|
2025-09-27 20:53:07 +02:00
|
|
|
create_parser.add_argument("-n", type=int, help="number of items")
|
|
|
|
|
create_parser.add_argument("-i", "--input", help="input file")
|
2025-09-28 13:51:53 +02:00
|
|
|
create_parser.add_argument(
|
|
|
|
|
"--name",
|
|
|
|
|
nargs="*",
|
|
|
|
|
help="the name of the folder you want to create\n Default: directory",
|
|
|
|
|
)
|
2025-09-28 19:51:06 +02:00
|
|
|
create_parser.add_argument(
|
|
|
|
|
"--filemode", action="store_true", help="creates files instead of directories"
|
|
|
|
|
)
|
2025-09-30 20:56:27 +02:00
|
|
|
create_parser.add_argument(
|
|
|
|
|
"-r",
|
|
|
|
|
"--recursive",
|
2025-10-01 08:06:26 +02:00
|
|
|
action="store_true",
|
|
|
|
|
# nargs="*",
|
2025-09-30 20:56:27 +02:00
|
|
|
help="Sets the recursive mode for folders to true. First argumet\n is for the depth and the second for the width.",
|
|
|
|
|
)
|
2025-09-28 13:51:53 +02:00
|
|
|
create_parser.set_defaults(command_class=Refit_Create)
|
2025-09-27 20:53:07 +02:00
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
# ---------------------------ARGPARSE END-----------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Dispatcher
|
2025-10-01 22:20:23 +02:00
|
|
|
# determines what code gets addressed based of the users chosen flags.
|
2025-09-28 13:51:53 +02:00
|
|
|
if hasattr(args, "command_class"):
|
2025-09-29 20:11:16 +02:00
|
|
|
logger.debug(f"In dispatcher with args: {args}")
|
2025-09-28 13:51:53 +02:00
|
|
|
Refit_Create = args.command_class
|
|
|
|
|
create_command_instance = Refit_Create(args)
|
|
|
|
|
create_command_instance()
|
2025-09-27 20:53:07 +02:00
|
|
|
else:
|
|
|
|
|
parser.print_help()
|
2025-09-29 20:11:16 +02:00
|
|
|
logger.info("No input, exiting with exit code: 1")
|
2025-09-27 20:53:07 +02:00
|
|
|
sys.exit(1)
|