From 24f570e888c4850f6e1dfaacfa63437011d4180f Mon Sep 17 00:00:00 2001 From: Rob Weber Date: Tue, 5 Dec 2017 14:20:32 -0600 Subject: [PATCH] added an advanced backup editor and combined settings.xml scripts into a launcher --- authorize_cloud.py | 37 ------- launcher.py | 61 +++++++++++ remove_auth.py | 13 --- resources/lib/advanced_editor.py | 172 +++++++++++++++++++++++++++++++ resources/lib/backup.py | 25 +++-- resources/settings.xml | 7 +- 6 files changed, 256 insertions(+), 59 deletions(-) delete mode 100644 authorize_cloud.py create mode 100644 launcher.py delete mode 100644 remove_auth.py create mode 100644 resources/lib/advanced_editor.py diff --git a/authorize_cloud.py b/authorize_cloud.py deleted file mode 100644 index d8d5b8f..0000000 --- a/authorize_cloud.py +++ /dev/null @@ -1,37 +0,0 @@ -import sys -import urlparse -import xbmcgui -import resources.lib.utils as utils -from resources.lib.authorizers import DropboxAuthorizer,GoogleDriveAuthorizer - -def get_params(): - param = {} - try: - for i in sys.argv: - args = i - if(args.startswith('?')): - args = args[1:] - param.update(dict(urlparse.parse_qsl(args))) - except: - pass - return param - -params = get_params() - -#drobpox -if(params['type'] == 'dropbox'): - authorizer = DropboxAuthorizer() - - if(authorizer.authorize()): - xbmcgui.Dialog().ok(utils.getString(30010),utils.getString(30027) + ' ' + utils.getString(30106)) - else: - xbmcgui.Dialog().ok(utils.getString(30010),utils.getString(30107) + ' ' + utils.getString(30027)) - -#google drive -elif(params['type'] == 'google_drive'): - authorizer = GoogleDriveAuthorizer() - - if(authorizer.authorize()): - xbmcgui.Dialog().ok("Backup",utils.getString(30098) + ' ' + utils.getString(30106)) - else: - xbmcgui.Dialog().ok("Backup",utils.getString(30107) + ' ' + utils.getString(30098)) diff --git a/launcher.py b/launcher.py new file mode 100644 index 0000000..37b7e4e --- /dev/null +++ b/launcher.py @@ -0,0 +1,61 @@ +import sys +import urlparse +import xbmc +import xbmcgui +import xbmcvfs +import resources.lib.utils as utils +from resources.lib.authorizers import DropboxAuthorizer,GoogleDriveAuthorizer +from resources.lib.advanced_editor import AdvancedBackupEditor + + +#launcher for various helpful functions found in the settings.xml area + +def authorize_cloud(cloudProvider): + #drobpox + if(cloudProvider == 'dropbox'): + authorizer = DropboxAuthorizer() + + if(authorizer.authorize()): + xbmcgui.Dialog().ok(utils.getString(30010),utils.getString(30027) + ' ' + utils.getString(30106)) + else: + xbmcgui.Dialog().ok(utils.getString(30010),utils.getString(30107) + ' ' + utils.getString(30027)) + + #google drive + elif(cloudProvider == 'google_drive'): + authorizer = GoogleDriveAuthorizer() + + if(authorizer.authorize()): + xbmcgui.Dialog().ok("Backup",utils.getString(30098) + ' ' + utils.getString(30106)) + else: + xbmcgui.Dialog().ok("Backup",utils.getString(30107) + ' ' + utils.getString(30098)) + +def remove_auth(): + #triggered from settings.xml - asks if user wants to delete OAuth token information + shouldDelete = xbmcgui.Dialog().yesno(utils.getString(30093),utils.getString(30094),utils.getString(30095),autoclose=7000) + + if(shouldDelete): + #delete any of the known token file types + xbmcvfs.delete(xbmc.translatePath(utils.data_dir() + "tokens.txt")) #dropbox + xbmcvfs.delete(xbmc.translatePath(utils.data_dir() + "google_drive.dat")) #google drive + +def get_params(): + param = {} + try: + for i in sys.argv: + args = i + if(args.startswith('?')): + args = args[1:] + param.update(dict(urlparse.parse_qsl(args))) + except: + pass + return param + +params = get_params() + +if(params['action'] == 'authorize_cloud'): + authorize_cloud(params['provider']) +elif(params['action'] == 'remove_auth'): + remove_auth() +elif(params['action'] == 'advanced_editor'): + editor = AdvancedBackupEditor() + editor.showMainScreen() diff --git a/remove_auth.py b/remove_auth.py deleted file mode 100644 index f9ac08e..0000000 --- a/remove_auth.py +++ /dev/null @@ -1,13 +0,0 @@ -import xbmc -import xbmcgui -import xbmcvfs -import resources.lib.utils as utils - -#triggered from settings.xml - asks if user wants to delete OAuth token information -shouldDelete = xbmcgui.Dialog().yesno(utils.getString(30093),utils.getString(30094),utils.getString(30095),autoclose=7000) - -if(shouldDelete): - #delete any of the known token file types - xbmcvfs.delete(xbmc.translatePath(utils.data_dir() + "tokens.txt")) #dropbox - xbmcvfs.delete(xbmc.translatePath(utils.data_dir() + "google_drive.dat")) #google drive - diff --git a/resources/lib/advanced_editor.py b/resources/lib/advanced_editor.py new file mode 100644 index 0000000..f066f57 --- /dev/null +++ b/resources/lib/advanced_editor.py @@ -0,0 +1,172 @@ +import json +import utils as utils +import xbmcvfs +import xbmc +import xbmcgui + +class BackupSetManager: + jsonFile = xbmc.translatePath(utils.data_dir() + "custom_paths.json") + paths = None + + def __init__(self): + self.paths = {} + + #try and read in the custom file + self._readFile() + + def addSet(self,aSet): + self.paths[aSet['name']] = {'root':aSet['root'],'dirs':[{"type":"include","path":aSet['root'],'recurse':True}]} + + #save the file + self._writeFile() + + def updateSet(self,name,aSet): + self.paths[name] = aSet + + #save the file + self._writeFile() + + def deleteSet(self,index): + #match the index to a key + keys = self.getSets() + + #delete this set + del self.paths[keys[index]] + + #save the file + self._writeFile() + + def getSets(self): + #list all current sets by name + keys = self.paths.keys() + keys.sort() + + return keys + + def getSet(self,index): + keys = self.getSets(); + + #return the set at this index + return {'name':keys[index],'set':self.paths[keys[index]]} + + def _writeFile(self): + #create the custom file + aFile = xbmcvfs.File(self.jsonFile,'w') + aFile.write(json.dumps(self.paths)) + aFile.close() + + def _readFile(self): + + if(xbmcvfs.exists(self.jsonFile)): + + #read in the custom file + aFile = xbmcvfs.File(self.jsonFile) + + #load custom dirs + self.paths = json.loads(aFile.read()) + aFile.close() + else: + #write a blank file + self._writeFile() + +class AdvancedBackupEditor: + dialog = None + + def __init__(self): + self.dialog = xbmcgui.Dialog() + + def createSet(self): + backupSet = None + + name = self.dialog.input("Set Name",defaultt='Backup Set') + + if(name != ''): + + #give a choice to start in home or enter a root path + enterHome = self.dialog.yesno('Root folder selection',line1='Browse Folder - starts in Kodi home',line2='Enter Own - enter path to start there',nolabel='Browse Folder',yeslabel='Enter Own') + + rootFolder = 'special://home' + if(enterHome): + rootFolder = self.dialog.input('Enter root path',defaultt=rootFolder) + + #check that this path even exists + if(not xbmcvfs.exists(xbmc.translatePath(rootFolder))): + self.dialog.ok('Path Error','Path does not exist',rootFolder) + else: + #select path to start set + rootFolder = self.dialog.browse(type=0,heading="Select Root",shares='files',defaultt=rootFolder) + + backupSet = {'name':name,'root':rootFolder} + + return backupSet + + def editSet(self,name,backupSet): + optionSelected = '' + + while(optionSelected != -1): + options = ['Add Exclusion','Root: ' + backupSet['root']] + + for aDir in backupSet['dirs']: + if(aDir['type'] == 'exclude'): + options.append('Exclude: ' + aDir['path']) + + optionSelected = self.dialog.select('Edit ' + name,options) + + if(optionSelected == 0): + #add an exclusion + excludeFolder = self.dialog.browse(type=0,heading="Add Exclusion",shares='files',defaultt=backupSet['root']) + + #will equal root if cancel is hit + if(excludeFolder != backupSet['root']): + backupSet['dirs'].append({"path":excludeFolder,"type":"exclude"}) + elif(optionSelected == 1): + self.dialog.ok('Root Folder','The root folder cannot be changed',backupSet['root']) + elif(optionSelected > 1): + #remove exclusion folder + del backupSet['dirs'][optionSelected - 2] + + return backupSet + + + def showMainScreen(self): + exitCondition = "" + customPaths = BackupSetManager() + + #show this every time + self.dialog.ok('Disclaimer','Canceling this menu will close and save changes') + + while(exitCondition != -1): + #load the custom paths + options = ['Add Set'] + + for aPath in customPaths.getSets(): + options.append(aPath) + + #show the gui + exitCondition = self.dialog.select('Advanced Editor',options) + + if(exitCondition >= 0): + if(exitCondition == 0): + newSet = self.createSet() + + customPaths.addSet(newSet) + else: + #bring up a context menu + menuOption = self.dialog.select(heading="Choose Action",list=['Edit','Delete'],preselect=0) + + if(menuOption == 0): + #get the set + aSet = customPaths.getSet(exitCondition -1) + + #edit the set + updatedSet = self.editSet(aSet['name'],aSet['set']) + + #save it + customPaths.updateSet(aSet['name'],updatedSet) + + elif(menuOption == 1): + if(self.dialog.yesno(heading="Delete Set",line1="Are you sure you want to delete?")): + #delete this path - subtract one because of "add" item + customPaths.deleteSet(exitCondition -1) + + diff --git a/resources/lib/backup.py b/resources/lib/backup.py index 8dffd45..abd9af6 100644 --- a/resources/lib/backup.py +++ b/resources/lib/backup.py @@ -8,6 +8,7 @@ from vfs import XBMCFileSystem,DropboxFileSystem,ZipFileSystem,GoogleDriveFilesy from progressbar import BackupProgressBar from resources.lib.guisettings import GuiSettingsManager from resources.lib.extractor import ZipExtractor +from resources.lib.advanced_editor import BackupSetManager def folderSort(aKey): result = aKey[0] @@ -171,13 +172,25 @@ class XbmcBackup: allFiles = [] fileManager = FileManager(self.xbmc_vfs) - #get file listings for all enabled directories - for aDir in self.simple_directory_list: - #if this dir enabled - if(utils.getSetting('backup_' + aDir) == 'true'): - #get a file listing and append it to the allfiles array - allFiles.append(self._addBackupDir(aDir,xbmc.translatePath(selectedDirs[aDir]['root']),selectedDirs[aDir]['dirs'])) + if(utils.getSetting('backup_selection_type') == 0): + #simple mode - get file listings for all enabled directories + for aDir in self.simple_directory_list: + #if this dir enabled + if(utils.getSetting('backup_' + aDir) == 'true'): + #get a file listing and append it to the allfiles array + allFiles.append(self._addBackupDir(aDir,xbmc.translatePath(selectedDirs[aDir]['root']),selectedDirs[aDir]['dirs'])) + else: + #advanced mode - open custom editor + setManager = BackupSetManager() + #go through the custom sets + for index in range(0,len(setManager.getSets())): + #get the set + aSet = setManager.getSet(index) + utils.log(str(aSet)) + #get file listing and append + allFiles.append(self._addBackupDir(aSet['name'],xbmc.translatePath(aSet['set']['root']),aSet['set']['dirs'])) + #create a validation file for backup rotation writeCheck = self._createValidationFile(allFiles) diff --git a/resources/settings.xml b/resources/settings.xml index a58610c..53e3861 100644 --- a/resources/settings.xml +++ b/resources/settings.xml @@ -13,9 +13,9 @@ - - - + + + @@ -26,6 +26,7 @@ +