|
|
|
@ -1,5 +1,6 @@ |
|
|
|
from magic import from_file |
|
|
|
from gapi.api import API |
|
|
|
from googleapiclient.http import MediaFileUpload |
|
|
|
import re |
|
|
|
APPLICATION_NAME = "Google Calendar API Python" |
|
|
|
|
|
|
|
@ -41,6 +42,7 @@ class drive_api(API): |
|
|
|
self.__fill_in__(root) |
|
|
|
|
|
|
|
def get_file_by_path(self,path,ret_file = True,no_parent = True): |
|
|
|
'''gets a file or folder by remote path''' |
|
|
|
if isinstance(path,str): |
|
|
|
path = split_path(path) |
|
|
|
parent = self.filesystem |
|
|
|
@ -51,7 +53,7 @@ class drive_api(API): |
|
|
|
self.__fill_in__(parent) |
|
|
|
parent = parent['folders'][sub] |
|
|
|
except KeyError: |
|
|
|
raise FileNotFoundError() |
|
|
|
raise FileNotFoundError |
|
|
|
try: |
|
|
|
if ret_file: |
|
|
|
if not 'files' in parent.keys(): |
|
|
|
@ -65,13 +67,19 @@ class drive_api(API): |
|
|
|
ret[0] = without(ret[0],'parent') |
|
|
|
return ret |
|
|
|
except KeyError: |
|
|
|
raise FileNotFoundError() |
|
|
|
raise FileNotFoundError |
|
|
|
|
|
|
|
def upload(self,local_path,remote_path): |
|
|
|
'''creates file if it does not exists otherwise update the file''' |
|
|
|
isdir = os.path.isdir(local_path) |
|
|
|
try: |
|
|
|
fid = get_file_by_path(remote_path) |
|
|
|
except FileNotFoundError: |
|
|
|
path = split_path(remote_path) |
|
|
|
end = path.pop() |
|
|
|
parent = mkdirs(path) |
|
|
|
parent_id = parent[0]['id'] |
|
|
|
|
|
|
|
|
|
|
|
def mkdirs(self,path): |
|
|
|
'''makes directories if they do not exist already''' |
|
|
|
@ -95,6 +103,7 @@ class drive_api(API): |
|
|
|
parent['folders'][name] = new_folder |
|
|
|
parent = new_folder |
|
|
|
return parent |
|
|
|
|
|
|
|
def __create_remote_folder__(self,name,parent): |
|
|
|
meta = { |
|
|
|
'name':name, |
|
|
|
@ -103,8 +112,8 @@ class drive_api(API): |
|
|
|
} |
|
|
|
return self.service.files().create(body = meta).execute() |
|
|
|
|
|
|
|
# finds all files and folders from a parent |
|
|
|
def __fill_in__(self,parent): |
|
|
|
'''finds all files and folders from a parent''' |
|
|
|
parent_id = parent[0]['id'] |
|
|
|
child_list = self.__list_children__(parent_id) |
|
|
|
parent['files'] = {} |
|
|
|
|