# Path: cloudvolume/secrets.py
# def google_credentials(bucket = ''):
#   global PROJECT_NAME
#   global GOOGLE_CREDENTIALS_CACHE
# 
#   if bucket in GOOGLE_CREDENTIALS_CACHE.keys():
#     return GOOGLE_CREDENTIALS_CACHE[bucket]
# 
#   paths = [
#     secretpath('secrets/google-secret.json')
#   ]
# 
#   if bucket:
#     paths = [ secretpath('secrets/{}-google-secret.json'.format(bucket)) ] + paths
# 
#   google_credentials = None
#   project_name = PROJECT_NAME
#   for google_credentials_path in paths:
#     if os.path.exists(google_credentials_path):
#       google_credentials = service_account.Credentials \
#         .from_service_account_file(google_credentials_path)
#       
#       with open(google_credentials_path, 'rt') as f:
#         project_name = json.loads(f.read())['project_id']
#       break
# 
#   if google_credentials == None:
#     print(colorize('yellow', 'Using default Google credentials. There is no ~/.cloudvolume/secrets/google-secret.json set.'))  
#   else:
#     GOOGLE_CREDENTIALS_CACHE[bucket] = (project_name, google_credentials)
# 
#   return project_name, google_credentials
# 
# def aws_credentials(bucket = '', service = 'aws'):
#   global AWS_CREDENTIALS_CACHE
# 
#   if service == 's3':
#     service = 'aws'
# 
#   if bucket in AWS_CREDENTIALS_CACHE.keys():
#     return AWS_CREDENTIALS_CACHE[bucket]
# 
#   default_file_path = 'secrets/{}-secret.json'.format(service)
# 
#   paths = [
#     secretpath(default_file_path)
#   ]
# 
#   if bucket:
#     paths = [ secretpath('secrets/{}-{}-secret.json'.format(bucket, service)) ] + paths
# 
#   aws_credentials = {}
#   aws_credentials_path = secretpath(default_file_path)
#   for aws_credentials_path in paths:
#     if os.path.exists(aws_credentials_path):
#       with open(aws_credentials_path, 'r') as f:
#         aws_credentials = json.loads(f.read())
#       break
#   
#   if not aws_credentials:
#     # did not find any secret json file, will try to find it in environment variables
#     if 'AWS_ACCESS_KEY_ID' in os.environ and 'AWS_SECRET_ACCESS_KEY' in os.environ:
#       aws_credentials = {
#         'AWS_ACCESS_KEY_ID': os.environ['AWS_ACCESS_KEY_ID'],
#         'AWS_SECRET_ACCESS_KEY': os.environ['AWS_SECRET_ACCESS_KEY'],
#       }
#     if 'AWS_DEFAULT_REGION' in os.environ:
#       aws_credentials['AWS_DEFAULT_REGION'] = os.environ['AWS_DEFAULT_REGION']
# 
#   AWS_CREDENTIALS_CACHE[service][bucket] = aws_credentials
#   return aws_credentials
# 
# Path: cloudvolume/exceptions.py
# class UnsupportedProtocolError(ValueError):
#   """Unknown protocol extension."""
#   pass


from six.moves import queue as Queue
from functools import partial
from google.cloud.storage import Client
from .secrets import google_credentials, aws_credentials
from .exceptions import UnsupportedProtocolError
import threading
import time
import boto3 
import tenacity
    with self._lock:
      self.outstanding = 0

  def __del__(self):
    self.reset_pool()

class S3ConnectionPool(ConnectionPool):
  def __init__(self, service, bucket):
    self.service = service
    self.bucket = bucket
    self.credentials = aws_credentials(bucket, service)
    super(S3ConnectionPool, self).__init__()

  @retry
  def _create_connection(self):
    if self.service in ('aws', 's3'):
      return boto3.client(
        's3',
        aws_access_key_id=self.credentials['AWS_ACCESS_KEY_ID'],
        aws_secret_access_key=self.credentials['AWS_SECRET_ACCESS_KEY'],
        region_name='us-east-1',
      )
    elif self.service == 'matrix':
      return boto3.client(
        's3',
        aws_access_key_id=self.credentials['AWS_ACCESS_KEY_ID'],
        aws_secret_access_key=self.credentials['AWS_SECRET_ACCESS_KEY'],
        endpoint_url='https://s3-hpcrc.rc.princeton.edu',
      )
    else:

# Path: cloudvolume/secrets.py
# def google_credentials(bucket = ''):
#   global PROJECT_NAME
#   global GOOGLE_CREDENTIALS_CACHE
# 
#   if bucket in GOOGLE_CREDENTIALS_CACHE.keys():
#     return GOOGLE_CREDENTIALS_CACHE[bucket]
# 
#   paths = [
#     secretpath('secrets/google-secret.json')
#   ]
# 
#   if bucket:
#     paths = [ secretpath('secrets/{}-google-secret.json'.format(bucket)) ] + paths
# 
#   google_credentials = None
#   project_name = PROJECT_NAME
#   for google_credentials_path in paths:
#     if os.path.exists(google_credentials_path):
#       google_credentials = service_account.Credentials \
#         .from_service_account_file(google_credentials_path)
#       
#       with open(google_credentials_path, 'rt') as f:
#         project_name = json.loads(f.read())['project_id']
#       break
# 
#   if google_credentials == None:
#     print(colorize('yellow', 'Using default Google credentials. There is no ~/.cloudvolume/secrets/google-secret.json set.'))  
#   else:
#     GOOGLE_CREDENTIALS_CACHE[bucket] = (project_name, google_credentials)
# 
#   return project_name, google_credentials
# 
# def aws_credentials(bucket = '', service = 'aws'):
#   global AWS_CREDENTIALS_CACHE
# 
#   if service == 's3':
#     service = 'aws'
# 
#   if bucket in AWS_CREDENTIALS_CACHE.keys():
#     return AWS_CREDENTIALS_CACHE[bucket]
# 
#   default_file_path = 'secrets/{}-secret.json'.format(service)
# 
#   paths = [
#     secretpath(default_file_path)
#   ]
# 
#   if bucket:
#     paths = [ secretpath('secrets/{}-{}-secret.json'.format(bucket, service)) ] + paths
# 
#   aws_credentials = {}
#   aws_credentials_path = secretpath(default_file_path)
#   for aws_credentials_path in paths:
#     if os.path.exists(aws_credentials_path):
#       with open(aws_credentials_path, 'r') as f:
#         aws_credentials = json.loads(f.read())
#       break
#   
#   if not aws_credentials:
#     # did not find any secret json file, will try to find it in environment variables
#     if 'AWS_ACCESS_KEY_ID' in os.environ and 'AWS_SECRET_ACCESS_KEY' in os.environ:
#       aws_credentials = {
#         'AWS_ACCESS_KEY_ID': os.environ['AWS_ACCESS_KEY_ID'],
#         'AWS_SECRET_ACCESS_KEY': os.environ['AWS_SECRET_ACCESS_KEY'],
#       }
#     if 'AWS_DEFAULT_REGION' in os.environ:
#       aws_credentials['AWS_DEFAULT_REGION'] = os.environ['AWS_DEFAULT_REGION']
# 
#   AWS_CREDENTIALS_CACHE[service][bucket] = aws_credentials
#   return aws_credentials
# 
# Path: cloudvolume/exceptions.py
# class UnsupportedProtocolError(ValueError):
#   """Unknown protocol extension."""
#   pass


# Path: cloudvolume/connectionpools.py
from six.moves import queue as Queue
from functools import partial
from google.cloud.storage import Client
from .secrets import google_credentials, aws_credentials
from .exceptions import UnsupportedProtocolError
import threading
import time
import boto3 
import tenacity

    with self._lock:
      self.outstanding = 0

  def __del__(self):
    self.reset_pool()

class S3ConnectionPool(ConnectionPool):
  def __init__(self, service, bucket):
    self.service = service
    self.bucket = bucket
    self.credentials = aws_credentials(bucket, service)
    super(S3ConnectionPool, self).__init__()

  @retry
  def _create_connection(self):
    if self.service in ('aws', 's3'):
      return boto3.client(
        's3',
        aws_access_key_id=self.credentials['AWS_ACCESS_KEY_ID'],
        aws_secret_access_key=self.credentials['AWS_SECRET_ACCESS_KEY'],
        region_name='us-east-1',
      )
    elif self.service == 'matrix':
      return boto3.client(
        's3',
        aws_access_key_id=self.credentials['AWS_ACCESS_KEY_ID'],
        aws_secret_access_key=self.credentials['AWS_SECRET_ACCESS_KEY'],
        endpoint_url='https://s3-hpcrc.rc.princeton.edu',
      )
    else:

      raise UnsupportedProtocolError("{} unknown. Choose from 's3' or 'matrix'.", self.service)