import logging
import time
import calendar

from google.cloud import storage
from tartifact_store import TartifactStore

from auth import FirebaseAuth
import pyrebase
import json

logging.basicConfig()


class GCloudArtifactStore(TartifactStore):
    def __init__(self, config, verbose=10, measure_timestamp_diff=True):
        self.logger = logging.getLogger('GCloudArtifactStore')
        self.logger.setLevel(verbose)

        if 'credentials' in config.keys():
            self.client = storage.Client.from_service_account_json(
                config['serviceAccount'])
        else:
            self.client = storage.Client()

        '''
        auth_config = config.get('auth')
        if not auth_config:
            self.client = storage.Client()
        else:
            assert auth_config['type'].lower() == 'firebase'
            app = pyrebase.initialize_app(auth_config)
            self.auth = FirebaseAuth(app,
                                     auth_config.get("use_email_auth"),
                                     auth_config.get("email"),
                                     auth_config.get("password"))

            self.client = storage.Client(credentials=self.auth.get_token())
        '''

        try:
            self.bucket = self.client.get_bucket(config['bucket'])
        except BaseException as e:
            self.logger.exception(e)
            self.bucket = self.client.create_bucket(config['bucket'])

        super(GCloudArtifactStore, self).__init__(measure_timestamp_diff)

    def _upload_file(self, key, local_path):
        self.bucket.blob(key).upload_from_filename(local_path)

    def _download_file(self, key, local_path):
        self.bucket.get_blob(key).download_to_filename(local_path)

    def _delete_file(self, key):
        blob = self.bucket.get_blob(key)
        if blob:
            blob.delete()

    def _get_file_url(self, key, method='GET'):

        expiration = long(time.time() + 100000)
        return self.bucket.blob(key).generate_signed_url(
            expiration,
            method=method)

    def _get_file_timestamp(self, key):
        blob = self.bucket.get_blob(key)
        if blob is None:
            return None
        time_updated = blob.updated
        if time_updated:
            timestamp = calendar.timegm(time_updated.timetuple())
            return timestamp
        else:
            return None

    def grant_write(self, key, user):
        blob = self.bucket.get_blob(key)
        if not blob:
            blob = self.bucket.blob(key)
            blob.upload_from_string("dummy")

        acl = blob.acl
        if user:
            acl.user(user).grant_owner()
        else:
            acl.all().grant_owner()

        acl.save()

    def get_qualified_location(self, key):
        return 'gs://' + self.bucket.name + '/' + key

    def get_bucket(self):
        return self.bucket.name

@@ -427,784 +427,446 @@
     
-if 'credentials' in config.keys():%0A            self.client = storage.Client.from_service_account_json(%0A                config%5B'serviceAccount'%5D)%0A        else:%0A            self.client = storage.Client()%0A%0A        '''%0A        auth_config = config.get('auth')%0A        if not auth_config:%0A            self.client = storage.Client()%0A        else:%0A            assert auth_config%5B'type'%5D.lower() == 'firebase'%0A            app = pyrebase.initialize_app(auth_config)%0A            self.auth = FirebaseAuth(app,%0A                                     auth_config.get(%22use_email_auth%22),%0A                                     auth_config.get(%22email%22),%0A                                     auth_config.get(%22password%22))%0A%0A            self.client = storage.Client(credentials=self.auth.get_token())
+self.config = config%0A%0A        try:%0A            self.bucket = self.getclient().get_bucket(config%5B'bucket'%5D)%0A        except BaseException as e:%0A            self.logger.exception(e)%0A            self.bucket = self.getclient().create_bucket(config%5B'bucket'%5D)%0A%0A        super(GCloudArtifactStore, self).__init__(measure_timestamp_diff)%0A%0A    def getclient(self):%0A        if 'credentials' in self.config.keys():%0A            return storage.Client %5C
 %0A   
@@ -874,21 +874,16 @@
     
-'''%0A%0A
         
 try:
@@ -882,295 +882,109 @@
     
-try:%0A            self.bucket = self.client.get_bucket(config%5B'bucket'%5D)%0A        except BaseException as e:%0A            self.logger.exception(e)%0A            self.bucket = self.client.create_bucket(config%5B'bucket'%5D)%0A%0A        super(GCloudArtifactStore, self).__init__(measure_timestamp_diff
+.from_service_account_json(config%5B'serviceAccount'%5D)%0A        else:%0A            return storage.Client(
 )%0A%0A 

make client transient in gcloud_artifact_storage