# Path: sippy/Core/Exceptions.py
# def dump_exception(msg, f = sys.stdout, extra = None):
#     exc_type, exc_value, exc_traceback = sys.exc_info()
#     if isinstance(exc_value, StdException):
#         cus_traceback = exc_value.traceback
#     else:
#         if hasattr(exc_value, 'traceback'):
#             exc_traceback = exc_value.traceback
#         cus_traceback = None
#     f.write('%s @%s[%d] %s:\n' % (datetime.now(), sys.argv[0], os.getpid(), msg))
#     f.write(SEPT)
#     if cus_traceback != None:
#         f.write('Traceback (most recent call last):\n')
#         print_list(cus_traceback, file = f)
#         f.write(format_exception_only(exc_type, exc_value)[0])
#     else:
#         print_exception(exc_type, exc_value, exc_traceback, file = f)
#     f.write(SEPT)
#     if extra != None:
#         f.write(extra)
#         f.write(SEPT)
#     f.flush()


from threading import Condition
from subprocess import Popen, PIPE
from sys import platform
from threading import Thread, Lock
from errno import EINTR
from sippy.Core.Exceptions import dump_exception
from sippy.Core.EventDispatcher import ED2
    from sys import exit
    from time import sleep
class External_command(object):
    work_available = None
    work = None

    def __init__(self, command, max_workers = _MAX_WORKERS):
        self.work_available = Condition()
        self.work = []
        for i in range(0, max_workers):
            _Worker(command, self)

    def process_command(self, data, result_callback, *callback_parameters):
        wi = Work_item(tuple(data), result_callback, callback_parameters)
        self.work_available.acquire()
        self.work.append(wi)
        self.work_available.notify()
        self.work_available.release()
        return wi

    def shutdown(self):
        self.work_available.acquire()
        self.work.append(None)
        self.work_available.notify()
        self.work_available.release()

    def process_result(self, result_callback, result, *callback_parameters):
        try:
            result_callback(result, *callback_parameters)
        except Exception as ex:
            if isinstance(ex, SystemExit):
                raise

# Path: sippy/Core/Exceptions.py
# def dump_exception(msg, f = sys.stdout, extra = None):
#     exc_type, exc_value, exc_traceback = sys.exc_info()
#     if isinstance(exc_value, StdException):
#         cus_traceback = exc_value.traceback
#     else:
#         if hasattr(exc_value, 'traceback'):
#             exc_traceback = exc_value.traceback
#         cus_traceback = None
#     f.write('%s @%s[%d] %s:\n' % (datetime.now(), sys.argv[0], os.getpid(), msg))
#     f.write(SEPT)
#     if cus_traceback != None:
#         f.write('Traceback (most recent call last):\n')
#         print_list(cus_traceback, file = f)
#         f.write(format_exception_only(exc_type, exc_value)[0])
#     else:
#         print_exception(exc_type, exc_value, exc_traceback, file = f)
#     f.write(SEPT)
#     if extra != None:
#         f.write(extra)
#         f.write(SEPT)
#     f.flush()


# Path: sippy/External_command.py
from threading import Condition
from subprocess import Popen, PIPE
from sys import platform
from threading import Thread, Lock
from errno import EINTR
from sippy.Core.Exceptions import dump_exception
from sippy.Core.EventDispatcher import ED2
    from sys import exit
    from time import sleep

class External_command(object):
    work_available = None
    work = None

    def __init__(self, command, max_workers = _MAX_WORKERS):
        self.work_available = Condition()
        self.work = []
        for i in range(0, max_workers):
            _Worker(command, self)

    def process_command(self, data, result_callback, *callback_parameters):
        wi = Work_item(tuple(data), result_callback, callback_parameters)
        self.work_available.acquire()
        self.work.append(wi)
        self.work_available.notify()
        self.work_available.release()
        return wi

    def shutdown(self):
        self.work_available.acquire()
        self.work.append(None)
        self.work_available.notify()
        self.work_available.release()

    def process_result(self, result_callback, result, *callback_parameters):
        try:
            result_callback(result, *callback_parameters)
        except Exception as ex:
            if isinstance(ex, SystemExit):
                raise

            dump_exception('External_command: unhandled exception in external command results callback')