Package: pygfolder
PyGFolderis a package for managing the Google Drive content as a Python dictionary, with the same interface asPyFolder.An easy example of usage is as follows:>>>frompygfolderimportPyGFolder>>>>>>pygfolder=PyGFolder("/path/to/GoogleDrive/folder")>>>pygfolder["file.txt"]=b"hello, this is going to be instantly the content of this file.">>>pygfolder["file.txt"]b"hello, this is going to be instantly the content of this file."PyGFolderallows to create/edit/remove elements from the google drive as if it was a Python Dict.InstallationCurrently, it is only supportedPython 3.4.1onwards:sudopip3installpygfolderIt requires the credentials file generated athttps://console.developers.google.comwithin your Google Drive account. Once you have generated the credentials for the Google Drive API (it is going to be a JSON file containing the OAUTH2 parameters such asclient_id,project_id,client_secret,auth_uri,…), download the file and save it in your$HOMEwith the name “.pygfolder”.First runFor the first run,PyGFoldermust request a token to the API. It can be accomplished by invoking therequest_token()method:>>>frompygfolderimportPyGFolder>>>>>>pygfolder=PyGFolder()>>>pygfolder.request_token()This call will print a URL that you might need to visit in order to give access toPyGFolder. The step will finish when the code is retrieved back and manually filled into “.pygfolder”.
An example of the file should be as follows:{"installed":{"auth_uri":"https://accounts.google.com/o/oauth2/auth","redirect_uris":["urn:ietf:wg:oauth:2.0:oob","http://localhost"],"project_id":"vast-ascent-XXXXXX","client_id":"XXXXXX.apps.googleusercontent.com","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","token_uri":"https://accounts.google.com/o/oauth2/token","code":"THE_CODE_RETURNED_BY_GOOGLE_MANUALLY_SET_HERE","client_secret":"XXXXXX"}}Once the code is filled,PyGFolderwill read it automatically (it has a trigger), and request an authorization token, which will be saved in the same file.
Next executions ofPyGFolderwill not require this process, thus it should be only run once per machine.Note that “~/.pygfolder” contains all the required credentials data forPyGFolderto run. This means that transfering this file into another computer will allowPyGFolderto work out-of-the-box.USAGEList elements in google drive:>>>frompygfolderimportPyGFolder>>>>>>pygfolder=PyGFolder("")>>>print(pygfolder.keys())# show files and folders>>>print(pygfolder.files())# show only files>>>print(pygfolder.folders())# show only foldersCreate a file with a specific content:>>>pygfolder['pygfolder_file.txt']=b"Hello, this is the content of this root file from now on"Access an element:>>>file_content=pygfolder['pygfolder_file.txt']# For accessing a file content>>>file_content=pygfolder['specific_folder']# For accessing a folder contentInPyGFolder, each folder is represented by aPyGFolderobject. For this reason, it is possible to access nested folders as follows:>>>folder=pygfolder['specific_folder1']['specific_folder2']>>>folder=pygfolder['specific_folder1/specific_folder2']# This is also equivalentDelete an element:>>>delpygfolder['pygfolder_file.txt'])Iterate over files:>>>forfile,contentinpygfolder.items()):>>>print(content)Export documents:Google Apps stores the created documents within Google Drive, but they are not directly downloadable. If you want to download any of these, it must be exported to a specificMimeType.>>>result=pygfolder.export('My presentation',mimetype="application/pdf")Create a folder:Folders are automatically handled byPyGFolder. An example that is going to forcePyGFolderto create a folder is as follows:>>>result=pygfolder["folder/content.txt"]=b"this will create automatically the folder 'folder' in order to create the file 'content.txt'"LICENSEIt is released under theMIT license.