From 205a13ee94a61f0ef761ac26a898eeaa5b2ded8c Mon Sep 17 00:00:00 2001 From: Neocky Date: Sat, 27 Mar 2021 18:58:16 +0100 Subject: [PATCH] Optimized config handling & beautified output Changes: - optimized config handling - added spinner to paper download - changed progress bar colors - better output for updating paper version - misc code cleanup --- src/handlers/handle_config.py | 58 +++++++++++++++--------------- src/plugin/plugin_downloader.py | 3 +- src/plugin/plugin_updatechecker.py | 16 ++++----- src/serverjar/serverjar_checker.py | 2 +- src/serverjar/serverjar_paper.py | 14 ++++---- 5 files changed, 47 insertions(+), 46 deletions(-) diff --git a/src/handlers/handle_config.py b/src/handlers/handle_config.py index 16ed848..54baf55 100644 --- a/src/handlers/handle_config.py +++ b/src/handlers/handle_config.py @@ -7,49 +7,47 @@ from utils.consoleoutput import oColors class configurationValues: - config = configparser.ConfigParser() - config.sections() - config.read("config.ini") - localPluginFolder = config['General']['LocalPluginFolder'] - pathToPluginFolder = Path(config['General']['PathToPluginFolder']) - seperateDownloadPath = config['General']['SeperateDownloadPath'] - pathToSeperateDownloadPath = Path(config['General']['PathToSeperateDownloadPath']) + def __init__(self): + config = configparser.ConfigParser() + config.sections() + config.read("config.ini") + localPluginFolder = config['General']['LocalPluginFolder'] + self.pathToPluginFolder = Path(config['General']['PathToPluginFolder']) + seperateDownloadPath = config['General']['SeperateDownloadPath'] + self.pathToSeperateDownloadPath = Path(config['General']['PathToSeperateDownloadPath']) - sftp_server = config['SFTP - Remote Server']['Server'] - sftp_user = config['SFTP - Remote Server']['Username'] - sftp_password = config['SFTP - Remote Server']['Password'] - sftp_port = config['SFTP - Remote Server']['Port'] - sftp_folderPath = config['SFTP - Remote Server']['PluginFolderOnServer'] - sftp_seperateDownloadPath = config['SFTP - Remote Server']['SeperateDownloadPath'] - sftp_pathToSeperateDownloadPath = config['SFTP - Remote Server']['PathToSeperateDownloadPath'] + self.sftp_server = config['SFTP - Remote Server']['Server'] + self.sftp_user = config['SFTP - Remote Server']['Username'] + self.sftp_password = config['SFTP - Remote Server']['Password'] + sftp_port = config['SFTP - Remote Server']['Port'] + self.sftp_folderPath = config['SFTP - Remote Server']['PluginFolderOnServer'] + sftp_seperateDownloadPath = config['SFTP - Remote Server']['SeperateDownloadPath'] + self.sftp_pathToSeperateDownloadPath = config['SFTP - Remote Server']['PathToSeperateDownloadPath'] - sftp_port = int(sftp_port) - if localPluginFolder == 'True': - localPluginFolder = True - else: - localPluginFolder = False + self.sftp_port = int(sftp_port) + if localPluginFolder == 'True': + self.localPluginFolder = True + else: + self.localPluginFolder = False - if seperateDownloadPath == 'True': - seperateDownloadPath = True - else: - seperateDownloadPath = False + if seperateDownloadPath == 'True': + self.seperateDownloadPath = True + else: + self.seperateDownloadPath = False - if sftp_seperateDownloadPath == 'True': - sftp_seperateDownloadPath = True - else: - sftp_seperateDownloadPath = False + if sftp_seperateDownloadPath == 'True': + self.sftp_seperateDownloadPath = True + else: + self.sftp_seperateDownloadPath = False def checkConfig(): - #currentFolder = os.getcwd() configAvailable = os.path.isfile("config.ini") - if not configAvailable: createConfig() print(oColors.brightRed + "Config created. Edit config before executing again!" + oColors.standardWhite) input("Press any key + enter to exit...") sys.exit() - #os.chdir(currentFolder) def createConfig(): diff --git a/src/plugin/plugin_downloader.py b/src/plugin/plugin_downloader.py index e28a4eb..15c6f6c 100644 --- a/src/plugin/plugin_downloader.py +++ b/src/plugin/plugin_downloader.py @@ -85,6 +85,7 @@ def searchPackage(ressourceName): def downloadSpecificVersion(ressourceId, downloadPath, versionID='latest'): + configValues = configurationValues() if versionID != 'latest': #url = f"https://spigotmc.org/resources/{ressourceId}/download?version={versionID}" print(oColors.brightRed + "Sorry but specific version downloads aren't supported because of cloudflare protection. :(" + oColors.standardWhite) @@ -104,7 +105,7 @@ def downloadSpecificVersion(ressourceId, downloadPath, versionID='latest'): else: filesizeData = calculateFileSizeKb(filesize) print("Downloaded " + (str(filesizeData)).rjust(9) + f" KB here {downloadPath}") - if not configurationValues.localPluginFolder: + if not configValues.localPluginFolder: sftpSession = createSFTPConnection() sftp_upload_file(sftpSession, downloadPath) diff --git a/src/plugin/plugin_updatechecker.py b/src/plugin/plugin_updatechecker.py index a02e949..df2eb6e 100644 --- a/src/plugin/plugin_updatechecker.py +++ b/src/plugin/plugin_updatechecker.py @@ -101,7 +101,7 @@ def checkInstalledPackage(inputSelectedObject="all"): print("│ No. │ Name │ Installed V. │ Latest V. │ Update available │") print("└─────┴────────────────────────────────┴──────────────┴──────────────┴───────────────────┘") try: - for plugin in track(pluginList, description="Checking for updates" ,transient=True, complete_style="cyan"): + for plugin in track(pluginList, description="Checking for updates" ,transient=True, complete_style="bright_yellow"): try: fileName = getFileName(plugin) fileVersion = getFileVersion(plugin) @@ -176,7 +176,7 @@ def updateInstalledPackage(inputSelectedObject='all'): print("│ No. │ Name │ Old V. │ New V. │") print("└─────┴────────────────────────────────┴────────────┴──────────┘") try: - for plugin in track(pluginList, description="Updating" ,transient=True, complete_style="red"): + for plugin in track(pluginList, description="Updating" ,transient=True, complete_style="bright_magenta"): try: fileName = getFileName(plugin) fileVersion = getFileVersion(plugin) @@ -197,9 +197,9 @@ def updateInstalledPackage(inputSelectedObject='all'): print(f" [{indexNumberUpdated+1}]".rjust(6), end='') print(" ", end='') print(f"{fileName}".ljust(33), end='') - print(f"{fileVersion}".ljust(8), end='') - print(" ", end='') - print(f"{latestVersion}".ljust(8)) + print(f"{fileVersion}".ljust(13), end='') + #print(" ", end='') + print(f"{latestVersion}".ljust(13)) if not configValues.localPluginFolder: if configValues.sftp_seperateDownloadPath is True: @@ -247,9 +247,9 @@ def updateInstalledPackage(inputSelectedObject='all'): print(f" [{indexNumberUpdated+1}]".rjust(6), end='') print(" ", end='') print(f"{fileName}".ljust(33), end='') - print(f"{fileVersion}".ljust(8), end='') - print(" ", end='') - print(f"{latestVersion}".ljust(8)) + print(f"{fileVersion}".ljust(13), end='') + #print(" ", end='') + print(f"{latestVersion}".ljust(13)) if not configValues.localPluginFolder: if configValues.sftp_seperateDownloadPath is True: diff --git a/src/serverjar/serverjar_checker.py b/src/serverjar/serverjar_checker.py index 0f6ab86..7e702b0 100644 --- a/src/serverjar/serverjar_checker.py +++ b/src/serverjar/serverjar_checker.py @@ -92,7 +92,7 @@ def updateServerjar(serverJarBuild='latest'): serverJarPath = Path(f"{serverRootPath}/{installedServerjarFullName}") if 'paper' in installedServerjarFullName: - print(f"Updating Paper to build: {serverJarBuild}") + print(oColors.brightBlack + f"Updating Paper to build: {serverJarBuild}" + oColors.standardWhite) if not configValues.localPluginFolder: try: papermc_downloader(serverJarBuild, installedServerjarFullName) diff --git a/src/serverjar/serverjar_paper.py b/src/serverjar/serverjar_paper.py index 67dbde3..f46f56a 100644 --- a/src/serverjar/serverjar_paper.py +++ b/src/serverjar/serverjar_paper.py @@ -3,6 +3,7 @@ import sys import re import urllib.request from pathlib import Path +from rich.console import Console from utils.consoleoutput import oColors from utils.web_request import doAPIRequest @@ -116,6 +117,7 @@ def paperCheckForUpdate(installedServerjarFullName): print(f"{paperInstalledBuild}".ljust(15), end='') print(f"{paperLatestBuild}".ljust(15), end='') print(f"{paperVersionBehind}".ljust(8)) + print(oColors.brightYellow + f"Versions behind: [{paperVersionBehind}]" + oColors.standardWhite) # https://papermc.io/api/docs/swagger-ui/index.html?configUrl=/api/openapi/swagger-config#/ @@ -156,16 +158,16 @@ def papermc_downloader(paperBuild='latest', installedServerjarName=None, mcVersi url = f"https://papermc.io/api/v2/projects/paper/versions/{mcVersion}/builds/{paperBuild}/downloads/{downloadFileName}" remotefile = urllib.request.urlopen(url) filesize = remotefile.info()['Content-Length'] - print(f"Starting Paper-{paperBuild} download for {mcVersion}...") - urllib.request.urlretrieve(url, downloadPackagePath) + print(f"Getting Paper {paperBuild} for {mcVersion}") + console = Console() + with console.status("Downloading...", spinner='line', spinner_style='bright_magenta') as status: + urllib.request.urlretrieve(url, downloadPackagePath) filesizeData = calculateFileSizeMb(filesize) - - print(f"Downloadsize: {filesizeData} MB") - print(f"File downloaded here: {downloadPackagePath}") + print("Downloaded " + (str(filesizeData)).rjust(9) + f" MB here {downloadPackagePath}") if not configValues.localPluginFolder: sftpSession = createSFTPConnection() sftp_upload_server_jar(sftpSession, downloadPackagePath) deleteTempPluginFolder(downloadPath) - print(oColors.brightGreen + "Downloaded successfully " + oColors.standardWhite + f"Paper-{paperBuild}" + \ + print(oColors.brightGreen + "Downloaded successfully " + oColors.standardWhite + f"Paper {paperBuild}" + \ oColors.brightGreen + " for " + oColors.standardWhite + f"{mcVersion}" + oColors.standardWhite)