Better output & first part of new config handling

Changes:
- better output when downloading files
- optimization when getting values from the config
This commit is contained in:
Neocky 2021-03-26 20:24:15 +01:00
parent e8e46bb014
commit 17611c741e
5 changed files with 133 additions and 87 deletions

View File

@ -1,14 +1,14 @@
from utils.consoleoutput import consoleTitle, clearConsole, printMainMenu from utils.consoleoutput import consoleTitle, clearConsole, printMainMenu
from utils.utilities import check_requirements from utils.utilities import check_requirements
from handlers.handle_input import createInputLists, getInput from handlers.handle_input import createInputLists, getInput
from handlers.handle_config import checkConfig from handlers.handle_config import checkConfig, configurationValues
def mainFunction(): def mainFunction():
consoleTitle() consoleTitle()
clearConsole() clearConsole()
checkConfig() #checkConfig()
check_requirements() #check_requirements()
createInputLists() createInputLists()
printMainMenu() printMainMenu()
getInput() getInput()

View File

@ -6,6 +6,42 @@ from pathlib import Path
from utils.consoleoutput import oColors from utils.consoleoutput import oColors
class configurationValues:
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'])
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']
self.sftp_port = int(sftp_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
def checkConfig(): def checkConfig():
currentFolder = os.getcwd() currentFolder = os.getcwd()
configAvailable = os.path.isfile("config.ini") configAvailable = os.path.isfile("config.ini")
@ -16,41 +52,41 @@ def checkConfig():
input("Press any key + enter to exit...") input("Press any key + enter to exit...")
sys.exit() sys.exit()
class configValues: #class configValues:
config = configparser.ConfigParser() # config = configparser.ConfigParser()
config.sections() # config.sections()
config.read("config.ini") # config.read("config.ini")
localPluginFolder = config['General']['LocalPluginFolder'] # localPluginFolder = config['General']['LocalPluginFolder']
pathToPluginFolder = Path(config['General']['PathToPluginFolder']) # pathToPluginFolder = Path(config['General']['PathToPluginFolder'])
seperateDownloadPath = config['General']['SeperateDownloadPath'] # seperateDownloadPath = config['General']['SeperateDownloadPath']
pathToSeperateDownloadPath = Path(config['General']['PathToSeperateDownloadPath']) # pathToSeperateDownloadPath = Path(config['General']['PathToSeperateDownloadPath'])
sftp_server = config['SFTP - Remote Server']['Server'] # sftp_server = config['SFTP - Remote Server']['Server']
sftp_user = config['SFTP - Remote Server']['Username'] # sftp_user = config['SFTP - Remote Server']['Username']
sftp_password = config['SFTP - Remote Server']['Password'] # sftp_password = config['SFTP - Remote Server']['Password']
sftp_port = config['SFTP - Remote Server']['Port'] # sftp_port = config['SFTP - Remote Server']['Port']
sftp_folderPath = config['SFTP - Remote Server']['PluginFolderOnServer'] # sftp_folderPath = config['SFTP - Remote Server']['PluginFolderOnServer']
sftp_seperateDownloadPath = config['SFTP - Remote Server']['SeperateDownloadPath'] # sftp_seperateDownloadPath = config['SFTP - Remote Server']['SeperateDownloadPath']
sftp_pathToSeperateDownloadPath = config['SFTP - Remote Server']['PathToSeperateDownloadPath'] # sftp_pathToSeperateDownloadPath = config['SFTP - Remote Server']['PathToSeperateDownloadPath']
sftp_port = int(sftp_port) # sftp_port = int(sftp_port)
if localPluginFolder == 'True': # if localPluginFolder == 'True':
localPluginFolder = True # localPluginFolder = True
else: # else:
localPluginFolder = False # localPluginFolder = False
if seperateDownloadPath == 'True': # if seperateDownloadPath == 'True':
seperateDownloadPath = True # seperateDownloadPath = True
else: # else:
seperateDownloadPath = False # seperateDownloadPath = False
if sftp_seperateDownloadPath == 'True': # if sftp_seperateDownloadPath == 'True':
sftp_seperateDownloadPath = True # sftp_seperateDownloadPath = True
else: # else:
sftp_seperateDownloadPath = False # sftp_seperateDownloadPath = False
os.chdir(currentFolder) os.chdir(currentFolder)
return configValues #return configValues
def createConfig(): def createConfig():

View File

@ -5,18 +5,11 @@ from pathlib import Path
from utils.consoleoutput import oColors from utils.consoleoutput import oColors
from utils.web_request import doAPIRequest from utils.web_request import doAPIRequest
from utils.utilities import createTempPluginFolder, deleteTempPluginFolder from utils.utilities import createTempPluginFolder, deleteTempPluginFolder, calculateFileSizeKb, calculateFileSizeMb
from handlers.handle_config import checkConfig from handlers.handle_config import checkConfig, configurationValues
from handlers.handle_sftp import sftp_upload_file, sftp_cdPluginDir, createSFTPConnection from handlers.handle_sftp import sftp_upload_file, sftp_cdPluginDir, createSFTPConnection
def calculateFileSize(downloadFileSize):
fileSizeDownload = int(downloadFileSize)
fileSizeKb = fileSizeDownload / 1024
roundedFileSize = round(fileSizeKb, 2)
return roundedFileSize
def handleRegexPackageName(packageNameFull): def handleRegexPackageName(packageNameFull):
packageNameFull2 = packageNameFull packageNameFull2 = packageNameFull
# trims the part of the package that has for example "[1.1 Off]" in it # trims the part of the package that has for example "[1.1 Off]" in it
@ -102,10 +95,15 @@ def downloadSpecificVersion(ressourceId, downloadPath, versionID='latest'):
remotefile = urllib.request.urlopen(url) remotefile = urllib.request.urlopen(url)
filesize = remotefile.info()['Content-Length'] filesize = remotefile.info()['Content-Length']
urllib.request.urlretrieve(url, downloadPath) urllib.request.urlretrieve(url, downloadPath)
filesizeData = calculateFileSize(filesize) filesize = int(filesize)
print(f"Downloadsize: {filesizeData} KB") print(" ", end='')
print(f"File downloaded here: {downloadPath}") if filesize >= 1000000:
if not checkConfig().localPluginFolder: filesizeData = calculateFileSizeMb(filesize)
print("Downloaded " + (str(filesizeData)).rjust(9) + f" MB here {downloadPath}")
else:
filesizeData = calculateFileSizeKb(filesize)
print("Downloaded " + (str(filesizeData)).rjust(9) + f" KB here {downloadPath}")
if not configurationValues.localPluginFolder:
sftpSession = createSFTPConnection() sftpSession = createSFTPConnection()
sftp_upload_file(sftpSession, downloadPath) sftp_upload_file(sftpSession, downloadPath)

View File

@ -8,7 +8,7 @@ from rich.progress import track
from utils.consoleoutput import oColors from utils.consoleoutput import oColors
from utils.web_request import doAPIRequest from utils.web_request import doAPIRequest
from handlers.handle_config import checkConfig from handlers.handle_config import checkConfig, configurationValues
from handlers.handle_sftp import createSFTPConnection, sftp_listAll from handlers.handle_sftp import createSFTPConnection, sftp_listAll
from plugin.plugin_downloader import getSpecificPackage from plugin.plugin_downloader import getSpecificPackage
@ -86,18 +86,19 @@ def eggCrackingJar(localJarFileName):
def checkInstalledPackage(inputSelectedObject="all"): def checkInstalledPackage(inputSelectedObject="all"):
configValues = configurationValues()
createPluginList() createPluginList()
if not checkConfig().localPluginFolder: if not configValues.localPluginFolder:
sftp = createSFTPConnection() sftp = createSFTPConnection()
pluginList = sftp_listAll(sftp) pluginList = sftp_listAll(sftp)
else: else:
pluginList = os.listdir(checkConfig().pathToPluginFolder) pluginList = os.listdir(configValues.pathToPluginFolder)
i = 0 i = 0
oldPackages = 0 oldPlugins = 0
print(oColors.brightBlack + f"Checking: {inputSelectedObject}" + oColors.standardWhite) print(oColors.brightBlack + f"Checking: {inputSelectedObject}" + oColors.standardWhite)
print("┌─────┬────────────────────────────────┬──────────────┬───────────┬───────────────────┐") print("┌─────┬────────────────────────────────┬──────────────┬──────────────┬───────────────────┐")
print("│ No. │ Name │ Installed V. │ Latest V. │ Update available │") print("│ No. │ Name │ Installed V. │ Latest V. │ Update available │")
print("└─────┴────────────────────────────────┴──────────────┴───────────┴───────────────────┘") print("└─────┴────────────────────────────────┴──────────────┴──────────────┴───────────────────┘")
try: 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="cyan"):
try: try:
@ -127,42 +128,45 @@ def checkInstalledPackage(inputSelectedObject="all"):
pluginIsOutdated = 'N/A' pluginIsOutdated = 'N/A'
if pluginIsOutdated == True: if pluginIsOutdated == True:
oldPackages = oldPackages + 1 oldPlugins = oldPlugins + 1
if inputSelectedObject != "*" and inputSelectedObject != "all": if inputSelectedObject != "*" and inputSelectedObject != "all":
if inputSelectedObject == pluginIdStr or re.search(inputSelectedObject, fileName, re.IGNORECASE): if inputSelectedObject == pluginIdStr or re.search(inputSelectedObject, fileName, re.IGNORECASE):
if pluginLatestVersion == 'N/A': if pluginLatestVersion == 'N/A':
print(oColors.brightBlack + f" [{1}]".ljust(8), end='') print(oColors.brightBlack + f" [{1}]".rjust(6), end='')
else: else:
print(f" [{1}]".ljust(8), end='') print(f" [{1}]".rjust(6), end='')
print(" ", end='')
print(f"{fileName}".ljust(33), end='') print(f"{fileName}".ljust(33), end='')
print(f"{fileVersion}".ljust(15), end='') print(f"{fileVersion}".ljust(15), end='')
print(f"{pluginLatestVersion}".ljust(12), end='') print(f"{pluginLatestVersion}".ljust(14), end='')
print(f" {pluginIsOutdated}".ljust(5) + oColors.standardWhite) print(f" {pluginIsOutdated}".ljust(5) + oColors.standardWhite)
break break
else: else:
if pluginLatestVersion == 'N/A': if pluginLatestVersion == 'N/A':
print(oColors.brightBlack + f" [{i+1}]".ljust(8), end='') print(oColors.brightBlack + f" [{i+1}]".rjust(6), end='')
else: else:
print(f" [{i+1}]".ljust(8), end='') print(f" [{i+1}]".rjust(6), end='')
print(" ", end='')
print(f"{fileName}".ljust(33), end='') print(f"{fileName}".ljust(33), end='')
print(f"{fileVersion}".ljust(15), end='') print(f"{fileVersion}".ljust(15), end='')
print(f"{pluginLatestVersion}".ljust(12), end='') print(f"{pluginLatestVersion}".ljust(14), end='')
print(f" {pluginIsOutdated}".ljust(5) + oColors.standardWhite) print(f" {pluginIsOutdated}".ljust(5) + oColors.standardWhite)
i += 1 i += 1
except TypeError: except TypeError:
print(oColors.brightRed + "Error occured: Aborted checking for updates." + oColors.standardWhite) print(oColors.brightRed + "Error occured: Aborted checking for updates." + oColors.standardWhite)
print(oColors.brightYellow + f"Old packages: [{oldPackages}/{i}]" + oColors.standardWhite) print(oColors.brightYellow + f"Outdated plugins: [{oldPlugins}/{i}]" + oColors.standardWhite)
def updateInstalledPackage(inputSelectedObject='all'): def updateInstalledPackage(inputSelectedObject='all'):
configValues = configurationValues()
createPluginList() createPluginList()
if not checkConfig().localPluginFolder: if not configValues.localPluginFolder:
sftp = createSFTPConnection() sftp = createSFTPConnection()
pluginList = sftp_listAll(sftp) pluginList = sftp_listAll(sftp)
else: else:
pluginList = os.listdir(checkConfig().pathToPluginFolder) pluginList = os.listdir(configValues.pathToPluginFolder)
i = 0 i = 0
pluginsUpdated = 0 pluginsUpdated = 0
indexNumberUpdated = 0 indexNumberUpdated = 0
@ -189,24 +193,25 @@ def updateInstalledPackage(inputSelectedObject='all'):
continue continue
if inputSelectedObject == pluginIdStr or re.search(inputSelectedObject, fileName, re.IGNORECASE): if inputSelectedObject == pluginIdStr or re.search(inputSelectedObject, fileName, re.IGNORECASE):
if INSTALLEDPLUGINLIST[i][3] == True: if INSTALLEDPLUGINLIST[i][3] == True:
print(f" [{indexNumberUpdated+1}]".ljust(8), end='') print(f" [{indexNumberUpdated+1}]".rjust(6), end='')
print(" ", end='')
print(f"{fileName}".ljust(33), end='') print(f"{fileName}".ljust(33), end='')
print(f"{fileVersion}".ljust(8), end='') print(f"{fileVersion}".ljust(8), end='')
print(" ", end='') print(" ", end='')
print(f"{latestVersion}".ljust(8)) print(f"{latestVersion}".ljust(8))
if not checkConfig().localPluginFolder: if not configValues.localPluginFolder:
if checkConfig().sftp_seperateDownloadPath is True: if configValues.sftp_seperateDownloadPath is True:
pluginPath = checkConfig().sftp_pathToSeperateDownloadPath pluginPath = configValues.sftp_pathToSeperateDownloadPath
else: else:
pluginPath = checkConfig().sftp_folderPath pluginPath = configValues.sftp_folderPath
pluginPath = Path(f"{pluginPath}/{plugin}") pluginPath = Path(f"{pluginPath}/{plugin}")
sftp = createSFTPConnection() sftp = createSFTPConnection()
indexNumberUpdated += 1 indexNumberUpdated += 1
pluginsUpdated += 1 pluginsUpdated += 1
try: try:
getSpecificPackage(pluginId, checkConfig().sftp_folderPath) getSpecificPackage(pluginId, configValues.sftp_folderPath)
if checkConfig().sftp_seperateDownloadPath is False: if configValues.sftp_seperateDownloadPath is False:
sftp.remove(pluginPath) sftp.remove(pluginPath)
except HTTPError as err: except HTTPError as err:
print(oColors.brightRed + f"Error: {err.code} - {err.reason}" + oColors.standardWhite) print(oColors.brightRed + f"Error: {err.code} - {err.reason}" + oColors.standardWhite)
@ -214,16 +219,16 @@ def updateInstalledPackage(inputSelectedObject='all'):
except FileNotFoundError: except FileNotFoundError:
print(oColors.brightRed + "Error: Old plugin file coulnd't be deleted" + oColors.standardWhite) print(oColors.brightRed + "Error: Old plugin file coulnd't be deleted" + oColors.standardWhite)
else: else:
if checkConfig().seperateDownloadPath is True: if configValues.seperateDownloadPath is True:
pluginPath = checkConfig().pathToSeperateDownloadPath pluginPath = configValues.pathToSeperateDownloadPath
else: else:
pluginPath = checkConfig().pathToPluginFolder pluginPath = configValues.pathToPluginFolder
pluginPath = Path(f"{pluginPath}/{plugin}") pluginPath = Path(f"{pluginPath}/{plugin}")
indexNumberUpdated += 1 indexNumberUpdated += 1
pluginsUpdated += 1 pluginsUpdated += 1
try: try:
getSpecificPackage(pluginId, checkConfig().pathToPluginFolder) getSpecificPackage(pluginId, configValues.pathToPluginFolder)
if checkConfig().seperateDownloadPath is False: if configValues.seperateDownloadPath is False:
os.remove(pluginPath) os.remove(pluginPath)
except HTTPError as err: except HTTPError as err:
print(oColors.brightRed + f"Error: {err.code} - {err.reason}" + oColors.standardWhite) print(oColors.brightRed + f"Error: {err.code} - {err.reason}" + oColors.standardWhite)
@ -238,24 +243,25 @@ def updateInstalledPackage(inputSelectedObject='all'):
if inputSelectedObject == 'all': if inputSelectedObject == 'all':
if INSTALLEDPLUGINLIST[i][3] == True: if INSTALLEDPLUGINLIST[i][3] == True:
print(f" [{indexNumberUpdated+1}]".ljust(8), end='') print(f" [{indexNumberUpdated+1}]".rjust(6), end='')
print(" ", end='')
print(f"{fileName}".ljust(33), end='') print(f"{fileName}".ljust(33), end='')
print(f"{fileVersion}".ljust(8), end='') print(f"{fileVersion}".ljust(8), end='')
print(" ", end='') print(" ", end='')
print(f"{latestVersion}".ljust(8)) print(f"{latestVersion}".ljust(8))
if not checkConfig().localPluginFolder: if not configValues.localPluginFolder:
if checkConfig().sftp_seperateDownloadPath is True: if configValues.sftp_seperateDownloadPath is True:
pluginPath = checkConfig().sftp_pathToSeperateDownloadPath pluginPath = configValues.sftp_pathToSeperateDownloadPath
else: else:
pluginPath = checkConfig().sftp_folderPath pluginPath = configValues.sftp_folderPath
pluginPath = f"{pluginPath}/{plugin}" pluginPath = f"{pluginPath}/{plugin}"
sftp = createSFTPConnection() sftp = createSFTPConnection()
indexNumberUpdated += 1 indexNumberUpdated += 1
pluginsUpdated += 1 pluginsUpdated += 1
try: try:
getSpecificPackage(pluginId, checkConfig().sftp_folderPath) getSpecificPackage(pluginId, configValues.sftp_folderPath)
if checkConfig().sftp_seperateDownloadPath is False: if configValues.sftp_seperateDownloadPath is False:
sftp.remove(pluginPath) sftp.remove(pluginPath)
except HTTPError as err: except HTTPError as err:
print(oColors.brightRed + f"Error: {err.code} - {err.reason}" + oColors.standardWhite) print(oColors.brightRed + f"Error: {err.code} - {err.reason}" + oColors.standardWhite)
@ -264,16 +270,16 @@ def updateInstalledPackage(inputSelectedObject='all'):
print(oColors.brightRed + f"Error: Old plugin file coulnd't be deleted" + oColors.standardWhite) print(oColors.brightRed + f"Error: Old plugin file coulnd't be deleted" + oColors.standardWhite)
else: else:
if checkConfig().seperateDownloadPath is True: if configValues.seperateDownloadPath is True:
pluginPath = checkConfig().pathToSeperateDownloadPath pluginPath = configValues.pathToSeperateDownloadPath
else: else:
pluginPath = checkConfig().pathToPluginFolder pluginPath = configValues.pathToPluginFolder
pluginPath = Path(f"{pluginPath}/{plugin}") pluginPath = Path(f"{pluginPath}/{plugin}")
indexNumberUpdated += 1 indexNumberUpdated += 1
pluginsUpdated += 1 pluginsUpdated += 1
try: try:
getSpecificPackage(pluginId, checkConfig().pathToPluginFolder) getSpecificPackage(pluginId, configValues.pathToPluginFolder)
if checkConfig().seperateDownloadPath is False: if configValues.seperateDownloadPath is False:
os.remove(pluginPath) os.remove(pluginPath)
except HTTPError as err: except HTTPError as err:
print(oColors.brightRed + f"Error: {err.code} - {err.reason}" + oColors.standardWhite) print(oColors.brightRed + f"Error: {err.code} - {err.reason}" + oColors.standardWhite)

View File

@ -6,7 +6,7 @@ import requests
from pathlib import Path from pathlib import Path
from utils.consoleoutput import oColors from utils.consoleoutput import oColors
from handlers.handle_config import checkConfig from handlers.handle_config import checkConfig, configurationValues
from handlers.handle_sftp import createSFTPConnection from handlers.handle_sftp import createSFTPConnection
@ -160,3 +160,9 @@ def calculateFileSizeMb(downloadFileSize):
fileSizeMb = fileSizeDownload / 1024 / 1024 fileSizeMb = fileSizeDownload / 1024 / 1024
roundedFileSize = round(fileSizeMb, 2) roundedFileSize = round(fileSizeMb, 2)
return roundedFileSize return roundedFileSize
def calculateFileSizeKb(downloadFileSize):
fileSizeDownload = int(downloadFileSize)
fileSizeKb = fileSizeDownload / 1024
roundedFileSize = round(fileSizeKb, 2)
return roundedFileSize