# Path: marvin_python_toolbox/common/data.py
# 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)
#         if not is_path_created:
#             raise InvalidConfigException('Data path does not exist!')
# 
#         return marvin_path
# 
#     @classmethod
#     def _convert_path_to_key(cls, path):
#         if path.startswith(os.path.sep):
#             path = os.path.relpath(path, start=cls.data_path)
#         return '/'.join(path.split(os.path.sep))
# 
#     @classmethod
#     def load_data(cls, relpath):
#         """
#         Load data from the following sources in order of priority:
# 
#         1. Filesystem
# 
#         :param relpath: path relative to "data_path"
#         :return: str - data content
#         """
#         filepath = os.path.join(cls.data_path, relpath)
#         with open(filepath) as fp:
#             content = fp.read()
# 
#         return content
# 
#     @classmethod
#     def download_file(cls, url, local_file_name=None, force=False, chunk_size=1024):
#         """
#         Download file from a given url
#         """
# 
#         local_file_name = local_file_name if local_file_name else url.split('/')[-1]
#         filepath = os.path.join(cls.data_path, local_file_name)
# 
#         if not os.path.exists(filepath) or force:
#             try:
#                 headers = requests.head(url, allow_redirects=True).headers
#                 length = headers.get('Content-Length')
# 
#                 logger.info("Starting download of {} file with {} bytes ...".format(url, length))
# 
#                 widgets = [
#                     'Downloading file please wait...', progressbar.Percentage(),
#                     ' ', progressbar.Bar(),
#                     ' ', progressbar.ETA(),
#                     ' ', progressbar.FileTransferSpeed(),
#                 ]
#                 bar = progressbar.ProgressBar(widgets=widgets, max_value=int(length) + chunk_size).start()
# 
#                 r = requests.get(url, stream=True)
# 
#                 with open(filepath, 'wb') as f:
#                     total_chunk = 0
# 
#                     for chunk in r.iter_content(chunk_size):
#                         if chunk:
#                             f.write(chunk)
#                             total_chunk += chunk_size
#                             bar.update(total_chunk)
# 
#                 bar.finish()
# 
#             except:
#                 if os.path.exists(filepath):
#                     os.remove(filepath)
# 
#                 raise
# 
#         return filepath
# 
# Path: marvin_python_toolbox/common/exceptions.py
# class InvalidConfigException(ConfigException):
#     """
#     Invalid Marvin Config Base Exception
#     """


import os
import pytest
    import mock
    import unittest.mock as mock
from marvin_python_toolbox.common.data import MarvinData
from marvin_python_toolbox.common.exceptions import InvalidConfigException
from io import IOBase
    from requests import Response
    from requests import Response
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

try:
except ImportError:



@pytest.fixture
def data_path():
    return os.path.expanduser("/tmp/data")


@pytest.fixture
def data_path_key():
    return MarvinData._key


def test_read_from_env(data_path_key, data_path):
    os.environ[data_path_key] = data_path
    assert MarvinData.data_path == os.environ[data_path_key]


def test_path_not_set(data_path_key):
    del os.environ[data_path_key]
    path_ = None
    try:
        path_ = MarvinData.data_path

# Path: marvin_python_toolbox/common/data.py
# 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)
#         if not is_path_created:
#             raise InvalidConfigException('Data path does not exist!')
# 
#         return marvin_path
# 
#     @classmethod
#     def _convert_path_to_key(cls, path):
#         if path.startswith(os.path.sep):
#             path = os.path.relpath(path, start=cls.data_path)
#         return '/'.join(path.split(os.path.sep))
# 
#     @classmethod
#     def load_data(cls, relpath):
#         """
#         Load data from the following sources in order of priority:
# 
#         1. Filesystem
# 
#         :param relpath: path relative to "data_path"
#         :return: str - data content
#         """
#         filepath = os.path.join(cls.data_path, relpath)
#         with open(filepath) as fp:
#             content = fp.read()
# 
#         return content
# 
#     @classmethod
#     def download_file(cls, url, local_file_name=None, force=False, chunk_size=1024):
#         """
#         Download file from a given url
#         """
# 
#         local_file_name = local_file_name if local_file_name else url.split('/')[-1]
#         filepath = os.path.join(cls.data_path, local_file_name)
# 
#         if not os.path.exists(filepath) or force:
#             try:
#                 headers = requests.head(url, allow_redirects=True).headers
#                 length = headers.get('Content-Length')
# 
#                 logger.info("Starting download of {} file with {} bytes ...".format(url, length))
# 
#                 widgets = [
#                     'Downloading file please wait...', progressbar.Percentage(),
#                     ' ', progressbar.Bar(),
#                     ' ', progressbar.ETA(),
#                     ' ', progressbar.FileTransferSpeed(),
#                 ]
#                 bar = progressbar.ProgressBar(widgets=widgets, max_value=int(length) + chunk_size).start()
# 
#                 r = requests.get(url, stream=True)
# 
#                 with open(filepath, 'wb') as f:
#                     total_chunk = 0
# 
#                     for chunk in r.iter_content(chunk_size):
#                         if chunk:
#                             f.write(chunk)
#                             total_chunk += chunk_size
#                             bar.update(total_chunk)
# 
#                 bar.finish()
# 
#             except:
#                 if os.path.exists(filepath):
#                     os.remove(filepath)
# 
#                 raise
# 
#         return filepath
# 
# Path: marvin_python_toolbox/common/exceptions.py
# class InvalidConfigException(ConfigException):
#     """
#     Invalid Marvin Config Base Exception
#     """


# Path: tests/common/test_data.py
import os
import pytest
    import mock
    import unittest.mock as mock
from marvin_python_toolbox.common.data import MarvinData
from marvin_python_toolbox.common.exceptions import InvalidConfigException
from io import IOBase
    from requests import Response
    from requests import Response

# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

try:
except ImportError:



@pytest.fixture
def data_path():
    return os.path.expanduser("/tmp/data")


@pytest.fixture
def data_path_key():
    return MarvinData._key


def test_read_from_env(data_path_key, data_path):
    os.environ[data_path_key] = data_path
    assert MarvinData.data_path == os.environ[data_path_key]


def test_path_not_set(data_path_key):
    del os.environ[data_path_key]
    path_ = None
    try:
        path_ = MarvinData.data_path

    except InvalidConfigException: