# Path: binstar_client/utils/notebook/data_uri.py
# class DataURIConverter(object):
#     def __init__(self, location):
#         self.check_pillow_installed()
#         self.location = location
# 
#     def check_pillow_installed(self):
#         if Image is None:
#             raise PillowNotInstalled()
# 
#     def __call__(self):
#         if os.path.exists(self.location):
#             with open(self.location, "rb") as fp:
#                 return self._encode(self.resize_and_convert(fp).read())
#         elif self.is_url():
#             content = requests.get(self.location).content
#             fp = io.BytesIO()
#             fp.write(content)
#             fp.seek(0)
#             return self._encode(self.resize_and_convert(fp).read())
#         else:
#             raise IOError("{} not found".format(self.location))
# 
#     def resize_and_convert(self, fp):
#         im = Image.open(fp)
#         im.thumbnail(THUMB_SIZE)
#         out = io.BytesIO()
#         im.save(out, format='png')
#         out.seek(0)
#         return out
# 
#     def is_py3(self):
#         return sys.version_info[0] == 3
# 
#     def is_url(self):
#         return self.location is not None and urlparse(self.location).scheme in ['http', 'https']
# 
#     def _encode(self, content):
#         if self.is_py3():
#             data64 = base64.b64encode(content).decode("ascii")
#         else:
#             data64 = content.encode('base64').replace("\n", "")
#         return data64
# 
# Path: binstar_client/utils/test/utils.py
# def data_dir(filename):
#     base_path = inspect.stack()[1][1]  # function caller path
#     test_data = os.path.join(os.path.dirname(base_path), 'data')
#     return os.path.join(test_data, filename)


import unittest
from binstar_client.utils.notebook.data_uri import DataURIConverter
from binstar_client.utils.test.utils import data_dir



class DataURIConverterTestCase(unittest.TestCase):
    def test_local_image(self):
        location = data_dir('bokeh-logo.png')
        output = DataURIConverter(location)()
        self.assertEqual(output[0:5], "iVBOR")

    def test_file_not_found(self):
        location = data_dir('no-exists.png')
        with self.assertRaises(IOError):
            DataURIConverter(location)()

    def test_is_python_3(self):
        output = DataURIConverter('')
        self.assertIsInstance(output.is_py3(), bool)

    def test_is_url(self):
        location = 'http://docs.continuum.io/_static/img/continuum_analytics_logo.png'

# Path: binstar_client/utils/notebook/data_uri.py
# class DataURIConverter(object):
#     def __init__(self, location):
#         self.check_pillow_installed()
#         self.location = location
# 
#     def check_pillow_installed(self):
#         if Image is None:
#             raise PillowNotInstalled()
# 
#     def __call__(self):
#         if os.path.exists(self.location):
#             with open(self.location, "rb") as fp:
#                 return self._encode(self.resize_and_convert(fp).read())
#         elif self.is_url():
#             content = requests.get(self.location).content
#             fp = io.BytesIO()
#             fp.write(content)
#             fp.seek(0)
#             return self._encode(self.resize_and_convert(fp).read())
#         else:
#             raise IOError("{} not found".format(self.location))
# 
#     def resize_and_convert(self, fp):
#         im = Image.open(fp)
#         im.thumbnail(THUMB_SIZE)
#         out = io.BytesIO()
#         im.save(out, format='png')
#         out.seek(0)
#         return out
# 
#     def is_py3(self):
#         return sys.version_info[0] == 3
# 
#     def is_url(self):
#         return self.location is not None and urlparse(self.location).scheme in ['http', 'https']
# 
#     def _encode(self, content):
#         if self.is_py3():
#             data64 = base64.b64encode(content).decode("ascii")
#         else:
#             data64 = content.encode('base64').replace("\n", "")
#         return data64
# 
# Path: binstar_client/utils/test/utils.py
# def data_dir(filename):
#     base_path = inspect.stack()[1][1]  # function caller path
#     test_data = os.path.join(os.path.dirname(base_path), 'data')
#     return os.path.join(test_data, filename)


# Path: binstar_client/utils/notebook/tests/test_data_uri.py
import unittest
from binstar_client.utils.notebook.data_uri import DataURIConverter
from binstar_client.utils.test.utils import data_dir




class DataURIConverterTestCase(unittest.TestCase):
    def test_local_image(self):
        location = data_dir('bokeh-logo.png')
        output = DataURIConverter(location)()
        self.assertEqual(output[0:5], "iVBOR")

    def test_file_not_found(self):
        location = data_dir('no-exists.png')
        with self.assertRaises(IOError):
            DataURIConverter(location)()

    def test_is_python_3(self):
        output = DataURIConverter('')
        self.assertIsInstance(output.is_py3(), bool)

    def test_is_url(self):
        location = 'http://docs.continuum.io/_static/img/continuum_analytics_logo.png'

        self.assertTrue(DataURIConverter(location).is_url())