# Path: dependency/UTMaster.py
# class UTMaster(object):
#     """
#     UTMaster dispatches ut command to ut threads
#     """
#     def __init__(self, queue, logger):
#         """
#         Args:
#             queue : the ut command queue
#             logger : the Log.Log() object
#         """
#         self._queue =  queue
#         self._logger = logger
#         self._errors = list()
# 
#     def Run(self):
#         """
#         thread entrence function
#         """
#         while not self._queue.empty():
#             try:
#                 cmd = self._queue.get(True, 1)
#             except Queue.Empty:
#                 break
#             ret, msg = Function.RunCommand(cmd, True)
#             if ret != 0:
#                 self._logger.LevPrint("ERROR", "run ut cmd(%s) failed: %s" % (cmd, msg))
#                 self._errors.append(msg)
#             else:
#                 self._logger.LevPrint("MSG", "run ut cmd(%s) OK\n%s" % (cmd, msg))
#             self._queue.task_done()
#         
#     def Start(self):
#         """
#         run all ut threads
#         """
#         num = 4
# 
#         if self._queue.qsize() < 4:
#             num = self._queue.qsize()
# 
#         workers = list()
#         for i in xrange(0, num):
#             t = threading.Thread(target=self.Run)
#             workers.append(t)
#             t.start()
#         # wait all ut comands done
#         self._queue.join()
#         # wait all ut threads exit
#         for worker in workers:
#             worker.join()
# 
#     def Errors(self):
#         """
#         return all error msg
#         """
#         return self._errors
# 
# Path: util/Log.py
# class Log(object):
#     """
#     Log class
#     """
#     class __impl(object):
#         """ Implementation of singleton interface"""
#         def __init__(self):
#             """
#             """
#             self.levMap = {"ERROR":(0, "\033[31m%s%s\033[0m"), "WARNING":(1, "\033[33m%s%s\033[0m"), \
#                             "INFO":(2, "\033[34m%s%s\033[0m"), "MSG":(3, "\033[32m%s%s\033[0m"), \
#                             "UNKNOW":(4, "\033[31m%s%s\033[0m")}
#             self.config_level = 5
# 
#         def setLogLevel(self, lev):
#             """
#             Description : set log file level.The log message lower than this level will not be printed.
#             Args : lev : log level will be set.Value is [0-5],0 will not print any log message, 5 will print all log message
#             Return : NULL 
#             """
#             self.config_level = lev
# 
#         def LevPrint(self, level, msg, prefix=True):
#             """
#             Description :  print log message which has higher level than config level
#             Args: 
#                 level : ["ERROR", "WARNING", "INFO", "MSG", "UNKNOW"]
#                 msg : string, log message
#                 prefix : True , print time information
#                          False , don't print time information
#             Return : None
#             """
#             if level in self.levMap and self.levMap[level][0] > self.config_level:
#                 return 
# 
#             now = ""
#             pmsg = ""
#             if prefix:
#                 now = "[" + time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) \
#                     + " " + str(threading.current_thread().ident) + "] "
#             if (level in self.levMap):
#                     pmsg = self.levMap[level][1] % (now, msg)
#             else:
#                     pmsg = "UNKNOW log level(%s)" % level 
#                     pmsg += self.levMap["UNKNOW"][1] % (now, msg)
#                 
#             print(pmsg)
#             sys.stdout.flush()
# 
#     
#     # class Log
#     __instance = None
#     def __init__(self):
#         """ Create singleton instance """
#         # Check whether we already have an instance
#         if Log.__instance is None:
#             # Create and remember instance
#             Log.__instance = Log.__impl()
# 
#         # Store instance reference as the only member in the handle
#         self.__dict__['_Log__instance'] = Log.__instance
# 
#     def __getattr__(self, attr):
#         """ Delegate access to implementation """
#         return getattr(self.__instance, attr)
# 
#     def __setattr__(self, attr, value):
#         """ Delegate access to implementation """
#         return setattr(self.__instance, attr, value)


import os
import sys
import Queue
import unittest
from dependency import UTMaster
from util import Log
"""


broc_path = os.path.realpath(os.path.join(os.path.realpath(__file__), '..', '..'))
sys.path.insert(0, broc_path)

class TestUTMaster(unittest.TestCase):
    """
    unit test for UTMaster
    """
    def setUp(self):
        """
        """
        pass

    def tearDown(self):
        """
        """
        pass

    def test_UTMaster(self):
        """
        test UTMaster
        """
        queue = Queue.Queue()
        queue.put("ls -al")
        queue.put("whoami")
        queue.put("gcc --version")
        queue.put("gcc --watch out")
        queue.put("echo 'broc is great'")

# Path: dependency/UTMaster.py
# class UTMaster(object):
#     """
#     UTMaster dispatches ut command to ut threads
#     """
#     def __init__(self, queue, logger):
#         """
#         Args:
#             queue : the ut command queue
#             logger : the Log.Log() object
#         """
#         self._queue =  queue
#         self._logger = logger
#         self._errors = list()
# 
#     def Run(self):
#         """
#         thread entrence function
#         """
#         while not self._queue.empty():
#             try:
#                 cmd = self._queue.get(True, 1)
#             except Queue.Empty:
#                 break
#             ret, msg = Function.RunCommand(cmd, True)
#             if ret != 0:
#                 self._logger.LevPrint("ERROR", "run ut cmd(%s) failed: %s" % (cmd, msg))
#                 self._errors.append(msg)
#             else:
#                 self._logger.LevPrint("MSG", "run ut cmd(%s) OK\n%s" % (cmd, msg))
#             self._queue.task_done()
#         
#     def Start(self):
#         """
#         run all ut threads
#         """
#         num = 4
# 
#         if self._queue.qsize() < 4:
#             num = self._queue.qsize()
# 
#         workers = list()
#         for i in xrange(0, num):
#             t = threading.Thread(target=self.Run)
#             workers.append(t)
#             t.start()
#         # wait all ut comands done
#         self._queue.join()
#         # wait all ut threads exit
#         for worker in workers:
#             worker.join()
# 
#     def Errors(self):
#         """
#         return all error msg
#         """
#         return self._errors
# 
# Path: util/Log.py
# class Log(object):
#     """
#     Log class
#     """
#     class __impl(object):
#         """ Implementation of singleton interface"""
#         def __init__(self):
#             """
#             """
#             self.levMap = {"ERROR":(0, "\033[31m%s%s\033[0m"), "WARNING":(1, "\033[33m%s%s\033[0m"), \
#                             "INFO":(2, "\033[34m%s%s\033[0m"), "MSG":(3, "\033[32m%s%s\033[0m"), \
#                             "UNKNOW":(4, "\033[31m%s%s\033[0m")}
#             self.config_level = 5
# 
#         def setLogLevel(self, lev):
#             """
#             Description : set log file level.The log message lower than this level will not be printed.
#             Args : lev : log level will be set.Value is [0-5],0 will not print any log message, 5 will print all log message
#             Return : NULL 
#             """
#             self.config_level = lev
# 
#         def LevPrint(self, level, msg, prefix=True):
#             """
#             Description :  print log message which has higher level than config level
#             Args: 
#                 level : ["ERROR", "WARNING", "INFO", "MSG", "UNKNOW"]
#                 msg : string, log message
#                 prefix : True , print time information
#                          False , don't print time information
#             Return : None
#             """
#             if level in self.levMap and self.levMap[level][0] > self.config_level:
#                 return 
# 
#             now = ""
#             pmsg = ""
#             if prefix:
#                 now = "[" + time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) \
#                     + " " + str(threading.current_thread().ident) + "] "
#             if (level in self.levMap):
#                     pmsg = self.levMap[level][1] % (now, msg)
#             else:
#                     pmsg = "UNKNOW log level(%s)" % level 
#                     pmsg += self.levMap["UNKNOW"][1] % (now, msg)
#                 
#             print(pmsg)
#             sys.stdout.flush()
# 
#     
#     # class Log
#     __instance = None
#     def __init__(self):
#         """ Create singleton instance """
#         # Check whether we already have an instance
#         if Log.__instance is None:
#             # Create and remember instance
#             Log.__instance = Log.__impl()
# 
#         # Store instance reference as the only member in the handle
#         self.__dict__['_Log__instance'] = Log.__instance
# 
#     def __getattr__(self, attr):
#         """ Delegate access to implementation """
#         return getattr(self.__instance, attr)
# 
#     def __setattr__(self, attr, value):
#         """ Delegate access to implementation """
#         return setattr(self.__instance, attr, value)


# Path: unittest/test_UTMaster.py
import os
import sys
import Queue
import unittest
from dependency import UTMaster
from util import Log

"""


broc_path = os.path.realpath(os.path.join(os.path.realpath(__file__), '..', '..'))
sys.path.insert(0, broc_path)

class TestUTMaster(unittest.TestCase):
    """
    unit test for UTMaster
    """
    def setUp(self):
        """
        """
        pass

    def tearDown(self):
        """
        """
        pass

    def test_UTMaster(self):
        """
        test UTMaster
        """
        queue = Queue.Queue()
        queue.put("ls -al")
        queue.put("whoami")
        queue.put("gcc --version")
        queue.put("gcc --watch out")
        queue.put("echo 'broc is great'")

        log = Log.Log()