# Path: transmogrify/optimize.py
# def optimize(image, fmt='jpeg', quality=80):
#     """
#     Optimize the image if the IMAGE_OPTIMIZATION_CMD is set.
# 
#     IMAGE_OPTIMIZATION_CMD must accept piped input
#     """
#     from io import BytesIO
# 
#     if IMAGE_OPTIMIZATION_CMD and is_tool(IMAGE_OPTIMIZATION_CMD):
#         image_buffer = BytesIO()
#         image.save(image_buffer, format=fmt, quality=quality)
#         image_buffer.seek(0)  # If you don't reset the file pointer, the read command returns an empty string
#         p1 = subprocess.Popen(IMAGE_OPTIMIZATION_CMD, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
#         output_optim, output_err = p1.communicate(image_buffer.read())
#         if not output_optim:
#             logger.debug("No image buffer received from IMAGE_OPTIMIZATION_CMD")
#             logger.debug("output_err: {0}".format(output_err))
#             return image
#         im = Image.open(BytesIO(output_optim))
#         return im
#     else:
#         return image
# 
# Path: transmogrify/settings.py
# DEFAULT_SETTINGS = {
#     'BASE_PATH': "/home/media/",
#     'DEBUG': False,
#     'EXTERNAL_PREFIX': "/external/",
#     'FALLBACK_SERVERS': (),
#     'IMAGE_OPTIMIZATION_CMD': '',
#     'NO_IMAGE_URL': "",
#     'OPENCV_PREFIX': '/usr/local/share/',
#     'ORIG_BASE_PATH': "/home/media/",
#     'ORIG_PATH_HANDLER': None,
#     'PATH_ALIASES': {},
#     'SECRET_KEY': "",
#     'USE_VHOSTS': False,
#     'VALID_DOMAINS': [],
#     'VHOST_DOC_BASE': "",
#     'VALID_IMAGE_EXTENSIONS': ['jpeg', 'jpg', 'gif', 'png', ],
#     'ALLOWED_PROCESSORS': ['__all__', ],
#     'CHECK_SECURITY': True,
# }
# USER_SETTINGS = DEFAULT_SETTINGS.copy()
#     USER_SETTINGS['SECRET_KEY'] = os.environ.get("TRANSMOGRIFY_SECRET")
#     USER_SETTINGS['SECRET_KEY'] = os.environ.get("TRANSMOGRIFY_SECRET_KEY")
#     USER_SETTINGS['DEBUG'] = bool_from_env("TRANSMOGRIFY_DEBUG")
#     USER_SETTINGS['BASE_PATH'] = os.environ.get("TRANSMOGRIFY_BASE_PATH")
#     USER_SETTINGS['USE_VHOSTS'] = bool_from_env("TRANSMOGRIFY_USE_VHOSTS", False)
#     USER_SETTINGS['VHOST_DOC_BASE'] = os.environ.get("TRANSMOGRIFY_VHOST_DOC_BASE", "")
#     USER_SETTINGS['NO_IMAGE_URL'] = os.environ.get("TRANSMOGRIFY_NO_IMG_URL", "")
#     USER_SETTINGS['PATH_ALIASES'] = dict(lists_from_env("TRANSMOGRIFY_PATH_ALIASES"))
#         USER_SETTINGS['FALLBACK_SERVERS'] = dict(lists_from_env("TRANSMOGRIFY_FALLBACK_SERVERS"))
#         USER_SETTINGS['FALLBACK_SERVERS'] = {}
#     USER_SETTINGS['ORIG_PATH_HANDLER'] = os.environ.get("TRANSMOGRIFY_ORIG_PATH_HANDLER", "")
#     USER_SETTINGS['ORIG_BASE_PATH'] = os.environ.get("TRANSMOGRIFY_ORIG_BASE_PATH", "/home/media/")
#     USER_SETTINGS['EXTERNAL_PREFIX'] = os.environ.get("TRANSMOGRIFY_EXTERNAL_PREFIX", "/external/")
#     USER_SETTINGS['ALLOWED_PROCESSORS'] = os.environ.get("TRANSMOGRIFY_ALLOWED_PROCESSORS", "__all__,").split(",")
#     USER_SETTINGS['IMAGE_OPTIMIZATION_CMD'] = list_from_env("TRANSMOGRIFY_IMAGE_OPTIMIZATION_CMD")
# PATH_ALIASES = {}
# FALLBACK_SERVERS = (
#     # Format is
#     # (regex, repl, host),
#     # (r"^/media/(.*), "\1", "http://www.example.com/"),
# )
# PROCESSORS = {}
# ALLOW_ALL_PROCESSORS = '__all__' in USER_SETTINGS['ALLOWED_PROCESSORS']
# def bool_from_env(key, default=False):
# def list_from_env(key, default=""):
# def lists_from_env(key, default=""):
# 
# Path: transmogrify/utils.py
# def is_tool(name):
# def download_url(url, destination):
# def is_valid_actionstring(action_string):
# def create_securityhash(action_tuples):
# def create_purge_securityhash():  # pragma: no cover
# def generate_url(url, action_string):
# def is_valid_security(action_tuples, security_hash):
# def resolve_request_path(requested_uri):
# def parse_action_tuples(filename):
# def process_url(url, server_name="", document_root=None, check_security=True):
# def get_cached_files(url, server_name="", document_root=None):


import os
from transmogrify import optimize, settings, utils
    from PIL import Image
    from PIL import Image
# import pytest



def test_convert_to_rgb():
    """
    Test that an image gets converted from CMYK to RGB
    """

    img = Image.open(os.path.join(settings.ORIG_BASE_PATH, '2188374.tif'))
    assert img.mode == 'CMYK'
    new_img = optimize.convert_to_rgb(img)
    assert new_img.mode == 'RGB'
    another_new_img = optimize.convert_to_rgb(new_img)
    assert another_new_img.mode == 'RGB'


def test_optimize():
    """
    Test that our mocked optimization tool goes through
    """

    assert utils.is_tool(settings.IMAGE_OPTIMIZATION_CMD)

    img = Image.open(os.path.join(settings.ORIG_BASE_PATH, 'horiz_img.jpg'))


# Path: transmogrify/optimize.py
# def optimize(image, fmt='jpeg', quality=80):
#     """
#     Optimize the image if the IMAGE_OPTIMIZATION_CMD is set.
# 
#     IMAGE_OPTIMIZATION_CMD must accept piped input
#     """
#     from io import BytesIO
# 
#     if IMAGE_OPTIMIZATION_CMD and is_tool(IMAGE_OPTIMIZATION_CMD):
#         image_buffer = BytesIO()
#         image.save(image_buffer, format=fmt, quality=quality)
#         image_buffer.seek(0)  # If you don't reset the file pointer, the read command returns an empty string
#         p1 = subprocess.Popen(IMAGE_OPTIMIZATION_CMD, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
#         output_optim, output_err = p1.communicate(image_buffer.read())
#         if not output_optim:
#             logger.debug("No image buffer received from IMAGE_OPTIMIZATION_CMD")
#             logger.debug("output_err: {0}".format(output_err))
#             return image
#         im = Image.open(BytesIO(output_optim))
#         return im
#     else:
#         return image
# 
# Path: transmogrify/settings.py
# DEFAULT_SETTINGS = {
#     'BASE_PATH': "/home/media/",
#     'DEBUG': False,
#     'EXTERNAL_PREFIX': "/external/",
#     'FALLBACK_SERVERS': (),
#     'IMAGE_OPTIMIZATION_CMD': '',
#     'NO_IMAGE_URL': "",
#     'OPENCV_PREFIX': '/usr/local/share/',
#     'ORIG_BASE_PATH': "/home/media/",
#     'ORIG_PATH_HANDLER': None,
#     'PATH_ALIASES': {},
#     'SECRET_KEY': "",
#     'USE_VHOSTS': False,
#     'VALID_DOMAINS': [],
#     'VHOST_DOC_BASE': "",
#     'VALID_IMAGE_EXTENSIONS': ['jpeg', 'jpg', 'gif', 'png', ],
#     'ALLOWED_PROCESSORS': ['__all__', ],
#     'CHECK_SECURITY': True,
# }
# USER_SETTINGS = DEFAULT_SETTINGS.copy()
#     USER_SETTINGS['SECRET_KEY'] = os.environ.get("TRANSMOGRIFY_SECRET")
#     USER_SETTINGS['SECRET_KEY'] = os.environ.get("TRANSMOGRIFY_SECRET_KEY")
#     USER_SETTINGS['DEBUG'] = bool_from_env("TRANSMOGRIFY_DEBUG")
#     USER_SETTINGS['BASE_PATH'] = os.environ.get("TRANSMOGRIFY_BASE_PATH")
#     USER_SETTINGS['USE_VHOSTS'] = bool_from_env("TRANSMOGRIFY_USE_VHOSTS", False)
#     USER_SETTINGS['VHOST_DOC_BASE'] = os.environ.get("TRANSMOGRIFY_VHOST_DOC_BASE", "")
#     USER_SETTINGS['NO_IMAGE_URL'] = os.environ.get("TRANSMOGRIFY_NO_IMG_URL", "")
#     USER_SETTINGS['PATH_ALIASES'] = dict(lists_from_env("TRANSMOGRIFY_PATH_ALIASES"))
#         USER_SETTINGS['FALLBACK_SERVERS'] = dict(lists_from_env("TRANSMOGRIFY_FALLBACK_SERVERS"))
#         USER_SETTINGS['FALLBACK_SERVERS'] = {}
#     USER_SETTINGS['ORIG_PATH_HANDLER'] = os.environ.get("TRANSMOGRIFY_ORIG_PATH_HANDLER", "")
#     USER_SETTINGS['ORIG_BASE_PATH'] = os.environ.get("TRANSMOGRIFY_ORIG_BASE_PATH", "/home/media/")
#     USER_SETTINGS['EXTERNAL_PREFIX'] = os.environ.get("TRANSMOGRIFY_EXTERNAL_PREFIX", "/external/")
#     USER_SETTINGS['ALLOWED_PROCESSORS'] = os.environ.get("TRANSMOGRIFY_ALLOWED_PROCESSORS", "__all__,").split(",")
#     USER_SETTINGS['IMAGE_OPTIMIZATION_CMD'] = list_from_env("TRANSMOGRIFY_IMAGE_OPTIMIZATION_CMD")
# PATH_ALIASES = {}
# FALLBACK_SERVERS = (
#     # Format is
#     # (regex, repl, host),
#     # (r"^/media/(.*), "\1", "http://www.example.com/"),
# )
# PROCESSORS = {}
# ALLOW_ALL_PROCESSORS = '__all__' in USER_SETTINGS['ALLOWED_PROCESSORS']
# def bool_from_env(key, default=False):
# def list_from_env(key, default=""):
# def lists_from_env(key, default=""):
# 
# Path: transmogrify/utils.py
# def is_tool(name):
# def download_url(url, destination):
# def is_valid_actionstring(action_string):
# def create_securityhash(action_tuples):
# def create_purge_securityhash():  # pragma: no cover
# def generate_url(url, action_string):
# def is_valid_security(action_tuples, security_hash):
# def resolve_request_path(requested_uri):
# def parse_action_tuples(filename):
# def process_url(url, server_name="", document_root=None, check_security=True):
# def get_cached_files(url, server_name="", document_root=None):


# Path: transmogrify/tests/test_optimize.py
import os
from transmogrify import optimize, settings, utils
    from PIL import Image
    from PIL import Image

# import pytest



def test_convert_to_rgb():
    """
    Test that an image gets converted from CMYK to RGB
    """

    img = Image.open(os.path.join(settings.ORIG_BASE_PATH, '2188374.tif'))
    assert img.mode == 'CMYK'
    new_img = optimize.convert_to_rgb(img)
    assert new_img.mode == 'RGB'
    another_new_img = optimize.convert_to_rgb(new_img)
    assert another_new_img.mode == 'RGB'


def test_optimize():
    """
    Test that our mocked optimization tool goes through
    """

    assert utils.is_tool(settings.IMAGE_OPTIMIZATION_CMD)

    img = Image.open(os.path.join(settings.ORIG_BASE_PATH, 'horiz_img.jpg'))


    new_img = optimize.optimize(img)