Files
python/refit/src/refit.py

106 lines
3.0 KiB
Python

import argparse
import sys
import os
import configparser
from modules.refit_logger import logger
from modules.refit_create import Refit_Create
CONFIG_FILE = "version.cfg"
# TODO: comment the actions done in read_version_config()
# TODO: see the TODO in librefit: move the config file section into the
# libary
def read_version_config():
logger.debug(f"Start read_version_config() with config file: {CONFIG_FILE}")
config = configparser.ConfigParser()
config.read(CONFIG_FILE)
if not os.path.exists(CONFIG_FILE):
logger.error(
f"FUNC: read_version_config() MSG: Could not find config file '{CONFIG_FILE}'"
)
return "x.x.x"
if "VERSION" not in config:
logger.error(f"Could not find VERSION-variable in config file '{CONFIG_FILE}'")
return "x.x.x"
try:
v = config["VERSION"]
major = v.get("major", "0")
minor = v.get("minor", "0")
patch = v.get("patch", "0")
logger.debug(f"Config file read successfully. Version: {major}.{minor}.{patch}")
return f"{major}.{minor}.{patch}"
except Exception:
logger.warning("Couldn not read version from config file")
return "x.x.x"
REFIT_VERSION = f"Refit Beta {read_version_config()}"
# ---------------------------ARGPARSE START---------------------------
# TODO: Rework the structure of the argument parsing
# 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
create_parser = subparser.add_parser("create", help="creates a new file/folder")
create_parser.add_argument("-n", type=int, help="number of items")
create_parser.add_argument("-i", "--input", help="input file")
create_parser.add_argument(
"--name",
nargs="*",
help="the name of the folder you want to create\n Default: directory",
)
create_parser.add_argument(
"--filemode", action="store_true", help="creates files instead of directories"
)
create_parser.add_argument(
"-r",
"--recursive",
type=int,
nargs=2,
help="Sets the recursive mode for folders to true. First argumet\n is for the depth and the second for the width.",
)
create_parser.set_defaults(command_class=Refit_Create)
args = parser.parse_args()
# ---------------------------ARGPARSE END-----------------------------
# Dispatcher
# determines what code gets addressed based of the users chosen flags.
if hasattr(args, "command_class"):
logger.debug(f"In dispatcher with args: {args}")
Refit_Create = args.command_class
create_command_instance = Refit_Create(args)
create_command_instance()
else:
parser.print_help()
logger.info("No input, exiting with exit code: 1")
sys.exit(1)