# Path: marvin_python_toolbox/_compatibility.py

# 
# Path: marvin_python_toolbox/common/utils.py
# def check_path(path, create=False):
#     """
#     Check for a path on filesystem
# 
#     :param path: str - path name
#     :param create: bool - create if do not exist
#     :return: bool - path exists
#     """
#     if not os.path.exists(path):
#         if create:
#             os.makedirs(path)
#             return os.path.exists(path)
#         else:
#             return False
# 
#     return True
# 
# Path: marvin_python_toolbox/common/exceptions.py
# class InvalidConfigException(ConfigException):
#     """
#     Invalid Marvin Config Base Exception
#     """
# 
# Path: marvin_python_toolbox/_logging.py
# def get_logger(name, namespace='marvin_python_toolbox',
#                log_level=DEFAULT_LOG_LEVEL, log_dir=DEFAULT_LOG_DIR):
#     """Build a logger that outputs to a file and to the console,"""
# 
#     log_level = (os.getenv('{}_LOG_LEVEL'.format(namespace.upper())) or
#                  os.getenv('LOG_LEVEL', log_level))
#     log_dir = (os.getenv('{}_LOG_DIR'.format(namespace.upper())) or
#                os.getenv('LOG_DIR', log_dir))
# 
#     logger = logging.getLogger('{}.{}'.format(namespace, name))
#     logger.setLevel(log_level)
# 
#     formatter = logging.Formatter(
#         '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# 
#     # Create a console stream handler
#     console_handler = logging.StreamHandler()
#     console_handler.setLevel(log_level)
#     console_handler.setFormatter(formatter)
#     logger.addHandler(console_handler)
# 
#     try:
#         log_path = os.path.abspath(log_dir)
#         log_filename = '{name}.{pid}.log'.format(
#             name=namespace, pid=os.getpid())
# 
#         file_path = str(os.path.join(log_path, log_filename))
# 
#         if not os.path.exists(log_path):  # pragma: no cover
#             os.makedirs(log_path, mode=774)
# 
#         # Create a file handler
#         file_handler = logging.FileHandler(file_path)
#         file_handler.setLevel(log_level)
#         file_handler.setFormatter(formatter)
#         logger.addHandler(file_handler)
#     except OSError as e:
#         logger.error('Could not create log file {file}: {error}'.format(
#             file=file_path, error=e.strerror))
# 
#     return logger


import os
import requests
import progressbar
from .._compatibility import six
from .utils import check_path
from .exceptions import InvalidConfigException
from six import with_metaclass
from .._logging import get_logger
# Use six to create code compatible with Python 2 and 3.
# See http://pythonhosted.org/six/

logger = get_logger('common.data')


class AbstractMarvinData(type):
    @property
    def data_path(cls):
        return cls.get_data_path()


class MarvinData(with_metaclass(AbstractMarvinData)):
    _key = 'MARVIN_DATA_PATH'

    @classmethod
    def get_data_path(cls):
        """
        Read data path from the following sources in order of priority:

        1. Environment variable

        If not found raises an exception

        :return: str - datapath
        """
        marvin_path = os.environ.get(cls._key)
        if not marvin_path:
            raise InvalidConfigException('Data path not set!')


# Path: marvin_python_toolbox/_compatibility.py

# 
# Path: marvin_python_toolbox/common/utils.py
# def check_path(path, create=False):
#     """
#     Check for a path on filesystem
# 
#     :param path: str - path name
#     :param create: bool - create if do not exist
#     :return: bool - path exists
#     """
#     if not os.path.exists(path):
#         if create:
#             os.makedirs(path)
#             return os.path.exists(path)
#         else:
#             return False
# 
#     return True
# 
# Path: marvin_python_toolbox/common/exceptions.py
# class InvalidConfigException(ConfigException):
#     """
#     Invalid Marvin Config Base Exception
#     """
# 
# Path: marvin_python_toolbox/_logging.py
# def get_logger(name, namespace='marvin_python_toolbox',
#                log_level=DEFAULT_LOG_LEVEL, log_dir=DEFAULT_LOG_DIR):
#     """Build a logger that outputs to a file and to the console,"""
# 
#     log_level = (os.getenv('{}_LOG_LEVEL'.format(namespace.upper())) or
#                  os.getenv('LOG_LEVEL', log_level))
#     log_dir = (os.getenv('{}_LOG_DIR'.format(namespace.upper())) or
#                os.getenv('LOG_DIR', log_dir))
# 
#     logger = logging.getLogger('{}.{}'.format(namespace, name))
#     logger.setLevel(log_level)
# 
#     formatter = logging.Formatter(
#         '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# 
#     # Create a console stream handler
#     console_handler = logging.StreamHandler()
#     console_handler.setLevel(log_level)
#     console_handler.setFormatter(formatter)
#     logger.addHandler(console_handler)
# 
#     try:
#         log_path = os.path.abspath(log_dir)
#         log_filename = '{name}.{pid}.log'.format(
#             name=namespace, pid=os.getpid())
# 
#         file_path = str(os.path.join(log_path, log_filename))
# 
#         if not os.path.exists(log_path):  # pragma: no cover
#             os.makedirs(log_path, mode=774)
# 
#         # Create a file handler
#         file_handler = logging.FileHandler(file_path)
#         file_handler.setLevel(log_level)
#         file_handler.setFormatter(formatter)
#         logger.addHandler(file_handler)
#     except OSError as e:
#         logger.error('Could not create log file {file}: {error}'.format(
#             file=file_path, error=e.strerror))
# 
#     return logger


# Path: marvin_python_toolbox/common/data.py
import os
import requests
import progressbar
from .._compatibility import six
from .utils import check_path
from .exceptions import InvalidConfigException
from six import with_metaclass
from .._logging import get_logger

# Use six to create code compatible with Python 2 and 3.
# See http://pythonhosted.org/six/

logger = get_logger('common.data')


class AbstractMarvinData(type):
    @property
    def data_path(cls):
        return cls.get_data_path()


class MarvinData(with_metaclass(AbstractMarvinData)):
    _key = 'MARVIN_DATA_PATH'

    @classmethod
    def get_data_path(cls):
        """
        Read data path from the following sources in order of priority:

        1. Environment variable

        If not found raises an exception

        :return: str - datapath
        """
        marvin_path = os.environ.get(cls._key)
        if not marvin_path:
            raise InvalidConfigException('Data path not set!')


        is_path_created = check_path(marvin_path, create=True)