<commit_before># -*- coding: utf-8 -*-
import inspect
from urllib.parse import urlparse

import pytest

localhost = '127.0.0.1'

connect_code_template = """
    import socket
    import pytest

    {3}
    def {2}():
        socket.socket().connect(('{0}', {1}))
"""

connect_unicode_code_template = """
    import socket
    import pytest

    {3}
    def {2}():
        socket.socket().connect((u'{0}', {1}))
"""

# `contextlib` used because otherwise 2.7 was occasionally hanging due to exception cases:
urlopen_code_template = """
    import pytest
    import contextlib
    try:
        from urllib.request import urlopen
    except ImportError:
        from urllib2 import urlopen

    {3}
    def {2}():
        with contextlib.closing(urlopen('http://{0}:{1}/')) as x:
            assert x.getcode() == 200
"""


def assert_host_blocked(result, host):
    result.stdout.fnmatch_lines('*A test tried to use socket.socket.connect() with host "{0}"*'.format(host))


@pytest.fixture
def assert_connect(httpbin, testdir):
    def assert_socket_connect(should_pass, **kwargs):
        # get the name of the calling function
        test_name = inspect.stack()[1][3]
        test_url = urlparse(httpbin.url)

        mark = ''
        cli_arg = kwargs.get('cli_arg', None)
        code_template = kwargs.get('code_template', connect_code_template)
        mark_arg = kwargs.get('mark_arg', None)

        if mark_arg and isinstance(mark_arg, str):
            mark = '@pytest.mark.allow_hosts("{0}")'.format(mark_arg)
        elif mark_arg and isinstance(mark_arg, list):
            mark = '@pytest.mark.allow_hosts(["{0}"])'.format('","'.join(mark_arg))<commit_msg>'Refactored by Sourcery'<commit_after># -*- coding: utf-8 -*-
import inspect
from urllib.parse import urlparse

import pytest

localhost = '127.0.0.1'

connect_code_template = """
    import socket
    import pytest

    {3}
    def {2}():
        socket.socket().connect(('{0}', {1}))
"""

connect_unicode_code_template = """
    import socket
    import pytest

    {3}
    def {2}():
        socket.socket().connect((u'{0}', {1}))
"""

# `contextlib` used because otherwise 2.7 was occasionally hanging due to exception cases:
urlopen_code_template = """
    import pytest
    import contextlib
    try:
        from urllib.request import urlopen
    except ImportError:
        from urllib2 import urlopen

    {3}
    def {2}():
        with contextlib.closing(urlopen('http://{0}:{1}/')) as x:
            assert x.getcode() == 200
"""


def assert_host_blocked(result, host):
    result.stdout.fnmatch_lines('*A test tried to use socket.socket.connect() with host "{0}"*'.format(host))


@pytest.fixture
def assert_connect(httpbin, testdir):
    def assert_socket_connect(should_pass, **kwargs):
        # get the name of the calling function
        test_name = inspect.stack()[1][3]
        test_url = urlparse(httpbin.url)

        mark = ''
        cli_arg = kwargs.get('cli_arg', None)
        code_template = kwargs.get('code_template', connect_code_template)
        mark_arg = kwargs.get('mark_arg', None)

        if mark_arg:
            if isinstance(mark_arg, str):
                mark = '@pytest.mark.allow_hosts("{0}")'.format(mark_arg)
            elif isinstance(mark_arg, list):
                mark = '@pytest.mark.allow_hosts(["{0}"])'.format('","'.join(mark_arg))
{'fraction_non_alphanumeric': 0.14434523809523808, 'fraction_numerical': 0.017857142857142856, 'mean_word_length': 2.9587750294464077, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2462266', 'n_tokens_mistral': 1095, 'n_tokens_neox': 1082, 'n_words': 249}