From 993d438ff705ae0b1cf85a1a365a4195a2a6d06b Mon Sep 17 00:00:00 2001 From: Neocky Date: Thu, 2 Jun 2022 19:52:49 +0200 Subject: [PATCH] Started the new config handling Implemented new and not complete config handling and implemented command parameter support --- {src => old_src}/__init__.py | 0 {src => old_src}/__main__.py | 0 old_src/config.ini | 26 +++ {src => old_src}/handlers/__init__.py | 0 old_src/handlers/handle_config.py | 91 ++++++++++ {src => old_src}/handlers/handle_ftp.py | 0 {src => old_src}/handlers/handle_input.py | 0 {src => old_src}/handlers/handle_sftp.py | 0 {src => old_src}/plugin/__init__.py | 0 {src => old_src}/plugin/plugin_downloader.py | 0 {src => old_src}/plugin/plugin_remover.py | 0 .../plugin/plugin_updatechecker.py | 0 {src => old_src}/serverjar/__init__.py | 0 .../serverjar/serverjar_checker.py | 0 {src => old_src}/serverjar/serverjar_paper.py | 0 {src => old_src}/utils/__init__.py | 0 {src => old_src}/utils/consoleoutput.py | 0 {src => old_src}/utils/utilities.py | 0 {src => old_src}/utils/web_request.py | 0 pluGET.py | 33 ++++ requirements.txt | 3 +- src/handlers/handle_config.py | 155 +++++++++--------- 22 files changed, 230 insertions(+), 78 deletions(-) rename {src => old_src}/__init__.py (100%) rename {src => old_src}/__main__.py (100%) create mode 100644 old_src/config.ini rename {src => old_src}/handlers/__init__.py (100%) create mode 100644 old_src/handlers/handle_config.py rename {src => old_src}/handlers/handle_ftp.py (100%) rename {src => old_src}/handlers/handle_input.py (100%) rename {src => old_src}/handlers/handle_sftp.py (100%) rename {src => old_src}/plugin/__init__.py (100%) rename {src => old_src}/plugin/plugin_downloader.py (100%) rename {src => old_src}/plugin/plugin_remover.py (100%) rename {src => old_src}/plugin/plugin_updatechecker.py (100%) rename {src => old_src}/serverjar/__init__.py (100%) rename {src => old_src}/serverjar/serverjar_checker.py (100%) rename {src => old_src}/serverjar/serverjar_paper.py (100%) rename {src => old_src}/utils/__init__.py (100%) rename {src => old_src}/utils/consoleoutput.py (100%) rename {src => old_src}/utils/utilities.py (100%) rename {src => old_src}/utils/web_request.py (100%) create mode 100644 pluGET.py diff --git a/src/__init__.py b/old_src/__init__.py similarity index 100% rename from src/__init__.py rename to old_src/__init__.py diff --git a/src/__main__.py b/old_src/__main__.py similarity index 100% rename from src/__main__.py rename to old_src/__main__.py diff --git a/old_src/config.ini b/old_src/config.ini new file mode 100644 index 0000000..3446c0b --- /dev/null +++ b/old_src/config.ini @@ -0,0 +1,26 @@ +[General] +; = If a local plugin folder exists (True/False) (If False SFTP/FTP will be used): +uselocalpluginfolder = True + +[Local - This Machine] +pathtopluginfolder = C:/Users/USER/Desktop/plugins +; = For a different folder to store the updated plugins change to (True/False) and the path below +seperatedownloadpath = False +pathtoseperatedownloadpath = C:/Users/USER/Desktop/plugins + +[SFTP - Remote Server] +server = 0.0.0.0 +username = user +password = password +; = If a different Port for SFTP needs to be used (Default: 22) +sftpport = 22 +;_ = If a different Port for FTP needs to be used (Default: 21) +ftpport = 21 +;__ = Change the path below if the plugin folder path is different on the SFTP/FTP server (Change only if you know what you are doing) +pluginfolderonserver = /plugins +;___ = If you want to use FTP instead of SFTP change to (False) else use (True) +use_sftp = True +;____ = For a different folder to store the updated plugins (Only with the update command!) change to (True/False) and the path below +seperatedownloadpath = False +pathtoseperatedownloadpath = /plugins + diff --git a/src/handlers/__init__.py b/old_src/handlers/__init__.py similarity index 100% rename from src/handlers/__init__.py rename to old_src/handlers/__init__.py diff --git a/old_src/handlers/handle_config.py b/old_src/handlers/handle_config.py new file mode 100644 index 0000000..b9a959b --- /dev/null +++ b/old_src/handlers/handle_config.py @@ -0,0 +1,91 @@ +import os +import sys +import configparser +from pathlib import Path + +from utils.consoleoutput import oColors + + +class configurationValues: + def __init__(self): + config = configparser.ConfigParser() + config.sections() + config.read("config.ini") + localPluginFolder = config['General']['UseLocalPluginFolder'] + self.pathToPluginFolder = Path(config['Local - This Machine']['PathToPluginFolder']) + seperateDownloadPath = config['Local - This Machine']['SeperateDownloadPath'] + self.pathToSeperateDownloadPath = Path(config['Local - This Machine']['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']['SFTPPort'] + ftp_port = config['SFTP - Remote Server']['FTPPort'] + self.sftp_folderPath = config['SFTP - Remote Server']['PluginFolderOnServer'] + sftp_useSftp = config['SFTP - Remote Server']['USE_SFTP'] + sftp_seperateDownloadPath = config['SFTP - Remote Server']['SeperateDownloadPath'] + self.sftp_pathToSeperateDownloadPath = config['SFTP - Remote Server']['PathToSeperateDownloadPath'] + + self.sftp_port = int(sftp_port) + self.ftp_port = int(ftp_port) + if localPluginFolder == 'True': + self.localPluginFolder = True + else: + self.localPluginFolder = False + + if seperateDownloadPath == 'True': + self.seperateDownloadPath = True + else: + self.seperateDownloadPath = False + + if sftp_seperateDownloadPath == 'True': + self.sftp_seperateDownloadPath = True + else: + self.sftp_seperateDownloadPath = False + + if sftp_useSftp == 'True': + self.sftp_useSftp = True + else: + self.sftp_useSftp = False + + +def checkConfig(): + 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() + + +def createConfig(): + config = configparser.ConfigParser(allow_no_value=True) + config['General'] = {} + config['General'][';'] = 'If a local plugin folder exists (True/False) (If False SFTP/FTP will be used):' + config['General']['UseLocalPluginFolder'] = 'True' + + config['Local - This Machine'] = {} + config['Local - This Machine']['PathToPluginFolder'] = 'C:/Users/USER/Desktop/plugins' + config['Local - This Machine'][';'] = 'For a different folder to store the updated plugins change to (True/False) and the path below' + config['Local - This Machine']['SeperateDownloadPath'] = 'False' + config['Local - This Machine']['PathToSeperateDownloadPath'] = 'C:/Users/USER/Desktop/plugins' + + config['SFTP - Remote Server'] = {} + config['SFTP - Remote Server']['Server'] = '0.0.0.0' + config['SFTP - Remote Server']['Username'] = 'user' + config['SFTP - Remote Server']['Password'] = 'password' + config['SFTP - Remote Server'][';'] = 'If a different Port for SFTP needs to be used (Default: 22)' + config['SFTP - Remote Server']['SFTPPort'] = '22' + config['SFTP - Remote Server'][';_'] = 'If a different Port for FTP needs to be used (Default: 21)' + config['SFTP - Remote Server']['FTPPort'] = '21' + config['SFTP - Remote Server'][';__'] = 'Change the path below if the plugin folder path is different on the SFTP/FTP server (Change only if you know what you are doing)' + config['SFTP - Remote Server']['PluginFolderOnServer'] = '/plugins' + config['SFTP - Remote Server'][';___'] = 'If you want to use FTP instead of SFTP change to (False) else use (True)' + config['SFTP - Remote Server']['USE_SFTP'] = 'True' + config['SFTP - Remote Server'][';____'] = 'For a different folder to store the updated plugins (Only with the update command!) change to (True/False) and the path below' + config['SFTP - Remote Server']['SeperateDownloadPath'] = 'False' + config['SFTP - Remote Server']['PathToSeperateDownloadPath'] = '/plugins' + + + with open('config.ini', 'w') as configfile: + config.write(configfile) diff --git a/src/handlers/handle_ftp.py b/old_src/handlers/handle_ftp.py similarity index 100% rename from src/handlers/handle_ftp.py rename to old_src/handlers/handle_ftp.py diff --git a/src/handlers/handle_input.py b/old_src/handlers/handle_input.py similarity index 100% rename from src/handlers/handle_input.py rename to old_src/handlers/handle_input.py diff --git a/src/handlers/handle_sftp.py b/old_src/handlers/handle_sftp.py similarity index 100% rename from src/handlers/handle_sftp.py rename to old_src/handlers/handle_sftp.py diff --git a/src/plugin/__init__.py b/old_src/plugin/__init__.py similarity index 100% rename from src/plugin/__init__.py rename to old_src/plugin/__init__.py diff --git a/src/plugin/plugin_downloader.py b/old_src/plugin/plugin_downloader.py similarity index 100% rename from src/plugin/plugin_downloader.py rename to old_src/plugin/plugin_downloader.py diff --git a/src/plugin/plugin_remover.py b/old_src/plugin/plugin_remover.py similarity index 100% rename from src/plugin/plugin_remover.py rename to old_src/plugin/plugin_remover.py diff --git a/src/plugin/plugin_updatechecker.py b/old_src/plugin/plugin_updatechecker.py similarity index 100% rename from src/plugin/plugin_updatechecker.py rename to old_src/plugin/plugin_updatechecker.py diff --git a/src/serverjar/__init__.py b/old_src/serverjar/__init__.py similarity index 100% rename from src/serverjar/__init__.py rename to old_src/serverjar/__init__.py diff --git a/src/serverjar/serverjar_checker.py b/old_src/serverjar/serverjar_checker.py similarity index 100% rename from src/serverjar/serverjar_checker.py rename to old_src/serverjar/serverjar_checker.py diff --git a/src/serverjar/serverjar_paper.py b/old_src/serverjar/serverjar_paper.py similarity index 100% rename from src/serverjar/serverjar_paper.py rename to old_src/serverjar/serverjar_paper.py diff --git a/src/utils/__init__.py b/old_src/utils/__init__.py similarity index 100% rename from src/utils/__init__.py rename to old_src/utils/__init__.py diff --git a/src/utils/consoleoutput.py b/old_src/utils/consoleoutput.py similarity index 100% rename from src/utils/consoleoutput.py rename to old_src/utils/consoleoutput.py diff --git a/src/utils/utilities.py b/old_src/utils/utilities.py similarity index 100% rename from src/utils/utilities.py rename to old_src/utils/utilities.py diff --git a/src/utils/web_request.py b/old_src/utils/web_request.py similarity index 100% rename from src/utils/web_request.py rename to old_src/utils/web_request.py diff --git a/pluGET.py b/pluGET.py new file mode 100644 index 0000000..527d18a --- /dev/null +++ b/pluGET.py @@ -0,0 +1,33 @@ +# Main function for pluGET +import argparse + +from src.handlers.handle_config import check_config, config_value + + +if __name__ == "__main__": + check_config() + #check_requirements() + parser = argparse.ArgumentParser(description="Just an example", + formatter_class=argparse.ArgumentDefaultsHelpFormatter) + #parser.add_argument("-a", "--archive", action="store_true", help="archive mode") + #parser.add_argument("--exclude", help="files to exclude") + parser.add_argument("mode", help="Mode (install/update)", nargs='?', default=None) + 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") + #consoleTitle() + #clearConsole() + #createInputLists() + #printMainMenu() + #getInput() + config = config_value() + print(config.connection) + print(config.path_to_plugin_folder) + print(config.sftp_port) + print(config.local_seperate_download_path) + diff --git a/requirements.txt b/requirements.txt index 9aa4868..670dafa 100644 --- a/requirements.txt +++ b/requirements.txt @@ -11,4 +11,5 @@ pysftp >= 0.2.9 rich >= 9.13.0 commonmark >= 0.9.1 Pygments >= 2.8.1 -typing_extensions >= 3.7.4.3 \ No newline at end of file +typing_extensions >= 3.7.4.3 +ruamel.yaml >= 0.17.21 \ No newline at end of file diff --git a/src/handlers/handle_config.py b/src/handlers/handle_config.py index b9a959b..7e1f2e7 100644 --- a/src/handlers/handle_config.py +++ b/src/handlers/handle_config.py @@ -1,91 +1,92 @@ +"""" + Handles the logic for the config validation, reading and creating +""" + import os import sys -import configparser +import ruamel.yaml from pathlib import Path -from utils.consoleoutput import oColors -class configurationValues: - def __init__(self): - config = configparser.ConfigParser() - config.sections() - config.read("config.ini") - localPluginFolder = config['General']['UseLocalPluginFolder'] - self.pathToPluginFolder = Path(config['Local - This Machine']['PathToPluginFolder']) - seperateDownloadPath = config['Local - This Machine']['SeperateDownloadPath'] - self.pathToSeperateDownloadPath = Path(config['Local - This Machine']['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']['SFTPPort'] - ftp_port = config['SFTP - Remote Server']['FTPPort'] - self.sftp_folderPath = config['SFTP - Remote Server']['PluginFolderOnServer'] - sftp_useSftp = config['SFTP - Remote Server']['USE_SFTP'] - sftp_seperateDownloadPath = config['SFTP - Remote Server']['SeperateDownloadPath'] - self.sftp_pathToSeperateDownloadPath = config['SFTP - Remote Server']['PathToSeperateDownloadPath'] - - self.sftp_port = int(sftp_port) - self.ftp_port = int(ftp_port) - if localPluginFolder == 'True': - self.localPluginFolder = True - else: - self.localPluginFolder = False - - if seperateDownloadPath == 'True': - self.seperateDownloadPath = True - else: - self.seperateDownloadPath = False - - if sftp_seperateDownloadPath == 'True': - self.sftp_seperateDownloadPath = True - else: - self.sftp_seperateDownloadPath = False - - if sftp_useSftp == 'True': - self.sftp_useSftp = True - else: - self.sftp_useSftp = False +class config_value(): + """ + Class which holds all the available configuration values from the config file and which will be used later in + the process of updating plugins + """ + def __init__(self): + yaml = ruamel.yaml.YAML() + with open("pluget_config.yaml", "r") as config_file: + data = yaml.load(config_file) + self.connection = data["Connection"] + self.path_to_plugin_folder = Path(data["Local"]["PathToPluginFolder"]) + self.local_seperate_download_path = True if data["Local"]["SeperateDownloadPath"] == True else False + self.path_to_seperate_download_path = Path(data["Local"]["PathToSeperateDownloadPath"]) + self.sftp_port = int(data["Remote"]["SFTP_Port"]) -def checkConfig(): - 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() + +def check_config() -> None: + """ + Check if there is a pluget_config.yml file in the same folder as pluget.py and if not create a new config + and exit the programm. + """ + if not os.path.isfile("pluget_config.yaml"): + create_config() -def createConfig(): - config = configparser.ConfigParser(allow_no_value=True) - config['General'] = {} - config['General'][';'] = 'If a local plugin folder exists (True/False) (If False SFTP/FTP will be used):' - config['General']['UseLocalPluginFolder'] = 'True' +def create_config() -> None: + """ + Creates the yaml config in the current directory with the filename pluget_config.yml + """ + # this is the whole yaml code because of weird formating indention is not possible + configuration = """\ +# +# Configuration File for pluGET +# https://www.github.com/Neocky/pluGET +# - config['Local - This Machine'] = {} - config['Local - This Machine']['PathToPluginFolder'] = 'C:/Users/USER/Desktop/plugins' - config['Local - This Machine'][';'] = 'For a different folder to store the updated plugins change to (True/False) and the path below' - config['Local - This Machine']['SeperateDownloadPath'] = 'False' - config['Local - This Machine']['PathToSeperateDownloadPath'] = 'C:/Users/USER/Desktop/plugins' +# What should be used for the connection (local, sftp, ftp) + Connection: local - config['SFTP - Remote Server'] = {} - config['SFTP - Remote Server']['Server'] = '0.0.0.0' - config['SFTP - Remote Server']['Username'] = 'user' - config['SFTP - Remote Server']['Password'] = 'password' - config['SFTP - Remote Server'][';'] = 'If a different Port for SFTP needs to be used (Default: 22)' - config['SFTP - Remote Server']['SFTPPort'] = '22' - config['SFTP - Remote Server'][';_'] = 'If a different Port for FTP needs to be used (Default: 21)' - config['SFTP - Remote Server']['FTPPort'] = '21' - config['SFTP - Remote Server'][';__'] = 'Change the path below if the plugin folder path is different on the SFTP/FTP server (Change only if you know what you are doing)' - config['SFTP - Remote Server']['PluginFolderOnServer'] = '/plugins' - config['SFTP - Remote Server'][';___'] = 'If you want to use FTP instead of SFTP change to (False) else use (True)' - config['SFTP - Remote Server']['USE_SFTP'] = 'True' - config['SFTP - Remote Server'][';____'] = 'For a different folder to store the updated plugins (Only with the update command!) change to (True/False) and the path below' - config['SFTP - Remote Server']['SeperateDownloadPath'] = 'False' - config['SFTP - Remote Server']['PathToSeperateDownloadPath'] = '/plugins' + Local: + PathToPluginFolder: C:/Users/USER/Desktop/plugins + # If a different folder should be used to store the updated plugins change to (True/False) and the path below + SeperateDownloadPath : False + PathToSeperateDownloadPath: C:/Users/USER/Desktop/plugins + + Remote: + Server: 0.0.0.0 + Username: user + Password: password + # If a different Port for SFTP/FTP will be used + SFTP_Port: 22 + FTP_Port: 21 + # If a different folder should be used to store the updated plugins change to (True/False) and the path below + SeperateDownloadPath : False + PathToSeperateDownloadPath: /plugins/updated + # Change the path below if the plugin folder path is different on the SFTP/FTP server (Change only if you know what you are doing) + PluginFolderOnServer: /plugins + """ + # load ruamel.yaml to get the # commands right in the yaml code + yaml = ruamel.yaml.YAML() + code = yaml.load(configuration) + print(code['Local']['SeperateDownloadPath']) + with open("pluget_config.yaml", "w") as config_file: + yaml.dump(code, config_file) + + config_file_path = os.path.abspath("pluget_config.yaml") + print(f"Path of config file: {config_file_path}") + print("Config created. Edit config before executing again!") + input("Press any key + enter to exit...") + sys.exit() - with open('config.ini', 'w') as configfile: - config.write(configfile) +def validate_config() -> bool: + """ + Validates the config variables after config class is loaded + """ + accepted_values = [ + ("local", "sftp", "ftp"), + ("true", "false") + ]