From defad1727af1bd5f311bcd71a10d0133abf9c601 Mon Sep 17 00:00:00 2001 From: Raphael Roberts Date: Sun, 18 Nov 2018 15:20:51 -0600 Subject: [PATCH] get_file_by_path works and added fullpath property --- gapi/drive_api.py | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/gapi/drive_api.py b/gapi/drive_api.py index c3eeb63..c1b00ba 100644 --- a/gapi/drive_api.py +++ b/gapi/drive_api.py @@ -55,12 +55,29 @@ class drive_file: self.api = api self.info = info self.parent = parent + self._fullpath = None @classmethod def from_id(cls,id,api,parent): info = api.service.files().get(fileId='root').execute() return cls(api,info,parent) + @property + def fullpath(self): + if self._fullpath is None: + if self.parent is None: + path = '/' + else: + path = pathjoin(self.parent.fullpath,self.name) + self._fullpath = path + return self._fullpath + + def __str__(self): + return '{}@{}'.format(type(self).__name__,self.fullpath) + + def __repr__(self): + return str(self) + class drive_folder(drive_file): def __init__(self, api, @@ -87,9 +104,9 @@ class drive_folder(drive_file): for child in child_list: name = child['name'] if is_folder(child): - parent.folders[name] = drive_folder(self.api,child,self) + self.folders[name] = drive_folder(self.api,child,self) else: - parent.files[name] = drive_file(self.api,child,self) + self.files[name] = drive_file(self.api,child,self) def __list_children__(self): page_token = None @@ -114,7 +131,6 @@ class drive_api(API): ): super().__init__('drive',scopes,app_name,client_secret_file,credentials_dir,version) self.root = drive_folder.from_id('root',self,None) - root def get_file_by_path(self,path,ret_file = True): if path == '/':