<commit_before>"""
This module contains the Connection class that manages the connection to the database,
 and the `conn` function that provides access to a persistent connection in datajoint.
"""
import warnings
from contextlib import contextmanager
import pymysql as client
import logging
from getpass import getpass

from .settings import config
from . import errors
from .dependencies import Dependencies
from .plugin import connection_plugins

# client errors to catch
client_errors = (client.err.InterfaceError, client.err.DatabaseError)


def get_host_hook(host_input):
    if '://' in host_input:
        plugin_name = host_input.split('://')[0]
        try:
            return connection_plugins[plugin_name]['object'].load().get_host(host_input)
        except KeyError:
            raise DataJointError(
                "Connection plugin '{}' not found.".format(plugin_name))
    else:
        return host_input


def connect_host_hook(connection_obj):
    if '://' in connection_obj.conn_info['host_input']:
        plugin_name = connection_obj.conn_info['host_input'].split('://')[0]
        try:
            connection_plugins[plugin_name]['object'].load().connect_host(connection_obj)
        except KeyError:
            raise DataJointError(
                "Connection plugin '{}' not found.".format(plugin_name))
    else:
        connection_obj.connect()


def translate_query_error(client_error, query):
    """
    Take client error and original query and return the corresponding DataJoint exception.
    :param client_error: the exception raised by the client interface
    :param query: sql query with placeholders
    :return: an instance of the corresponding subclass of datajoint.errors.DataJointError
    """
    # Loss of connection errors
    if isinstance(client_error, client.err.InterfaceError) and client_error.args[0] == "(0, '')":<commit_msg>Fix how DJError raised on uninstalled plugin.<commit_after>"""
This module contains the Connection class that manages the connection to the database,
 and the `conn` function that provides access to a persistent connection in datajoint.
"""
import warnings
from contextlib import contextmanager
import pymysql as client
import logging
from getpass import getpass

from .settings import config
from . import errors
from .dependencies import Dependencies
from .plugin import connection_plugins

# client errors to catch
client_errors = (client.err.InterfaceError, client.err.DatabaseError)


def get_host_hook(host_input):
    if '://' in host_input:
        plugin_name = host_input.split('://')[0]
        try:
            return connection_plugins[plugin_name]['object'].load().get_host(host_input)
        except KeyError:
            raise errors.DataJointError(
                "Connection plugin '{}' not found.".format(plugin_name))
    else:
        return host_input


def connect_host_hook(connection_obj):
    if '://' in connection_obj.conn_info['host_input']:
        plugin_name = connection_obj.conn_info['host_input'].split('://')[0]
        try:
            connection_plugins[plugin_name]['object'].load().connect_host(connection_obj)
        except KeyError:
            raise errors.DataJointError(
                "Connection plugin '{}' not found.".format(plugin_name))
    else:
        connection_obj.connect()


def translate_query_error(client_error, query):
    """
    Take client error and original query and return the corresponding DataJoint exception.
    :param client_error: the exception raised by the client interface
    :param query: sql query with placeholders
    :return: an instance of the corresponding subclass of datajoint.errors.DataJointError
    """
    # Loss of connection errors
    if isinstance(client_error, client.err.InterfaceError) and client_error.args[0] == "(0, '')":
{'fraction_non_alphanumeric': 0.0906463478717814, 'fraction_numerical': 0.0021019442984760903, 'mean_word_length': 3.642682926829268, 'pattern_counts': {'":': 2, '<': 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': '4703196', 'n_tokens_mistral': 1006, 'n_tokens_neox': 978, 'n_words': 314}