Added input handling and first utilities

This commit is contained in:
Neocky 2022-06-07 22:40:27 +02:00
parent 8e51b1976d
commit ac139ed048
3 changed files with 184 additions and 7 deletions

View File

@ -2,13 +2,15 @@
Handles the main function and the argument passing for the whole pluGET program
"""
import argparse
import sys
import argparse
# check if folder 'src' is accessible with all modules needed and if not exit
try:
from src.handlers.handle_config import check_config, validate_config, config_value
from src.utils.console_output import rename_console_title, clear_console, print_logo
from src.utils.utilities import rich_print_error, api_test_spiget
from src.handlers.handle_input import handle_input
except:
print("Folder 'src' not found in the directory or missing files detected! Please redownload the files from here: https://www.github.com/Neocky/pluGET")
sys.exit()
@ -17,7 +19,9 @@ except:
if __name__ == "__main__":
check_config()
rename_console_title()
api_test_spiget()
#check_requirements()
validate_config()
parser = argparse.ArgumentParser(description="Just an example",
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
#parser.add_argument("-a", "--archive", action="store_true", help="archive mode")
@ -26,19 +30,18 @@ if __name__ == "__main__":
parser.add_argument("plugin", help="Plugin Name", nargs='?', default=None)
args = vars(parser.parse_args())
if args["mode"] is not None and args["plugin"] is not None:
#argument_code(args["mode"], args["plugin"])
print("arguments")
else:
print("no arguments")
clear_console()
#createInputLists()
#getInput()
print_logo()
rich_print_error("test")
rich_print_error("test2")
handle_input()
config = config_value()
validate_config()
print(config.connection)
print(config.path_to_plugin_folder)
print(config.sftp_port)
print(config.local_seperate_download_path)
input()

View File

@ -0,0 +1,94 @@
""""
Handles the input through the pluGET command line
"""
from src.utils.utilities import rich_print_error
# check
# update
# get
# get-paper
# get-purpur
# get-airplane
# exit
# remove
# search ???
def handle_input() -> None:
"""
Manages the correct function calling from the given input
"""
while True:
try:
input_command, input_selected_object, input_parameter = get_input()
except TypeError:
# KeyboardInterrupt was triggered and None was returned so exit
return
match input_command:
case "get":
match input_selected_object.isdigit():
case True:
print("get specific package")
#getSpecificPackage(inputSelectedObject, pluginPath, inputParams)
case _:
print("get search specific package")
#searchPackage(inputSelectedObject)
case "update":
print("update package")
match input_selected_object:
case "serverjar":
print("update serverjar")
#updateServerjar(inputParams)
case _:
print("update package")
#updateInstalledPackage(inputSelectedObject)
case "check":
print("check package")
match input_selected_object:
case "serverjar":
print("check serverjar")
#checkInstalledServerjar()
case _:
print("check plugins")
#checkInstalledPackage(inputSelectedObject, inputParams)
case "search":
print("search package")
#searchPackage(inputSelectedObject)
case "remove":
print("remove package")
#removePlugin(inputSelectedObject)
case "get-paper":
# download papermc
print("download papermc")
#papermc_downloader(inputSelectedObject, inputParams)
case _:
rich_print_error("Error: Command not found. Please try again. :(")
rich_print_error("Use: 'help command' to get all available commands")
def get_input() -> None:
"""
Gets command line input and calls the handle input function
"""
input_command = None
while True:
try:
input_command, input_selected_object, *input_parameter = input("pluGET >> ").split()
break
except ValueError:
if input_command == None:
# request input again if no input was given or not enough
continue
else:
rich_print_error("Wrong input! Use: > 'command' 'selectedObject' [optionalParams]")
rich_print_error("Use: 'help command' to get all available commands")
except KeyboardInterrupt:
return
input_parameter = input_parameter[0] if input_parameter else None
return input_command, input_selected_object, input_parameter

80
src/utils/utilities.py Normal file
View File

@ -0,0 +1,80 @@
"""
Holds all the utilitie code for pluGET and the webrequests function
"""
import os
import sys
import requests
import shutil
from pathlib import Path
from rich.console import Console
def api_do_request(url) -> list:
"""
Handles the webrequest and returns a json list
"""
webrequest_header = {'user-agent': 'pluGET/1.0'}
try:
response = requests.get(url, headers=webrequest_header)
except:
print("Couldn't create webrequest")
return
api_json_data = response.json()
return api_json_data
def api_test_spiget() -> None:
"""
Test if the Spiget api sends a 200 status code back
"""
try:
r = requests.get('https://api.spiget.org/v2/status')
except (requests.exceptions.HTTPError, requests.exceptions.ConnectionError):
rich_print_error("Error: Couldn't make a connection to the API. Check you connection to the internet!", style="bright_red")
rich_print_error("Press any key + enter to exit...")
sys.exit()
if r.status_code != 200:
rich_print_error("Error: Problems with the API detected. Plese try it again later!")
rich_print_error("Press any key + enter to exit...")
sys.exit()
return
def rich_print_error(error_message) -> None:
"""
Prints a formatted error message from rich
"""
console = Console()
console.print(error_message, style="bright_red")
return
def create_temp_plugin_folder() -> Path:
"""
Creates a temporary folder to store plugins inside
Returns full path of temporary folder
"""
path_temp_plugin_folder = Path("./TempSFTPFolder")
if os.path.isdir(path_temp_plugin_folder):
return path_temp_plugin_folder
try:
os.mkdir(path_temp_plugin_folder)
except OSError:
rich_print_error(f"Error: Creation of directory {path_temp_plugin_folder} failed")
rich_print_error("Please check for missing permissions in folder tree!")
input("Press any key + enter to exit...")
sys.exit()
return path_temp_plugin_folder
def remove_temp_plugin_folder() -> None:
"""
Removes the temporary plugin folder and all content inside it
"""
try:
shutil.rmtree(Path("./TempSFTPFolder"))
except OSError as e:
rich_print_error(f"Error: {e.filename} - {e.strerror}")
return