# Path: mrq/context.py
# def get_current_job(greenlet_id=None):
#     if greenlet_id is None:
#         greenlet_id = id(gevent.getcurrent())
#     pair = _GLOBAL_CONTEXT["greenlets"].get(greenlet_id)
#     if not pair:
#         return None
#     return pair[1]
# 
# def get_current_worker():
#     return _GLOBAL_CONTEXT["worker"]


from future import standard_library
from past.builtins import basestring
from .context import get_current_job, get_current_worker
    from termcolor import cprint
    from pymongo.collection import Collection
    from socket import socket as _socketmodule
    from gevent.socket import socket as _geventmodule
    from gevent.ssl import SSLSocket as _sslmodule   # pylint: disable=no-name-in-module
    from redis import StrictRedis
    from http.client import HTTPConnection, HTTPSConnection
        from requests.packages.urllib3.connection import (
            HTTPConnection,
            UnverifiedHTTPSConnection,
            VerifiedHTTPSConnection
        )
        from urllib3.connection import (
            HTTPConnection,
            UnverifiedHTTPSConnection,
            VerifiedHTTPSConnection
        )
    from pymongo.cursor import Cursor
import time
import random
import re
import copy
    import pymongo as pymongomodule
    """ Patch higher-level modules to provide insights on what is performing IO for each job. """

    patch_io_httplib(config)
    patch_io_redis(config)
    patch_io_pymongo_cursor(config)

    #patch_io_dns()
    #patch_io_subprocess()


def patch_pymongo(config):
    """ Monkey-patch pymongo's collections to add some logging """

    # Nothing to change!
    if not config["print_mongodb"] and not config["trace_io"]:
        return


    # Print because we are very early and log() may not be ready yet.
    cprint("Monkey-patching MongoDB methods...", "white")

    def gen_monkey_patch(base_object, method):
        base_method = getattr(base_object, method)

        def mrq_monkey_patched(self, *args, **kwargs):

            if config["trace_io"]:
                comment = "mrq"

                worker = get_current_worker()

# Path: mrq/context.py
# def get_current_job(greenlet_id=None):
#     if greenlet_id is None:
#         greenlet_id = id(gevent.getcurrent())
#     pair = _GLOBAL_CONTEXT["greenlets"].get(greenlet_id)
#     if not pair:
#         return None
#     return pair[1]
# 
# def get_current_worker():
#     return _GLOBAL_CONTEXT["worker"]


# Path: mrq/monkey.py
from future import standard_library
from past.builtins import basestring
from .context import get_current_job, get_current_worker
    from termcolor import cprint
    from pymongo.collection import Collection
    from socket import socket as _socketmodule
    from gevent.socket import socket as _geventmodule
    from gevent.ssl import SSLSocket as _sslmodule   # pylint: disable=no-name-in-module
    from redis import StrictRedis
    from http.client import HTTPConnection, HTTPSConnection
        from requests.packages.urllib3.connection import (
            HTTPConnection,
            UnverifiedHTTPSConnection,
            VerifiedHTTPSConnection
        )
        from urllib3.connection import (
            HTTPConnection,
            UnverifiedHTTPSConnection,
            VerifiedHTTPSConnection
        )
    from pymongo.cursor import Cursor
import time
import random
import re
import copy
    import pymongo as pymongomodule

    """ Patch higher-level modules to provide insights on what is performing IO for each job. """

    patch_io_httplib(config)
    patch_io_redis(config)
    patch_io_pymongo_cursor(config)

    #patch_io_dns()
    #patch_io_subprocess()


def patch_pymongo(config):
    """ Monkey-patch pymongo's collections to add some logging """

    # Nothing to change!
    if not config["print_mongodb"] and not config["trace_io"]:
        return


    # Print because we are very early and log() may not be ready yet.
    cprint("Monkey-patching MongoDB methods...", "white")

    def gen_monkey_patch(base_object, method):
        base_method = getattr(base_object, method)

        def mrq_monkey_patched(self, *args, **kwargs):

            if config["trace_io"]:
                comment = "mrq"

                worker = get_current_worker()

                job = get_current_job()