<commit_before>
from edmunds.foundation.patterns.manager import Manager
from flask_sqlalchemy import SQLAlchemy
from edmunds.database.drivers.mysql import MySql
from edmunds.database.drivers.postgresql import PostgreSql
from edmunds.database.drivers.sqlite import Sqlite
from threading import Lock


class DatabaseManager(Manager):
    """
    Database Manager
    """

    _sql_alchemy_instance = None

    @staticmethod
    def get_sql_alchemy_instance():
        """
        Get sql alchemy instance
        :return:    SQLAlchemy
        :rtype:     SQLAlchemy
        """

        if DatabaseManager._sql_alchemy_instance is None:
            DatabaseManager._sql_alchemy_instance = SQLAlchemy()
        return DatabaseManager._sql_alchemy_instance

    def __init__(self, app):
        """
        Initiate the manager
        :param app:             The application
        :type  app:             Application
        """

        super(DatabaseManager, self).__init__(app, app.config('app.database.instances', []))

        self._files_path = 'database'
        self._load_lock_sql_alchemy = Lock()

    def _load(self):
        """
        Load all the instances
        """

        if self._instances is not None:
            return
        with self._load_lock_sql_alchemy:
            if self._instances is not None:
                return

            self._init_sql_alchemy()

            return super(DatabaseManager, self)._load()

    def _init_sql_alchemy(self):
        """
        Init sql alchemy
        :return:    void
        """

        database_uri = None
        binds = {}

        for instances_config_item in self._instances_config:
            instance_database_uri = None

            if instances_config_item['driver'] == MySql \
                    or instances_config_item['driver'] == PostgreSql:

                if 'user' not in instances_config_item \
                        or 'pass' not in instances_config_item \
                        or 'host' not in instances_config_item \
                        or 'table' not in instances_config_item:
                    raise RuntimeError("Database-driver '%s' is missing some configuration ('user', 'pass', 'host' and 'table' are required)." % instances_config_item['name'])

                driver = instances_config_item['driver'].__name__.lower()
                mysql_user = instances_config_item['user']
                mysql_pass = instances_config_item['pass']
                mysql_host = instances_config_item['host']
                mysql_table = instances_config_item['table']
                mysql_port = instances_config_item['port'] if 'port' in instances_config_item else 3306

                instance_database_uri = '%s://%s:%s@%s:%s/%s' % (driver, mysql_user, mysql_pass, mysql_host, mysql_port, mysql_table)

            if instances_config_item['driver'] == Sqlite:
                if 'file' not in instances_config_item:
                    raise RuntimeError("Database-driver '%s' is missing some configuration ('file' is required)." % instances_config_item['name'])

                sqlite_file = instances_config_item['file']
                sqlite_storage_name = instances_config_item['storage'] if 'storage' in instances_config_item else None
                sqlite_path = self._app.fs(name=sqlite_storage_name).path(sqlite_file)

                instance_database_uri = 'sqlite://%s' % sqlite_path

            binds[instances_config_item['name']] = instance_database_uri
            if database_uri is None:
                database_uri = instance_database_uri

        self._app.config.update({
            'SQLALCHEMY_DATABASE_URI': database_uri,<commit_msg>#12: Fixed default port for postgresql<commit_after>
from edmunds.foundation.patterns.manager import Manager
from flask_sqlalchemy import SQLAlchemy
from edmunds.database.drivers.mysql import MySql
from edmunds.database.drivers.postgresql import PostgreSql
from edmunds.database.drivers.sqlite import Sqlite
from threading import Lock


class DatabaseManager(Manager):
    """
    Database Manager
    """

    _sql_alchemy_instance = None

    @staticmethod
    def get_sql_alchemy_instance():
        """
        Get sql alchemy instance
        :return:    SQLAlchemy
        :rtype:     SQLAlchemy
        """

        if DatabaseManager._sql_alchemy_instance is None:
            DatabaseManager._sql_alchemy_instance = SQLAlchemy()
        return DatabaseManager._sql_alchemy_instance

    def __init__(self, app):
        """
        Initiate the manager
        :param app:             The application
        :type  app:             Application
        """

        super(DatabaseManager, self).__init__(app, app.config('app.database.instances', []))

        self._files_path = 'database'
        self._load_lock_sql_alchemy = Lock()

    def _load(self):
        """
        Load all the instances
        """

        if self._instances is not None:
            return
        with self._load_lock_sql_alchemy:
            if self._instances is not None:
                return

            self._init_sql_alchemy()

            return super(DatabaseManager, self)._load()

    def _init_sql_alchemy(self):
        """
        Init sql alchemy
        :return:    void
        """

        database_uri = None
        binds = {}

        for instances_config_item in self._instances_config:
            instance_database_uri = None

            if instances_config_item['driver'] == MySql \
                    or instances_config_item['driver'] == PostgreSql:

                if 'user' not in instances_config_item \
                        or 'pass' not in instances_config_item \
                        or 'host' not in instances_config_item \
                        or 'table' not in instances_config_item:
                    raise RuntimeError("Database-driver '%s' is missing some configuration ('user', 'pass', 'host' and 'table' are required)." % instances_config_item['name'])

                driver = instances_config_item['driver'].__name__.lower()
                database_user = instances_config_item['user']
                database_pass = instances_config_item['pass']
                database_host = instances_config_item['host']
                database_table = instances_config_item['table']
                if 'port' in instances_config_item:
                    database_port = instances_config_item['port']
                elif instances_config_item['driver'] == PostgreSql:
                    database_port = 5432
                else:
                    database_port = 3306

                instance_database_uri = '%s://%s:%s@%s:%s/%s' % (driver, database_user, database_pass, database_host, database_port, database_table)

            if instances_config_item['driver'] == Sqlite:
                if 'file' not in instances_config_item:
                    raise RuntimeError("Database-driver '%s' is missing some configuration ('file' is required)." % instances_config_item['name'])

                sqlite_file = instances_config_item['file']
                sqlite_storage_name = instances_config_item['storage'] if 'storage' in instances_config_item else None
                sqlite_path = self._app.fs(name=sqlite_storage_name).path(sqlite_file)

                instance_database_uri = 'sqlite://%s' % sqlite_path

            binds[instances_config_item['name']] = instance_database_uri
            if database_uri is None:
                database_uri = instance_database_uri

        self._app.config.update({
            'SQLALCHEMY_DATABASE_URI': database_uri,
{'fraction_non_alphanumeric': 0.0844087437450619, 'fraction_numerical': 0.0018435607058203845, 'mean_word_length': 2.195204038704249, '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': '229809', 'n_tokens_mistral': 2108, 'n_tokens_neox': 1955, 'n_words': 454}