<commit_before>from magpie import __meta__, services, app
from magpie.constants import get_constant
from magpie.definitions.pyramid_definitions import asbool
from magpie.services import ServiceAccess
from magpie.utils import get_magpie_url, get_settings_from_config_ini, get_header, CONTENT_TYPE_JSON, CONTENT_TYPE_HTML
from six.moves.urllib.parse import urlparse
from distutils.version import LooseVersion
from pyramid.testing import setUp as PyramidSetUp
# noinspection PyPackageRequirements
from webtest import TestApp
# noinspection PyPackageRequirements
from webtest.response import TestResponse
from typing import TYPE_CHECKING
import unittest
import requests
import warnings
import json
import six
# noinspection PyPackageRequirements
import pytest
if TYPE_CHECKING:
    from tests.interfaces import Base_Magpie_TestCase  # noqa: F401
    from magpie.definitions.typedefs import (  # noqa: F401
        Str, Callable, Dict, HeadersType, OptionalHeaderCookiesType, Optional, Type,
        AnyMagpieTestType, AnyResponseType, TestAppOrUrlType
    )

OptionalStringType = six.string_types + tuple([type(None)])


class RunOption(object):
    __slots__ = ["_name", "_enabled", "_marker"]

    def __init__(self, name, marker=None):
        self._name = name
        self._marker = marker if marker else name.lower().replace("magpie_test_", "")
        self._enabled = self._default_run()

    def __call__(self, *args, **kwargs):
        """Return (condition, reason) matching ``unittest.skipUnless`` decorator."""
        return self._enabled, self.message

    def __str__(self):
        return self.message

    def __repr__(self):
        return "{}[{}]".format(type(self).__name__, self.message)

    def _default_run(self):
        option_value = asbool(get_constant(self._name, default_value=True,
                                           raise_missing=False, raise_not_set=False, print_missing=True))
        return True if option_value is None else option_value

    @property
    def message(self):
        option = " '{}' ".format(self._marker)
        status = "Run" if self._enabled else "Skip"
        return "{}{}tests requested [{}={}].".format(status, option, self._name, self._enabled)

    @property
    def name(self):
        return self._name

    @property
    def enabled(self):
        return self._enabled

    @property
    def marker(self):
        return self._marker


# noinspection PyPep8Naming
def RunDecorator(run_option):
    # type: (RunOption) -> Callable
    """
    Decorates the test/class with ``pytest.mark`` and ``unittest.skipUnless``
    using the provided test condition represented by the given ``RunOption``.

    Allows to decorate a function or class such that::

        option = RunDecorator(RunOption("MAGPIE_TEST_CUSTOM_MARKER"))

        @option
        def test_func():
            <test>

    is equivalent to::

        @pytest.mark.custom_marker
        @unittest.skipUnless(runner.MAGPIE_TEST_CUSTOM_MARKER, reason="...")
        def test_func():
            <test>

    """
    # noinspection PyUnusedLocal
    def wrap(test_func, *args, **kwargs):
        pytest_marker = pytest.mark.__getattr__(run_option.marker)
        unittest_skip = unittest.skipUnless(*run_option())
        test_func = pytest_marker(test_func)
        test_func = unittest_skip(test_func)
        return test_func

    return wrap


class RunOptionDecorator(object):
    """
    Simplifies the call to::

        RunDecorator(RunOption("MAGPIE_TEST_CUSTOM_MARKER"))

    by::

        RunOptionDecorator("MAGPIE_TEST_CUSTOM_MARKER")
    """
    def __new__(cls, name):
        return RunDecorator(RunOption(name))


def config_setup_from_ini(config_ini_file_path):
    settings = get_settings_from_config_ini(config_ini_file_path)
    config = PyramidSetUp(settings=settings)
    return config


def get_test_magpie_app(settings=None):
    # parse settings from ini file to pass them to the application
    config = config_setup_from_ini(get_constant("MAGPIE_INI_FILE_PATH"))
    config.include("ziggurat_foundations.ext.pyramid.sign_in")
    config.include("ziggurat_foundations.ext.pyramid.get_user")
    config.registry.settings["magpie.url"] = "http://localhost:80"
    config.registry.settings["magpie.db_migration"] = False
    if settings:
        config.registry.settings.update(settings)
    # create the test application
    config.include("magpie")
    magpie_app = TestApp(app.main({}, **config.registry.settings))
    return magpie_app


def get_app_or_url(test_item):
    # type: (AnyMagpieTestType) -> TestAppOrUrlType
    if isinstance(test_item, TestApp) or isinstance(test_item, six.string_types):
        return test_item
    app_or_url = getattr(test_item, "app", None) or getattr(test_item, "url", None)
    if not app_or_url:
        raise ValueError("Invalid test class, application or URL could not be found.")
    return app_or_url


def get_hostname(test_item):
    # type: (AnyMagpieTestType) -> Str
    app_or_url = get_app_or_url(test_item)
    if isinstance(app_or_url, TestApp):<commit_msg>dont force test skip migration, use config settings<commit_after>from magpie import __meta__, services, app
from magpie.constants import get_constant
from magpie.definitions.pyramid_definitions import asbool
from magpie.services import ServiceAccess
from magpie.utils import get_magpie_url, get_settings_from_config_ini, get_header, CONTENT_TYPE_JSON, CONTENT_TYPE_HTML
from six.moves.urllib.parse import urlparse
from distutils.version import LooseVersion
from pyramid.testing import setUp as PyramidSetUp
# noinspection PyPackageRequirements
from webtest import TestApp
# noinspection PyPackageRequirements
from webtest.response import TestResponse
from typing import TYPE_CHECKING
import unittest
import requests
import warnings
import json
import six
# noinspection PyPackageRequirements
import pytest
if TYPE_CHECKING:
    from tests.interfaces import Base_Magpie_TestCase  # noqa: F401
    from magpie.definitions.typedefs import (  # noqa: F401
        Str, Callable, Dict, HeadersType, OptionalHeaderCookiesType, Optional, Type,
        AnyMagpieTestType, AnyResponseType, TestAppOrUrlType
    )

OptionalStringType = six.string_types + tuple([type(None)])


class RunOption(object):
    __slots__ = ["_name", "_enabled", "_marker"]

    def __init__(self, name, marker=None):
        self._name = name
        self._marker = marker if marker else name.lower().replace("magpie_test_", "")
        self._enabled = self._default_run()

    def __call__(self, *args, **kwargs):
        """Return (condition, reason) matching ``unittest.skipUnless`` decorator."""
        return self._enabled, self.message

    def __str__(self):
        return self.message

    def __repr__(self):
        return "{}[{}]".format(type(self).__name__, self.message)

    def _default_run(self):
        option_value = asbool(get_constant(self._name, default_value=True,
                                           raise_missing=False, raise_not_set=False, print_missing=True))
        return True if option_value is None else option_value

    @property
    def message(self):
        option = " '{}' ".format(self._marker)
        status = "Run" if self._enabled else "Skip"
        return "{}{}tests requested [{}={}].".format(status, option, self._name, self._enabled)

    @property
    def name(self):
        return self._name

    @property
    def enabled(self):
        return self._enabled

    @property
    def marker(self):
        return self._marker


# noinspection PyPep8Naming
def RunDecorator(run_option):
    # type: (RunOption) -> Callable
    """
    Decorates the test/class with ``pytest.mark`` and ``unittest.skipUnless``
    using the provided test condition represented by the given ``RunOption``.

    Allows to decorate a function or class such that::

        option = RunDecorator(RunOption("MAGPIE_TEST_CUSTOM_MARKER"))

        @option
        def test_func():
            <test>

    is equivalent to::

        @pytest.mark.custom_marker
        @unittest.skipUnless(runner.MAGPIE_TEST_CUSTOM_MARKER, reason="...")
        def test_func():
            <test>

    """
    # noinspection PyUnusedLocal
    def wrap(test_func, *args, **kwargs):
        pytest_marker = pytest.mark.__getattr__(run_option.marker)
        unittest_skip = unittest.skipUnless(*run_option())
        test_func = pytest_marker(test_func)
        test_func = unittest_skip(test_func)
        return test_func

    return wrap


class RunOptionDecorator(object):
    """
    Simplifies the call to::

        RunDecorator(RunOption("MAGPIE_TEST_CUSTOM_MARKER"))

    by::

        RunOptionDecorator("MAGPIE_TEST_CUSTOM_MARKER")
    """
    def __new__(cls, name):
        return RunDecorator(RunOption(name))


def config_setup_from_ini(config_ini_file_path):
    settings = get_settings_from_config_ini(config_ini_file_path)
    config = PyramidSetUp(settings=settings)
    return config


def get_test_magpie_app(settings=None):
    # parse settings from ini file to pass them to the application
    config = config_setup_from_ini(get_constant("MAGPIE_INI_FILE_PATH"))
    config.include("ziggurat_foundations.ext.pyramid.sign_in")
    config.include("ziggurat_foundations.ext.pyramid.get_user")
    config.registry.settings["magpie.url"] = "http://localhost:80"
    if settings:
        config.registry.settings.update(settings)
    # create the test application
    config.include("magpie")
    magpie_app = TestApp(app.main({}, **config.registry.settings))
    return magpie_app


def get_app_or_url(test_item):
    # type: (AnyMagpieTestType) -> TestAppOrUrlType
    if isinstance(test_item, TestApp) or isinstance(test_item, six.string_types):
        return test_item
    app_or_url = getattr(test_item, "app", None) or getattr(test_item, "url", None)
    if not app_or_url:
        raise ValueError("Invalid test class, application or URL could not be found.")
    return app_or_url


def get_hostname(test_item):
    # type: (AnyMagpieTestType) -> Str
    app_or_url = get_app_or_url(test_item)
    if isinstance(app_or_url, TestApp):
{'fraction_non_alphanumeric': 0.09451702296120348, 'fraction_numerical': 0.0017814726840855108, 'mean_word_length': 3.7043761638733708, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1302069', 'n_tokens_mistral': 3122, 'n_tokens_neox': 3053, 'n_words': 716}