2012-10-26 16:42:10 +02:00
|
|
|
import utils as utils
|
2012-11-05 19:25:25 +01:00
|
|
|
import xbmc
|
2012-11-02 21:59:40 +01:00
|
|
|
import xbmcvfs
|
2012-11-04 21:39:17 +01:00
|
|
|
import xbmcgui
|
2014-07-30 22:16:58 +02:00
|
|
|
import zipfile
|
|
|
|
import zlib
|
2014-10-09 21:30:39 +02:00
|
|
|
import os
|
2012-11-02 21:59:40 +01:00
|
|
|
from dropbox import client, rest, session
|
2014-10-09 21:30:39 +02:00
|
|
|
from pydrive.auth import GoogleAuth
|
|
|
|
from pydrive.drive import GoogleDrive
|
2012-10-26 16:42:10 +02:00
|
|
|
|
|
|
|
class Vfs:
|
2012-11-04 21:39:17 +01:00
|
|
|
root_path = None
|
2012-10-26 16:42:10 +02:00
|
|
|
|
2013-01-28 22:44:22 +01:00
|
|
|
def __init__(self,rootString):
|
|
|
|
self.set_root(rootString)
|
|
|
|
|
2012-11-04 21:39:17 +01:00
|
|
|
def set_root(self,rootString):
|
2012-11-05 18:13:48 +01:00
|
|
|
old_root = self.root_path
|
2012-11-04 21:39:17 +01:00
|
|
|
self.root_path = rootString
|
2014-08-01 00:29:59 +02:00
|
|
|
|
2012-11-04 21:39:17 +01:00
|
|
|
#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 + "/"
|
2012-11-05 18:13:48 +01:00
|
|
|
|
|
|
|
#return the old root
|
|
|
|
return old_root
|
2012-11-04 21:39:17 +01:00
|
|
|
|
|
|
|
def listdir(self,directory):
|
2012-10-26 16:42:10 +02:00
|
|
|
return {}
|
|
|
|
|
2012-11-04 21:39:17 +01:00
|
|
|
def mkdir(self,directory):
|
2012-10-26 16:42:10 +02:00
|
|
|
return True
|
|
|
|
|
2012-11-06 18:37:39 +01:00
|
|
|
def put(self,source,dest):
|
2012-10-26 16:42:10 +02:00
|
|
|
return True
|
2012-11-02 21:59:40 +01:00
|
|
|
|
2012-11-04 21:39:17 +01:00
|
|
|
def rmdir(self,directory):
|
2012-11-02 21:59:40 +01:00
|
|
|
return True
|
|
|
|
|
2014-08-01 20:12:43 +02:00
|
|
|
def rmfile(self,aFile):
|
|
|
|
return True
|
|
|
|
|
2012-11-04 21:39:17 +01:00
|
|
|
def exists(self,aFile):
|
2012-11-02 21:59:40 +01:00
|
|
|
return True
|
2014-07-30 22:16:58 +02:00
|
|
|
|
|
|
|
def rename(self,aFile,newName):
|
|
|
|
return True
|
|
|
|
|
|
|
|
def cleanup(self):
|
|
|
|
return True
|
2012-10-26 16:42:10 +02:00
|
|
|
|
2012-11-02 21:59:40 +01:00
|
|
|
class XBMCFileSystem(Vfs):
|
2013-01-28 22:44:22 +01:00
|
|
|
|
2012-11-04 21:39:17 +01:00
|
|
|
def listdir(self,directory):
|
2012-11-02 21:59:40 +01:00
|
|
|
return xbmcvfs.listdir(directory)
|
|
|
|
|
2012-11-04 21:39:17 +01:00
|
|
|
def mkdir(self,directory):
|
2014-08-08 17:37:16 +02:00
|
|
|
return xbmcvfs.mkdir(xbmc.translatePath(directory))
|
2012-11-02 21:59:40 +01:00
|
|
|
|
2012-11-06 18:37:39 +01:00
|
|
|
def put(self,source,dest):
|
2014-08-08 17:37:16 +02:00
|
|
|
return xbmcvfs.copy(xbmc.translatePath(source),xbmc.translatePath(dest))
|
2012-11-06 18:37:39 +01:00
|
|
|
|
2012-11-04 21:39:17 +01:00
|
|
|
def rmdir(self,directory):
|
2012-11-02 21:59:40 +01:00
|
|
|
return xbmcvfs.rmdir(directory,True)
|
|
|
|
|
2014-08-01 20:12:43 +02:00
|
|
|
def rmfile(self,aFile):
|
|
|
|
return xbmcvfs.delete(aFile)
|
|
|
|
|
2014-07-30 22:16:58 +02:00
|
|
|
def rename(self,aFile,newName):
|
|
|
|
return xbmcvfs.rename(aFile, newName)
|
|
|
|
|
2012-11-04 21:39:17 +01:00
|
|
|
def exists(self,aFile):
|
2012-11-02 21:59:40 +01:00
|
|
|
return xbmcvfs.exists(aFile)
|
|
|
|
|
2014-07-30 22:16:58 +02:00
|
|
|
class ZipFileSystem(Vfs):
|
|
|
|
zip = None
|
|
|
|
|
|
|
|
def __init__(self,rootString,mode):
|
|
|
|
self.root_path = ""
|
2015-04-24 16:23:56 +02:00
|
|
|
self.zip = zipfile.ZipFile(rootString,mode=mode,compression=zipfile.ZIP_DEFLATED,allowZip64=True)
|
2014-07-30 22:16:58 +02:00
|
|
|
|
|
|
|
def listdir(self,directory):
|
2014-08-01 19:57:55 +02:00
|
|
|
return [[],[]]
|
2014-07-30 22:16:58 +02:00
|
|
|
|
|
|
|
def mkdir(self,directory):
|
|
|
|
#self.zip.write(directory[len(self.root_path):])
|
2014-08-01 19:57:55 +02:00
|
|
|
return False
|
2014-07-30 22:16:58 +02:00
|
|
|
|
|
|
|
def put(self,source,dest):
|
2014-08-08 17:26:39 +02:00
|
|
|
|
|
|
|
aFile = xbmcvfs.File(xbmc.translatePath(source),'r')
|
|
|
|
|
2015-04-24 16:23:56 +02:00
|
|
|
self.zip.writestr(utils.encode(dest),aFile.read())
|
2014-08-08 17:26:39 +02:00
|
|
|
|
2014-07-30 22:16:58 +02:00
|
|
|
return True
|
|
|
|
|
|
|
|
def rmdir(self,directory):
|
2014-08-01 19:57:55 +02:00
|
|
|
return False
|
2014-07-30 22:16:58 +02:00
|
|
|
|
|
|
|
def exists(self,aFile):
|
2014-08-01 19:57:55 +02:00
|
|
|
return False
|
2014-07-30 22:16:58 +02:00
|
|
|
|
|
|
|
def cleanup(self):
|
|
|
|
self.zip.close()
|
2014-08-01 19:57:55 +02:00
|
|
|
|
|
|
|
def extract(self,path):
|
|
|
|
#extract zip file to path
|
|
|
|
self.zip.extractall(path)
|
2014-07-30 22:16:58 +02:00
|
|
|
|
2012-11-04 21:39:17 +01:00
|
|
|
class DropboxFileSystem(Vfs):
|
2012-11-05 19:25:25 +01:00
|
|
|
client = None
|
2014-10-09 21:30:39 +02:00
|
|
|
APP_KEY = ''
|
|
|
|
APP_SECRET = ''
|
2012-11-04 21:39:17 +01:00
|
|
|
|
2013-01-28 22:44:22 +01:00
|
|
|
def __init__(self,rootString):
|
2013-03-04 17:22:09 +01:00
|
|
|
self.set_root(rootString)
|
2014-10-09 21:30:39 +02:00
|
|
|
|
|
|
|
self.APP_KEY = utils.getSetting('dropbox_key')
|
|
|
|
self.APP_SECRET = utils.getSetting('dropbox_secret')
|
|
|
|
|
2013-01-28 22:44:22 +01:00
|
|
|
self.setup()
|
|
|
|
|
|
|
|
def setup(self):
|
2014-10-09 21:30:39 +02:00
|
|
|
if(self.APP_KEY == '' or self.APP_SECRET == ''):
|
2012-12-30 22:18:59 +01:00
|
|
|
xbmcgui.Dialog().ok(utils.getString(30010),utils.getString(30058),utils.getString(30059))
|
|
|
|
return
|
|
|
|
|
2012-11-05 19:25:25 +01:00
|
|
|
user_token_key,user_token_secret = self.getToken()
|
|
|
|
|
2014-10-09 21:30:39 +02:00
|
|
|
sess = session.DropboxSession(self.APP_KEY,self.APP_SECRET,"app_folder")
|
2012-11-04 21:39:17 +01:00
|
|
|
|
2012-11-05 19:25:25 +01:00
|
|
|
if(user_token_key == '' and user_token_secret == ''):
|
2012-11-04 21:39:17 +01:00
|
|
|
token = sess.obtain_request_token()
|
|
|
|
url = sess.build_authorize_url(token)
|
2012-11-05 19:25:25 +01:00
|
|
|
|
|
|
|
#print url in log
|
|
|
|
utils.log("Authorize URL: " + url)
|
2012-11-06 20:09:33 +01:00
|
|
|
xbmcgui.Dialog().ok(utils.getString(30010),utils.getString(30056),utils.getString(30057))
|
2012-11-05 19:25:25 +01:00
|
|
|
|
|
|
|
#if user authorized this will work
|
|
|
|
user_token = sess.obtain_access_token(token)
|
|
|
|
self.setToken(user_token.key,user_token.secret)
|
|
|
|
|
|
|
|
else:
|
|
|
|
sess.set_token(user_token_key,user_token_secret)
|
|
|
|
|
|
|
|
self.client = client.DropboxClient(sess)
|
2012-12-13 21:18:14 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
utils.log(str(self.client.account_info()))
|
|
|
|
except:
|
|
|
|
#this didn't work, delete the token file
|
|
|
|
self.deleteToken()
|
2012-11-05 19:25:25 +01:00
|
|
|
|
2012-11-05 22:46:17 +01:00
|
|
|
def listdir(self,directory):
|
|
|
|
if(self.client != None and self.exists(directory)):
|
|
|
|
files = []
|
|
|
|
dirs = []
|
|
|
|
metadata = self.client.metadata(directory)
|
|
|
|
|
|
|
|
for aFile in metadata['contents']:
|
|
|
|
if(aFile['is_dir']):
|
2014-10-10 15:49:21 +02:00
|
|
|
dirs.append(utils.encode(aFile['path'][len(directory):]))
|
2012-11-05 22:46:17 +01:00
|
|
|
else:
|
2014-10-10 15:49:21 +02:00
|
|
|
files.append(utils.encode(aFile['path'][len(directory):]))
|
2012-11-05 22:46:17 +01:00
|
|
|
|
|
|
|
return [dirs,files]
|
|
|
|
else:
|
|
|
|
return [[],[]]
|
|
|
|
|
|
|
|
|
2012-11-05 19:25:25 +01:00
|
|
|
def mkdir(self,directory):
|
2013-03-04 17:22:09 +01:00
|
|
|
directory = self._fix_slashes(directory)
|
2012-11-05 19:25:25 +01:00
|
|
|
if(self.client != None):
|
|
|
|
if(not self.exists(directory)):
|
|
|
|
self.client.file_create_folder(directory)
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2012-11-05 22:46:17 +01:00
|
|
|
def rmdir(self,directory):
|
2013-03-04 17:22:09 +01:00
|
|
|
directory = self._fix_slashes(directory)
|
2012-11-05 22:46:17 +01:00
|
|
|
if(self.client != None and self.exists(directory)):
|
2014-07-29 15:43:34 +02:00
|
|
|
#dropbox is stupid and will refuse to do this sometimes, need to delete recursively
|
|
|
|
dirs,files = self.listdir(directory)
|
|
|
|
|
|
|
|
for aDir in dirs:
|
|
|
|
self.rmdir(aDir)
|
|
|
|
|
|
|
|
#finally remove the root directory
|
2012-11-05 22:46:17 +01:00
|
|
|
self.client.file_delete(directory)
|
2014-08-01 20:12:43 +02:00
|
|
|
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2014-11-21 20:30:22 +01:00
|
|
|
def rmfile(self,aFile):
|
2014-08-01 20:12:43 +02:00
|
|
|
aFile = self._fix_slashes(aFile)
|
2014-11-21 20:30:22 +01:00
|
|
|
|
2014-08-01 20:12:43 +02:00
|
|
|
if(self.client != None and self.exists(aFile)):
|
|
|
|
self.client.file_delete(aFile)
|
|
|
|
return True
|
2012-11-05 22:46:17 +01:00
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2012-11-05 19:25:25 +01:00
|
|
|
def exists(self,aFile):
|
2013-03-04 17:22:09 +01:00
|
|
|
aFile = self._fix_slashes(aFile)
|
2012-11-05 19:25:25 +01:00
|
|
|
if(self.client != None):
|
2012-11-04 21:39:17 +01:00
|
|
|
try:
|
2012-11-05 19:25:25 +01:00
|
|
|
meta_data = self.client.metadata(aFile)
|
|
|
|
#if we make it here the file does exist
|
|
|
|
return True
|
2012-11-04 21:39:17 +01:00
|
|
|
except:
|
2012-11-05 19:25:25 +01:00
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return False
|
2012-11-04 21:39:17 +01:00
|
|
|
|
2014-06-03 20:34:02 +02:00
|
|
|
def put(self,source,dest,retry=True):
|
2013-03-04 17:22:09 +01:00
|
|
|
dest = self._fix_slashes(dest)
|
|
|
|
|
2012-11-05 19:25:25 +01:00
|
|
|
if(self.client != None):
|
|
|
|
f = open(source,'rb')
|
2012-11-05 22:46:17 +01:00
|
|
|
try:
|
|
|
|
response = self.client.put_file(dest,f,True)
|
|
|
|
return True
|
|
|
|
except:
|
|
|
|
#if we have an exception retry
|
2014-06-03 20:34:02 +02:00
|
|
|
if(retry):
|
|
|
|
return self.put(source,dest,False)
|
|
|
|
else:
|
|
|
|
#tried once already, just quit
|
|
|
|
return False
|
2012-11-05 19:25:25 +01:00
|
|
|
else:
|
|
|
|
return False
|
2012-11-06 18:37:39 +01:00
|
|
|
|
|
|
|
def get_file(self,source,dest):
|
|
|
|
if(self.client != None):
|
|
|
|
#write the file locally
|
|
|
|
out = open(dest,'wb')
|
|
|
|
f = self.client.get_file(source).read()
|
|
|
|
out.write(f)
|
|
|
|
out.close()
|
2013-03-04 17:27:25 +01:00
|
|
|
return True
|
2012-11-06 18:37:39 +01:00
|
|
|
else:
|
|
|
|
return False
|
2013-03-04 17:22:09 +01:00
|
|
|
|
|
|
|
def _fix_slashes(self,filename):
|
|
|
|
return filename.replace('\\','/')
|
|
|
|
|
2012-11-05 19:25:25 +01:00
|
|
|
def setToken(self,key,secret):
|
|
|
|
#write the token files
|
|
|
|
token_file = open(xbmc.translatePath(utils.data_dir() + "tokens.txt"),'w')
|
|
|
|
token_file.write("%s|%s" % (key,secret))
|
|
|
|
token_file.close()
|
|
|
|
|
|
|
|
def getToken(self):
|
|
|
|
#get tokens, if they exist
|
|
|
|
if(xbmcvfs.exists(xbmc.translatePath(utils.data_dir() + "tokens.txt"))):
|
|
|
|
token_file = open(xbmc.translatePath(utils.data_dir() + "tokens.txt"))
|
|
|
|
key,secret = token_file.read().split('|')
|
|
|
|
token_file.close()
|
|
|
|
|
|
|
|
return [key,secret]
|
|
|
|
else:
|
|
|
|
return ["",""]
|
2012-12-13 21:18:14 +01:00
|
|
|
|
|
|
|
def deleteToken(self):
|
|
|
|
if(xbmcvfs.exists(xbmc.translatePath(utils.data_dir() + "tokens.txt"))):
|
|
|
|
xbmcvfs.delete(xbmc.translatePath(utils.data_dir() + "tokens.txt"))
|
2012-11-04 21:39:17 +01:00
|
|
|
|
|
|
|
|
2014-10-09 21:30:39 +02:00
|
|
|
class GoogleDriveFilesystem(Vfs):
|
|
|
|
drive = None
|
|
|
|
history = {}
|
|
|
|
CLIENT_ID = ''
|
|
|
|
CLIENT_SECRET = ''
|
|
|
|
FOLDER_TYPE = 'application/vnd.google-apps.folder'
|
|
|
|
|
|
|
|
def __init__(self,rootString):
|
|
|
|
self.set_root(rootString)
|
|
|
|
|
|
|
|
self.CLIENT_ID = utils.getSetting('google_drive_id')
|
|
|
|
self.CLIENT_SECRET = utils.getSetting('google_drive_secret')
|
|
|
|
|
|
|
|
self.setup()
|
|
|
|
|
|
|
|
def setup(self):
|
|
|
|
#create authorization helper and load default settings
|
|
|
|
gauth = GoogleAuth(xbmc.validatePath(xbmc.translatePath(utils.addon_dir() + '/resources/lib/pydrive/settings.yaml')))
|
|
|
|
gauth.LoadClientConfigSettings()
|
|
|
|
|
|
|
|
#check if this user is already authorized
|
|
|
|
if(not xbmcvfs.exists(xbmc.translatePath(utils.data_dir() + "google_drive.dat"))):
|
|
|
|
settings = {"client_id":self.CLIENT_ID,'client_secret':self.CLIENT_SECRET}
|
|
|
|
|
|
|
|
drive_url = gauth.GetAuthUrl(settings)
|
|
|
|
|
|
|
|
utils.log("Google Drive Authorize URL: " + drive_url)
|
|
|
|
|
|
|
|
code = xbmcgui.Dialog().input('Google Drive Validation Code','Input the Validation code after authorizing this app')
|
|
|
|
|
|
|
|
gauth.Auth(code)
|
|
|
|
gauth.SaveCredentialsFile(xbmc.validatePath(xbmc.translatePath(utils.data_dir() + 'google_drive.dat')))
|
|
|
|
else:
|
|
|
|
gauth.LoadCredentialsFile(xbmc.validatePath(xbmc.translatePath(utils.data_dir() + 'google_drive.dat')))
|
|
|
|
|
|
|
|
#create the drive object
|
|
|
|
self.drive = GoogleDrive(gauth)
|
2014-10-09 21:42:59 +02:00
|
|
|
|
|
|
|
#make sure we have the folder we need
|
|
|
|
xbmc_folder = self._getGoogleFile(self.root_path)
|
2014-10-09 22:19:24 +02:00
|
|
|
print xbmc_folder
|
2014-10-09 21:42:59 +02:00
|
|
|
if(xbmc_folder == None):
|
|
|
|
self.mkdir(self.root_path)
|
|
|
|
|
2014-10-09 21:30:39 +02:00
|
|
|
|
|
|
|
def listdir(self,directory):
|
|
|
|
files = []
|
|
|
|
dirs = []
|
|
|
|
|
|
|
|
if(not directory.startswith('/')):
|
|
|
|
directory = '/' + directory
|
|
|
|
|
|
|
|
#get the id of this folder
|
|
|
|
parentFolder = self._getGoogleFile(directory)
|
|
|
|
|
|
|
|
#need to do this after
|
|
|
|
if(not directory.endswith('/')):
|
|
|
|
directory = directory + '/'
|
|
|
|
|
|
|
|
if(parentFolder != None):
|
|
|
|
|
|
|
|
fileList = self.drive.ListFile({'q':"'" + parentFolder['id'] + "' in parents and trashed = false"}).GetList()
|
|
|
|
|
|
|
|
for aFile in fileList:
|
|
|
|
if(aFile['mimeType'] == self.FOLDER_TYPE):
|
2014-10-10 15:49:21 +02:00
|
|
|
dirs.append(utils.encode(aFile['title']))
|
2014-10-09 21:30:39 +02:00
|
|
|
else:
|
2014-10-10 15:49:21 +02:00
|
|
|
files.append(utils.encode(aFile['title']))
|
2014-10-09 21:30:39 +02:00
|
|
|
|
|
|
|
|
|
|
|
return [dirs,files]
|
2012-11-04 21:39:17 +01:00
|
|
|
|
2014-10-09 21:30:39 +02:00
|
|
|
def mkdir(self,directory):
|
|
|
|
result = True
|
|
|
|
|
|
|
|
if(not directory.startswith('/')):
|
|
|
|
directory = '/' + directory
|
|
|
|
|
2014-10-09 22:19:24 +02:00
|
|
|
if(directory.endswith('/')):
|
|
|
|
directory = directory[:-1]
|
|
|
|
|
2014-10-09 21:30:39 +02:00
|
|
|
#split the string by the directory separator
|
|
|
|
pathList = os.path.split(directory)
|
2014-10-09 22:19:24 +02:00
|
|
|
|
2014-10-09 21:30:39 +02:00
|
|
|
if(pathList[0] == '/'):
|
2014-10-09 22:19:24 +02:00
|
|
|
|
2014-10-09 21:30:39 +02:00
|
|
|
#we're at the root, just make the folder
|
|
|
|
newFolder = self.drive.CreateFile({'title': pathList[1], 'parent':'root','mimeType':self.FOLDER_TYPE})
|
|
|
|
newFolder.Upload()
|
|
|
|
else:
|
|
|
|
#get the id of the parent folder
|
|
|
|
parentFolder = self._getGoogleFile(pathList[0])
|
|
|
|
|
|
|
|
if(parentFolder != None):
|
2014-10-09 22:19:24 +02:00
|
|
|
newFolder = self.drive.CreateFile({'title': pathList[1],"parents":[{'kind':'drive#fileLink','id':parentFolder['id']}],'mimeType':self.FOLDER_TYPE})
|
2014-10-09 21:30:39 +02:00
|
|
|
newFolder.Upload()
|
|
|
|
else:
|
|
|
|
result = False
|
|
|
|
|
|
|
|
return result
|
2012-11-04 21:39:17 +01:00
|
|
|
|
2014-10-09 21:30:39 +02:00
|
|
|
def put(self,source,dest):
|
|
|
|
result = True
|
|
|
|
|
|
|
|
#make the name separate from the path
|
|
|
|
if(not dest.startswith('/')):
|
|
|
|
dest = '/' + dest
|
|
|
|
|
|
|
|
pathList = os.path.split(dest)
|
|
|
|
|
|
|
|
#get the parent location
|
|
|
|
parentFolder = self._getGoogleFile(pathList[0])
|
|
|
|
|
|
|
|
if(parentFolder != None):
|
|
|
|
#create a new file in this folder
|
|
|
|
newFile = self.drive.CreateFile({"title":pathList[1],"parents":[{'kind':'drive#fileLink','id':parentFolder['id']}]})
|
|
|
|
newFile.SetContentFile(source)
|
|
|
|
newFile.Upload()
|
|
|
|
else:
|
|
|
|
result = False
|
|
|
|
|
|
|
|
return result
|
2012-11-04 21:39:17 +01:00
|
|
|
|
2014-10-09 21:30:39 +02:00
|
|
|
def get_file(self,source, dest):
|
2014-11-05 15:47:47 +01:00
|
|
|
result = True
|
|
|
|
|
2014-10-09 21:30:39 +02:00
|
|
|
#get the id of this file
|
|
|
|
file = self._getGoogleFile(source)
|
|
|
|
|
|
|
|
if(file != None):
|
|
|
|
file.GetContentFile(dest)
|
2014-11-05 15:47:47 +01:00
|
|
|
else:
|
|
|
|
result = False
|
|
|
|
|
|
|
|
return result
|
2014-10-09 21:30:39 +02:00
|
|
|
|
|
|
|
def rmdir(self,directory):
|
|
|
|
result = True
|
|
|
|
|
|
|
|
#check that the folder exists
|
|
|
|
folder = self._getGoogleFile(directory)
|
|
|
|
|
|
|
|
if(folder != None):
|
|
|
|
#delete the folder
|
|
|
|
folder.Delete()
|
|
|
|
else:
|
|
|
|
result = False
|
2012-11-04 21:39:17 +01:00
|
|
|
|
2014-10-09 21:30:39 +02:00
|
|
|
return result
|
2012-11-04 21:39:17 +01:00
|
|
|
|
2014-10-09 21:30:39 +02:00
|
|
|
def rmfile(self,aFile):
|
|
|
|
#really just the same as the remove directory function
|
|
|
|
return self.rmdir(aFile)
|
|
|
|
|
|
|
|
def exists(self,aFile):
|
|
|
|
#attempt to get this file
|
|
|
|
foundFile = self._getGoogleFile(aFile)
|
|
|
|
|
|
|
|
if(foundFile != None):
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def rename(self,aFile,newName):
|
|
|
|
return True
|
|
|
|
|
|
|
|
def _getGoogleFile(self,file):
|
|
|
|
result = None
|
2014-10-09 22:19:24 +02:00
|
|
|
|
2014-10-09 21:30:39 +02:00
|
|
|
#file must start with / and not end with one (even directory)
|
|
|
|
if(not file.startswith('/')):
|
|
|
|
file = '/' + file
|
|
|
|
|
|
|
|
if(file.endswith('/')):
|
|
|
|
file = file[:-1]
|
|
|
|
|
2014-10-09 22:19:24 +02:00
|
|
|
if(self.history.has_key(file)):
|
2012-11-04 21:39:17 +01:00
|
|
|
|
2014-10-09 21:30:39 +02:00
|
|
|
result = self.history[file]
|
|
|
|
else:
|
|
|
|
pathList = os.path.split(file)
|
|
|
|
|
|
|
|
#end of recurision, we got the root
|
|
|
|
if(pathList[0] == '/'):
|
|
|
|
#get the id of this file (if it exists)
|
|
|
|
file_list = self.drive.ListFile({'q':"title='" + pathList[1] + "' and 'root' in parents and trashed=false"}).GetList()
|
|
|
|
|
|
|
|
if(len(file_list) > 0):
|
|
|
|
result = file_list[0]
|
|
|
|
self.history[pathList[1]] = result
|
|
|
|
else:
|
|
|
|
#recurse down the tree
|
|
|
|
current_file = pathList[1]
|
|
|
|
|
2014-10-09 22:19:24 +02:00
|
|
|
parentId = self._getGoogleFile(pathList[0])
|
2014-10-09 21:30:39 +02:00
|
|
|
|
|
|
|
if(parentId != None):
|
|
|
|
self.history[pathList[0]] = parentId
|
|
|
|
|
|
|
|
#attempt to get the id of this file, with this parent
|
|
|
|
file_list = file_list = self.drive.ListFile({'q':"title='" + current_file + "' and '" + parentId['id'] + "' in parents and trashed=false"}).GetList()
|
|
|
|
|
|
|
|
if(len(file_list) > 0):
|
|
|
|
result = file_list[0]
|
|
|
|
self.history[file] = result
|
|
|
|
|
|
|
|
|
|
|
|
return result
|
|
|
|
|