Skip to main content

PyGFolder is a package for managing Google Drive folders as a dictionary.

Project description

PyGFolder is a package for managing the Google Drive content as a Python dictionary, with the same interface as PyFolder.

https://badge.fury.io/py/pygfolder.svg

An easy example of usage is as follows:

>>> from pygfolder import PyGFolder
>>>
>>> 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."

PyGFolder allows to create/edit/remove elements from the google drive as if it was a Python Dict.

Installation

Currently, it is only supported Python 3.4.1 onwards:

sudo pip3 install pygfolder

It requires the credentials file generated at https://console.developers.google.com within 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 as client_id, project_id, client_secret, auth_uri, ), download the file and save it in your $HOME with the name “.pygfolder”.

First run

For the first run, PyGFolder must request a token to the API. It can be accomplished by invoking the request_token() method:

>>> from pygfolder import PyGFolder
>>>
>>> pygfolder = PyGFolder()
>>> pygfolder.request_token()

This call will print a URL that you might need to visit in order to give access to PyGFolder. 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, PyGFolder will read it automatically (it has a trigger), and request an authorization token, which will be saved in the same file. Next executions of PyGFolder will not require this process, thus it should be only run once per machine.

Note that “~/.pygfolder” contains all the required credentials data for PyGFolder to run. This means that transfering this file into another computer will allow PyGFolder to work out-of-the-box.

USAGE

  • List elements in google drive:

>>> from pygfolder import PyGFolder
>>>
>>> pygfolder = PyGFolder("")
>>> print(pygfolder.keys())     # show files and folders
>>> print(pygfolder.files())    # show only files
>>> print(pygfolder.folders())  # show only folders
  • Create 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 content

In PyGFolder, each folder is represented by a PyGFolder object. 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 equivalent
  • Delete an element:

>>> del pygfolder['pygfolder_file.txt'])
  • Iterate over files:

>>> for file, content in pygfolder.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 specific MimeType.

>>> result = pygfolder.export('My presentation', mimetype="application/pdf")
  • Create a folder:

Folders are automatically handled by PyGFolder. An example that is going to force PyGFolder to 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'"

LICENSE

It is released under the MIT license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pygfolder-0.0.5.tar.gz (7.2 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page