# Path: apiritif/action_plugins.py
# class ActionHandlerFactory:
#     registry = {}
# 
#     @classmethod
#     def register(cls, name):
#         def inner_wrapper(wrapped_class):
#             cls.registry[name] = wrapped_class
#             return wrapped_class
# 
#         return inner_wrapper
# 
#     @classmethod
#     def create_handler(cls, name, **kwargs):
#         if name not in cls.registry:
#             log.warning('Handler %s does not exist in the registry', name)
#             return
# 
#         exec_class = cls.registry[name]
#         return exec_class(**kwargs)
# 
#     @classmethod
#     def create_all(cls, **kwargs):
#         return [cls.create_handler(name, **kwargs) for name in cls.registry]
# 
# def import_plugins():
#     path = os.environ.get(PLUGINS_PATH, None)
#     if not path:
#         log.debug('Plugins PATH not found, continue without plugins')
#         return
# 
#     # add plugins path to PYTHONPATH
#     sys.path.append(path)
# 
#     package = Path(path).resolve().name
#     log.info(f'Plugins package {package}')
# 
#     #  modules listing in the root package
#     for (_, module_name, _) in iter_modules([path]):
#         log.info(f'Importing module {module_name}')
#         import_module(module_name)
# 
# Path: apiritif/utils.py
# VERSION = "1.1.3"
# def get_trace(error):
# def graceful():
# def headers_as_text(headers_dict):
# def shorten(string, upto, end_with="..."):
# def assert_regexp(regex, text, match=False, msg=None):
# def assert_not_regexp(regex, text, match=False, msg=None):
# class NormalShutdown(BaseException):


import copy
import unicodecsv as csv
import json
import logging
import multiprocessing
import os
import sys
import time
import traceback
import apiritif
import apiritif.thread as thread
import apiritif.store as store
from multiprocessing.pool import ThreadPool
from optparse import OptionParser
from threading import Thread
from nose2.main import PluggableTestProgram
from nose2.events import Plugin
from apiritif.action_plugins import ActionHandlerFactory, import_plugins
from apiritif.utils import NormalShutdown, log, get_trace, VERSION, graceful

    def close(self):
        log.info("Workers finished, awaiting result writer")
        while not store.writer.is_queue_empty() and store.writer.is_alive():
            time.sleep(0.1)
        log.info("Results written, shutting down")
        super(Worker, self).close()

    def run_nose(self, params):
        """
        :type params: Params
        """
        if not params.tests:
            raise RuntimeError("Nothing to test.")

        thread.set_index(params.thread_index)
        log.debug("[%s] Starting nose2 iterations: %s", params.worker_index, params)
        assert isinstance(params.tests, list)
        # argv.extend(['--with-apiritif', '--nocapture', '--exe', '--nologcapture'])

        end_time = self.params.ramp_up + self.params.hold_for
        end_time += time.time() if end_time else 0
        time.sleep(params.delay)
        store.writer.concurrency += 1

        config = {"tests": params.tests}
        if params.verbose:
            config["verbosity"] = 3

        iteration = 0

# Path: apiritif/action_plugins.py
# class ActionHandlerFactory:
#     registry = {}
# 
#     @classmethod
#     def register(cls, name):
#         def inner_wrapper(wrapped_class):
#             cls.registry[name] = wrapped_class
#             return wrapped_class
# 
#         return inner_wrapper
# 
#     @classmethod
#     def create_handler(cls, name, **kwargs):
#         if name not in cls.registry:
#             log.warning('Handler %s does not exist in the registry', name)
#             return
# 
#         exec_class = cls.registry[name]
#         return exec_class(**kwargs)
# 
#     @classmethod
#     def create_all(cls, **kwargs):
#         return [cls.create_handler(name, **kwargs) for name in cls.registry]
# 
# def import_plugins():
#     path = os.environ.get(PLUGINS_PATH, None)
#     if not path:
#         log.debug('Plugins PATH not found, continue without plugins')
#         return
# 
#     # add plugins path to PYTHONPATH
#     sys.path.append(path)
# 
#     package = Path(path).resolve().name
#     log.info(f'Plugins package {package}')
# 
#     #  modules listing in the root package
#     for (_, module_name, _) in iter_modules([path]):
#         log.info(f'Importing module {module_name}')
#         import_module(module_name)
# 
# Path: apiritif/utils.py
# VERSION = "1.1.3"
# def get_trace(error):
# def graceful():
# def headers_as_text(headers_dict):
# def shorten(string, upto, end_with="..."):
# def assert_regexp(regex, text, match=False, msg=None):
# def assert_not_regexp(regex, text, match=False, msg=None):
# class NormalShutdown(BaseException):


# Path: apiritif/loadgen.py
import copy
import unicodecsv as csv
import json
import logging
import multiprocessing
import os
import sys
import time
import traceback
import apiritif
import apiritif.thread as thread
import apiritif.store as store
from multiprocessing.pool import ThreadPool
from optparse import OptionParser
from threading import Thread
from nose2.main import PluggableTestProgram
from nose2.events import Plugin
from apiritif.action_plugins import ActionHandlerFactory, import_plugins
from apiritif.utils import NormalShutdown, log, get_trace, VERSION, graceful


    def close(self):
        log.info("Workers finished, awaiting result writer")
        while not store.writer.is_queue_empty() and store.writer.is_alive():
            time.sleep(0.1)
        log.info("Results written, shutting down")
        super(Worker, self).close()

    def run_nose(self, params):
        """
        :type params: Params
        """
        if not params.tests:
            raise RuntimeError("Nothing to test.")

        thread.set_index(params.thread_index)
        log.debug("[%s] Starting nose2 iterations: %s", params.worker_index, params)
        assert isinstance(params.tests, list)
        # argv.extend(['--with-apiritif', '--nocapture', '--exe', '--nologcapture'])

        end_time = self.params.ramp_up + self.params.hold_for
        end_time += time.time() if end_time else 0
        time.sleep(params.delay)
        store.writer.concurrency += 1

        config = {"tests": params.tests}
        if params.verbose:
            config["verbosity"] = 3

        iteration = 0

        handlers = ActionHandlerFactory.create_all()