# Path: snakemakelib/log.py
# class LoggerManager(object):
#     __metaclass__ = Singleton
# 
#     _loggers = {}
#     _fmt = "%(asctime)s (%(levelname)s) %(name)s :  %(message)s"
#     _ch = logging.StreamHandler()
#     _formatter = logging.Formatter(_fmt)
#     _ch.setFormatter(_formatter)
#     _has_loaded_config = False
# 
#     def __init__(self, *args, **kwargs):
#         if not LoggerManager._has_loaded_config:
#             # Add snakemakelib root handler
#             LoggerManager._loggers['snakemakelib'] = logging.getLogger('snakemakelib')
#             LoggerManager._loggers['snakemakelib'].setLevel(logging.WARNING)
#             LoggerManager._loggers['snakemakelib'].addHandler(LoggerManager._ch)
#             self._load_config()
#             LoggerManager._has_loaded_config = True
# 
#     def _load_config(self):
#         conf = {}
#         if os.path.exists("logconf.yaml"):
#             with open ("logconf.yaml", "r") as fh:
#                 conf = yaml.load(fh)
#         else:
#             conf = self._load_sml_config()
#         if conf:
#             logging.config.dictConfig(conf)
#      
#     def _load_sml_config(self):
#         cfg = {}
#         for fn in [os.path.join(os.getenv("HOME"), ".smlconf.yaml"),
#                    os.path.join(os.curdir, "smlconf.yaml")]:
#             if (fn is None):
#                 continue
#             if not os.path.exists(fn):
#                 continue
#             with open(fn, "r") as fh:
#                 cfg_tmp = yaml.load(fh)
#             if cfg_tmp:
#                 cfg.update(cfg_tmp)
#         return cfg.get("logging", {})
# 
#     @staticmethod
#     def getLogger(name=None):
#         if not name:
#             smllogger = logging.getLogger()
#             return logging.getLogger()
#         elif name not in LoggerManager._loggers.keys():
#             LoggerManager._loggers[name] = logging.getLogger(str(name))
#             LoggerManager._loggers[name].addHandler(LoggerManager._ch)
#             return LoggerManager._loggers[name]
#         else:
#             logging.warn("Trying to get already existing logger")
# 
# Path: snakemakelib/stat.py
# def is_installed(prog):
#     if not shutil.which(prog) is None:
#         return True
#     if os.path.exists(prog):
#         return True
#     return False


import os
import shutil
from datetime import datetime, date
from snakemakelib.log import LoggerManager
from snakemakelib.stat import is_installed
def set_cmd(home, cmd, module):
    """Set the command, checking if the program is installed in the
    process.

    Args:
      home (str): path to application
      cmd (str): the actual command name
      module (str): the calling module

    Returns:
      str: full path to the command

    Example:

        cmd = set_cmd("/path/to/cmd", "helloworld")
        print(cmd)
        # prints /path/to/cmd/helloworld

    Raises:
      NotInstalledError: if program not found in path

    """
    if home:
        cmd = os.path.join(home, cmd)
    else:
        try:
            cmd = shutil.which(cmd)
        except:
            pass


# Path: snakemakelib/log.py
# class LoggerManager(object):
#     __metaclass__ = Singleton
# 
#     _loggers = {}
#     _fmt = "%(asctime)s (%(levelname)s) %(name)s :  %(message)s"
#     _ch = logging.StreamHandler()
#     _formatter = logging.Formatter(_fmt)
#     _ch.setFormatter(_formatter)
#     _has_loaded_config = False
# 
#     def __init__(self, *args, **kwargs):
#         if not LoggerManager._has_loaded_config:
#             # Add snakemakelib root handler
#             LoggerManager._loggers['snakemakelib'] = logging.getLogger('snakemakelib')
#             LoggerManager._loggers['snakemakelib'].setLevel(logging.WARNING)
#             LoggerManager._loggers['snakemakelib'].addHandler(LoggerManager._ch)
#             self._load_config()
#             LoggerManager._has_loaded_config = True
# 
#     def _load_config(self):
#         conf = {}
#         if os.path.exists("logconf.yaml"):
#             with open ("logconf.yaml", "r") as fh:
#                 conf = yaml.load(fh)
#         else:
#             conf = self._load_sml_config()
#         if conf:
#             logging.config.dictConfig(conf)
#      
#     def _load_sml_config(self):
#         cfg = {}
#         for fn in [os.path.join(os.getenv("HOME"), ".smlconf.yaml"),
#                    os.path.join(os.curdir, "smlconf.yaml")]:
#             if (fn is None):
#                 continue
#             if not os.path.exists(fn):
#                 continue
#             with open(fn, "r") as fh:
#                 cfg_tmp = yaml.load(fh)
#             if cfg_tmp:
#                 cfg.update(cfg_tmp)
#         return cfg.get("logging", {})
# 
#     @staticmethod
#     def getLogger(name=None):
#         if not name:
#             smllogger = logging.getLogger()
#             return logging.getLogger()
#         elif name not in LoggerManager._loggers.keys():
#             LoggerManager._loggers[name] = logging.getLogger(str(name))
#             LoggerManager._loggers[name].addHandler(LoggerManager._ch)
#             return LoggerManager._loggers[name]
#         else:
#             logging.warn("Trying to get already existing logger")
# 
# Path: snakemakelib/stat.py
# def is_installed(prog):
#     if not shutil.which(prog) is None:
#         return True
#     if os.path.exists(prog):
#         return True
#     return False


# Path: snakemakelib/utils.py
import os
import shutil
from datetime import datetime, date
from snakemakelib.log import LoggerManager
from snakemakelib.stat import is_installed

def set_cmd(home, cmd, module):
    """Set the command, checking if the program is installed in the
    process.

    Args:
      home (str): path to application
      cmd (str): the actual command name
      module (str): the calling module

    Returns:
      str: full path to the command

    Example:

        cmd = set_cmd("/path/to/cmd", "helloworld")
        print(cmd)
        # prints /path/to/cmd/helloworld

    Raises:
      NotInstalledError: if program not found in path

    """
    if home:
        cmd = os.path.join(home, cmd)
    else:
        try:
            cmd = shutil.which(cmd)
        except:
            pass


    if not is_installed(cmd):