# 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
            self.workers.map(spawn_worker, args)
        finally:
            self.workers.close()
            self.workers.join()
        # TODO: watch the total test duration, if set, 'cause iteration might last very long


class ApiritifSession(PluggableTestProgram.sessionClass):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.stop_reason = ""

    def set_stop_reason(self, msg):
        if not self.stop_reason:
            self.stop_reason = msg


class Worker(ThreadPool):
    def __init__(self, params):
        """
        :type params: Params
        """
        super(Worker, self).__init__(params.concurrency)
        self.params = params
        if self.params.report.lower().endswith(".ldjson"):
            store.writer = LDJSONSampleWriter(self.params.report)
        else:
            store.writer = JTLSampleWriter(self.params.report)

    def start(self):

# 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

            self.workers.map(spawn_worker, args)
        finally:
            self.workers.close()
            self.workers.join()
        # TODO: watch the total test duration, if set, 'cause iteration might last very long


class ApiritifSession(PluggableTestProgram.sessionClass):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.stop_reason = ""

    def set_stop_reason(self, msg):
        if not self.stop_reason:
            self.stop_reason = msg


class Worker(ThreadPool):
    def __init__(self, params):
        """
        :type params: Params
        """
        super(Worker, self).__init__(params.concurrency)
        self.params = params
        if self.params.report.lower().endswith(".ldjson"):
            store.writer = LDJSONSampleWriter(self.params.report)
        else:
            store.writer = JTLSampleWriter(self.params.report)

    def start(self):

        import_plugins()