# Path: cloudvolume/lib.py
# def mkdir(path):
#   path = toabs(path)
# 
#   try:
#     if path != '' and not os.path.exists(path):
#       os.makedirs(path)
#   except OSError as e:
#     if e.errno == 17: # File Exists
#       time.sleep(0.1)
#       return mkdir(path)
#     else:
#       raise
# 
#   return path
# 
# def colorize(color, text):
#   color = color.upper()
#   return COLORS[color] + text + COLORS['RESET']


from typing import Optional, Union, Dict
from collections import defaultdict
from google.oauth2 import service_account
from .lib import mkdir, colorize
import os
import json




HOME = os.path.expanduser('~')
CLOUD_VOLUME_DIR = os.path.join(HOME, '.cloudvolume')
CLOUD_VOLUME_DIR = os.environ.get("CLOUD_VOLUME_DIR", CLOUD_VOLUME_DIR)
CLOUD_VOLUME_CACHE_DIR = os.environ.get("CLOUD_VOLUME_CACHE_DIR", None)

try:
  mkdir(CLOUD_VOLUME_DIR)
except PermissionError: # allow operation in read-only mode
  pass

def secretpath(filepath):
  preferred = os.path.join(CLOUD_VOLUME_DIR, filepath)
  
  if os.path.exists(preferred):
    return preferred

  backcompat = [
    os.path.join(HOME, '.neuroglancer'), # older
    '/' # original
  ]

  backcompat = [ os.path.join(path, filepath) for path in backcompat ] 

  for path in backcompat:
    if os.path.exists(path):

# Path: cloudvolume/lib.py
# def mkdir(path):
#   path = toabs(path)
# 
#   try:
#     if path != '' and not os.path.exists(path):
#       os.makedirs(path)
#   except OSError as e:
#     if e.errno == 17: # File Exists
#       time.sleep(0.1)
#       return mkdir(path)
#     else:
#       raise
# 
#   return path
# 
# def colorize(color, text):
#   color = color.upper()
#   return COLORS[color] + text + COLORS['RESET']


# Path: cloudvolume/secrets.py
from typing import Optional, Union, Dict
from collections import defaultdict
from google.oauth2 import service_account
from .lib import mkdir, colorize
import os
import json





HOME = os.path.expanduser('~')
CLOUD_VOLUME_DIR = os.path.join(HOME, '.cloudvolume')
CLOUD_VOLUME_DIR = os.environ.get("CLOUD_VOLUME_DIR", CLOUD_VOLUME_DIR)
CLOUD_VOLUME_CACHE_DIR = os.environ.get("CLOUD_VOLUME_CACHE_DIR", None)

try:
  mkdir(CLOUD_VOLUME_DIR)
except PermissionError: # allow operation in read-only mode
  pass

def secretpath(filepath):
  preferred = os.path.join(CLOUD_VOLUME_DIR, filepath)
  
  if os.path.exists(preferred):
    return preferred

  backcompat = [
    os.path.join(HOME, '.neuroglancer'), # older
    '/' # original
  ]

  backcompat = [ os.path.join(path, filepath) for path in backcompat ] 

  for path in backcompat:
    if os.path.exists(path):

      print(colorize('yellow', 'Deprecation Warning: {} is now preferred to {}.'.format(preferred, path)))  