Started the new config handling

Implemented new and not complete config handling and implemented command parameter support
This commit is contained in:
Neocky 2022-06-02 19:52:49 +02:00
parent 3faf9785d7
commit 993d438ff7
22 changed files with 230 additions and 78 deletions

26
old_src/config.ini Normal file
View File

@ -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

View File

@ -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)

33
pluGET.py Normal file
View File

@ -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)

View File

@ -12,3 +12,4 @@ rich >= 9.13.0
commonmark >= 0.9.1
Pygments >= 2.8.1
typing_extensions >= 3.7.4.3
ruamel.yaml >= 0.17.21

View File

@ -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")
]