pep8 comments

This commit is contained in:
Rob Weber
2019-11-25 15:19:57 -06:00
parent bbbfc3dd84
commit f7665c8ddd
11 changed files with 234 additions and 235 deletions

View File

@ -19,14 +19,14 @@ def folderSort(aKey):
class XbmcBackup:
#constants for initiating a back or restore
# constants for initiating a back or restore
Backup = 0
Restore = 1
#list of dirs for the "simple" file selection
# list of dirs for the "simple" file selection
simple_directory_list = ['addons','addon_data','database','game_saves','playlists','profiles','thumbnails','config']
#file systems
# file systems
xbmc_vfs = None
remote_vfs = None
saved_remote_vfs = None
@ -34,13 +34,13 @@ class XbmcBackup:
restoreFile = None
remote_base_path = None
#for the progress bar
# for the progress bar
progressBar = None
filesLeft = 0
filesTotal = 1
restore_point = None
skip_advanced = False #if we should check for the existance of advancedsettings in the restore
skip_advanced = False # if we should check for the existance of advancedsettings in the restore
def __init__(self):
self.xbmc_vfs = XBMCFileSystem(xbmc.translatePath('special://home'))
@ -68,17 +68,17 @@ class XbmcBackup:
return result
#reverse - should reverse the resulting, default is true - newest to oldest
def listBackups(self,reverse=True):
# reverse - should reverse the resulting, default is true - newest to oldest
def listBackups(self, reverse=True):
result = []
#get all the folders in the current root path
# get all the folders in the current root path
dirs,files = self.remote_vfs.listdir(self.remote_base_path)
for aDir in dirs:
if(self.remote_vfs.exists(self.remote_base_path + aDir + "/xbmcbackup.val")):
#format the name according to regional settings
# format the name according to regional settings
folderName = self._dateFormat(aDir)
result.append((aDir,folderName))
@ -89,33 +89,33 @@ class XbmcBackup:
if(file_ext == 'zip' and len(folderName) == 12 and folderName.isdigit()):
#format the name according to regional settings
# format the name according to regional settings
folderName = self._dateFormat(folderName)
result.append((aFile ,folderName))
result.append((aFile, folderName))
result.sort(key=folderSort,reverse=reverse)
return result
def selectRestore(self,restore_point):
def selectRestore(self, restore_point):
self.restore_point = restore_point
def skipAdvanced(self):
self.skip_advanced = True
def backup(self,progressOverride=False):
shouldContinue = self._setupVFS(self.Backup,progressOverride)
def backup(self, progressOverride=False):
shouldContinue = self._setupVFS(self.Backup, progressOverride)
if(shouldContinue):
utils.log(utils.getString(30023) + " - " + utils.getString(30016))
#check if remote path exists
# check if remote path exists
if(self.remote_vfs.exists(self.remote_vfs.root_path)):
#may be data in here already
# may be data in here already
utils.log(utils.getString(30050))
else:
#make the remote directory
# make the remote directory
self.remote_vfs.mkdir(self.remote_vfs.root_path)
utils.log(utils.getString(30051))
@ -123,35 +123,35 @@ class XbmcBackup:
allFiles = []
if(int(utils.getSetting('backup_selection_type')) == 0):
#read in a list of the directories to backup
# read in a list of the directories to backup
selectedDirs = self._readBackupConfig(utils.addon_dir() + "/resources/data/default_files.json")
#simple mode - get file listings for all enabled directories
# simple mode - get file listings for all enabled directories
for aDir in self.simple_directory_list:
#if this dir enabled
# if this dir enabled
if(utils.getSetting('backup_' + aDir) == 'true'):
#get a file listing and append it to the allfiles array
# get a file listing and append it to the allfiles array
allFiles.append(self._addBackupDir(aDir,selectedDirs[aDir]['root'],selectedDirs[aDir]['dirs']))
else:
#advanced mode - load custom paths
# advanced mode - load custom paths
selectedDirs = self._readBackupConfig(utils.data_dir() + "/custom_paths.json")
#get the set names
# get the set names
keys = list(selectedDirs.keys())
#go through the custom sets
# go through the custom sets
for aKey in keys:
#get the set
# get the set
aSet = selectedDirs[aKey]
#get file listing and append
# get file listing and append
allFiles.append(self._addBackupDir(aKey,aSet['root'],aSet['dirs']))
#create a validation file for backup rotation
# create a validation file for backup rotation
writeCheck = self._createValidationFile(allFiles)
if(not writeCheck):
#we may not be able to write to this destination for some reason
# we may not be able to write to this destination for some reason
shouldContinue = xbmcgui.Dialog().yesno(utils.getString(30089),utils.getString(30090), utils.getString(30044),autoclose=25000)
if(not shouldContinue):
@ -159,7 +159,7 @@ class XbmcBackup:
orig_base_path = self.remote_vfs.root_path
#backup all the files
# backup all the files
self.filesLeft = self.filesTotal
for fileGroup in allFiles:
self.xbmc_vfs.set_root(xbmc.translatePath(fileGroup['source']))
@ -170,20 +170,20 @@ class XbmcBackup:
utils.showNotification(utils.getString(30092))
utils.log(utils.getString(30092))
#reset remote and xbmc vfs
# reset remote and xbmc vfs
self.xbmc_vfs.set_root("special://home/")
self.remote_vfs.set_root(orig_base_path)
if(utils.getSetting("compress_backups") == 'true'):
fileManager = FileManager(self.xbmc_vfs)
#send the zip file to the real remote vfs
# send the zip file to the real remote vfs
zip_name = self.remote_vfs.root_path[:-1] + ".zip"
self.remote_vfs.cleanup()
self.xbmc_vfs.rename(xbmc.translatePath("special://temp/xbmc_backup_temp.zip"), xbmc.translatePath("special://temp/" + zip_name))
fileManager.addFile(xbmc.translatePath("special://temp/" + zip_name))
#set root to data dir home
# set root to data dir home
self.xbmc_vfs.set_root(xbmc.translatePath("special://temp/"))
self.remote_vfs = self.saved_remote_vfs
@ -191,16 +191,16 @@ class XbmcBackup:
fileCopied = self._copyFiles(fileManager.getFiles(),self.xbmc_vfs, self.remote_vfs)
if(not fileCopied):
#zip archive copy filed, inform the user
# zip archive copy filed, inform the user
shouldContinue = xbmcgui.Dialog().ok(utils.getString(30089),utils.getString(30090), utils.getString(30091))
#delete the temp zip file
# delete the temp zip file
self.xbmc_vfs.rmfile(xbmc.translatePath("special://temp/" + zip_name))
#remove old backups
# remove old backups
self._rotateBackups()
#close any files
# close any files
self._closeVFS()
def restore(self,progressOverride=False,selectedSets=None):
@ -209,16 +209,16 @@ class XbmcBackup:
if(shouldContinue):
utils.log(utils.getString(30023) + " - " + utils.getString(30017))
#catch for if the restore point is actually a zip file
# catch for if the restore point is actually a zip file
if(self.restore_point.split('.')[-1] == 'zip'):
self.progressBar.updateProgress(2, utils.getString(30088))
utils.log("copying zip file: " + self.restore_point)
#set root to data dir home
# set root to data dir home
self.xbmc_vfs.set_root(xbmc.translatePath("special://temp/"))
if(not self.xbmc_vfs.exists(xbmc.translatePath("special://temp/" + self.restore_point))):
#copy just this file from the remote vfs
# copy just this file from the remote vfs
zipFile = []
zipFile.append(self.remote_base_path + self.restore_point)
@ -226,12 +226,12 @@ class XbmcBackup:
else:
utils.log("zip file exists already")
#extract the zip file
# extract the zip file
zip_vfs = ZipFileSystem(xbmc.translatePath("special://temp/"+ self.restore_point),'r')
extractor = ZipExtractor()
if(not extractor.extract(zip_vfs, xbmc.translatePath("special://temp/"), self.progressBar)):
#we had a problem extracting the archive, delete everything
# we had a problem extracting the archive, delete everything
zip_vfs.cleanup()
self.xbmc_vfs.rmfile(xbmc.translatePath("special://temp/" + self.restore_point))
@ -241,60 +241,60 @@ class XbmcBackup:
zip_vfs.cleanup()
self.progressBar.updateProgress(0,utils.getString(30049) + "......")
#set the new remote vfs and fix xbmc path
# set the new remote vfs and fix xbmc path
self.remote_vfs = XBMCFileSystem(xbmc.translatePath("special://temp/" + self.restore_point.split(".")[0] + "/"))
self.xbmc_vfs.set_root(xbmc.translatePath("special://home/"))
#for restores remote path must exist
# for restores remote path must exist
if(not self.remote_vfs.exists(self.remote_vfs.root_path)):
xbmcgui.Dialog().ok(utils.getString(30010),utils.getString(30045),self.remote_vfs.root_path)
return
valFile = self._checkValidationFile(self.remote_vfs.root_path)
if(valFile == None):
#don't continue
# don't continue
return
utils.log(utils.getString(30051))
allFiles = []
fileManager = FileManager(self.remote_vfs)
#check for the existance of an advancedsettings file
# check for the existance of an advancedsettings file
if(self.remote_vfs.exists(self.remote_vfs.root_path + "config/advancedsettings.xml") and not self.skip_advanced):
#let the user know there is an advanced settings file present
# let the user know there is an advanced settings file present
restartXbmc = xbmcgui.Dialog().yesno(utils.getString(30038),utils.getString(30039),utils.getString(30040), utils.getString(30041))
if(restartXbmc):
#add only this file to the file list
# add only this file to the file list
fileManager.addFile(self.remote_vfs.root_path + "config/advancedsettings.xml")
self._copyFiles(fileManager.getFiles(),self.remote_vfs,self.xbmc_vfs)
#let the service know to resume this backup on startup
# let the service know to resume this backup on startup
self._createResumeBackupFile()
#do not continue running
# do not continue running
xbmcgui.Dialog().ok(utils.getString(30077),utils.getString(30078))
return
#use a multiselect dialog to select sets to restore
# use a multiselect dialog to select sets to restore
restoreSets = [n['name'] for n in valFile['directories']]
#if passed in list, skip selection
# if passed in list, skip selection
if(selectedSets == None):
selectedSets = xbmcgui.Dialog().multiselect(utils.getString(30131),restoreSets)
else:
selectedSets = [restoreSets.index(n) for n in selectedSets if n in restoreSets] #if set name not found just skip it
selectedSets = [restoreSets.index(n) for n in selectedSets if n in restoreSets] # if set name not found just skip it
if(selectedSets != None):
#go through each of the directories in the backup and write them to the correct location
# go through each of the directories in the backup and write them to the correct location
for index in selectedSets:
#add this directory
# add this directory
aDir = valFile['directories'][index]
self.xbmc_vfs.set_root(xbmc.translatePath(aDir['path']))
if(self.remote_vfs.exists(self.remote_vfs.root_path + aDir['name'] + '/')):
#walk the directory
# walk the directory
fileManager.walkTree(self.remote_vfs.root_path + aDir['name'] + '/')
self.filesTotal = self.filesTotal + fileManager.size()
allFiles.append({"source":self.remote_vfs.root_path + aDir['name'],"dest":self.xbmc_vfs.root_path,"files":fileManager.getFiles()})
@ -302,7 +302,7 @@ class XbmcBackup:
utils.log("error path not found: " + self.remote_vfs.root_path + aDir['name'])
xbmcgui.Dialog().ok(utils.getString(30010),utils.getString(30045),self.remote_vfs.root_path + aDir['name'])
#restore all the files
# restore all the files
self.filesLeft = self.filesTotal
for fileGroup in allFiles:
self.remote_vfs.set_root(fileGroup['source'])
@ -312,35 +312,35 @@ class XbmcBackup:
self.progressBar.updateProgress(99,"Clean up operations .....")
if(self.restore_point.split('.')[-1] == 'zip'):
#delete the zip file and the extracted directory
# delete the zip file and the extracted directory
self.xbmc_vfs.rmfile(xbmc.translatePath("special://temp/" + self.restore_point))
self.xbmc_vfs.rmdir(self.remote_vfs.root_path)
#update the guisettings information (or what we can from it)
# update the guisettings information (or what we can from it)
gui_settings = GuiSettingsManager()
gui_settings.run()
#call update addons to refresh everything
# call update addons to refresh everything
xbmc.executebuiltin('UpdateLocalAddons')
def _setupVFS(self,mode=-1,progressOverride=False):
#set windows setting to true
# set windows setting to true
window = xbmcgui.Window(10000)
window.setProperty(utils.__addon_id__ + ".running","true")
#append backup folder name
# append backup folder name
progressBarTitle = utils.getString(30010) + " - "
if(mode == self.Backup and self.remote_vfs.root_path != ''):
if(utils.getSetting("compress_backups") == 'true'):
#delete old temp file
# delete old temp file
if(self.xbmc_vfs.exists(xbmc.translatePath('special://temp/xbmc_backup_temp.zip'))):
if(not self.xbmc_vfs.rmfile(xbmc.translatePath('special://temp/xbmc_backup_temp.zip'))):
#we had some kind of error deleting the old file
# we had some kind of error deleting the old file
xbmcgui.Dialog().ok(utils.getString(30010),utils.getString(30096),utils.getString(30097))
return False
#save the remote file system and use the zip vfs
# save the remote file system and use the zip vfs
self.saved_remote_vfs = self.remote_vfs
self.remote_vfs = ZipFileSystem(xbmc.translatePath("special://temp/xbmc_backup_temp.zip"),"w")
@ -351,7 +351,7 @@ class XbmcBackup:
self.remote_vfs.set_root(self.remote_vfs.root_path + self.restore_point + "/")
progressBarTitle = progressBarTitle + utils.getString(30023) + ": " + utils.getString(30017)
else:
#kill the program here
# kill the program here
self.remote_vfs = None
return False
@ -359,11 +359,11 @@ class XbmcBackup:
utils.log(utils.getString(30048) + ": " + self.remote_vfs.root_path)
#setup the progress bar
# setup the progress bar
self.progressBar = BackupProgressBar(progressOverride)
self.progressBar.create(progressBarTitle,utils.getString(30049) + "......")
#if we made it this far we're good
# if we made it this far we're good
return True
def _closeVFS(self):
@ -371,7 +371,7 @@ class XbmcBackup:
self.remote_vfs.cleanup()
self.progressBar.close()
#reset the window setting
# reset the window setting
window = xbmcgui.Window(10000)
window.setProperty(utils.__addon_id__ + ".running","")
@ -381,7 +381,7 @@ class XbmcBackup:
utils.log("Source: " + source.root_path)
utils.log("Desintation: " + dest.root_path)
#make sure the dest folder exists - can cause write errors if the full path doesn't exist
# make sure the dest folder exists - can cause write errors if the full path doesn't exist
if(not dest.exists(dest.root_path)):
dest.mkdir(dest.root_path)
@ -397,13 +397,13 @@ class XbmcBackup:
wroteFile = True
destFile = dest.root_path + aFile[len(source.root_path):]
if(isinstance(source,DropboxFileSystem)):
#if copying from cloud storage we need the file handle, use get_file
# if copying from cloud storage we need the file handle, use get_file
wroteFile = source.get_file(aFile,destFile)
else:
#copy using normal method
# copy using normal method
wroteFile = dest.put(aFile,destFile)
#if result is still true but this file failed
# if result is still true but this file failed
if(not wroteFile and result):
result = False
@ -418,18 +418,18 @@ class XbmcBackup:
for aDir in dirList:
fileManager.addDir(aDir)
#walk all the root trees
# walk all the root trees
fileManager.walk()
#update total files
# update total files
self.filesTotal = self.filesTotal + fileManager.size()
return {"name":folder_name,"source":root_path,"dest":self.remote_vfs.root_path,"files":fileManager.getFiles()}
def _dateFormat(self,dirName):
#create date_time object from foldername YYYYMMDDHHmm
# create date_time object from foldername YYYYMMDDHHmm
date_time = datetime(int(dirName[0:4]),int(dirName[4:6]),int(dirName[6:8]),int(dirName[8:10]),int(dirName[10:12]))
#format the string based on region settings
# format the string based on region settings
result = utils.getRegionalTimestamp(date_time, ['dateshort','time'])
return result
@ -442,21 +442,21 @@ class XbmcBackup:
total_backups = int(utils.getSetting('backup_rotation'))
if(total_backups > 0):
#get a list of valid backup folders
# get a list of valid backup folders
dirs = self.listBackups(reverse=False)
if(len(dirs) > total_backups):
#remove backups to equal total wanted
# remove backups to equal total wanted
remove_num = 0
self.filesTotal = self.filesTotal + remove_num + 1
#update the progress bar if it is available
# update the progress bar if it is available
while(remove_num < (len(dirs) - total_backups) and not self.progressBar.checkCancel()):
self._updateProgress(utils.getString(30054) + " " + dirs[remove_num][1])
utils.log("Removing backup " + dirs[remove_num][0])
if(dirs[remove_num][0].split('.')[-1] == 'zip'):
#this is a file, remove it that way
# this is a file, remove it that way
self.remote_vfs.rmfile(self.remote_base_path + dirs[remove_num][0])
else:
self.remote_vfs.rmdir(self.remote_base_path + dirs[remove_num][0] + "/")
@ -478,11 +478,11 @@ class XbmcBackup:
success = self.remote_vfs.put(xbmc.translatePath(utils.data_dir() + "xbmcbackup.val"),self.remote_vfs.root_path + "xbmcbackup.val")
#remove the validation file
# remove the validation file
xbmcvfs.delete(xbmc.translatePath(utils.data_dir() + "xbmcbackup.val"))
if(success):
#android requires a .nomedia file to not index the directory as media
# android requires a .nomedia file to not index the directory as media
if(not xbmcvfs.exists(xbmc.translatePath(utils.data_dir() + ".nomedia"))):
nmFile = xbmcvfs.File(xbmc.translatePath(utils.data_dir() + ".nomedia"),'w')
nmFile.close()
@ -494,7 +494,7 @@ class XbmcBackup:
def _checkValidationFile(self,path):
result = None
#copy the file and open it
# copy the file and open it
if(isinstance(self.remote_vfs,DropboxFileSystem)):
self.remote_vfs.get_file(path + "xbmcbackup.val", xbmc.translatePath(utils.data_dir() + "xbmcbackup_restore.val"))
else:
@ -504,7 +504,7 @@ class XbmcBackup:
jsonString = vFile.read()
vFile.close()
#delete after checking
# delete after checking
xbmcvfs.delete(xbmc.translatePath(utils.data_dir() + "xbmcbackup_restore.val"))
try:
@ -517,7 +517,7 @@ class XbmcBackup:
result = None
except ValueError:
#may fail on older archives
# may fail on older archives
result = None
return result
@ -560,17 +560,17 @@ class FileManager:
dirs,files = self.vfs.listdir(directory)
if(recurse):
#create all the subdirs first
# create all the subdirs first
for aDir in dirs:
dirPath = xbmc.validatePath(xbmc.translatePath(directory + self.pathSep + aDir))
file_ext = aDir.split('.')[-1]
#check if directory is excluded
# check if directory is excluded
if(not any(dirPath.startswith(exDir) for exDir in self.exclude_dir)):
self.addFile("-" + dirPath)
#catch for "non directory" type files
# catch for "non directory" type files
shouldWalk = True
for s in file_ext:
@ -580,7 +580,7 @@ class FileManager:
if(shouldWalk):
self.walkTree(dirPath)
#copy all the files
# copy all the files
for aFile in files:
filePath = xbmc.translatePath(directory + self.pathSep + aFile)
self.addFile(filePath)
@ -592,17 +592,17 @@ class FileManager:
self.excludeFile(xbmc.translatePath(dirMeta['path']))
def addFile(self,filename):
#write the full remote path name of this file
# write the full remote path name of this file
utils.log("Add File: " + filename)
self.fileArray.append(filename)
def excludeFile(self,filename):
#remove trailing slash
# remove trailing slash
if(filename[-1] == '/' or filename[-1] == '\\'):
filename = filename[:-1]
#write the full remote path name of this file
# write the full remote path name of this file
utils.log("Exclude File: " + filename)
self.exclude_dir.append(filename)