|
|
|
@ -44,6 +44,64 @@ def remote_path_join(path,*paths): |
|
|
|
ret += path |
|
|
|
return ret |
|
|
|
|
|
|
|
class drive_file: |
|
|
|
def __init__(self, |
|
|
|
api, |
|
|
|
info, |
|
|
|
parent |
|
|
|
): |
|
|
|
self.id = info.pop('id') |
|
|
|
self.name = info.pop('name') |
|
|
|
self.api = api |
|
|
|
self.info = info |
|
|
|
self.parent = parent |
|
|
|
|
|
|
|
@classmethod |
|
|
|
def from_id(cls,id,api,parent): |
|
|
|
info = api.service.files().get(fileId='root').execute() |
|
|
|
return cls(api,info,parent) |
|
|
|
|
|
|
|
class drive_folder(drive_file): |
|
|
|
def __init__(self, |
|
|
|
api, |
|
|
|
info, |
|
|
|
parent |
|
|
|
): |
|
|
|
super().__init__(api,info,parent) |
|
|
|
self.folders = {} |
|
|
|
self.files = {} |
|
|
|
|
|
|
|
def create_folder(self,name): |
|
|
|
meta = { |
|
|
|
'name':name, |
|
|
|
'mimeType':'application/vnd.google-apps.folder', |
|
|
|
'parents' : [self.id] |
|
|
|
} |
|
|
|
info = self.api.service.files().create(body = meta).execute() |
|
|
|
self.folders['name'] = drive_folder(self.api,info,self) |
|
|
|
|
|
|
|
def refresh(self): |
|
|
|
'''finds all files and folders from a parent''' |
|
|
|
parent_id = self.id |
|
|
|
child_list = self.__list_children__() |
|
|
|
for child in child_list: |
|
|
|
name = child['name'] |
|
|
|
if is_folder(child): |
|
|
|
parent.folders[name] = drive_folder(self.api,child,self) |
|
|
|
else: |
|
|
|
parent.files[name] = drive_file(self.api,child,self) |
|
|
|
|
|
|
|
def __list_children__(self): |
|
|
|
page_token = None |
|
|
|
first = True |
|
|
|
while page_token or first: |
|
|
|
first = False |
|
|
|
response = self.api.service.files().list(q = "'{}' in parents and trashed = false".format(self.id),pageToken=page_token).execute() |
|
|
|
page_token = response.get('nextPageToken',None) |
|
|
|
for file in response['files']: |
|
|
|
yield file |
|
|
|
|
|
|
|
# def create_file(self, |
|
|
|
class drive_api(API): |
|
|
|
|
|
|
|
def __init__( |
|
|
|
@ -55,29 +113,29 @@ class drive_api(API): |
|
|
|
version = 'v3', |
|
|
|
): |
|
|
|
super().__init__('drive',scopes,app_name,client_secret_file,credentials_dir,version) |
|
|
|
root = {} |
|
|
|
self.filesystem = {'folders':{'/':root}} |
|
|
|
root_meta = self.service.files().get(fileId='root').execute() |
|
|
|
del root_meta['kind'] |
|
|
|
root[0] = root_meta |
|
|
|
root['parent'] = self.filesystem |
|
|
|
self.__fill_in__(root) |
|
|
|
self.root = drive_folder.from_id('root',self,None) |
|
|
|
root |
|
|
|
|
|
|
|
def get_file_by_path(self,path,ret_file = True,no_parent = True): |
|
|
|
if path == '/': |
|
|
|
return self.root |
|
|
|
'''gets a file or folder by remote path''' |
|
|
|
if isinstance(path,str): |
|
|
|
path = split_path(path) |
|
|
|
else: |
|
|
|
path = path.copy() |
|
|
|
parent = self.filesystem |
|
|
|
parent = self.root |
|
|
|
end = path.pop() |
|
|
|
for sub in path: |
|
|
|
try: |
|
|
|
if not 'folders' in parent.keys(): |
|
|
|
self.__fill_in__(parent) |
|
|
|
parent = parent['folders'][sub] |
|
|
|
except KeyError: |
|
|
|
raise FileNotFoundError |
|
|
|
if sub == '/': |
|
|
|
pass |
|
|
|
else: |
|
|
|
try: |
|
|
|
if not parent.folders: |
|
|
|
parent.refresh() |
|
|
|
parent = parent.folders[sub] |
|
|
|
except KeyError: |
|
|
|
raise FileNotFoundError |
|
|
|
try: |
|
|
|
if ret_file: |
|
|
|
if not 'files' in parent.keys(): |
|
|
|
@ -166,38 +224,12 @@ class drive_api(API): |
|
|
|
return new_f_meta |
|
|
|
|
|
|
|
def __create_remote_folder__(self,name,parent): |
|
|
|
meta = { |
|
|
|
'name':name, |
|
|
|
'mimeType':'application/vnd.google-apps.folder', |
|
|
|
'parents' : [parent[0]['id']] |
|
|
|
} |
|
|
|
return self.service.files().create(body = meta).execute() |
|
|
|
|
|
|
|
|
|
|
|
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'] = {} |
|
|
|
parent['folders'] = {} |
|
|
|
for child in child_list: |
|
|
|
del child['kind'] |
|
|
|
name = child.pop('name') |
|
|
|
child['parent'] = parent |
|
|
|
if is_folder(child): |
|
|
|
parent['folders'][name] = {} |
|
|
|
parent['folders'][name][0] = child |
|
|
|
else: |
|
|
|
parent['files'][name] = child |
|
|
|
|
|
|
|
def __list_children__(self,parent_id): |
|
|
|
page_token = None |
|
|
|
first = True |
|
|
|
while page_token or first: |
|
|
|
first = False |
|
|
|
response = self.service.files().list(q = "'{}' in parents and trashed = false".format(parent_id),pageToken=page_token).execute() |
|
|
|
page_token = response.get('nextPageToken',None) |
|
|
|
for file in response['files']: |
|
|
|
yield file |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
|
my_api = drive_api(APPLICATION_NAME,r'..\test\drive\client_secret.json',r'..\test\drive') |
|
|
|
|