<commit_before>from __future__ import print_function
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from pydrive.files import GoogleDriveFile, FileNotUploadedError, ApiRequestError
from base64 import b64decode, b64encode

from Encryption import RSAAESEncryption
from CCBackEnd import *
import sys
import re
import xmltodict

# Ipython autocomplete
try:
    # >=ipython-1.0
    from IPython import get_ipython
except ImportError:
    try:
        # support >=ipython-0.11, <ipython-1.0
        from IPython.core.ipapi import get as get_ipython
    except ImportError:
        # support <ipython-0.11
        try:
            from IPython.ipapi import get as get_ipython
        except ImportError:
            print('Not ipython')

if sys.version_info.major > 2:
    raw_input = input  # future compatibility


class NotS3Error(RuntimeError):
    """This is not an S3 backend"""


class GDriveClient(CCBackEnd):
    """
    Google Drive client based on PyDrive, which is based on google apis.
    To use, you need to enable gdrive APIs and make an OAuth2 id
    """

    @staticmethod
    def Authenticate(secrets, credentials):
        """
        Authenticates with Gdrive
        @param secrets: 		str, name of client secrets file
        @param credentials: 	str, name of saved credentials file
        @return:	GoogleAuth object with authentications
        """
        authenticator = GoogleAuth()
        authenticator.settings = {
            'client_config_backend': 'file',
            'client_config_file':    secrets,
            'save_credentials':      False,
            'oauth_scope':           ['https://www.googleapis.com/auth/drive']
        }

        # get credentials
        authenticator.LoadCredentialsFile(credentials)
        if authenticator.credentials is None:  # no credentials
            response = raw_input('No credentials. Authenticate with local web browser? [y]/n > ')
            if response.lower() in ['y', 'yes'] or len(response) == 0:
                authenticator.LocalWebserverAuth()
            else:
                authenticator.CommandLineAuth()
        elif authenticator.access_token_expired:  # stale credentials
            authenticator.Refresh()
        else:
            authenticator.Authorize()

        authenticator.SaveCredentialsFile(credentials)  # update/save credentials

        return authenticator

    def __init__(self, secrets = 'client_secrets.json', credentials = 'gd<PASSWORD>-credentials.<PASSWORD>', config = None):
        """
        Constructor
        @param secrets:			str, name of client secrets file
        @param credentials: 	str, name of saved credentials file
        @param config:			str, name of config file, if given will override other settings
        """
        super(GDriveClient, self).__init__()

        # fields
        self.drive = None
        self.current_directory_object = None
        self.dir = None
        self.service = None
        self.rootID = None

        if not config:
            ### Authentication
            authenticator = GDriveClient.Authenticate(secrets, credentials)

            ### Get actual drive client
            self.initialise_drive(authenticator)
        else:
            self.load_config(config)

        # if running in ipython, hook in autocomplete
        try:
            self.hook_ipython_completer()
        except Exception as e:
            print(e.__str__())

    def initialise_drive(self, authenticator):
        """
        Initializes internal gdrive client
        @param authenticator: 	authenticated GoogleAuth instance
        @return:
        """
        self.drive = GoogleDrive(authenticator)
        self.current_directory_object = self.get_file_by_ID('root')  # cmd-like movement in google drive structure
        self.rootID = self.current_directory_id  # remember root ID
        self.dir = '/'
        self.list_objects(True)  # this call inits self.drive.auth.service, which exposes lower-level api calls
        self.service = self.drive.auth.service  # less typing XD

    def load_config(self, file_name):
        """
        Load a config file
        @param file_name: 	str, name of file
        @return:
        """
        with open(file_name) as configFile:
            config = xmltodict.parse(configFile.read())

        authenticator = self.Authenticate(config['DriveClient-Config']['Authentication']['Client-Secrets'], config['DriveClient-Config']['Authentication']['Credentials'])
        self.initialise_drive(authenticator)

        if bool(config['DriveClient-Config']['FileIO']['@Encryption']):
            self.encryptor = RSAAESEncryption(config['DriveClient-Config']['FileIO']['Encryption']['RSAkey'])

    def save_config(self, file_name):
        """
        Saves the current configs
        @param file_name:
        @return:
        """

    @property
    def current_directory_id(self):
        """<commit_msg>remove superfluous config loading in gdrive client<commit_after>from __future__ import print_function
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from pydrive.files import GoogleDriveFile, FileNotUploadedError, ApiRequestError
from CCBackEnd import *
import sys
import re

# Ipython autocomplete
try:
    # >=ipython-1.0
    from IPython import get_ipython
except ImportError:
    try:
        # support >=ipython-0.11, <ipython-1.0
        from IPython.core.ipapi import get as get_ipython
    except ImportError:
        # support <ipython-0.11
        try:
            from IPython.ipapi import get as get_ipython
        except ImportError:
            print('Not ipython')

if sys.version_info.major > 2:
    raw_input = input  # future compatibility


class NotS3Error(RuntimeError):
    """This is not an S3 backend"""


class GDriveClient(CCBackEnd):
    """
    Google Drive client based on PyDrive, which is based on google apis.
    To use, you need to enable gdrive APIs and make an OAuth2 id
    """

    @staticmethod
    def Authenticate(secrets, credentials):
        """
        Authenticates with Gdrive
        @param secrets: 		str, name of client secrets file
        @param credentials: 	str, name of saved credentials file
        @return:	GoogleAuth object with authentications
        """
        authenticator = GoogleAuth()
        authenticator.settings = {
            'client_config_backend': 'file',
            'client_config_file':    secrets,
            'save_credentials':      False,
            'oauth_scope':           ['https://www.googleapis.com/auth/drive']
        }

        # get credentials
        authenticator.LoadCredentialsFile(credentials)
        if authenticator.credentials is None:  # no credentials
            response = raw_input('No credentials. Authenticate with local web browser? [y]/n > ')
            if response.lower() in ['y', 'yes'] or len(response) == 0:
                authenticator.LocalWebserverAuth()
            else:
                authenticator.CommandLineAuth()
        elif authenticator.access_token_expired:  # stale credentials
            authenticator.Refresh()
        else:
            authenticator.Authorize()

        authenticator.SaveCredentialsFile(credentials)  # update/save credentials

        return authenticator

    def __init__(self, secrets = 'client_secrets.json', credentials = '<PASSWORD>'):
        """
        Constructor
        @param secrets:			str, name of client secrets file
        @param credentials: 	str, name of saved credentials file
        """
        super(GDriveClient, self).__init__()

        # fields
        self.drive = None
        self.current_directory_object = None
        self.dir = None
        self.service = None
        self.rootID = None

        ### Authentication
        authenticator = GDriveClient.Authenticate(secrets, credentials)

        ### Get actual drive client
        self.initialise_drive(authenticator)

        # if running in ipython, hook in autocomplete
        try:
            self.hook_ipython_completer()
        except Exception as e:
            print(e.__str__())

    def initialise_drive(self, authenticator):
        """
        Initializes internal gdrive client
        @param authenticator: 	authenticated GoogleAuth instance
        @return:
        """
        self.drive = GoogleDrive(authenticator)
        self.current_directory_object = self.get_file_by_ID('root')  # cmd-like movement in google drive structure
        self.rootID = self.current_directory_id  # remember root ID
        self.dir = '/'
        self.list_objects(True)  # this call inits self.drive.auth.service, which exposes lower-level api calls
        self.service = self.drive.auth.service  # less typing XD

    @property
    def current_directory_id(self):
        """
{'fraction_non_alphanumeric': 0.08382687927107062, 'fraction_numerical': 0.004100227790432802, 'mean_word_length': 2.4557260920897286, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 14, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4303233', 'n_tokens_mistral': 2426, 'n_tokens_neox': 2330, 'n_words': 651}