<commit_before>import httplib
from .replay_settings import ReplaySettings
from stubs.base import ReplayHTTPConnection, ReplayHTTPSConnection


#------------------------------------------------------------------------------
# Hold onto original objects for un-patching later
#------------------------------------------------------------------------------

_original_http_connection = httplib.HTTPConnection
_original_https_connection = httplib.HTTPSConnection

try:
    import requests.packages.urllib3.connectionpool
    _original_requests_verified_https_connection = \
        requests.packages.urllib3.connectionpool.VerifiedHTTPSConnection
    _original_requests_http_connection = \
        requests.packages.urllib3.connectionpool.HTTPConnection
except ImportError:
    pass

try:
    import urllib3
    _original_urllib3_verified_https_connection = \
        urllib3.connectionpool.VerifiedHTTPSConnection
    _original_urllib3_http_connection = urllib3.connectionpool.HTTPConnection
except ImportError:
    pass


#------------------------------------------------------------------------------
# Patching methods
#------------------------------------------------------------------------------

def _patch_httplib(settings):
    httplib.HTTPSConnection = httplib.HTTPS._connection_class = \
        ReplayHTTPSConnection
    httplib.HTTPSConnection._replay_settings = settings
    httplib.HTTPConnection = httplib.HTTP._connection_class = \
        ReplayHTTPConnection
    httplib.HTTPConnection._replay_settings = settings


def _patch_requests(settings):
    try:
        import requests.packages.urllib3.connectionpool
        from .stubs.requests_stubs import ReplayRequestsHTTPSConnection
        requests.packages.urllib3.connectionpool.VerifiedHTTPSConnection = \
            ReplayRequestsHTTPSConnection
        requests.packages.urllib3.connectionpool.VerifiedHTTPSConnection.\
            _replay_settings = settings
        requests.packages.urllib3.connectionpool.HTTPConnection = \
            ReplayHTTPConnection
        requests.packages.urllib3.connectionpool.HTTPConnection.\
            _replay_settings = settings
    except ImportError, e:
        print repr(e)


def _patch_urllib3(settings):
    try:
        import urllib3.connectionpool
        from .stubs.urllib3_stubs import ReplayUrllib3HTTPSConnection
        urllib3.connectionpool.VerifiedHTTPSConnection = \
            ReplayUrllib3HTTPSConnection
        urllib3.connectionpool.VerifiedHTTPSConnection._replay_settings = \
            settings
        urllib3.connectionpool.HTTPConnection = ReplayHTTPConnection
        urllib3.connectionpool.HTTPConnection._replay_settings = settings
    except ImportError, e:
        print repr(e)


def start_replay(
        replay_file_name,
        url_key=None,
        body_key=None,
        headers_key=None):
    """
    Start using the ``httreplay`` library.<commit_msg>Don't complain about missing modules while patching.<commit_after>import httplib
from .replay_settings import ReplaySettings
from stubs.base import ReplayHTTPConnection, ReplayHTTPSConnection


#------------------------------------------------------------------------------
# Hold onto original objects for un-patching later
#------------------------------------------------------------------------------

_original_http_connection = httplib.HTTPConnection
_original_https_connection = httplib.HTTPSConnection

try:
    import requests.packages.urllib3.connectionpool
    _original_requests_verified_https_connection = \
        requests.packages.urllib3.connectionpool.VerifiedHTTPSConnection
    _original_requests_http_connection = \
        requests.packages.urllib3.connectionpool.HTTPConnection
except ImportError:
    pass

try:
    import urllib3
    _original_urllib3_verified_https_connection = \
        urllib3.connectionpool.VerifiedHTTPSConnection
    _original_urllib3_http_connection = urllib3.connectionpool.HTTPConnection
except ImportError:
    pass


#------------------------------------------------------------------------------
# Patching methods
#------------------------------------------------------------------------------

def _patch_httplib(settings):
    httplib.HTTPSConnection = httplib.HTTPS._connection_class = \
        ReplayHTTPSConnection
    httplib.HTTPSConnection._replay_settings = settings
    httplib.HTTPConnection = httplib.HTTP._connection_class = \
        ReplayHTTPConnection
    httplib.HTTPConnection._replay_settings = settings


def _patch_requests(settings):
    try:
        import requests.packages.urllib3.connectionpool
        from .stubs.requests_stubs import ReplayRequestsHTTPSConnection
        requests.packages.urllib3.connectionpool.VerifiedHTTPSConnection = \
            ReplayRequestsHTTPSConnection
        requests.packages.urllib3.connectionpool.VerifiedHTTPSConnection.\
            _replay_settings = settings
        requests.packages.urllib3.connectionpool.HTTPConnection = \
            ReplayHTTPConnection
        requests.packages.urllib3.connectionpool.HTTPConnection.\
            _replay_settings = settings
    except ImportError, e:
        pass


def _patch_urllib3(settings):
    try:
        import urllib3.connectionpool
        from .stubs.urllib3_stubs import ReplayUrllib3HTTPSConnection
        urllib3.connectionpool.VerifiedHTTPSConnection = \
            ReplayUrllib3HTTPSConnection
        urllib3.connectionpool.VerifiedHTTPSConnection._replay_settings = \
            settings
        urllib3.connectionpool.HTTPConnection = ReplayHTTPConnection
        urllib3.connectionpool.HTTPConnection._replay_settings = settings
    except ImportError, e:
        pass


def start_replay(
        replay_file_name,
        url_key=None,
        body_key=None,
        headers_key=None):
    """
    Start using the ``httreplay`` library.
{'fraction_non_alphanumeric': 0.15692992975843756, 'fraction_numerical': 0.007538118896693507, 'mean_word_length': 2.305775764439411, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '1156674', 'n_tokens_mistral': 1505, 'n_tokens_neox': 1489, 'n_words': 229}