from __future__ import absolute_import

import os
import sys
import errno
import struct
import cPickle
import idiokit
import subprocess
import contextlib
import socket as native_socket
from idiokit import socket, select
from . import events, rules, taskfarm, bot


class _ConnectionLost(Exception):
    pass


@contextlib.contextmanager
def wrapped_socket_errnos(*errnos):
    try:
        yield
    except (native_socket.error, socket.SocketError) as error:
        socket_errno = error.args[0]
        if socket_errno in errnos:
            raise _ConnectionLost(os.strerror(socket_errno))