<commit_before>import pymysql
import re
from .utils import to_camel_case
from . import DataJointError
from .heading import Heading
from .settings import prefix_to_role
import logging
from .erd import DBConnGraph
from . import config

logger = logging.getLogger(__name__)

# The following two regular expression are equivalent but one works in python
# and the other works in MySQL
table_name_regexp_sql = re.compile('^(#|_|__|~)?[a-z][a-z0-9_]*$')
table_name_regexp = re.compile('^(|#|_|__|~)[a-z][a-z0-9_]*$')  # MySQL does not accept this but MariaDB does


def conn_container():
    """
    creates a persistent connections for everyone to use
    """
    _connObj = None  # persistent connection object used by dj.conn()

    def conn_function(host=None, user=None, passwd=None, init_fun=None, reset=False):
        """
        Manage a persistent connection object.
        This is one of several ways to configure and access a datajoint connection.
        Users may customize their own connection manager.

        Set rest=True to reset the persistent connection object
        """
        nonlocal _connObj
        if not _connObj or reset:
            host = host if host is not None else config['database.host']
            user = user if user is not None else config['database.user']
            passwd = passwd if passwd is not None else config['database.password']

            if passwd is None: passwd = input("Please enter database password: ")

            init_fun = init_fun if init_fun is not None else config['connection.init_function']
            _connObj = Connection(host, user, passwd, init_fun)
        return _connObj

    return conn_function

# The function conn is used by others to obtain the package wide persistent connection object
conn = conn_container()




class Connection(object):
    """
    A dj.Connection object manages a connection to a database server.
    It also catalogues modules, schemas, tables, and their dependencies (foreign keys).

    Most of the parameters below should be set in the local configuration file.

    :param host: host name
    :param user: user name
    :param passwd: password
    :param init_fun: initialization function

    """

    def __init__(self, host, user, passwd, init_fun=None):
        if ':' in host:
            host, port = host.split(':')
            port = int(port)
        else:
            port = config['database.port']
        self.conn_info = dict(host=host, port=port, user=user, passwd=passwd)
        self._conn = pymysql.connect(init_command=init_fun, **self.conn_info)
        if self.is_connected:
            print("Connected", user + '@' + host + ':' + str(port))
        else:
            raise DataJointError('Connection failed.')
        self._conn.autocommit(True)

        self.db_to_mod = {}  # modules indexed by dbnames
        self.mod_to_db = {}  # database names indexed by modules
        self.table_names = {}  # tables names indexed by [dbname][class_name]
        self.headings = {}  # contains headings indexed by [dbname][table_name]
        self.tableInfo = {}  # table information indexed by [dbname][table_name]

        # dependencies from foreign keys
        self.parents = {}  # maps table names to their parent table names (primary foreign key)
        self.referenced = {}  # maps table names to table names they reference (non-primary foreign key
        self._graph = DBConnGraph(self)  # initialize an empty connection graph
        self._in_transaction = False

    def __eq__(self, other):
        return self.conn_info == other.conn_info<commit_msg>added Connection.__del__ for closing the connection<commit_after>import pymysql
import re
from .utils import to_camel_case
from . import DataJointError
from .heading import Heading
from .settings import prefix_to_role
import logging
from .erd import DBConnGraph
from . import config

logger = logging.getLogger(__name__)

# The following two regular expression are equivalent but one works in python
# and the other works in MySQL
table_name_regexp_sql = re.compile('^(#|_|__|~)?[a-z][a-z0-9_]*$')
table_name_regexp = re.compile('^(|#|_|__|~)[a-z][a-z0-9_]*$')  # MySQL does not accept this but MariaDB does


def conn_container():
    """
    creates a persistent connections for everyone to use
    """
    _connObj = None  # persistent connection object used by dj.conn()

    def conn_function(host=None, user=None, passwd=None, init_fun=None, reset=False):
        """
        Manage a persistent connection object.
        This is one of several ways to configure and access a datajoint connection.
        Users may customize their own connection manager.

        Set rest=True to reset the persistent connection object
        """
        nonlocal _connObj
        if not _connObj or reset:
            host = host if host is not None else config['database.host']
            user = user if user is not None else config['database.user']
            passwd = passwd if passwd is not None else config['database.password']

            if passwd is None: passwd = input("Please enter database password: ")

            init_fun = init_fun if init_fun is not None else config['connection.init_function']
            _connObj = Connection(host, user, passwd, init_fun)
        return _connObj

    return conn_function

# The function conn is used by others to obtain the package wide persistent connection object
conn = conn_container()




class Connection(object):
    """
    A dj.Connection object manages a connection to a database server.
    It also catalogues modules, schemas, tables, and their dependencies (foreign keys).

    Most of the parameters below should be set in the local configuration file.

    :param host: host name
    :param user: user name
    :param passwd: password
    :param init_fun: initialization function

    """

    def __init__(self, host, user, passwd, init_fun=None):
        if ':' in host:
            host, port = host.split(':')
            port = int(port)
        else:
            port = config['database.port']
        self.conn_info = dict(host=host, port=port, user=user, passwd=passwd)
        self._conn = pymysql.connect(init_command=init_fun, **self.conn_info)
        if self.is_connected:
            print("Connected", user + '@' + host + ':' + str(port))
        else:
            raise DataJointError('Connection failed.')
        self._conn.autocommit(True)

        self.db_to_mod = {}  # modules indexed by dbnames
        self.mod_to_db = {}  # database names indexed by modules
        self.table_names = {}  # tables names indexed by [dbname][class_name]
        self.headings = {}  # contains headings indexed by [dbname][table_name]
        self.tableInfo = {}  # table information indexed by [dbname][table_name]

        # dependencies from foreign keys
        self.parents = {}  # maps table names to their parent table names (primary foreign key)
        self.referenced = {}  # maps table names to table names they reference (non-primary foreign key
        self._graph = DBConnGraph(self)  # initialize an empty connection graph
        self._in_transaction = False

    def __del__(self):
        logger.info('Disconnecting {user}@{host}:{port}'.format(**self.conn_info))
        self._conn.close()

    def __eq__(self, other):
        return self.conn_info == other.conn_info
{'fraction_non_alphanumeric': 0.08941208492106696, 'fraction_numerical': 0.0010887316276537834, 'mean_word_length': 2.946831364124597, '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': '625380', 'n_tokens_mistral': 2046, 'n_tokens_neox': 1983, 'n_words': 717}