mirror of
https://github.com/Neocky/pluGET.git
synced 2024-04-29 16:12:30 +00:00
Split everything in sub packages; Created main function; Added many exception handlers for SFTP
Added: - Split everything into sub packages - created a main file: __main__.py - added many exception handlings with SFTP Edited: - launcher.bat calls now the __main__.py file
This commit is contained in:
0
src/handlers/__init__.py
Normal file
0
src/handlers/__init__.py
Normal file
53
src/handlers/handle_config.py
Normal file
53
src/handlers/handle_config.py
Normal file
@@ -0,0 +1,53 @@
|
||||
import os.path
|
||||
import sys
|
||||
import configparser
|
||||
|
||||
from utils.consoleoutput import oColors
|
||||
|
||||
|
||||
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()
|
||||
|
||||
class configValues:
|
||||
config = configparser.ConfigParser()
|
||||
config.sections()
|
||||
config.read("config.ini")
|
||||
localPluginFolder = config['General']['LocalPluginFolder']
|
||||
pathToPluginFolder = config['General']['PathToPluginFolder']
|
||||
|
||||
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']['PluginFolderForUpload']
|
||||
|
||||
sftp_port = int(sftp_port)
|
||||
if localPluginFolder == 'True':
|
||||
localPluginFolder = True
|
||||
else:
|
||||
localPluginFolder = False
|
||||
|
||||
return configValues
|
||||
|
||||
|
||||
def createConfig():
|
||||
config = configparser.ConfigParser(allow_no_value=True)
|
||||
config['General'] = {}
|
||||
config['General'][';'] = 'If a local plugin folder exists (True/False): (If False use SFTP)'
|
||||
config['General']['LocalPluginFolder'] = 'True'
|
||||
config['General']['PathToPluginFolder'] = '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'] = 'longpassword'
|
||||
config['SFTP - Remote Server'][';'] = 'Normally you won*t need to change anything below this line'
|
||||
config['SFTP - Remote Server']['Port'] = '22'
|
||||
config['SFTP - Remote Server']['PluginFolderForUpload'] = '.\\plugins'
|
||||
|
||||
with open('./config.ini', 'w') as configfile:
|
||||
config.write(configfile)
|
90
src/handlers/handle_input.py
Normal file
90
src/handlers/handle_input.py
Normal file
@@ -0,0 +1,90 @@
|
||||
import sys
|
||||
import time
|
||||
|
||||
from utils.consoleoutput import oColors
|
||||
from utils.utilities import getHelp
|
||||
from handlers.handle_config import checkConfig
|
||||
from plugin.plugin_downloader import searchPackage, getSpecificPackage
|
||||
from plugin.plugin_updatechecker import updateInstalledPackage, checkInstalledPackage
|
||||
from plugin.plugin_remover import removePlugin
|
||||
|
||||
def createInputLists():
|
||||
global COMMANDLIST
|
||||
COMMANDLIST = [
|
||||
'get',
|
||||
'update',
|
||||
'check',
|
||||
'exit',
|
||||
'help',
|
||||
'remove'
|
||||
]
|
||||
global INPUTSELECTEDOBJECT
|
||||
INPUTSELECTEDOBJECT = [
|
||||
'all',
|
||||
'*'
|
||||
]
|
||||
|
||||
|
||||
def handleInput(inputCommand, inputSelectedObject, inputParams):
|
||||
while True:
|
||||
if inputCommand == 'get':
|
||||
if inputSelectedObject.isdigit():
|
||||
if not checkConfig().localPluginFolder:
|
||||
getSpecificPackage(inputSelectedObject, checkConfig().sftp_folderPath, inputParams)
|
||||
break
|
||||
else:
|
||||
getSpecificPackage(inputSelectedObject, checkConfig().pathToPluginFolder, inputParams)
|
||||
break
|
||||
else:
|
||||
searchPackage(inputSelectedObject)
|
||||
break
|
||||
if inputCommand == 'update':
|
||||
updateInstalledPackage(inputSelectedObject)
|
||||
break
|
||||
if inputCommand == 'check':
|
||||
checkInstalledPackage(inputSelectedObject)
|
||||
break
|
||||
if inputCommand == 'exit':
|
||||
sys.exit()
|
||||
if inputCommand == 'help':
|
||||
getHelp()
|
||||
break
|
||||
if inputCommand == 'remove':
|
||||
removePlugin(inputSelectedObject)
|
||||
break
|
||||
else:
|
||||
print(oColors.brightRed + "Command not found. Please try again." + oColors.standardWhite)
|
||||
getInput()
|
||||
getInput()
|
||||
|
||||
|
||||
def getInput():
|
||||
while True:
|
||||
try:
|
||||
inputCommand, inputSelectedObject, *inputParams = input("pluGET >> ").split()
|
||||
break
|
||||
except ValueError:
|
||||
print(oColors.brightRed + "Wrong input! Use: > *command* *selectedObject* *optionalParams*" + oColors.standardWhite)
|
||||
|
||||
inputParams = inputParams[0] if inputParams else None
|
||||
print(inputCommand)
|
||||
print(inputSelectedObject)
|
||||
print(inputParams)
|
||||
handleInput(inputCommand, inputSelectedObject, inputParams)
|
||||
|
||||
|
||||
# only for testing purposes
|
||||
def outputTest():
|
||||
print("Hello world")
|
||||
print("Waiting still seconds: 5", end='\r')
|
||||
time.sleep(1)
|
||||
print("Waiting still seconds: 4", end='\r')
|
||||
time.sleep(1)
|
||||
print("Waiting still seconds: 3", end='\r')
|
||||
time.sleep(1)
|
||||
print("Waiting still seconds: 2", end='\r')
|
||||
time.sleep(1)
|
||||
print("Waiting still seconds: 1", end='\r')
|
||||
time.sleep(1)
|
||||
print("Done ✅☑✔ ")
|
||||
input("Press key to end program...")
|
59
src/handlers/handle_sftp.py
Normal file
59
src/handlers/handle_sftp.py
Normal file
@@ -0,0 +1,59 @@
|
||||
import sys
|
||||
import pysftp
|
||||
import paramiko
|
||||
|
||||
from utils.consoleoutput import oColors
|
||||
from handlers.handle_config import checkConfig
|
||||
|
||||
|
||||
def createSFTPConnection():
|
||||
cnopts = pysftp.CnOpts()
|
||||
cnopts.hostkeys = None # TODO fix this
|
||||
try:
|
||||
sftp = pysftp.Connection(checkConfig().sftp_server, username=checkConfig().sftp_user, \
|
||||
password=checkConfig().sftp_password, port=checkConfig().sftp_port, cnopts=cnopts)
|
||||
except paramiko.ssh_exception.AuthenticationException:
|
||||
print(oColors.brightRed + "[SFTP]: Wrong Username/Password" + oColors.standardWhite)
|
||||
except paramiko.ssh_exception.SSHException:
|
||||
print(oColors.brightRed + "[SFTP]: The SFTP server isn't available." + oColors.standardWhite)
|
||||
try:
|
||||
return sftp
|
||||
except UnboundLocalError:
|
||||
print(oColors.brightRed + "[SFTP]: Check your config.ini!" + oColors.standardWhite)
|
||||
print(oColors.brightRed + "Exiting program..." + oColors.standardWhite)
|
||||
sys.exit()
|
||||
|
||||
|
||||
def sftp_showPlugins(sftp):
|
||||
sftp.cd('plugins')
|
||||
for attr in sftp.listdir_attr():
|
||||
print(attr.filename, attr)
|
||||
|
||||
|
||||
def sftp_cdPluginDir(sftp):
|
||||
sftp.cd('plugins')
|
||||
|
||||
|
||||
def sftp_upload_file(sftp, itemPath):
|
||||
try:
|
||||
sftp.chdir('plugins')
|
||||
sftp.put(itemPath)
|
||||
|
||||
except FileNotFoundError:
|
||||
print(oColors.brightRed + "The *plugins* folder couldn*t be found on the remote host!" + oColors.standardWhite)
|
||||
print(oColors.brightRed + "Aborting installation." + oColors.standardWhite)
|
||||
sftp.close()
|
||||
|
||||
|
||||
def sftp_listAll(sftp):
|
||||
try:
|
||||
sftp.chdir('plugins')
|
||||
installedPlugins = sftp.listdir()
|
||||
|
||||
except FileNotFoundError:
|
||||
print(oColors.brightRed + "The *plugins* folder couldn*t be found on the remote host!" + oColors.standardWhite)
|
||||
|
||||
try:
|
||||
return installedPlugins
|
||||
except UnboundLocalError:
|
||||
print(oColors.brightRed + "No plugins were found." + oColors.standardWhite)
|
Reference in New Issue
Block a user