<commit_before>"""
SQLAlchemy config implementation.
"""

from .core import BaseDataConfig
from ..compat import string_types
from ..exceptions import LibraryRequiredError
from ..structures import IgnoreCaseDict


try:
    import sqlalchemy
except:
    sqlalchemy = None


__all__ = [
    'SQLAlchemyConfig',
]


class SQLAlchemyConfig(BaseDataConfig):
    """
    A SQLAlchemy configuration based on `BaseDataConfig`.

    The library SQLAlchemy must be installed.

    Example usage:

    .. code-block:: python

        from central.config.database import SQLAlchemyConfig
        from sqlalchemy import create_engine

        engine = create_engine('postgresql://scott:tiger@localhost/test')

        config = SQLAlchemyConfig(engine, 'select key, value from configurations')
        config.load()

        timeout = config.get('timeout')

    :param engine: The SQLAlchemy engine to connect to the database.
    :param str query: The SQL query to obtain properties stored in an RDBMS.
    :param str key_column_name: The column containing the keys.
    :param str value_column_name: The column containing the values.
    """
    def __init__(self, engine, query, key_column_name='key', value_column_name='value'):
        if sqlalchemy is None:
            raise LibraryRequiredError('SQLAlchemy', 'https://pypi.python.org/pypi/SQLAlchemy')

        super(SQLAlchemyConfig, self).__init__()

        if engine is None:
            raise TypeError('engine cannot be None')

        if not isinstance(query, string_types):
            raise TypeError('query must be a str')

        if not isinstance(key_column_name, string_types):
            raise TypeError('key_column_name must be a str')

        if not isinstance(value_column_name, string_types):
            raise TypeError('value_column_name must be a str')

        self._engine = engine
        self._query = query
        self._key_column_name = key_column_name
        self._value_column_name = value_column_name

    @property
    def engine(self):
        """
        Get the SQLAlchemy engine to connect to the database.
        :return: The SQLAlchemy engine to connect to the database.
        """
        return self._engine

    @property
    def query(self):
        """
        Get the SQL query to obtain properties stored in an RDBMS.
        :return str: The SQL query to obtain properties stored in an RDBMS.
        """
        return self._query

    @property
    def key_column_name(self):
        """
        Get the column containing the keys.
        :return str: The column containing the keys.
        """
        return self._key_column_name

    @property
    def value_column_name(self):
        """
        Get the column containing the values.
        :return str: The column containing the values.
        """
        return self._value_column_name

    def load(self):
        """
        Load the configuration stored in the database.
        """
        result = self._engine.execute(self._query)

        try:
            data = IgnoreCaseDict()

            for row in result:
                key = row[self._key_column_name]
                value = row[self._value_column_name]

                data[key] = value

            self._data = data
        finally:
            result.close()
<commit_msg>Add support for dict values<commit_after>"""
SQLAlchemy config implementation.
"""

from collections import Mapping
from .core import BaseDataConfig
from ..compat import string_types
from ..exceptions import LibraryRequiredError
from ..structures import IgnoreCaseDict
from ..utils import make_ignore_case


try:
    import sqlalchemy
except:
    sqlalchemy = None


__all__ = [
    'SQLAlchemyConfig',
]


class SQLAlchemyConfig(BaseDataConfig):
    """
    A SQLAlchemy configuration based on `BaseDataConfig`.

    The library SQLAlchemy must be installed.

    Example usage:

    .. code-block:: python

        from central.config.database import SQLAlchemyConfig
        from sqlalchemy import create_engine

        engine = create_engine('postgresql://scott:tiger@localhost/test')

        config = SQLAlchemyConfig(engine, 'select key, value from configurations')
        config.load()

        timeout = config.get('timeout')

    :param engine: The SQLAlchemy engine to connect to the database.
    :param str query: The SQL query to obtain properties stored in an RDBMS.
    :param str key_column_name: The column containing the keys.
    :param str value_column_name: The column containing the values.
    """
    def __init__(self, engine, query, key_column_name='key', value_column_name='value'):
        if sqlalchemy is None:
            raise LibraryRequiredError('SQLAlchemy', 'https://pypi.python.org/pypi/SQLAlchemy')

        super(SQLAlchemyConfig, self).__init__()

        if engine is None:
            raise TypeError('engine cannot be None')

        if not isinstance(query, string_types):
            raise TypeError('query must be a str')

        if not isinstance(key_column_name, string_types):
            raise TypeError('key_column_name must be a str')

        if not isinstance(value_column_name, string_types):
            raise TypeError('value_column_name must be a str')

        self._engine = engine
        self._query = query
        self._key_column_name = key_column_name
        self._value_column_name = value_column_name

    @property
    def engine(self):
        """
        Get the SQLAlchemy engine to connect to the database.
        :return: The SQLAlchemy engine to connect to the database.
        """
        return self._engine

    @property
    def query(self):
        """
        Get the SQL query to obtain properties stored in an RDBMS.
        :return str: The SQL query to obtain properties stored in an RDBMS.
        """
        return self._query

    @property
    def key_column_name(self):
        """
        Get the column containing the keys.
        :return str: The column containing the keys.
        """
        return self._key_column_name

    @property
    def value_column_name(self):
        """
        Get the column containing the values.
        :return str: The column containing the values.
        """
        return self._value_column_name

    def load(self):
        """
        Load the configuration stored in the database.
        """
        result = self._engine.execute(self._query)

        try:
            data = IgnoreCaseDict()

            for row in result:
                key = row[self._key_column_name]
                value = row[self._value_column_name]

                if isinstance(value, Mapping):
                    value = make_ignore_case(value)

                data[key] = value

            self._data = data
        finally:
            result.close()

{'fraction_non_alphanumeric': 0.07651481645289694, 'fraction_numerical': 0.0, 'mean_word_length': 2.355093966369931, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '6783855', 'n_tokens_mistral': 1850, 'n_tokens_neox': 1750, 'n_words': 583}