# Path: argos/utils/cls.py
# class SingletonMixin(object):
#     """ Mixin to ensure the class is a singleton.
# 
#         The instance method is thread-safe but the returned object not! You still have to implement
#         your own locking for that.
#     """
#     __singletons = {}
# 
#     def __init__(self, **kwargs):
#         super(SingletonMixin, self).__init__(**kwargs)
# 
#         cls = type(self)
#         logger.debug("Creating singleton: {} (awaiting lock)".format(cls))
#         cls._checkNotYetPresent()
#         SingletonMixin.__singletons[cls] = self
# 
# 
#     @classmethod
#     def instance(cls, **kwargs):
#         """ Returns the singleton's instance.
#         """
#         if cls in SingletonMixin.__singletons:
#             return SingletonMixin.__singletons[cls]
#         else:
#             return cls(**kwargs)
# 
# 
#     @classmethod
#     def _checkNotYetPresent(cls):
#         """ Checks that the newClass is not yet present in the singleton
# 
#             Also check that no descendants are present. This is typically due to bugs.
#         """
#         assert cls not in SingletonMixin.__singletons, "Constructor called twice: {}".format(cls)
# 
#         for existingClass in SingletonMixin.__singletons.keys():
#             assert not issubclass(cls, existingClass), \
#                 "Sub type of {} already present: {}".format(cls, existingClass)
# 
#             assert not issubclass(existingClass, cls), \
#                 "Ancestor of {} already present: {}".format(cls, existingClass)


import logging
import os.path
from os import listdir
from cmlib import CmLib, CmLibModel, DATA_DIR
from argos.utils.cls import SingletonMixin

DEFAULT_COLOR_MAP = "MatPlotLib/Magma"
assert DEFAULT_COLOR_MAP in DEF_FAV_COLOR_MAPS, "Default color map not in default favorites."


class CmLibSingleton(CmLib, SingletonMixin):

    def __init__(self, **kwargs):
        super(CmLibSingleton, self).__init__(**kwargs)

        logger.debug("CmLib singleton: {}".format(self))

        cmDataDir = DATA_DIR
        logger.info("Importing color map library from: {}".format(cmDataDir))

        # Don't import from Color Brewer since those are already included in MatPlotLib.
        # With sub-sampling the color maps similar maps can be achieved as the Color Brewer maps.
        excludeList = ['ColorBrewer2']
        for path in listdir(cmDataDir):
            if path in excludeList:
                logger.debug("Not importing catalogue from exlude list: {}".format(excludeList))
                continue

            fullPath = os.path.join(cmDataDir, path)
            if os.path.isdir(fullPath):
                self.load_catalog(fullPath)

        logger.debug("Number of color maps: {}".format(len(self.color_maps)))



# Path: argos/utils/cls.py
# class SingletonMixin(object):
#     """ Mixin to ensure the class is a singleton.
# 
#         The instance method is thread-safe but the returned object not! You still have to implement
#         your own locking for that.
#     """
#     __singletons = {}
# 
#     def __init__(self, **kwargs):
#         super(SingletonMixin, self).__init__(**kwargs)
# 
#         cls = type(self)
#         logger.debug("Creating singleton: {} (awaiting lock)".format(cls))
#         cls._checkNotYetPresent()
#         SingletonMixin.__singletons[cls] = self
# 
# 
#     @classmethod
#     def instance(cls, **kwargs):
#         """ Returns the singleton's instance.
#         """
#         if cls in SingletonMixin.__singletons:
#             return SingletonMixin.__singletons[cls]
#         else:
#             return cls(**kwargs)
# 
# 
#     @classmethod
#     def _checkNotYetPresent(cls):
#         """ Checks that the newClass is not yet present in the singleton
# 
#             Also check that no descendants are present. This is typically due to bugs.
#         """
#         assert cls not in SingletonMixin.__singletons, "Constructor called twice: {}".format(cls)
# 
#         for existingClass in SingletonMixin.__singletons.keys():
#             assert not issubclass(cls, existingClass), \
#                 "Sub type of {} already present: {}".format(cls, existingClass)
# 
#             assert not issubclass(existingClass, cls), \
#                 "Ancestor of {} already present: {}".format(cls, existingClass)


# Path: argos/repo/colors.py
import logging
import os.path
from os import listdir
from cmlib import CmLib, CmLibModel, DATA_DIR
from argos.utils.cls import SingletonMixin


DEFAULT_COLOR_MAP = "MatPlotLib/Magma"
assert DEFAULT_COLOR_MAP in DEF_FAV_COLOR_MAPS, "Default color map not in default favorites."


class CmLibSingleton(CmLib, SingletonMixin):

    def __init__(self, **kwargs):
        super(CmLibSingleton, self).__init__(**kwargs)

        logger.debug("CmLib singleton: {}".format(self))

        cmDataDir = DATA_DIR
        logger.info("Importing color map library from: {}".format(cmDataDir))

        # Don't import from Color Brewer since those are already included in MatPlotLib.
        # With sub-sampling the color maps similar maps can be achieved as the Color Brewer maps.
        excludeList = ['ColorBrewer2']
        for path in listdir(cmDataDir):
            if path in excludeList:
                logger.debug("Not importing catalogue from exlude list: {}".format(excludeList))
                continue

            fullPath = os.path.join(cmDataDir, path)
            if os.path.isdir(fullPath):
                self.load_catalog(fullPath)

        logger.debug("Number of color maps: {}".format(len(self.color_maps)))



class CmLibModelSingleton(CmLibModel, SingletonMixin):