gdrive2 Functions and Commands

The following submodules are present in the package:

gdrive2
|_______gd
|_______drive_util
|_______auth_util
|_______paths

The main module that is automatically imported when gdrive2 is imported is gdrive2.gd.Other modules are utility modules, whose functions are used in gd. paths module only contains file paths which are imported as constants in other modules.

gdrive2.gd module

This module need not be imported explicilty and is imported automatically when gdrive2 is imported. All the commands and the functions that can be readily used are from this module. The following command gives the list of commands callable. From python console, gdrive2.help() can be used.

 $ gd -h

 usage: gd [-h] func ...

 <func> = function name
 <args> = arguements

 Overview of initializing/listing fucnctions:
--------------------------------
'init'   : initialize gdrive2, add new parents to initialized dir., add new clients
'status' : show parents list, show stage list, show users and clients list
'reset'  : changes default parent, client secrets
           changes parent information like user_names, paths, root folders (Ex: shared drives), clients
           deletes parents, delete authentication data,
'ls'     : lists all files/folders in parent paths, shows registered usernames and their shared drives, clients
'find'   : Searches for files and folders based on query
'cd'     : changes parent_path directly without using 'reset' function
'mkdir'  : creates new folder in the parent or path provided
'rm'     : creates existing folder/file in the parent or path provided

 Overview of push/pull functions:
 ---------------------------------
 'add'    : adds paths to the stage, clear stage
 'push'   : uploads files/folders to parent_paths
 'pull'   : downloads files/folders from parent_paths

 Miscelleneous functions:
 ---------------------------------
 'help'   : Shows the list of functions or commands available
 'rmgd'   : Removes the gd file created by importing gdrive2
 'default' : Brings the package to its default state (removes all clients, auth. data and the gd commandline functionality)
 'version' : Prints the current version

 positional arguments:
   func
   args

 optional arguments:
-h, --help  show this help message and exit

Initilizing/Listing:

gdrive2.gd.init(args)

[syntax when imported / syntax when called via CMD]

Initializes the current working directory and Creates an info file and authenticates the username.

If used for the first time, the username and authentication are requested.

Parameters

args (list) – list of arguement strings.

Returns

Just creates the .gd folder

Return type

None

Notes

The following commands go into args :

‘-h’ / -h or ‘-help’ / -help : shows help

‘-add’ / -add : adds new parent to the current directory

‘–add-client’ / –add-client : registers new client secrets file.

Examples

init([]) / gd init

init([‘-add’]) / gd init -add

gdrive2.gd.status(args)

[syntax when imported / syntax when called via CMD]

Displays parent information, staged paths and usernames registered.

Parameters

args (list) – list of arguement strings.

Returns

information – Depends on the arguements passed.

Return type

list

Notes

The following commands go into args :

  1. ‘-h’ / -h or ‘-help’ / -help : shows help

  2. status([]) / gd status

    displays parent details and staged files

    Returns (if imported)List with contents as shown below

    [<dictionary as in .gdinfo.json>, <list of existing staged paths>, <list of missing or non-existing staged paths>]

  3. status([‘-stage’]) / gd status -stage

    displays only stage contents

    Returns (if imported)List with contents as shown below

    [<list of existing staged paths>, <list of missing or non-existing staged paths>]

  4. status([‘-users’]) / gd status -users

    displays all users registered on the system

    Returns (if imported)List with contents as shown below

    [<user_names>]

  5. status([‘-clients’]) / gd status -clients

    displays all client secrets or APIs registered on the system

    Returns (if imported)List with contents as shown below

    [<names of client secrets json files>]

Examples

status([]) / gd status

status([‘-stage’]) / gd status -stage

status([‘-users’]) / gd status -users

status([‘-clients’]) / gd status -clients

gdrive2.gd.reset(args)

[syntax when imported / syntax when called via CMD]

Modifies/deletes the parent information of the current working directory. Deletes authentication data in the local system Resets the current working directory

Parameters

args (list) – list of arguement strings.

Returns

Just modifies or deletes the info file or the credentials.

Return type

None

Notes

The following commands go into args :

  1. ‘-h’ / -h or ‘-help’ / -help : shows help

  2. reset([‘<parent_name>’, ]) / gd reset <parent_name>

    If no arg. after ‘<parent_name>’, it prompts inputs for user_name, path and default prop. of existing parent

    optional arguements after ‘<parent_name>’ / <parent_name>:

    ‘-user’ / -user : resets user_name/account in the parent name given.

    ‘-path’ / -path : resets path in the parent name given.

    ‘-drive’/ -drive : resets drive in the parent name given (useful to set shared drives as roots)

    ‘-d’ / -d : deletes parent name given and its path, user_name and id

    ‘-default’ / -default : set parent name given as ‘default’

    ‘-client’ / -client : resets the client secrets file name for the parent.

  3. reset([‘-info’]) / gd reset -info

    erases all parents and user_names for current directory and re-initializes

  4. reset([‘-user’, ]) / gd reset -user

    required arguements after ‘-user’ / -user :

    ‘<user_name>’ / <user_name> : deletes <user_name>’s authentication data in the SYSTEM

    ‘-a’ / -a : deletes all users authentication data in the SYSTEM

  5. reset([‘-client’, ]) / gd reset -client

    required arguements after ‘-client’ / -client :

    ‘<client_name>’ / <client_name> : deletes <client_name>.json

    ‘-a’ / -a : deletes all client secrets files

Examples

reset([‘origin’, ‘-path’]) / gd reset origin -path

reset([‘-info’]) / gd reset -info

reset([‘-user’, ‘<user_name>’]) / gd reset -user <user_name>

gdrive2.gd.ls(args)

[syntax when imported / syntax when called via CMD]

list files/folder names in the parent_path or specified path

Parameters

args (list) – list of arguement strings.

Returns

information

List as shown here for files -

[file_paths_list, file_ids_list]

List as shown here for files when -t is used-

[file_paths_list, file_ids_list, file_types_list]

List of usernames when -users is used

List of names of client secrets json files when -clients is used.

List of shared drives for a <username> when -shared is used, as shown below -

[drive_names_list, drive_ids_list]

Return type

list

Notes

The following commands go into args :

  1. ‘-h’ / -h or ‘-help’ / -help : shows help

  2. ls([]) / gd ls

    shows files/folders in the <default> parent cwd

  3. ls([‘<parent_name>’,]) / gd ls <parent_name>

    If no arg. after ‘<parent_name>’, it shows files or folders in the <parent_name> cwd

    ‘<path>’ / <path> : shows files/folders in <path> in the <parent_name>

    ‘-id’ <id> / -id <id> : shows files/folders in folder with <id> in <parent_name>

    ‘-i’ / -i : shows files/folders in parent_name and/or path with ids

    ‘-t’ / -t : returns file types too when imported

  4. ls([‘-users’]) / gd ls -users

    shows all usernames registered in the SYSTEM

  5. ls([‘-users’]) / gd ls -users

    shows all usernames registered in the SYSTEM

  6. ls([‘-shared’, ‘<username>’]) / gd ls -shared <username>

    shows all shared drives in <user_name>

Examples

ls([]) / gd ls

ls([‘origin’, ‘-path’, ‘-i’]) / gd ls origin -path -i

ls([‘-info’]) / gd ls -info

ls([‘-users’]) / gd ls -users

ls([‘-shared’, ‘<username>’]) / gd ls -shared <username>

gdrive2.gd.find(args)

[syntax when imported / syntax when called via CMD]

Searched for file and folder names using a query. Also, can be used to get the file path if its id is known.

Parameters

args (list) – list of arguement strings.

Returns

information – (list of paths, list of ids) for “<query>”, and

(path to file with <id>, list of ids forming the path) for the required file when “-id” / -id is passed.

Return type

tuple

Notes

The following commands go into args :

  1. ‘-h’ / -h or ‘-help’ / -help : shows help

  2. find([“<query>”]) / gd find “<query>”

    Uses the <query> string to find the file in default parent path

  3. find([“<query>”, ‘–path-search’]) / gd find “<query>” –path-search

    Searches the glob pattern in <query> in the file paths instead of file names

  4. find([‘-id’, ‘<id>’]) / gd find -id <id>

    Uses the <id> string to find the file or folder in default parent path

  5. find([‘<parent_name>’, “<query>”]) / gd find <parent_name> “<query>”

    Uses the <query> string to find the file in <parent_name>’s path. ‘-id’ can be used in place of “<query>” to get file path of that id.

  6. find([“<query>”, ‘-fold-path’, ‘<fold_path>’]) / gd find “<query>” -fold-path <fold_path>

    Finds the file in folder with <fold_path> specified. If no parent_name specified, default parent is considered.

  1. find([“<query>”, ‘-fold-id’, ‘<fold_id>’]) / gd find “<query>” -fold-id <fold_id>

    Finds the file in folder with <fold_id> specified. If no parent_name specified, default parent is considered.

  2. find([“<query>”, -<tier>]) / gd find “<query>” -<tier>

    Finds the file/folder upto the specified <tier> in the file hierarchy.

    Only following arguements can be passed as -<tier>:

    ‘-<int>’ / -<int> : if integer, upto that tier

    ‘-curr’ / -curr : Current tier (same as tier = 1) (Default)

    ‘-all’ / -all : all tiers

Additional optional arguements: -fold-path, -fold-id, -<tier>, -id, –path-search

Examples

find([“.jpg”]) / gd find “.jpg”

find([“.”, ‘-fold-path’, ‘folder1/folder2’]) / gd find “.” -fold-path folder1/folder2

find([‘origin2’, ‘-4’, “.xlsx and .csv”]) / gd find origin2 -4 “*.xlsx and *.csv”

find([“fruits and not . and not -“]) / gd find “fruits and not . and not -

Query for gdrive2.find:

A query made with fnmatch (slighlty different from glob) patterns must be passed as a string enclosed by ” “. It can have only the following logical operators - and, or and not. All the fnmatch patterns and the operators must be separated by spaces as shown in examples below. not operates on the immediately following pattern. Similarly, and and or operates on the following pattern or on the result of a not operation. To search for only files, append %f in the beginning of the query and for only folders, append %d. In the and-separated query, the first string (after %f, %d or not, if present) sets the initial list of paths. The latter strings or conditions connected by ‘and’ to the previous one must lead to a subset of paths of the previous list. For the conditions connected by ‘or’, they are evaluated separately and the final list is the union of the sets obtained from individual condition evaluations. See examples below:

Consider a file hierarchy as shown below for example:

My Drive                                  - - - - - - - tier 1
|
|___fruits                                - - - - - - - tier 2
|      |___hard                           - - - - - - - tier 3
|      |     |__apple.jpg                 - - - - - - - tier 4
|      |     |__guava.png                 - - - - - - - tier 4
|      |
|      |___soft                           - - - - - - - tier 3
|            |__grapes.jpg                - - - - - - - tier 4
|
|___flowers                               - - - - - - - tier 2
      |___yellow                          - - - - - - - tier 3
      |      |__sunflower.tiff            - - - - - - - tier 4
      |
      |___red                             - - - - - - - tier 3
           |__roses.jpg                   - - - - - - - tier 4
           |__poppy.png                   - - - - - - - tier 4

For the above drive, following are some examples for the query use for tier = 4 files, with –path-search option (searches entire paths to the files in the specified tier instead of just filenames at in the tier) enabled:

"*.jpg* and *soft* or *.png*"     -  Searches files and folders ending with .jpg
                                      and having 'soft' in pathname, or files and
                                      folders ending with .png

                                      1. fruits/soft/grapes.jpg
                                      2. fruits/hard/guava.png
                                      3. flowers/red/poppy.png

"f* and *f"                       -  Searches files and folders names which start
                                      with f and end with f.

                                      1. flowers/yellow/sunflower.tiff

"%f f*ng"                         -  Searches files which
                                      start with f and end with ng.

                                      1. fruits/hard/guava.png
                                      2. flowers/red/poppy.png

"*.*"                                                         -  Searches files and folders containing '.'

                                                                              1. fruits/hard/apple.jpg
                                                                              2. fruits/hard/guava.png
                                                                              3. fruits/soft/grapes.jpg
                                                                              4. flowers/yellow/sunflower.tiff
                                                                              5. flowers/red/roses.jpg
                                                                              6. flowers/red/poppy.png


"%d f*g and not png"                          -  Seaches for folders starting with f and
                                      ending with g, but not png files.

                                      1. fruits/hard/apple.jpg
                                                                              2. fruits/soft/grapes.jpg
                                                                              3. flowers/red/roses.jpg

Fnmatch Wilcard characters:

Wildcard

Meaning

matches everything

?

matches any single character

[seq]

matches any character in seq.

[!*seq*]

matches any character not in seq.

Note that these do not have ** character as in glob patterns. To get the functionality of path search, use –path-search arguement in gdrive2.find or path_search = True in gdrive2.drive_util.query_to_paths().

gdrive2.gd.cd(args)

[syntax when imported / syntax when called via CMD]

Changes parent_paths (the cwd paths of parents)

Parameters

args (list) – List of arguement strings.

Returns

Edits the folders or files on local system

Return type

None

Notes

The following commands go into args :

  1. ‘-h’ / -h or ‘-help’ / -help : shows help

The <path> being passed need no be limited to be within the parent_path folder. It can to any file or folder within current driveID. It can be relative to the parent_path or can be absolute wrt to driveId.

  1. cd([‘<path>’]) / gd cd <path>

    changes cwd to <path> for default parent

  2. cd([‘<parent_name>’, ‘<path>’]) / gd cd <parent_path> <path>

    changes cwd to <path> for <parent_name>

Examples

cd([‘<path>’]) / gd cd <path>

cd([‘<parent_name>’, ‘<path>’]) / gd cd <parent_name> <path>

gdrive2.gd.mkdir(args)

[syntax when imported / syntax when called via CMD]

Makes an empty folder in specified path

Parameters

args (list) – list of arguement strings.

Returns

Edits the folders or files on local system

Return type

None

Notes

The following commands go into args :

  1. ‘-h’ / -h or ‘-help’ / -help : shows help

The <path> being passed need no be limited to be within the parent_path folder. It can to any file or folder within current driveID. It can be relative to the parent_path or can be absolute wrt to driveId.

  1. mkdir([‘<path>’]) / gd mkdir <path>

    creates folder at the path in default parent

  2. mkdir([‘<parent_name>’, ‘<path>’]) / gd mkdir <parent_ame> <path>

    creates folder at the path in <parent_name> parent

Examples

mkdir([‘<path>’]) / gd mkdir <path>

mkdir([‘<parent_name>’, ‘<path>’]) / gd mkdir <parent_name> <path>

gdrive2.gd.rm(args)

[syntax when imported / syntax when called via CMD]

Deletes the file or folder at specified path in Google Drive

Parameters

args (list) – list of arguement strings.

Returns

Edits the folders or files on local system

Return type

None

Notes

The following commands go into args :

  1. ‘-h’ / -h or ‘-help’ / -help : shows help

The <path> being passed need no be limited to be within the parent_path folder.

It can to any file or folder within current driveID.

It can be relative to the parent_path or can be absolute wrt to driveId.

  1. rm([‘<path>’]) / gd rm <path>

    trashes files or folders at the parent_path in <default> parent

  2. rm([‘<parent_name>’, ‘<path>’]) / gd rm <parent_name> <path>

    trashes files or folders in <path> in <parent_name> parent

  3. rm([‘<parent_name>’, ‘<path>, ‘-f’]) / gd rm <parent_name> <path> -f

    Permanently deletes files or folders in <path> in the <parent_name>

Examples

rm([‘<parent_name>’, ‘<path>’]) / gd rm <parent_name> <path>

rm([‘<parent_name>’, ‘<path>, ‘-f’]) / gd rm <parent_name> <path> -f

Upload/Download:

gdrive2.gd.add(args)

[syntax when imported / syntax when called via CMD]

Adds paths to stage for pushing to Google Drive. This function just adds the paths to .gdstage file. It doesn’t push any file. For pushing, use function push() / gd push after adding.

Parameters

args (list) – list of arguement strings.

Returns

Edits the folders/files on local system

Return type

None

Notes

The following commands go into args :

  1. ‘-h’ / -h or ‘-help’ / -help : shows help

The <path> must be in local system

  1. add([‘<path1>’, ‘<path2>’, ‘<path3>’, …]) / gd add <path1> <path2> <path3> …

    adds multiple <path>s to the gd stage

  2. add([‘-clear’]) / gd add -clear

    clears all paths in the stage

Examples

add([‘<path1>’, ‘<path2>’, ‘<path3>’, …]) / gd add <path1> <path2> <path3> …

add([‘-clear’]) / gd add -clear

gdrive2.gd.push(args)

[syntax when imported / syntax when called via CMD]

Pushes or uploads files into path of the parent_name specified.

Parameters

args (list) – list of arguement strings.

Returns

Edits the folders or files on local system

Return type

None

Notes

The following commands go into args :

  1. ‘-h’ / -h or ‘-help’ / -help : shows help

  2. push([]) / gd push

    Pushes staged files to default parent

  3. push([‘<parent_name1>’, ‘<parent_name2>’, ‘<parent_name3>’, … ]) / gd push <parent_name1> <parent_name2> <parent_name3> …

    Pushes staged files to <parent_name> folder

Optional arguements if pushed files already exist on drive-

‘-c’ : creates copy ‘-s’ : skip ‘-o’ : overwrites existing file ‘-i’ : prompt for each file (DEFAULT)

Examples

push([‘-i’]) / gd push -i

push([‘<parent_name1>’, ‘-s’, ‘<parent_name2>’]) / gd push <parent_name1> -s <parent_name2>

gdrive2.gd.pull(args)

[syntax when imported / syntax when called via CMD]

Pulls or downloads files from drive

Parameters

args (list) – list of arguement strings.

Returns

Edits the folders or files on local system

Return type

None

Notes

The following commands go into args :

  1. ‘-h’ / -h or ‘-help’ / -help : shows help

The <path> being passed need no be limited to be within the parent_path folder. It can to any file/folder within current driveID. It can be relative to the parent_path or can be absolute wrt to driveId.

  1. pull([]) / gd pull

    Downloads complete default parent folder to current dir.

  2. pull([‘<parent_name>’]) / gd pull <parent_name>

    Downloads complete <parent_name> folder to current dir. If arguements passed in the list, then the first arguement must always be the parent name

  3. pull([‘<parent_name>’, ‘<path1>’, <path2>, <path3>, …]) / gd pull <parent_name> <path1> <path2> <path3> …

    Downloads all the paths specified into current dir from the username related to this parent.

  4. pull([‘<parent_name>’, ‘-id’, ‘<path_id1>’, ‘<path_id2>’, …]) / gd pull <parent_name> -id <path_id1> <path_id2> …

    Use of ‘-id’ : Downloads from all the path_ids specified into current dir from the username related to this parent.

  5. pull([‘<parent_name>’, ‘-dest’, ‘<save_path>’]) / gd pull <parent_name> -dest <save_path>

    Use of ‘-dest’ : downloads into <save_path> in local system specified from parent_path”)

Optional arguements if pulled files already exist on local system -

‘-c’ : creates copy ‘-s’ : skip ‘-o’ : overwrites existing file ‘-i’ : prompt for each file (DEFAULT)

Examples

pull([‘-i’]) / gd pull -i

pull([‘<parent_name>’, ‘<path1>’, <path2>, ‘-s’, ‘-id’, ‘id3’, ‘-dest’, ‘<save_path>’]) / gd pull <parent_name> <path1> <path2> -s -id <id3> -dest <save_path>

Miscellaneous:

gdrive2.gd.help(args)

displays help text (if imported, args = [])

gdrive2.gd.default(args)

brings the package to its default (if imported, args = [])

gdrive2.gd.version(args)

prints the current version of gdrive2

gdrive2.drive_util module

These functions are utility functions which are used in gd to control files in gdrive2. The functions here take pyDrive.GoogleDrive() object as an arguement to do this.

gdrive2.drive_util.create_folder(folder_name, parent_folder_id, drive)

Creates folder in drive with title = folder_name

Parameters
  • folder_name (string) – name of folder

  • parent_folder_id (string) – ID of the parent folder

  • drive (pydrive.GoogleDrive() object) –

Returns

id of the created folder

Return type

string

gdrive2.drive_util.create_folders_path(path)

Creates non-existing folders along the local system path

Parameters

path (string) – path to a folder

Returns

Creates folders in the local system

Return type

None

gdrive2.drive_util.delete(drive, drive_path=None, drive_path_id=None, relative_id=None, hard_delete=False, default_root='root')

deletes a file or folder using the drive_path or the drive_path_id.

A typical drive path must be like this :

Folder1_title/Folder2_title/Folder3_title or file_title

Parameters
  • drive (pydrive.GoogleDrive() object) –

  • drive_path (stringor None) – path to the file or folder in drive to be deleted

  • drive_path_id (string or None) –

    id of the folder or file to be deleted.

    Only one of drive_path and drive_path_id is sufficient.

  • relative_id (None or string (optional)) – None if absolute paths are desired (default) and <current working folder id> if relative paths are desired

  • hard_delete (bool (optional)) –

    If False, files are moved to trash (default)

    If True, files are deleted permanently

  • default_root (string (optional)) – id of the drive

Returns

Deletes the specified files or folders

Return type

None

gdrive2.drive_util.download(drive, drive_path=None, drive_path_id=None, download_path='/home/docs/checkouts/readthedocs.org/user_builds/gdrive2/checkouts/latest/docs', prompt='ask', default_root='root')

Downloads a file or folder at drive_path into the folder at download_path Either id or path - one of them is sufficient

A typical drive path must be like this :

Folder1_title/Folder2_title/Folder3_title or file_title

Parameters
  • drive (pydrive.GoogleDrive() object) –

  • drive_path (string or None) – path to file/folder on drive (None is default)

  • drive_path_id (string or None) –

    id of file/folder in drive.

    Either the drive path o the drive_path_id is sufficient (None is default)

  • download_path (string (optional)) –

    path to folder into which download will be done

    current_working directory is default

  • prompt (string (optional)) –

For prompt, use the following:

‘ask’ asks user to skip or overwrite if file already exists in drive (default)

‘skip’ or ‘s’ skips file if already exists

‘overwrite’ or ‘o’ overwrites the file if it already exists

‘copy’ creates an extra copy

Returns

Just downloads the folder or file and returns None

Return type

None

gdrive2.drive_util.download_file_by_id(file_id, download_path, drive, prompt='ask', file_count=1, total_count=1)

Downloads a file at drive_path into the folder at download_path if file_id is known

A typical drive path must be like this :

Folder1_title/Folder2_title/Folder3_title or file_title

Parameters
  • file_id (string) – file’s id in drive

  • download_path (string) – path to folder into which download will be done

  • drive (pydrive.GoogleDrive() object) –

  • prompt (string (optional)) –

  • file_count (int (optional)) – 1 (default) to print the file’s count when downloading folder

  • total_count (int (optional)) – 1 (default) to print the total files in a folder when downloading

For prompt, use the following:

‘ask’ asks user to skip or overwrite if file already exists in drive (default)

‘skip’ or ‘s’ skips file if already exists

‘overwrite’ or ‘o’ overwrites the file if it already exists

‘copy’ creates an extra copy

Returns

multiple outputs – Just downloads the file returns None if no change in prompt, returns change in prompt from user otherwise

Return type

None or string

gdrive2.drive_util.fetchMetadata(drive_file, fields=None)

A replica of FetchMetadata() from GoogleDriveFile class of pyDrive 1.3.1 Changes made : replaced ‘supportsTeamDrives’ with ‘supportsAllDrive’

Parameters
  • drive_file (pydrive.GoogleDriveFile() object) –

  • fields (string (optional)) – If metadata about a specific field is required, pass it here as querry string. For example : fields = “field1,field2,field3”

Returns

Updates drive_file with metadata.

Return type

None

gdrive2.drive_util.get_id_by_name(name, parent_folder_id, drive, file_type='all')

Returns list of ids for folders or files with title = name in the drive folder with id = parent_folder_id

Parameters
  • name (string) – name of folder or file

  • parent_folder_id (string) – ID of the parent folder

  • drive (pydrive.GoogleDrive() object) –

  • file_type (string (optional)) –

Notes

For file_type, use one of the following:

‘folder’ to return only folders’ ids

‘not-folder’ to return ids other than folders

‘all’ to return all ids with the name (default)

Returns

ids and mime types – a tuple of 2 lists : (list of ids, list of corresponding mime types)

Return type

tuple

gdrive2.drive_util.get_path_from_id(drive, file_id, default_root='root')

Returns drive path of file with specified id.

A typical drive path must be like this :

Folder1_title/Folder2_title/Folder3_title or file_title

Parameters
  • drive (pydrive.GoogleDrive() object) –

  • file_id (string) – id of file whose path is required

  • default_root (string (optional)) – id of the drive

Returns

path to file or folder – (string of path, list of ids leading up to file_id)

Return type

tuple

gdrive2.drive_util.get_path_ids(drive_path, drive, create_missing_folders=True, relative_id=None, path_to='folder', default_root='root')

Returns list of ids for folders or files in the path to the drive_path

A typical drive path must be like this :

Folder1_title/Folder2_title/Folder3_title or file_title

The last id in this list will be of a folder or a file, which the user must determine with the path_to arguement

Parameters
  • drive_path (string) – path to a file or folder in drive

  • drive (pydrive.GoogleDrive() object) –

  • create_missing_folders (bool (optional)) –

    True if create the folders in the path if they dont exist

    False returns a error if any of the folder in the path doesnt exist

  • relative_id (None or string (optional)) – None if absolute paths are desired and <current working folder id> if relative paths are desired

  • path_to (string (optional)) –

    ‘folder’ to return only folders’ ids (default)

    ’not-folder’ to return ids other than folders

Returns

path_ids

returns a list of all ids in the path like this -

[Folder1_id, Folder2_id, Folder3_id] if path_to = ‘folder’

[Folder1_id, Folder2_id, File id] if path_to = ‘not-folder’

[Folder1_id, Folder2_id, ‘no-file-found’] if path_to = ‘not-folder’ and the file at the end of path doesnt exist

Gives error if there are more than one files or more than folders with same name

Return type

list

gdrive2.drive_util.isdir(drive, file_id)

checks if the file with file_id is a directory.

Parameters
  • drive (pydrive.GoogleDrive() object or None) –

  • file_id (string) – The file_id of file whose mimeType is to be checked

Returns

Whether dir. or not – True if it is a directory/folder

Return type

bool

Notes

If drive is None, it considers file_ids as a local file file path.

gdrive2.drive_util.list_all_contents(init_folder_path, init_folder_id=None, drive=None, dynamic_show=False, tier='all', show_ids=False, get_types=False, default_root='root')

Lists “relative” paths to nested files and folders in a folder with path = folder_path Relative paths from the folder at init_folder_path

A typical drive path must be like this :

Folder1_title/Folder2_title/Folder3_title or file_title

For exampleif init_folder_path = Folder1_title

relative paths are Folder2_title and Folder2_title/Folder3_title

If a file is present at init_folder_path, just returns the file_name and id

Parameters
  • init_folder_path (string) – path to folder on current system

  • init_folder_id (string or None (optional)) –

    id of the folder at init_folder_path

    Only one of init_folder_path and init_folder_id is sufficient

    None if unknown, but init_folder_path is known

  • drive (pydrive.GoogleDrive() drive object or None (optional)) – None for listing paths in local system; otherwise, lists paths in drive

  • dynamic_show (bool (optional)) – if True, prints each nested_path dynamically, False doesn’t

  • tier (string or int(optional)) –

    ‘all’ returns all nested paths

    ’curr’ returns contents immediately below current folder

    If int, tier should be the number of tiers in hierarchy of nested files to list. For example, tier = 1 is same as tier == ‘curr’

    Interger-type tier works only for system = ‘drive’

  • show_ids (bool (optional)) – If True, prints ids along with file names when dynamic_show = True

  • get_types (bool (optional)) – If True, even the file types are returned as in the tuple and works

  • default_root (string (optional)) – id of the drive

Returns

contnets of the folder

a tuple of 3 (or 4) elements

(the paths_list with the relative paths, the list of ids of contents if used for gdrive2 (same as paths_list for drive = None), the list of file_types (included only if get_types = True), total_count = the number of ‘non-folder’ items in the folder at folder_path)

Return type

tuple

gdrive2.drive_util.parse_drive_path(path, drive, parent_id, default_root='root')

Parses path into an absolute drive path

A typical drive path must be like this :

Folder1_title/Folder2_title/Folder3_title or file_title

Parameters
  • path (string) – drive path to be parsed

  • drive (pydrive.GoogleDrive() object) –

  • parent_id (string) – parent id required to parse relative paths

  • default_root (string (optional)) – id of the drive

Returns

parsed drive path

Return type

string

gdrive2.drive_util.query_to_paths(drive, query, path, path_id=None, tier='all', path_search=False, default_root='root')

Used in gdrive2.find function to obtain paths from queries. A query includes fnmatch patterns connected by ‘and’ and/or ‘or’ operators

Parameters
  • drive (pydrive.GoogleDrive() object) –

  • query (string) – query to search for a file in specified path

  • path (string) – directory path in drive where the query is applied and files are searched. All paths queried will be relative to this

  • path_id (string (optional)) – id of the directory path

  • tier (string or int) – The tier in the hierarchy of files

  • path_search (bool) – If true, looks for the fnmatch in the files paths instead of filenames in the specified tier This is similar to use of ** instead of * in a glob pattern.

  • default_root (string (optional)) – The id of the drive.

Notes

If tier = ‘all’, all tiers are searched (optional)

If tier = ‘curr’, only immediate children are searched

If integer passed, search will be done upto that tier.

For example, tier = 1 is same as tier = ‘curr’

Returns

paths and path_ids satisfying the query – (list of paths, list of path_ids)

Return type

tuple

Notes

If drive is None, it looks for files in the local system.

gdrive2.drive_util.upload(curr_path, drive_parent_folder_path, drive, prompt='ask', default_root='root')

Uploads a folder/file at curr_path into the folder at drive_parent_folder_path Use drive_parent_folder_path = ‘’ for uploading into the root folder

A typical drive path must be like this :

Folder1_title/Folder2_title/Folder3_title or file_title

Parameters
  • curr_path (string) – path to folder or file on current system

  • drive_parent_folder_path (string) – path to drive folder into which upload will be done

  • drive (pydrive.GoogleDrive() object) –

  • prompt (string (optional)) –

For prompt, use the following:

‘ask’ asks user to skip or overwrite if file already exists in drive (default)

‘skip’ or ‘s’ skips file if already exists

‘overwrite’ or ‘o’ overwrites the file if it already exists

‘copy’ creates an extra copy

Returns

Just uploads the folder or file

Return type

None

gdrive2.drive_util.upload_file_by_id(curr_file_path, drive_folder_id, drive, prompt='ask', file_count=1, total_count=1)

Uploads a file with current path = curr_file_path on system into a drive folder with id = drive_folder_id

A typical drive path must be like this :

Folder1_title/Folder2_title/Folder3_title or file_title

Parameters
  • curr_file_path (string) – path to file on current system

  • drive_folder_id (string) – id of drive folder into which upload will be done

  • drive (pydrive.GoogleDrive() object) –

  • prompt (string (optional)) –

  • file_count (int (optional)) – 1 (default) to print the file’s count when uploading folder

  • total_count (int (optional)) – 1 (DEFAULT) to print the total files in a folder when uploading

For prompt, use the following:

‘ask’ asks user to skip or overwrite if file already exists in drive (default)

‘skip’ or ‘s’ skips file if already exists

‘overwrite’ or ‘o’ overwrites the file if it already exists

‘copy’ creates an extra copy

Returns

multiple outputs – Uploads the file into the drive folder with id = drive_folder_id

returns None if no change in prompt,

returns change in prompt from user otherwise

Return type

None or string

gdrive2.auth_util module

This module contains functions which are responsible for managing the pyDrive.GoogleAuth() object. The functions here are used in gd to authenticate users, check for username cedentials and adding client files on the system.

gdrive2.auth_util.auth_from_cred(gauth, user_name=None, client='client_secrets')

Stores the authentication information in the gauth object. This is also required to retrieve credential data and refresh expired access tokens. It checks for <cred_id>.txt and client_secrets/<client>.json paths and updates these paths with authentication.

Parameters
  • gauth (pydrive.GoogleAuth() object) –

  • user_name (string (optional)) – the username which requires authentication

  • client (string (optional)) – client <name> corresponding to <name>.json in client_secrets/ folder If no client is passed, the DEFAULT_CLIENT is used.

Returns

Authenticates the username with the client provided

Return type

None

gdrive2.auth_util.check_creds_list(user_name, check_only=False)

This checks if creds list (CRED_MAP) exists and creates one if it doesn’t. It looks for username in the CRED_MAP, if it doesnt exist creates unique id for the username and generates a credentials file for it.

Parameters
  • user_name (string) – username to check

  • check_only (boolean (optional)) – If True, returns a boolean value stating whether the username is previously registered or not.

Returns

  • whether username exists already (boolean) – if check_only = True

  • cred_id (string) – the credentials file’s unique id if check_only = False

gdrive2.auth_util.copy_client_secrets(client_path, client=None)

Copies client secrets .json files from client_path to the client_secrets/ in the package folder

Parameters
  • client_path (string) – path to look for the source .json file

  • client (string (optional)) – Name to be assigned to the client file. If None, DEFAULT_CLIENT name is assigned. Note that client should not include the extension ‘.json’ in it

Returns

Just copies the .json file

Return type

None