# 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
Copyright 2017 BlazeMeter Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""




# TODO how to implement hits/s control/shape?
# TODO: VU ID for script
# TODO: disable assertions for load mode


def spawn_worker(params):
    """
    This method has to be module level function

    :type params: Params
    """
    setup_logging(params)

# 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

Copyright 2017 BlazeMeter Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""




# TODO how to implement hits/s control/shape?
# TODO: VU ID for script
# TODO: disable assertions for load mode


def spawn_worker(params):
    """
    This method has to be module level function

    :type params: Params
    """
    setup_logging(params)

    log.info("Adding worker: idx=%s\tconcurrency=%s\tresults=%s", params.worker_index, params.concurrency,