コード例 #1
0
 def from_id(cls, creds_json, root_id, *args, **kwargs) -> cls:
     drive = GoogleDrive(creds_json)
     root = drive.item_by_id(root_id)
     if root.isfolder():
         return cls(root, *args, **kwargs)
     else:
         raise FileNotFoundError("ID {} is not a folder".format(root_id))
コード例 #2
0
 def from_id(cls, creds_json, root_id, *args, **kwargs) -> cls:
     drive = GoogleDrive(creds_json)
     root = drive.item_by_id(root_id)
     if root.isfolder():
         new_obj = cls(root, *args, **kwargs)
         new_obj.creator = "from_path"
         return new_obj
     else:
         raise FileNotFoundError("ID {} is not a folder".format(root_id))
コード例 #3
0
def setup(token_file, gauth_file=None):
    token_file = pathlib.Path(token_file)
    if gauth_file is not None:
        gauth_file = pathlib.Path(gauth_file)
        with gauth_file.open('r') as fp:
            gauth = json.load(fp)
    else:
        print("You can enter your own API key or use the built-in one.\n"
              "The built-in API key is potentially slower as more people\n"
              "are using it. Also, it might be blocked due to it not (yet)\n"
              "being verified by Google. ")

        try:
            use_own_api = distutils.util.strtobool(
                input("Do you want to use your own API key? (y/N)").lower())
        except ValueError:
            use_own_api = False

        if use_own_api:
            client_id = input("Client ID: ").strip()
            client_secret = input("Client Secret: ").strip()
        else:
            print("======")
            print(
                "IMPORTANT: Google has started to lockdown their Google Drive API. This might affect access to your remotes."
            )
            print(
                "Until this is settled you'll see a warning about this application not being verified by Google which you need to accept in order to proceed."
            )
            print(
                "Read more on https://github.com/Lykos153/git-annex-remote-googledrive#google-drive-api-lockdown"
            )
            print("======")
            client_id = DEFAULT_CLIENT_ID
            client_secret = DEFAULT_CLIENT_SECRET

        gauth = {
            'installed': {
                'client_id': client_id,
                'client_secret': client_secret,
                'auth_uri': 'https://accounts.google.com/o/oauth2/auth',
                'token_uri': 'https://accounts.google.com/o/oauth2/token',
                'revoke_uri': None,
                'redirect_uri': 'urn:ietf:wg:oauth:2.0:oob',
            }
        }

    creds = GoogleDrive.auth(gauth)

    with token_file.open('w') as fp:
        fp.write(creds)
    print()
    print("Setup complete. An auth token was stored in {}.".format(token_file),
          "Now run 'git annex initremote' with your desired parameters.")
    try:
        if token_file == _get_token_path():
            return
    except git.exc.InvalidGitRepositoryError:
        pass
    print("Don't forget to specify token=<path/to/token.json>")
コード例 #4
0
 def from_path(cls, creds_json, rootpath, *args, **kwargs) -> cls:
     drive = GoogleDrive(creds_json)
     root = drive.create_path(rootpath)
     new_obj = cls(root, *args, **kwargs)
     new_obj.creator = "from_path"
     return new_obj
コード例 #5
0
def remote_tmpdir(gdrive: GoogleDrive) -> DriveFolder:
    tmpdir = gdrive.create_path(remote_tmpdir_prefix)
    yield tmpdir
    tmpdir.remove()
コード例 #6
0
def gdrive_appdata() -> GoogleDrive:
    with open(token_file_appdata) as fh:
        credentials = fh.read()
    return GoogleDrive(credentials)
コード例 #7
0
 def test_mkdir(self, gdrive: GoogleDrive):
     folder = gdrive.mkdir(random_string())
     assert isinstance(folder, (DriveFolder))
     assert folder.isfolder()
コード例 #8
0
 def test_auth_creds(self):
     creds = Credentials.from_authorized_user_file(token_file)
     GoogleDrive(creds)
コード例 #9
0
 def test_auth_json_creds(self, gdrive: GoogleDrive):
     json_creds = gdrive.json_creds()
     GoogleDrive(json_creds, autorefresh=True)
     # Check if creds stay the same when not refreshing
     g = GoogleDrive(json_creds, autorefresh=False)
     assert g.json_creds() == json_creds
コード例 #10
0
 def from_path(cls, creds_json, rootpath, *args, **kwargs) -> cls:
     drive = GoogleDrive(creds_json)
     root = drive.create_path(rootpath)
     return cls(root, *args, **kwargs)