Drive¶
implement api to access google drive
-
class
pysuite.drive.
Drive
(service: googleapiclient.discovery.Resource, max_retry: int = 0, sleep: int = 5)[source]¶ Bases:
object
Class to interact with Google Drive API
- Parameters
service – an authorized Google Drive service client.
max_retry – max number of retry on quota exceeded error. if 0 or less, no retry will be attempted.
sleep – base number of seconds between retries. the sleep time is exponentially increased after each retry.
-
copy
(id: str, name: str, parent_id: Optional[str] = None) → str[source]¶ copy target file and give the new file specified name. return the id of the created file.
- Parameters
id – target file to be copied.
name – name of the new file.
parent_id – the id of the folder where the new file is placed in. If None, the file will be placed in Google Drive root.
- Returns
id of the created new file.
-
create_folder
(name: str, parent_ids: Optional[pysuite.drive.Drive.list] = None) → str[source]¶ create a folder on google drive by the given name.
- Parameters
name – name of the folder to be created.
parent_ids – list of ids where you want to create your folder in.
- Returns
id of the created folder.
-
delete
(id: str, recursive: bool = False)[source]¶ delete target file from google drive TODO: implement recursive delete
- Parameters
id – id of target object.
recursive – if True and target id represents a folder, remove all nested files and folders.
- Returns
None
-
download
(id: str, to_file: Union[str, pathlib.PosixPath])[source]¶ download the google drive file with the requested id to target local file.
- Parameters
id – id of the google drive file
to_file – local file path
- Returns
None
-
find
(name_contains: Optional[str] = None, name_not_contains: Optional[str] = None, parent_id: Optional[str] = None) → list[source]¶ find all files whose name contain specified string and do not contain specified string. Note that Google API has unexpected behavior when searching for strings in name. It is can only search first 26 character. In addition, it seems to search from first alphabetic character and Assume there are the following files: ‘positive_a’, ‘positive_b’, ‘a’, ‘_a’, ‘ba’
- Example
>>> self.find(name_contains='a') # this finds only 'a' and '_a', not 'positive_a' or 'ba'
- Parameters
name_contains – a string contained in the name
name_not_contains – a string that is not contained in the name
parent_id – parent folder id
- Returns
a list of dictionaries containing id and name of found files.
-
get_id
(name: str, parent_id: Optional[str] = None)[source]¶ get the id of the file with specified name. if more than one file are found, an error will be raised.
- Parameters
name – name of the file to be searched.
parent_id – id of the folder to limit the search. If None, the full Google drive will be searched.
- Returns
the id of the file if found. Or None if no such name is found.
-
get_name
(id: str) → str[source]¶ get the name of the Google drive object.
- Parameters
id – id of the target Google drive object
- Returns
name of the object
-
list
(id: str, regex: str = None, recursive: bool = False, depth: int = 3) → list[source]¶ list the content of the folder by the given id.
- Parameters
id – id of the folder to be listed.
regex – an regular expression used to filter returned file and folders.
recursive – if True, children of the folder will also be listed.
depth – number of recursion if recursive is True. This is to prevent cyclic nesting or deep nested folders.
- Returns
a list of dictionaries containing id, name of the object contained in the target folder and list of parent ids.
modify the permission of the target object and share with the provided emails.
- Parameters
id – id of target object.
emails – list of emails to be shared with.
role – type of permission. accepted values are: ‘owner’, ‘organizer’, ‘fileOrganzier’, ‘writer’, ‘commenter’ and ‘reader’.
notify – Whether notifying emails about the sharing.
- Returns
name of the object shared.
-
update
(id: str, from_file: Union[str, pathlib.PosixPath])[source]¶ update the Google drive with local file.
- Parameters
id – id of the Google drive file to be updated
from_file – path to local file.
- Returns
None
-
upload
(from_file: Union[str, pathlib.PosixPath], name: Optional[str] = None, mimetype: Optional[str] = None, parent_id: Optional[str] = None) → str[source]¶ upload local file to gdrive.
- Parameters
from_file – path to local file.
name – name of google drive file. If None, the name of local file will be used.
mimetype – Mime-type of the file. If None then a mime-type will be guessed from the file extension.
parent_id – id of the folder you want to upload the file to. If None, it will be uploaded to root of Google drive.
- Returns
id of the uploaded file