From cb6fdd7efe89dc9658cb5681d3e45c97e708b36d Mon Sep 17 00:00:00 2001 From: Rob Weber Date: Sun, 4 Nov 2012 14:39:17 -0600 Subject: [PATCH] finished xbmcvfs wrapper, started dropbox wrapper --- resources/lib/backup.py | 100 +++++++++++-------------- resources/lib/dropbox/pkg_resources.py | 4 +- resources/lib/vfs.py | 64 ++++++++++++---- resources/settings.xml | 6 +- 4 files changed, 97 insertions(+), 77 deletions(-) diff --git a/resources/lib/backup.py b/resources/lib/backup.py index ecf626d..d8f8dc5 100644 --- a/resources/lib/backup.py +++ b/resources/lib/backup.py @@ -4,17 +4,15 @@ import xbmcvfs import utils as utils import os import time -from vfs import XBMCFilesystem,DropboxFilesystem +from vfs import XBMCFileSystem,DropboxFileSystem class FileManager: - walk_path = '' fileArray = None verbose_log = False not_dir = ['.zip','.xsp','.rar'] vfs = None - def __init__(self,path,vfs): - self.walk_path = path + def __init__(self,vfs): self.vfs = vfs def createFileList(self): @@ -24,35 +22,35 @@ class FileManager: #figure out which syncing options to run if(utils.getSetting('backup_addons') == 'true'): self.addFile("-addons") - self.walkTree(self.walk_path + "addons/") + self.walkTree(self.vfs.root_path + "addons/") self.addFile("-userdata") if(utils.getSetting('backup_addon_data') == 'true'): self.addFile("-userdata/addon_data") - self.walkTree(self.walk_path + "userdata/addon_data/") + self.walkTree(self.vfs.root_path + "userdata/addon_data/") if(utils.getSetting('backup_database') == 'true'): self.addFile("-userdata/Database") - self.walkTree(self.walk_path + "userdata/Database") + self.walkTree(self.vfs.root_path + "userdata/Database") if(utils.getSetting("backup_playlists") == 'true'): self.addFile("-userdata/playlists") - self.walkTree(self.walk_path + "userdata/playlists") + self.walkTree(self.vfs.root_path + "userdata/playlists") if(utils.getSetting("backup_thumbnails") == "true"): self.addFile("-userdata/Thumbnails") - self.walkTree(self.walk_path + "userdata/Thumbnails") + self.walkTree(self.vfs.root_path + "userdata/Thumbnails") if(utils.getSetting("backup_config") == "true"): self.addFile("-userdata/keymaps") - self.walkTree(self.walk_path + "userdata/keymaps") + self.walkTree(self.vfs.root_path + "userdata/keymaps") self.addFile("-userdata/peripheral_data") - self.walkTree(self.walk_path + "userdata/peripheral_data") + self.walkTree(self.vfs.root_path + "userdata/peripheral_data") #this part is an oddity - dirs,configFiles = self.vfs.listdir(self.walk_path + "userdata/") + dirs,configFiles = self.vfs.listdir(self.vfs.root_path + "userdata/") for aFile in configFiles: if(aFile.endswith(".xml")): self.addFile("userdata/" + aFile) @@ -65,7 +63,7 @@ class FileManager: dirPath = xbmc.translatePath(directory + "/" + aDir) file_ext = aDir.split('.')[-1] - self.addFile("-" + dirPath[len(self.walk_path):].decode("UTF-8")) + self.addFile("-" + dirPath[len(self.vfs.root_path):].decode("UTF-8")) #catch for "non directory" type files if (not any(file_ext in s for s in self.not_dir)): self.walkTree(dirPath) @@ -73,7 +71,7 @@ class FileManager: #copy all the files for aFile in files: filePath = xbmc.translatePath(directory + "/" + aFile) - self.addFile(filePath[len(self.walk_path):].decode("UTF-8")) + self.addFile(filePath[len(self.vfs.root_path):].decode("UTF-8")) def addFile(self,filename): #write the full remote path name of this file @@ -91,9 +89,8 @@ class XbmcBackup: #remote file system vfs = None - local_path = '' - remote_root = '' - remote_path = '' + local_vfs = None + remote_vfs = None restoreFile = None #for the progress bar @@ -104,7 +101,8 @@ class XbmcBackup: fileManager = None def __init__(self): - self.local_path = xbmc.makeLegalFilename(xbmc.translatePath("special://home"),False); + self.local_vfs = XBMCFileSystem() + self.local_vfs.set_root(xbmc.translatePath("special://home")) self.configureVFS() @@ -112,22 +110,15 @@ class XbmcBackup: def configureVFS(self): if(utils.getSetting('remote_selection') == '1'): - self.remote_root = utils.getSetting('remote_path_2') + self.remote_vfs = XBMCFileSystem() + self.remote_vfs.set_root(utils.getSetting('remote_path_2')) utils.setSetting("remote_path","") - self.vfs = XBMCFilesystem() elif(utils.getSetting('remote_selection') == '0'): - self.remote_root = utils.getSetting("remote_path") - self.vfs = XBMCFilesystem() + self.remote_vfs = XBMCFileSystem() + self.remote_vfs.set_root(utils.getSetting("remote_path")) elif(utils.getSetting('remote_selection') == '2'): - self.remote_root = '/xbmc' - - #fix slashes - self.remote_root = self.remote_root.replace("\\","/") - - #check if trailing slash is included - if(self.remote_root[-1:] != "/"): - self.remote_root = self.remote_root + "/" - + self.remote_vfs = DropboxFileSystem() + self.remote_vfs.set_root(utils.getSetting("dropbox_path")) def run(self,mode=-1,runSilent=False): #check if we should use the progress bar @@ -140,25 +131,23 @@ class XbmcBackup: mode = int(utils.getSetting('addon_mode')) #append backup folder name - if(mode == self.Backup and self.remote_root != ''): - self.remote_path = self.remote_root + time.strftime("%Y%m%d") + "/" - elif(mode == self.Restore and utils.getSetting("backup_name") != '' and self.remote_root != ''): - self.remote_path = self.remote_root + utils.getSetting("backup_name") + "/" + if(mode == self.Backup and self.remote_vfs.root_path != ''): + self.remote_vfs.set_root(self.remote_vfs.root_path + time.strftime("%Y%m%d") + "/") + elif(mode == self.Restore and utils.getSetting("backup_name") != '' and self.remote_vfs.root_path != ''): + self.remote_vfs.set_root(self.remote_vfs.root_path + utils.getSetting("backup_name") + "/") else: - self.remote_path = "" + self.remote_vfs = None - utils.log(utils.getString(30047) + ": " + self.local_path) - utils.log(utils.getString(30048) + ": " + self.remote_path) - - + utils.log(utils.getString(30047) + ": " + self.local_vfs.root_path) + utils.log(utils.getString(30048) + ": " + self.remote_vfs.root_path) #run the correct mode if(mode == self.Backup): utils.log(utils.getString(30023) + " - " + utils.getString(30016)) - self.fileManager = FileManager(self.local_path,XBMCFileSystem()) + self.fileManager = FileManager(self.local_vfs) #for backups check if remote path exists - if(self.vfs.exists(self.remote_path)): + if(self.remote_vfs.exists(self.remote_vfs.root_path)): #this will fail - need a disclaimer here utils.log(utils.getString(30050)) @@ -168,9 +157,10 @@ class XbmcBackup: total_backups = int(utils.getSetting('backup_rotation')) if(total_backups > 0): - dirs,files = self.vfs.listdir(self.remote_root) + dirs,files = self.remote_vfs.listdir(self.remote_vfs.root_path) if(len(dirs) > total_backups): #remove backups to equal total wanted + dirs.sort() remove_num = len(dirs) - total_backups - 1 self.filesTotal = self.filesTotal + remove_num + 1 @@ -178,19 +168,19 @@ class XbmcBackup: while(remove_num >= 0 and not self.checkCancel()): self.updateProgress(utils.getString(30054) + " " + dirs[remove_num]) utils.log("Removing backup " + dirs[remove_num]) - self.vfs.rmdir(self.remote_root + dirs[remove_num] + "/") + self.remote_vfs.rmdir(self.remote_vfs.root_path + dirs[remove_num] + "/") remove_num = remove_num - 1 else: utils.log(utils.getString(30023) + " - " + utils.getString(30017)) - self.fileManager = FileManager(self.remote_path,self.vfs) + self.fileManager = FileManager(self.remote_vfs) #for restores remote path must exist - if(self.vfs.exists(self.remote_path)): + if(self.remote_vfs.exists(self.remote_vfs.root_path)): self.restoreFiles() else: - xbmcgui.Dialog().ok(utils.getString(30010),utils.getString(30045),self.remote_path) + xbmcgui.Dialog().ok(utils.getString(30010),utils.getString(30045),self.remote_vfs.root_path) if(utils.getSetting('run_silent') == 'false' and not runSilent): self.progressBar.close() @@ -198,7 +188,7 @@ class XbmcBackup: def syncFiles(self): #make the remote directory - self.vfs.mkdir(self.remote_path) + self.remote_vfs.mkdir(self.remote_vfs.root_path) utils.log(utils.getString(30051)) self.fileManager.createFileList() @@ -206,7 +196,7 @@ class XbmcBackup: allFiles = self.fileManager.getFileList() #write list from local to remote - self.writeFiles(allFiles,self.local_path,self.remote_path) + self.writeFiles(allFiles,self.local_vfs,self.remote_vfs) def restoreFiles(self): self.fileManager.createFileList() @@ -215,25 +205,25 @@ class XbmcBackup: allFiles = self.fileManager.getFileList() #write list from remote to local - self.writeFiles(allFiles,self.remote_path,self.local_path) + self.writeFiles(allFiles,self.remote_vfs,self.local_vfs) #call update addons to refresh everything xbmc.executebuiltin('UpdateLocalAddons') def writeFiles(self,fileList,source,dest): - utils.log("Writing files to: " + dest) + utils.log("Writing files to: " + dest.root_path) self.filesTotal = len(fileList) self.filesLeft = self.filesTotal #write each file from source to destination for aFile in fileList: if(not self.checkCancel()): - utils.log('Writing file: ' + source + aFile,xbmc.LOGDEBUG) + utils.log('Writing file: ' + source.root_path + aFile,xbmc.LOGDEBUG) self.updateProgress(aFile) if (aFile.startswith("-")): - xbmcvfs.mkdir(xbmc.makeLegalFilename(dest + aFile[1:],False)) + dest.mkdir(dest.root_path + aFile[1:]) else: - xbmcvfs.copy(xbmc.makeLegalFilename(source + aFile),xbmc.makeLegalFilename(dest + aFile,False)) + dest.copy(source.root_path + aFile,dest.root_path + aFile) def updateProgress(self,message=''): self.filesLeft = self.filesLeft - 1 @@ -251,4 +241,4 @@ class XbmcBackup: return result def isReady(self): - return True if self.remote_root != '' else False + return True if self.remote_vfs != None else False diff --git a/resources/lib/dropbox/pkg_resources.py b/resources/lib/dropbox/pkg_resources.py index e9616bb..d6f7535 100644 --- a/resources/lib/dropbox/pkg_resources.py +++ b/resources/lib/dropbox/pkg_resources.py @@ -1,4 +1,4 @@ import resources.lib.utils as utils -def resource_filename(*args): - return utils.addon_dir() + "resources/lib/dropbox/trusted-certs.crt" +def resource_filename(*args): + return utils.addon_dir() + "/resources/lib/dropbox/trusted-certs.crt" diff --git a/resources/lib/vfs.py b/resources/lib/vfs.py index 9467626..8aebc9e 100644 --- a/resources/lib/vfs.py +++ b/resources/lib/vfs.py @@ -1,45 +1,77 @@ import utils as utils import xbmcvfs +import xbmcgui from dropbox import client, rest, session APP_KEY = 'f5wlmek6aoriqax' APP_SECRET = 'b1461sje1kxgzet' class Vfs: - type = 'none' + root_path = None - def listdir(directory): + def set_root(self,rootString): + self.root_path = rootString + + #fix slashes + self.root_path = self.root_path.replace("\\","/") + + #check if trailing slash is included + if(self.root_path[-1:] != "/"): + self.root_path = self.root_path + "/" + + def listdir(self,directory): return {} - def mkdir(directory): + def mkdir(self,directory): return True - def copy(source,dest): + def copy(self,source,dest): return True - def rmdir(directory): + def rmdir(self,directory): return True - def exists(aFile): + def exists(self,aFile): return True class XBMCFileSystem(Vfs): - self.root_path - def listdir(directory): + + def listdir(self,directory): return xbmcvfs.listdir(directory) - def mkdir(directory): + def mkdir(self,directory): return xbmcvfs.mkdir(directory) - def rmdir(directory): + def copy(self,source,dest): + return xbmcvfs.copy(source,dest) + + def rmdir(self,directory): return xbmcvfs.rmdir(directory,True) - def exists(aFile): + def exists(self,aFile): return xbmcvfs.exists(aFile) -class DropboxFilesystem(Vfs): - +class DropboxFileSystem(Vfs): + user_token = None + def __init__(self): - session = session.DropboxSession(APP_KEY,APP_SECRET,"app_folder") - token = session.obtain_request_token() - access_token = session.obtain_access_token(token) + self.user_token = utils.getSetting('dropbox_token') + sess = session.DropboxSession(APP_KEY,APP_SECRET,"app_folder") + + if(self.user_token == ''): + token = sess.obtain_request_token() + url = sess.build_authorize_url(token) + try: + self.user_token = sess.obtain_access_token(token) + utils.setSetting("dropbox_token",self.user_token) + except: + xbmcgui.Dialog().ok(utils.getString(30010),"Authorize Dropbox URL, also in log",url) + utils.log("Authorize URL: " + url) + + self.client = client.DropboxClient(sess) + utils.log(self.client.account_info()) + + + + + diff --git a/resources/settings.xml b/resources/settings.xml index 1687ff3..72f36cc 100644 --- a/resources/settings.xml +++ b/resources/settings.xml @@ -5,10 +5,8 @@ - - - - + +