pycharmers.sdk.google_drive module

A Wrapper class for GoogleDrive in PyDrive

class pycharmers.sdk.google_drive.PyCharmersGoogleDrive(settings_file='settings.yaml', http_timeout=None)[source]

Bases: pydrive.drive.GoogleDrive

Wrapper class for GoogleDrive.

Parameters
  • settings_file (str) – path of settings file. Defaults to 'settings.yaml' .

  • http_timeout (int) – HTTP timeout. Defaults to None.

Examples

>>> from pycharmers.sdk import PyCharmersGoogleDrive
>>> drive = PyCharmersGoogleDrive(settings_file='dir/subdir/settings.yaml')
>>> drive

When executed as above, the directory structure is as follows.

$ tree .
.
└── dir
    └── subdir
        ├── client_secrets.json
        ├── credentials.json
        └── settings.yaml

settings.yaml is like this .

client_config_backend: settings
client_config:
    client_id: <CLIENT_ID>
    client_secret: <CLIENT_SECRET
save_credentials: True
save_credentials_backend: file
save_credentials_file: credentials.json
get_refresh_token: True
oauth_scope:
    - https://www.googleapis.com/auth/drive.file
    - https://www.googleapis.com/auth/drive.install
    - https://www.googleapis.com/auth/drive
static authenticate(settings_file='settings.yaml', http_timeout=None)[source]

Get an Authentication. See the PyDrive’s documentation

Parameters
  • settings_file (str) – path of settings file. Defaults to 'settings.yaml' .

  • http_timeout (int) – HTTP timeout. Defaults to None.

Returns

Wrapper class for oauth2client library in google-api-python-client.

Return type

GoogleAuth

static arrange_queries(queries=[], ext=None, isfile=None, trashed=False, verbose=False)[source]

Arrange queries for Files.List()

Parameters
  • queries (list) – Current queries. Defaults to [] .

  • ext (str) – File Extensions. Defaults to None .

  • isfile (bool) – If this value is True, extract only “file”, else if this value is False, extract only “folder”, else (if this value is None ) extract “both”.

  • trashed (bool) – Whether trashed file or not. Defaults to False

  • verbose (bool) – Whether to print the query or not. Defaults to False

Returns

parameter to be sent to Files.List() .

Return type

dict

Examples

>>> from pycharmers.sdk import PyCharmersGoogleDrive
>>> PyCharmersGoogleDrive.arrange_queries(queries=[], ext=".mp4", isfile=True, trashed=False)
{'q': 'title contains ".mp4" and mimeType != "application/vnd.google-apps.folder" and trashed = false'}
>>> PyCharmersGoogleDrive.arrange_queries(queries=[], ext=None, isfile=False, trashed=None)
{'q': 'mimeType  = "application/vnd.google-apps.folder" and trashed = none'}
getListFile(param=None)[source]

Create an instance of GoogleDriveFileList with auth of this instance.

Parameters

param (dict) – parameter to be sent to Files.List() .

Returns

Google Drive File List.

Return type

GoogleDriveFileList

get_file_list(filename=None, dirname=None, dirId='root', ext=None, isfile=None, trashed=False, recursive=False, verbose=False)[source]

Use queries effortlessly to get a list of files.

Parameters
  • filename (str) – Exact File Name. Defaults to None .

  • dirname (str) – Directory Name. Defaults to None .

  • dirId (str) – Directory Id. Defaults to None .

  • ext (str) – File Extensions. Defaults to None .

  • isfile (bool) – If this value is True, extract only “file”, else if this value is False, extract only “folder”, else (if this value is None ) extract “both”.

  • trashed (bool) – Whether trashed file or not. Defaults to False

  • recursive (bool) – Whether to find files recursively. Defaults to False

  • verbose (bool) – Whether to print the query or not. Defaults to False

Returns

Google Drive File List.

Return type

GoogleDriveFileList

Examples

>>> from pycharmers.sdk import PyCharmersGoogleDrive
>>> drive = PyCharmersGoogleDrive(settings_file="settings.json")
>>> for f in drive.get_file_list(dirname="DIRNAME"):
...     print(f["title"], f["id"])
get_dirId(dirname=None, dirId='root')[source]

Get directory Id.

Parameters
  • dirname (str) – Directory Name. Defaults to None .

  • dirId (str) – Directory Id. Defaults to "root" .

Returns

Directory Id.

Return type

str

upload(filepath, verbose=True)[source]

Upload file.

Parameters

filepath (str) – name of the file to be uploaded.

Returns

Google Drive File Info.

Return type

GoogleDriveFile

download_file(filename=None, dirname=None, dirId='root', ext=None, isfile=None, trashed=False, dst=None, mimetype=None, remove_bom=False, verbose=True)[source]

Download file.

Parameters
  • filename (str) – File Name. Defaults to None .

  • dirname (str) – Directory Name. Defaults to None .

  • dirId (str) – Directory Id. Defaults to None .

  • ext (str) – File Extensions. Defaults to None .

  • isfile (bool) – If this value is True, extract only “file”, else if this value is False, extract only “folder”, else (if this value is None ) extract “both”.

  • trashed (bool) – Whether trashed file or not. Defaults to False

  • dst (str) – Where to download the file. Defaults to None .

  • mimetype (str) – mimeType of the file.. Defaults to None .

  • remove_bom (bool) – Whether to remove the byte order marking.. Defaults to False.

  • verbose (bool) – Whether to print the result or not. Defaults to True

Returns

Path to downloaded file.

Return type

str

Examples

>>> from pycharmers.sdk import PyCharmersGoogleDrive
>>> drive = PyCharmersGoogleDrive(settings_file="settings.json")
>>> drive.download_file(filename="file.png", dirId="root", verbose=True)
tree(dirname=None, dirId='root')[source]

list contents of directories in a tree-like format.

Parameters
  • dirname (str) – Directory Name. Defaults to None .

  • dirId (str) – Directory Id. Defaults to "root" .

TODO: To display the file contents in a tree-like format.

Examples

>>> from pycharmers.sdk import PyCharmersGoogleDrive
>>> drive = PyCharmersGoogleDrive(settings_file="settings.json")
>>> drive.tree()
tree_recursive(dirname=None, dirId='root', parents=[])[source]

[summary]

Parameters
  • dirname (str) – Directory Name. Defaults to None .

  • dirId (str) – Directory Id. Defaults to "root" .

  • parents (list) – Parent Directories. Defaults to [].

Returns

All filepaths from initial dirname ( dirId )

Return type

list

class pycharmers.sdk.google_drive.QUERY[source]

Bases: object

Query to be sent to Files.List() .

References

URL

Japanese cheatsheet

https://note.nkmk.me/python-pydrive-list-file/

Commonly Used MIME Types

https://learndataanalysis.org/commonly-used-mime-types/

G Suite and Drive MIME Types

https://developers.google.com/drive/api/v3/mime-types

FOLDERS = 'mimeType = "application/vnd.google-apps.folder"'
FILES = 'mimeType != "application/vnd.google-apps.folder"'
TITLE_MATCH = 'title = "{q}"'
TITLE_CONTAIN = 'title contains "{q}"'
PARENT = '"{q}" in parents'
TRASHED = 'trashed = {q}'
static show()[source]

Show all Queries.

Examples

>>> from pycharmers.sdk.google_drive import QUERY
>>> QUERY.show()